Skip to content

Commit

Permalink
feat(ts): add support for boolean type for query arguments
Browse files Browse the repository at this point in the history
Part of #49
  • Loading branch information
php-coder committed Apr 14, 2024
1 parent ea6df41 commit e6cd468
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 5 additions & 1 deletion examples/ts/express/mysql/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Express, NextFunction, Request, Response } from 'express'
import { Pool } from 'mysql'

const parseBoolean = (value: any) => {
return typeof value === 'string' && value === 'true'
}

const register = (app: Express, pool: Pool) => {

app.get('/v1/categories/count', (req: Request, res: Response, next: NextFunction) => {
Expand Down Expand Up @@ -64,7 +68,7 @@ const register = (app: Express, pool: Pool) => {
app.get('/v1/categories/search', (req: Request, res: Response, next: NextFunction) => {
pool.query(
'SELECT id , name , name_ru , slug , hidden FROM categories WHERE hidden = :hidden',
{ "hidden": req.query.hidden },
{ "hidden": parseBoolean(req.query.hidden) },
(err, rows, fields) => {
if (err) {
return next(err)
Expand Down
2 changes: 1 addition & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const createEndpoints = async (destDir, { lang }, config) => {
const paramName = p.substring(2)
const prefix = placeholdersMap['js'][bindTarget]
// LATER: add support for path (method.params.path) and body (method.dto.fields) parameters
if (method && bindTarget === 'q' && retrieveType(method.params.query, paramName) === 'boolean') {
if (bindTarget === 'q' && retrieveType(method.params.query, paramName) === 'boolean') {
return `"${paramName}": parseBoolean(${prefix}.${paramName})`
}
return `"${paramName}": ${prefix}.${paramName}`
Expand Down
7 changes: 6 additions & 1 deletion src/templates/routes.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Express, NextFunction, Request, Response } from 'express'
import { Pool } from 'mysql'

<%# LATER: add it only when there is at least one parameter of boolean type -%>
const parseBoolean = (value: any) => {
return typeof value === 'string' && value === 'true'
}

const register = (app: Express, pool: Pool) => {
<%
endpoints.forEach(function(endpoint) {
Expand All @@ -16,7 +21,7 @@ endpoints.forEach(function(endpoint) {
const sql = formatQuery(method.query)
const params = extractParamsFromQuery(method.query)
const formattedParams = params.length > 0
? '\n { ' + formatParamsAsJavaScriptObject(params) + ' },'
? '\n { ' + formatParamsAsJavaScriptObject(params, method) + ' },'
: ''
if (hasGetOne || hasGetMany) {
Expand Down

0 comments on commit e6cd468

Please sign in to comment.