Skip to content

Commit ba2007c

Browse files
committed
feat(ts): add support for boolean type for query arguments
1 parent ea6df41 commit ba2007c

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

examples/ts/express/mysql/routes.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Express, NextFunction, Request, Response } from 'express'
22
import { Pool } from 'mysql'
33

4+
const parseBoolean = (value: any) => {
5+
return typeof value === 'string' && value === 'true'
6+
}
7+
48
const register = (app: Express, pool: Pool) => {
59

610
app.get('/v1/categories/count', (req: Request, res: Response, next: NextFunction) => {
@@ -64,7 +68,7 @@ const register = (app: Express, pool: Pool) => {
6468
app.get('/v1/categories/search', (req: Request, res: Response, next: NextFunction) => {
6569
pool.query(
6670
'SELECT id , name , name_ru , slug , hidden FROM categories WHERE hidden = :hidden',
67-
{ "hidden": req.query.hidden },
71+
{ "hidden": parseBoolean(req.query.hidden) },
6872
(err, rows, fields) => {
6973
if (err) {
7074
return next(err)

src/cli.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ const createEndpoints = async (destDir, { lang }, config) => {
246246
const paramName = p.substring(2)
247247
const prefix = placeholdersMap['js'][bindTarget]
248248
// LATER: add support for path (method.params.path) and body (method.dto.fields) parameters
249-
if (method && bindTarget === 'q' && retrieveType(method.params.query, paramName) === 'boolean') {
249+
if (bindTarget === 'q' && retrieveType(method.params.query, paramName) === 'boolean') {
250250
return `"${paramName}": parseBoolean(${prefix}.${paramName})`
251251
}
252252
return `"${paramName}": ${prefix}.${paramName}`

src/templates/routes.ts.ejs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { Express, NextFunction, Request, Response } from 'express'
22
import { Pool } from 'mysql'
33

4+
<%# LATER: add it only when there is at least one parameter of boolean type -%>
5+
const parseBoolean = (value: any) => {
6+
return typeof value === 'string' && value === 'true'
7+
}
8+
49
const register = (app: Express, pool: Pool) => {
510
<%
611
endpoints.forEach(function(endpoint) {
@@ -16,7 +21,7 @@ endpoints.forEach(function(endpoint) {
1621
const sql = formatQuery(method.query)
1722
const params = extractParamsFromQuery(method.query)
1823
const formattedParams = params.length > 0
19-
? '\n { ' + formatParamsAsJavaScriptObject(params) + ' },'
24+
? '\n { ' + formatParamsAsJavaScriptObject(params, method) + ' },'
2025
: ''
2126
2227
if (hasGetOne || hasGetMany) {

0 commit comments

Comments
 (0)