@lbu/code-gen
Advanced tools
Comparing version 0.0.75 to 0.0.76
@@ -417,3 +417,2 @@ import { Logger } from "@lbu/insight"; | ||
* Add tags to this route. | ||
* Tag handlers are executed before group and specific route handlers | ||
*/ | ||
@@ -450,4 +449,5 @@ tags(...value: string[]): this; | ||
/** | ||
* Create a new route group | ||
* Path will be concatenated with the current path of this group | ||
* Create a new route group. | ||
* Path will be concatenated with the current path of this group. | ||
* This resets the default tags, query, params, body and response type. | ||
*/ | ||
@@ -480,2 +480,32 @@ group(name: string, path: string): this; | ||
head(path?: string, name?: string); | ||
/** | ||
* Default tags for all routes created by this RouteCreator. | ||
*/ | ||
tags(...value: string[]): this; | ||
/** | ||
* Default query type for all routes created by this RouteCreator. | ||
*/ | ||
query(builder: TypeBuilderLike): this; | ||
/** | ||
* Default params type for all routes created by this RouteCreator. | ||
*/ | ||
params(builder: TypeBuilderLike): this; | ||
/** | ||
* Default body type for all routes created by this RouteCreator. | ||
*/ | ||
body(builder: TypeBuilderLike): this; | ||
/** | ||
* Default files type for all routes created by this RouteCreator. | ||
*/ | ||
files(builder: TypeBuilderLike): this; | ||
/** | ||
* Default response type for all routes created by this RouteCreator. | ||
*/ | ||
response(builder: TypeBuilderLike): this; | ||
} | ||
@@ -482,0 +512,0 @@ |
{ | ||
"name": "@lbu/code-gen", | ||
"version": "0.0.75", | ||
"version": "0.0.76", | ||
"description": "Generate various boring parts of your server", | ||
@@ -19,5 +19,5 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@lbu/cli": "0.0.75", | ||
"@lbu/insight": "0.0.75", | ||
"@lbu/stdlib": "0.0.75" | ||
"@lbu/cli": "0.0.76", | ||
"@lbu/insight": "0.0.76", | ||
"@lbu/stdlib": "0.0.76" | ||
}, | ||
@@ -45,3 +45,3 @@ "maintainers": [ | ||
}, | ||
"gitHead": "b89ec55fceee61902fe1ee79e4c658ff70f0b129" | ||
"gitHead": "4d5245ee1c104c3fb11e37afd6a9a226fa29706c" | ||
} |
@@ -109,11 +109,2 @@ import { isNil } from "@lbu/stdlib"; | ||
if (this.paramsBuilder) { | ||
result.params = buildOrInfer(this.paramsBuilder); | ||
if (isNil(result.params.name)) { | ||
result.params.group = result.group; | ||
result.params.name = `${result.name}Params`; | ||
} | ||
} | ||
if (this.bodyBuilder) { | ||
@@ -146,9 +137,28 @@ result.body = buildOrInfer(this.bodyBuilder); | ||
if (result.path.indexOf(":") !== -1 && result.params === undefined) { | ||
// Looks like there is a path param but no definition for it | ||
result.params = createParamsFromPath( | ||
result.group, | ||
`${result.name}Params`, | ||
result.path, | ||
); | ||
const pathParamKeys = collectPathParams(result.path); | ||
if (this.paramsBuilder || pathParamKeys.length > 0) { | ||
const paramsResult = this.paramsBuilder | ||
? buildOrInfer(this.paramsBuilder) | ||
: buildOrInfer({}); | ||
paramsResult.group = result.group; | ||
paramsResult.name = `${result.name}Params`; | ||
for (const param of pathParamKeys) { | ||
if (isNil(paramsResult.keys?.[param])) { | ||
throw new Error( | ||
`Route ${result.group}->${result.name} is missing a type definition for '${param}' parameter.`, | ||
); | ||
} | ||
} | ||
for (const key of Object.keys(paramsResult.keys ?? {})) { | ||
if (pathParamKeys.indexOf(key) === -1) { | ||
throw new Error( | ||
`Route ${result.group}->${result.name} has type definition for '${key}' but is not found in the path: ${result.path}`, | ||
); | ||
} | ||
} | ||
result.params = paramsResult; | ||
} | ||
@@ -161,20 +171,17 @@ | ||
/** | ||
* Build an object for route params if the user forgot to do that | ||
* @param {string} group | ||
* @param {string} name | ||
* Collect all path params | ||
* | ||
* @param {string} path | ||
* @returns {object} | ||
* @returns {string[]} | ||
*/ | ||
function createParamsFromPath(group, name, path) { | ||
const T = new TypeCreator(group); | ||
const obj = T.object(name); | ||
const keys = {}; | ||
function collectPathParams(path) { | ||
const keys = []; | ||
for (const part of path.split("/")) { | ||
if (part.startsWith(":")) { | ||
keys[part.substring(1)] = T.string(); | ||
keys.push(part.substring(1)); | ||
} | ||
} | ||
return buildOrInfer(obj.keys(keys)); | ||
return keys; | ||
} | ||
@@ -192,5 +199,74 @@ | ||
} | ||
this.defaultTags = []; | ||
this.queryBuilder = undefined; | ||
this.paramsBuilder = undefined; | ||
this.bodyBuilder = undefined; | ||
this.filesBuilder = undefined; | ||
this.responseBuilder = undefined; | ||
} | ||
/** | ||
* @param {string} values | ||
* @returns {RouteCreator} | ||
*/ | ||
tags(...values) { | ||
for (const v of values) { | ||
this.data.tags.push(lowerCaseFirst(v)); | ||
} | ||
return this; | ||
} | ||
/** | ||
* @param {TypeBuilderLike} builder | ||
* @returns {RouteCreator} | ||
*/ | ||
query(builder) { | ||
this.queryBuilder = builder; | ||
return this; | ||
} | ||
/** | ||
* @param {TypeBuilderLike} builder | ||
* @returns {RouteCreator} | ||
*/ | ||
params(builder) { | ||
this.paramsBuilder = builder; | ||
return this; | ||
} | ||
/** | ||
* @param {TypeBuilderLike} builder | ||
* @returns {RouteCreator} | ||
*/ | ||
body(builder) { | ||
this.bodyBuilder = builder; | ||
return this; | ||
} | ||
/** | ||
* @param {TypeBuilderLike} builder | ||
* @returns {RouteCreator} | ||
*/ | ||
files(builder) { | ||
this.filesBuilder = builder; | ||
return this; | ||
} | ||
/** | ||
* @param {TypeBuilderLike} builder | ||
* @returns {RouteCreator} | ||
*/ | ||
response(builder) { | ||
this.responseBuilder = builder; | ||
return this; | ||
} | ||
/** | ||
* @param {string} name | ||
@@ -210,3 +286,3 @@ * @param {string} path | ||
get(path, name) { | ||
return new RouteBuilder( | ||
return this.create( | ||
"GET", | ||
@@ -225,3 +301,3 @@ this.data.group, | ||
post(path, name) { | ||
return new RouteBuilder( | ||
return this.create( | ||
"POST", | ||
@@ -240,3 +316,3 @@ this.data.group, | ||
put(path, name) { | ||
return new RouteBuilder( | ||
return this.create( | ||
"PUT", | ||
@@ -255,3 +331,3 @@ this.data.group, | ||
delete(path, name) { | ||
return new RouteBuilder( | ||
return this.create( | ||
"DELETE", | ||
@@ -270,3 +346,3 @@ this.data.group, | ||
head(path, name) { | ||
return new RouteBuilder( | ||
return this.create( | ||
"HEAD", | ||
@@ -278,2 +354,42 @@ this.data.group, | ||
} | ||
/** | ||
* Create a new RouteBuilder and add the defaults if exists. | ||
* | ||
* @param {string} method | ||
* @param {string} group | ||
* @param {string} name | ||
* @param {string} path | ||
* @returns {RouteBuilder} | ||
*/ | ||
create(method, group, name, path) { | ||
const b = new RouteBuilder(method, group, name, path); | ||
b.tags(...this.defaultTags); | ||
if (!isNil(this.paramsBuilder)) { | ||
b.params(this.paramsBuilder); | ||
} | ||
if (!isNil(this.queryBuilder)) { | ||
b.query(this.queryBuilder); | ||
} | ||
if ( | ||
!isNil(this.bodyBuilder) && | ||
["POST", "PUT", "DELETE"].indexOf(method) !== -1 | ||
) { | ||
b.body(this.bodyBuilder); | ||
} | ||
if (!isNil(this.filesBuilder) && ["POST", "PUT"].indexOf(method) !== -1) { | ||
b.files(this.filesBuilder); | ||
} | ||
if (!isNil(this.responseBuilder)) { | ||
b.response(this.responseBuilder); | ||
} | ||
return b; | ||
} | ||
} | ||
@@ -280,0 +396,0 @@ |
@@ -0,1 +1,2 @@ | ||
import { isNil } from "@lbu/stdlib"; | ||
import { addToData } from "../../generate.js"; | ||
@@ -263,7 +264,8 @@ import { TypeBuilder, TypeCreator } from "../../types/index.js"; | ||
for (const key of Object.keys(item.keys)) { | ||
const it = item.keys[key]; | ||
const it = getItem(item.keys[key]); | ||
// We don't support optional field searching, since it will break the way we do the | ||
// query generation e.g. NULL IS NULL is always true and thus the search results are | ||
// invalid | ||
if (it.isOptional || !it?.sql?.searchable) { | ||
// invalid. However if a default value is set, we expect that this will be honored | ||
// throughout all of the application. | ||
if ((it.isOptional && isNil(it.defaultValue)) || !it?.sql?.searchable) { | ||
continue; | ||
@@ -273,3 +275,3 @@ } | ||
// Also supports referenced fields | ||
const type = getItem(it)?.type; | ||
const type = it.type; | ||
@@ -276,0 +278,0 @@ if (type === "number" || type === "date") { |
@@ -56,4 +56,6 @@ import { isNil, isPlainObject, merge } from "@lbu/stdlib"; | ||
default(rawString) { | ||
this.data.defaultValue = rawString; | ||
this.data.isOptional = !isNil(rawString); | ||
if (this.data.isOptional) { | ||
this.data.defaultValue = rawString.toString(); | ||
} | ||
@@ -60,0 +62,0 @@ return this; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
272099
8495
+ Added@lbu/cli@0.0.76(transitive)
+ Added@lbu/insight@0.0.76(transitive)
+ Added@lbu/stdlib@0.0.76(transitive)
- Removed@lbu/cli@0.0.75(transitive)
- Removed@lbu/insight@0.0.75(transitive)
- Removed@lbu/stdlib@0.0.75(transitive)
Updated@lbu/cli@0.0.76
Updated@lbu/insight@0.0.76
Updated@lbu/stdlib@0.0.76