Skip to content

Commit

Permalink
feat(js): 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 13, 2024
1 parent dd8e260 commit ea6df41
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
4 changes: 4 additions & 0 deletions examples/js/express/mysql/endpoints.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
type: integer
hidden:
type: boolean
params:
query:
hidden:
type: boolean

- path: /v1/categories/:categoryId
get:
Expand Down
6 changes: 5 additions & 1 deletion examples/js/express/mysql/routes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const parseBoolean = (value) => {
return value === 'true'
}

const register = (app, pool) => {

app.get('/v1/categories/count', (req, res, next) => {
Expand Down Expand Up @@ -61,7 +65,7 @@ const register = (app, pool) => {
app.get('/v1/categories/search', (req, res, next) => {
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
8 changes: 6 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const lengthOfLongestString = (arr) => arr
)

// returns user-defined variable's type or null
// Accepts method.dto.fields fieldsInfo
// Accepts method.dto.fields or method.params as fieldsInfo
const retrieveType = (fieldsInfo, fieldName) => {
const hasTypeInfo = fieldsInfo.hasOwnProperty(fieldName) && fieldsInfo[fieldName].hasOwnProperty('type')
if (hasTypeInfo) {
Expand Down Expand Up @@ -235,7 +235,7 @@ const createEndpoints = async (destDir, { lang }, config) => {

// [ "p.page", "b.num" ] => '"page": req.params.page, "num": req.body.num'
// (used only with Express)
"formatParamsAsJavaScriptObject": (params) => {
"formatParamsAsJavaScriptObject": (params, method) => {
if (params.length === 0) {
return params
}
Expand All @@ -245,6 +245,10 @@ const createEndpoints = async (destDir, { lang }, config) => {
const bindTarget = p.substring(0, 1)
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') {
return `"${paramName}": parseBoolean(${prefix}.${paramName})`
}
return `"${paramName}": ${prefix}.${paramName}`
}
).join(', ')
Expand Down
7 changes: 6 additions & 1 deletion src/templates/routes.js.ejs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<%# LATER: add it only when there is at least one parameter of boolean type -%>
const parseBoolean = (value) => {
return value === 'true'
}

const register = (app, pool) => {
<%
endpoints.forEach(function(endpoint) {
Expand All @@ -13,7 +18,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 ea6df41

Please sign in to comment.