@compas/code-gen
Advanced tools
Comparing version 0.0.125 to 0.0.126
@@ -172,2 +172,8 @@ import { Logger } from "@compas/insight"; | ||
/** | ||
* Convert OpenAPI to Compas spec and add to this app instance. | ||
* Add all unreferenced items to the default group for easier discoverability. | ||
*/ | ||
extendWithOpenApi(defaultGroup: string, data: any): App; | ||
/** | ||
* Call the generators with the provided options | ||
@@ -174,0 +180,0 @@ * and writes the output |
export { TypeCreator } from "./src/builders/index.js"; | ||
export { App } from "./src/App.js"; | ||
export { loadFromRemote, loadFromOpenAPISpec } from "./src/loaders.js"; | ||
export { loadFromRemote } from "./src/loaders.js"; |
{ | ||
"name": "@compas/code-gen", | ||
"version": "0.0.125", | ||
"version": "0.0.126", | ||
"description": "Generate various boring parts of your server", | ||
@@ -18,5 +18,5 @@ "main": "./index.js", | ||
"dependencies": { | ||
"@compas/cli": "0.0.125", | ||
"@compas/insight": "0.0.125", | ||
"@compas/stdlib": "0.0.125" | ||
"@compas/cli": "0.0.126", | ||
"@compas/insight": "0.0.126", | ||
"@compas/stdlib": "0.0.126" | ||
}, | ||
@@ -44,3 +44,3 @@ "maintainers": [ | ||
}, | ||
"gitHead": "a44a773801f5fd21f6b92a751cef57d3e3e563c6" | ||
"gitHead": "80a9c008cad24f72ec8d0af3c69c229f371712c1" | ||
} |
@@ -16,2 +16,4 @@ import { newLogger, printProcessMemoryUsage } from "@compas/insight"; | ||
import { getInternalRoutes } from "./generator/router/index.js"; | ||
import { recursivelyRemoveInternalFields } from "./internal.js"; | ||
import { loadFromOpenAPISpec } from "./loaders.js"; | ||
import { lowerCaseFirst } from "./utils.js"; | ||
@@ -186,22 +188,14 @@ | ||
extend(data) { | ||
if (!isNil(validateCodeGenType)) { | ||
// Validators present, use the result of them. | ||
const { data: value, errors } = validateCodeGenStructure(data); | ||
if (errors) { | ||
this.logger.error(errors[0]); | ||
process.exit(1); | ||
} | ||
return this.extendInternal(data, false); | ||
} | ||
// Make a deep copy without null prototypes | ||
data = {}; | ||
merge(data, value); | ||
} | ||
for (const groupData of Object.values(data)) { | ||
for (const item of Object.values(groupData)) { | ||
this.addToData(item); | ||
} | ||
} | ||
return this; | ||
/** | ||
* Extend from the OpenAPI spec | ||
* | ||
* @param {string} defaultGroup | ||
* @param {object} data | ||
* @returns {this} | ||
*/ | ||
extendWithOpenApi(defaultGroup, data) { | ||
return this.extendInternal(loadFromOpenAPISpec(defaultGroup, data), true); | ||
} | ||
@@ -319,2 +313,36 @@ | ||
/** | ||
* Internally used extend | ||
* | ||
* @param {object} rawStructure | ||
* @param {boolean} allowInternalProperties | ||
* @returns {this} | ||
*/ | ||
extendInternal(rawStructure, allowInternalProperties) { | ||
if (!isNil(validateCodeGenType)) { | ||
// Validators present, use the result of them. | ||
const { data: value, errors } = validateCodeGenStructure(rawStructure); | ||
if (errors) { | ||
this.logger.error(errors[0]); | ||
process.exit(1); | ||
} | ||
// Make a deep copy without null prototypes | ||
rawStructure = {}; | ||
merge(rawStructure, value); | ||
} | ||
if (!allowInternalProperties) { | ||
recursivelyRemoveInternalFields(rawStructure); | ||
} | ||
for (const groupData of Object.values(rawStructure)) { | ||
for (const item of Object.values(groupData)) { | ||
this.addToData(item); | ||
} | ||
} | ||
return this; | ||
} | ||
/** | ||
* Process unprocessed list, normalize references | ||
@@ -321,0 +349,0 @@ * Depends on referentType being available |
@@ -50,6 +50,10 @@ import { isPlainObject } from "@compas/stdlib"; | ||
return new ArrayType().values(value[0]).build(); | ||
} | ||
if (Array.isArray(value) && value.length !== 1) { | ||
} else if (typeof value === "function") { | ||
throw new Error( | ||
`Can't infer type of function. Did you forget to call '${ | ||
value.name ?? "anonymous" | ||
}'?`, | ||
); | ||
} else if (Array.isArray(value) && value.length !== 1) { | ||
throw new Error( | ||
`Inferred arrays can only have a single element. Found '${value}' which has ${value.length} elements.`, | ||
@@ -56,0 +60,0 @@ ); |
@@ -16,3 +16,3 @@ // Generated by @compas/code-gen | ||
/** | ||
* @typedef {{"type": "anyOf", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "values": (CodeGenType)[], }} CodeGenAnyOfType | ||
* @typedef {{"type": "anyOf", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "internalSettings": {}, "values": (CodeGenType)[], }} CodeGenAnyOfType | ||
*/ | ||
@@ -23,24 +23,24 @@ /** | ||
/** | ||
* @typedef {{"type": "any", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, "rawValue"?: undefined|string, "rawValueImport": {"javaScript"?: undefined|string, "typeScript"?: undefined|string, }, "rawValidator"?: undefined|string, "rawValidatorImport": {"javaScript"?: undefined|string, "typeScript"?: undefined|string, }, }} CodeGenAnyType | ||
* @typedef {{"type": "any", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, "internalSettings": {}, "rawValue"?: undefined|string, "rawValueImport": {"javaScript"?: undefined|string, "typeScript"?: undefined|string, }, "rawValidator"?: undefined|string, "rawValidatorImport": {"javaScript"?: undefined|string, "typeScript"?: undefined|string, }, }} CodeGenAnyType | ||
*/ | ||
/** | ||
* @typedef {{"type": "array", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "min"?: undefined|number, "max"?: undefined|number, }, "values": CodeGenType, }} CodeGenArrayType | ||
* @typedef {{"type": "array", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "min"?: undefined|number, "max"?: undefined|number, }, "internalSettings": {}, "values": CodeGenType, }} CodeGenArrayType | ||
*/ | ||
/** | ||
* @typedef {{"type": "boolean", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "allowNull": boolean, }, "oneOf"?: undefined|boolean, }} CodeGenBooleanType | ||
* @typedef {{"type": "boolean", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "allowNull": boolean, }, "internalSettings": {}, "oneOf"?: undefined|boolean, }} CodeGenBooleanType | ||
*/ | ||
/** | ||
* @typedef {{"type": "date", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, "min"?: undefined|Date, "max"?: undefined|Date, "inFuture"?: undefined|boolean, "inPast"?: undefined|boolean, }, }} CodeGenDateType | ||
* @typedef {{"type": "date", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, "min"?: undefined|Date, "max"?: undefined|Date, "inFuture"?: undefined|boolean, "inPast"?: undefined|boolean, }, "internalSettings": {}, }} CodeGenDateType | ||
*/ | ||
/** | ||
* @typedef {{"type": "file", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, }} CodeGenFileType | ||
* @typedef {{"type": "file", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "internalSettings": {}, }} CodeGenFileType | ||
*/ | ||
/** | ||
* @typedef {{"type": "generic", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "keys": CodeGenType, "values": CodeGenType, }} CodeGenGenericType | ||
* @typedef {{"type": "generic", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "internalSettings": {}, "keys": CodeGenType, "values": CodeGenType, }} CodeGenGenericType | ||
*/ | ||
/** | ||
* @typedef {{"type": "number", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "floatingPoint": boolean, "min"?: undefined|number, "max"?: undefined|number, "allowNull": boolean, }, "oneOf"?: undefined|(number)[], }} CodeGenNumberType | ||
* @typedef {{"type": "number", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "floatingPoint": boolean, "min"?: undefined|number, "max"?: undefined|number, "allowNull": boolean, }, "internalSettings": {}, "oneOf"?: undefined|(number)[], }} CodeGenNumberType | ||
*/ | ||
/** | ||
* @typedef {{"type": "object", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, "strict": boolean, }, "shortName"?: undefined|string, "keys": Object<string, CodeGenType>, "enableQueries": boolean, "queryOptions"?: undefined|{"withSoftDeletes": boolean, "withDates": boolean, "withPrimaryKey": boolean, "isView": boolean, }, "relations": (CodeGenRelationType)[], "where"?: undefined|{"type": string, "fields": ({"key": string, "name": string, "variant": "equal"|"notEqual"|"in"|"notIn"|"greaterThan"|"lowerThan"|"isNull"|"isNotNull"|"includeNotNull"|"like"|"iLike"|"notLike", })[], }, "orderBy"?: undefined|{"type": string, "specType": string, "fields": ({"key": string, "optional": boolean, })[], }, "partial"?: undefined|{"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: undefined|string, "isJsonb": boolean, })[], }, }} CodeGenObjectType | ||
* @typedef {{"type": "object", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, "strict": boolean, }, "internalSettings": {}, "shortName"?: undefined|string, "keys": Object<string, CodeGenType>, "enableQueries": boolean, "queryOptions"?: undefined|{"withSoftDeletes": boolean, "withDates": boolean, "withPrimaryKey": boolean, "isView": boolean, }, "relations": (CodeGenRelationType)[], "where"?: undefined|{"type": string, "fields": ({"key": string, "name": string, "variant": "equal"|"notEqual"|"in"|"notIn"|"greaterThan"|"lowerThan"|"isNull"|"isNotNull"|"includeNotNull"|"like"|"iLike"|"notLike", })[], }, "orderBy"?: undefined|{"type": string, "specType": string, "fields": ({"key": string, "optional": boolean, })[], }, "partial"?: undefined|{"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: undefined|string, "isJsonb": boolean, })[], }, }} CodeGenObjectType | ||
*/ | ||
@@ -51,12 +51,12 @@ /** | ||
/** | ||
* @typedef {{"type": "reference", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "reference": CodeGenType|{"uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, }, }} CodeGenReferenceType | ||
* @typedef {{"type": "reference", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "internalSettings": {}, "reference": CodeGenType|{"uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, }, }} CodeGenReferenceType | ||
*/ | ||
/** | ||
* @typedef {{"type": "string", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "trim": boolean, "lowerCase": boolean, "upperCase": boolean, "min": number, "max"?: undefined|number, "pattern"?: undefined|string, "allowNull": boolean, }, "oneOf"?: undefined|(string)[], }} CodeGenStringType | ||
* @typedef {{"type": "string", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "trim": boolean, "lowerCase": boolean, "upperCase": boolean, "min": number, "max"?: undefined|number, "pattern"?: undefined|string, "allowNull": boolean, }, "internalSettings": {}, "oneOf"?: undefined|(string)[], }} CodeGenStringType | ||
*/ | ||
/** | ||
* @typedef {{"type": "uuid", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, }} CodeGenUuidType | ||
* @typedef {{"type": "uuid", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, "internalSettings": {}, }} CodeGenUuidType | ||
*/ | ||
/** | ||
* @typedef {{"type": "route", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "method": "GET"|"POST"|"PUT"|"DELETE"|"HEAD"|"PATCH", "idempotent": boolean, "path": string, "tags": (string)[], "query"?: undefined|CodeGenType, "params"?: undefined|CodeGenType, "body"?: undefined|CodeGenType, "files"?: undefined|CodeGenType, "response"?: undefined|CodeGenType, }} CodeGenRouteType | ||
* @typedef {{"type": "route", "docString": string, "isOptional": boolean, "defaultValue"?: undefined|string|boolean|number, "uniqueName"?: undefined|string, "group"?: undefined|string, "name"?: undefined|string, "sql"?: undefined|{"primary": boolean, "searchable": boolean, }, "validator": {}, "internalSettings": {"requestBodyType": "json"|"form-data", }, "method": "GET"|"POST"|"PUT"|"DELETE"|"HEAD"|"PATCH", "idempotent": boolean, "path": string, "tags": (string)[], "query"?: undefined|CodeGenType, "params"?: undefined|CodeGenType, "body"?: undefined|CodeGenType, "files"?: undefined|CodeGenType, "response"?: undefined|CodeGenType, }} CodeGenRouteType | ||
*/ | ||
@@ -63,0 +63,0 @@ /** |
@@ -133,6 +133,24 @@ import { newLogger } from "@compas/insight"; | ||
compasStruct.body = transformBody(context, item.requestBody, compasStruct); | ||
let contentKey = "application/json"; | ||
compasStruct.internalSettings = { | ||
requestBodyType: "json", | ||
}; | ||
if ( | ||
isNil(item.requestBody?.content?.["application/json"]) && | ||
!isNil(item.requestBody?.content?.["multipart/form-data"]) | ||
) { | ||
contentKey = "multipart/form-data"; | ||
compasStruct.internalSettings.requestBodyType = "form-data"; | ||
} | ||
compasStruct.body = transformBody( | ||
context, | ||
contentKey, | ||
item.requestBody, | ||
compasStruct, | ||
); | ||
compasStruct.response = transformResponse( | ||
context, | ||
item.responses?.["200"], | ||
item.responses?.["200"] ?? item.responses?.["201"], | ||
compasStruct, | ||
@@ -225,11 +243,12 @@ ); | ||
* @param context | ||
* @param contentKey | ||
* @param input | ||
* @param compasStruct | ||
*/ | ||
function transformBody(context, input, compasStruct) { | ||
if (isNil(input?.content?.["application/json"]?.schema)) { | ||
function transformBody(context, contentKey, input, compasStruct) { | ||
if (isNil(input?.content?.[contentKey])) { | ||
return undefined; | ||
} | ||
const item = convertSchema(context, input.content["application/json"].schema); | ||
const item = convertSchema(context, input.content[contentKey].schema); | ||
item.group = compasStruct.group; | ||
@@ -236,0 +255,0 @@ item.name = `${compasStruct.name}Body`; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
408108
70
13181
+ Added@compas/cli@0.0.126(transitive)
+ Added@compas/insight@0.0.126(transitive)
+ Added@compas/stdlib@0.0.126(transitive)
+ Added@types/node@14.14.37(transitive)
- Removed@compas/cli@0.0.125(transitive)
- Removed@compas/insight@0.0.125(transitive)
- Removed@compas/stdlib@0.0.125(transitive)
- Removed@types/node@14.14.35(transitive)
Updated@compas/cli@0.0.126
Updated@compas/insight@0.0.126
Updated@compas/stdlib@0.0.126