New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@compas/code-gen

Package Overview
Dependencies
Maintainers
1
Versions
202
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@compas/code-gen - npm Package Compare versions

Comparing version 0.0.105 to 0.0.106

10

package.json
{
"name": "@compas/code-gen",
"version": "0.0.105",
"version": "0.0.106",
"description": "Generate various boring parts of your server",

@@ -18,5 +18,5 @@ "main": "./index.js",

"dependencies": {
"@compas/cli": "0.0.105",
"@compas/insight": "0.0.105",
"@compas/stdlib": "0.0.105"
"@compas/cli": "0.0.106",
"@compas/insight": "0.0.106",
"@compas/stdlib": "0.0.106"
},

@@ -44,3 +44,3 @@ "maintainers": [

},
"gitHead": "6ab87bab8ca01fd9a83d105d5bca7eca87e5c931"
"gitHead": "c567f9d3f31a2e19a7cc88f0ea4d19a4e290b597"
}

@@ -7,84 +7,72 @@ // Generated by @compas/code-gen

/**
* @name CodeGenAnyOfType
* @typedef {{"type": "anyOf", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "values": (CodeGenType)[], }}
* @typedef {import("@compas/code-gen").App} App
*/
/**
* @name CodeGenType
* @typedef {CodeGenAnyType|CodeGenAnyOfType|CodeGenArrayType|CodeGenBooleanType|CodeGenDateType|CodeGenFileType|CodeGenGenericType|CodeGenNumberType|CodeGenObjectType|CodeGenReferenceType|CodeGenRelationType|CodeGenStringType|CodeGenUuidType|CodeGenRouteType}
* @typedef {import("@compas/code-gen").TypeCreator} TypeCreator
*/
/**
* @name CodeGenAnyType
* @typedef {{"type": "any", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, "rawValue"?: string, "rawValueImport": {"javaScript"?: string, "typeScript"?: string, }, "rawValidator"?: string, "rawValidatorImport": {"javaScript"?: string, "typeScript"?: string, }, }}
* @typedef {import("@compas/code-gen").RouteCreator} RouteCreator
*/
/**
* @name CodeGenArrayType
* @typedef {{"type": "array", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "min"?: number, "max"?: number, }, "values": CodeGenType, }}
* @typedef {{"type": "anyOf", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "values": (CodeGenType)[], }} CodeGenAnyOfType
*/
/**
* @name CodeGenBooleanType
* @typedef {{"type": "boolean", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "allowNull": boolean, }, "oneOf"?: boolean, }}
* @typedef {CodeGenAnyType|CodeGenAnyOfType|CodeGenArrayType|CodeGenBooleanType|CodeGenDateType|CodeGenFileType|CodeGenGenericType|CodeGenNumberType|CodeGenObjectType|CodeGenReferenceType|CodeGenRelationType|CodeGenStringType|CodeGenUuidType|CodeGenRouteType} CodeGenType
*/
/**
* @name CodeGenDateType
* @typedef {{"type": "date", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, }}
* @typedef {{"type": "any", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, "rawValue"?: string, "rawValueImport": {"javaScript"?: string, "typeScript"?: string, }, "rawValidator"?: string, "rawValidatorImport": {"javaScript"?: string, "typeScript"?: string, }, }} CodeGenAnyType
*/
/**
* @name CodeGenFileType
* @typedef {{"type": "file", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, }}
* @typedef {{"type": "array", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "min"?: number, "max"?: number, }, "values": CodeGenType, }} CodeGenArrayType
*/
/**
* @name CodeGenGenericType
* @typedef {{"type": "generic", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "keys": CodeGenType, "values": CodeGenType, }}
* @typedef {{"type": "boolean", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "allowNull": boolean, }, "oneOf"?: boolean, }} CodeGenBooleanType
*/
/**
* @name CodeGenNumberType
* @typedef {{"type": "number", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "floatingPoint": boolean, "min"?: number, "max"?: number, "allowNull": boolean, }, "oneOf"?: (number)[], }}
* @typedef {{"type": "date", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, }} CodeGenDateType
*/
/**
* @name CodeGenObjectType
* @typedef {{"type": "object", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"strict": boolean, }, "keys": Object<string, CodeGenType>, "enableQueries": boolean, "queryOptions"?: {"withSoftDeletes": boolean, "withDates": boolean, "withPrimaryKey": boolean, "isView": boolean, }, "relations": (CodeGenRelationType)[], "shortName"?: string, "where"?: {"type": string, "fields": ({"key": string, "name": string, "variant": "equal"|"notEqual"|"in"|"notIn"|"greaterThan"|"lowerThan"|"isNull"|"isNotNull"|"includeNotNull"|"like"|"notLike", })[], }, "partial"?: {"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: string, "isJsonb": boolean, })[], }, }}
* @typedef {{"type": "file", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, }} CodeGenFileType
*/
/**
* @name CodeGenRelationType
* @typedef {{"type": "relation", "subType": "manyToOne"|"oneToMany"|"oneToOne"|"oneToOneReverse", "reference": CodeGenReferenceType, "ownKey": string, "referencedKey"?: string, "isOptional": boolean, }}
* @typedef {{"type": "generic", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "keys": CodeGenType, "values": CodeGenType, }} CodeGenGenericType
*/
/**
* @name CodeGenReferenceType
* @typedef {{"type": "reference", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "reference": CodeGenType|{"uniqueName"?: string, "group"?: string, "name"?: string, }, }}
* @typedef {{"type": "number", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "floatingPoint": boolean, "min"?: number, "max"?: number, "allowNull": boolean, }, "oneOf"?: (number)[], }} CodeGenNumberType
*/
/**
* @name CodeGenStringType
* @typedef {{"type": "string", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "trim": boolean, "lowerCase": boolean, "upperCase": boolean, "min": number, "max"?: number, "pattern"?: string, "allowNull": boolean, }, "oneOf"?: (string)[], }}
* @typedef {{"type": "object", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"strict": boolean, }, "keys": Object<string, CodeGenType>, "enableQueries": boolean, "queryOptions"?: {"withSoftDeletes": boolean, "withDates": boolean, "withPrimaryKey": boolean, "isView": boolean, }, "relations": (CodeGenRelationType)[], "shortName"?: string, "where"?: {"type": string, "fields": ({"key": string, "name": string, "variant": "equal"|"notEqual"|"in"|"notIn"|"greaterThan"|"lowerThan"|"isNull"|"isNotNull"|"includeNotNull"|"like"|"notLike", })[], }, "partial"?: {"insertType": string, "updateType": string, "fields": ({"key": string, "defaultValue"?: string, "isJsonb": boolean, })[], }, }} CodeGenObjectType
*/
/**
* @name CodeGenUuidType
* @typedef {{"type": "uuid", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, }}
* @typedef {{"type": "relation", "subType": "manyToOne"|"oneToMany"|"oneToOne"|"oneToOneReverse", "reference": CodeGenReferenceType, "ownKey": string, "referencedKey"?: string, "isOptional": boolean, }} CodeGenRelationType
*/
/**
* @name CodeGenRouteType
* @typedef {{"type": "route", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "method": "GET"|"POST"|"PUT"|"DELETE"|"HEAD"|"PATCH", "path": string, "tags": (string)[], "query"?: CodeGenType, "params"?: CodeGenType, "body"?: CodeGenType, "files"?: CodeGenType, "response"?: CodeGenType, }}
* @typedef {{"type": "reference", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "reference": CodeGenType|{"uniqueName"?: string, "group"?: string, "name"?: string, }, }} CodeGenReferenceType
*/
/**
* @name CodeGenContext
* @typedef {{"options": CodeGenGenerateOpts, "structure": CodeGenStructure, "extension": ".js"|".ts", "importExtension": string, "outputFiles": (CodeGenFile)[], "rootExports": (string)[], "errors": ({"key": "sqlMissingPrimaryKey", "typeName": string, }|{"key": "sqlForgotEnableQueries", "typeName": string, "referencedByType": string, }|{"key": "sqlMissingOneToMany", "referencedByGroup": string, "referencedByType": string, "typeName": string, "relationOwnKey": string, }|{"key": "sqlEnableValidator", }|{"key": "sqlThrowingValidators", })[], }}
* @typedef {{"type": "string", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"convert": boolean, "trim": boolean, "lowerCase": boolean, "upperCase": boolean, "min": number, "max"?: number, "pattern"?: string, "allowNull": boolean, }, "oneOf"?: (string)[], }} CodeGenStringType
*/
/**
* @name CodeGenGenerateOpts
* @typedef {{"enabledGroups": (string)[], "isBrowser": boolean, "isNode": boolean, "isNodeServer": boolean, "enabledGenerators": ("type"|"validator"|"router"|"sql"|"apiClient"|"reactQuery")[], "useTypescript": boolean, "dumpStructure": boolean, "dumpApiStructure": boolean, "dumpPostgres": boolean, "throwingValidators": boolean, "fileHeader": string, "outputDirectory": string, }}
* @typedef {{"type": "uuid", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {"allowNull": boolean, }, }} CodeGenUuidType
*/
/**
* @name CodeGenStructure
* @typedef {Object<string, Object<string, CodeGenType>>}
* @typedef {{"type": "route", "docString": string, "isOptional": boolean, "defaultValue"?: string|boolean|number, "uniqueName"?: string, "group"?: string, "name"?: string, "sql"?: {"primary": boolean, "searchable": boolean, }, "validator": {}, "method": "GET"|"POST"|"PUT"|"DELETE"|"HEAD"|"PATCH", "path": string, "tags": (string)[], "query"?: CodeGenType, "params"?: CodeGenType, "body"?: CodeGenType, "files"?: CodeGenType, "response"?: CodeGenType, }} CodeGenRouteType
*/
/**
* @name CodeGenFile
* @typedef {{"relativePath": string, "contents": string, }}
* @typedef {{"options": CodeGenGenerateOpts, "structure": CodeGenStructure, "extension": ".js"|".ts", "importExtension": string, "outputFiles": (CodeGenFile)[], "rootExports": (string)[], "errors": ({"key": "sqlMissingPrimaryKey", "typeName": string, }|{"key": "sqlForgotEnableQueries", "typeName": string, "referencedByType": string, }|{"key": "sqlMissingOneToMany", "referencedByGroup": string, "referencedByType": string, "typeName": string, "relationOwnKey": string, }|{"key": "sqlEnableValidator", }|{"key": "sqlThrowingValidators", })[], }} CodeGenContext
*/
/**
* @name CodeGenTemplateState
* @typedef {{"phase": "init"|"collect"|"finish", }}
* @typedef {{"enabledGroups": (string)[], "isBrowser": boolean, "isNode": boolean, "isNodeServer": boolean, "enabledGenerators": ("type"|"validator"|"router"|"sql"|"apiClient"|"reactQuery")[], "useTypescript": boolean, "dumpStructure": boolean, "dumpApiStructure": boolean, "dumpPostgres": boolean, "throwingValidators": boolean, "fileHeader": string, "outputDirectory": string, }} CodeGenGenerateOpts
*/
/**
* @name CodeGenTypeSettings
* @typedef {{"isJSON"?: boolean, "nestedIsJSON"?: boolean, "useDefaults"?: boolean, "useTypescript"?: boolean, "isNode"?: boolean, "isBrowser"?: boolean, "suffix"?: string, "fileTypeIO"?: "input"|"outputRouter"|"outputClient", }}
* @typedef {Object<string, Object<string, CodeGenType>>} CodeGenStructure
*/
/**
* @typedef {{"relativePath": string, "contents": string, }} CodeGenFile
*/
/**
* @typedef {{"phase": "init"|"collect"|"finish", }} CodeGenTemplateState
*/
/**
* @typedef {{"isJSON"?: boolean, "nestedIsJSON"?: boolean, "useDefaults"?: boolean, "useTypescript"?: boolean, "isNode"?: boolean, "isBrowser"?: boolean, "suffix"?: string, "fileTypeIO"?: "input"|"outputRouter"|"outputClient", }} CodeGenTypeSettings
*/

@@ -23,3 +23,3 @@ import { js } from "../tag/index.js";

imports.destructureImport("query", "@compas/store");
imports.destructureImport("isQueryObject", "@compas/store");
imports.destructureImport("isQueryPart", "@compas/store");

@@ -26,0 +26,0 @@ // Generate field sets and the check function

@@ -95,6 +95,6 @@ import { isNil } from "@compas/stdlib";

},
rawValidator: "isQueryObject",
rawValidator: "isQueryPart",
rawValidatorImport: {
javaScript: `import { isQueryObject } from "@compas/store";`,
typeScript: `import { isQueryObject } from "@compas/store";`,
javaScript: `import { isQueryPart } from "@compas/store";`,
typeScript: `import { isQueryPart } from "@compas/store";`,
},

@@ -194,3 +194,3 @@ },

str += `
if (isQueryObject(where.${field.name})) {
if (isQueryPart(where.${field.name})) {
strings.push(\` AND $\{tableName}"${field.key}" = ANY(\`, ")");

@@ -214,3 +214,3 @@ values.push(where.${field.name}, undefined);

str += `
if (isQueryObject(where.${field.name})) {
if (isQueryPart(where.${field.name})) {
strings.push(\` AND $\{tableName}"${field.key}" != ANY(\`, ")");

@@ -217,0 +217,0 @@ values.push(where.${field.name}, undefined);

@@ -7,4 +7,3 @@ import { isNil } from "@compas/stdlib";

/**
* @name StateFn
* @callback
* @callback StateFn
* @param {CodeGenTemplateState} state

@@ -11,0 +10,0 @@ * @returns {boolean|number|string|undefined|StateFn|Function}

@@ -113,8 +113,9 @@ import { isNil } from "@compas/stdlib";

const typeFile = js`
${[...context.types.rawImports]}
// An export soo all things work correctly with linters, ts, ...
export const __generated__ = true;
${[...context.types.rawImports]}
// An export soo all things work correctly with linters, ts, ...
export const __generated__ = true;
${getMemoizedNamedTypes(context)}
`;
${getStaticImportedTypesForPackages(context)}
${getMemoizedNamedTypes(context)}
`;

@@ -362,4 +363,6 @@ context.outputFiles.push({

intermediate += `export type ${name} = `;
intermediate += type;
intermediate += `;`;
} else {
intermediate += `/**\n * @name ${name}\n`;
intermediate += `/**\n`;

@@ -371,16 +374,48 @@ if (uniqueNameDocsMap[name]) {

intermediate += ` * @typedef {`;
intermediate += type;
intermediate += `} ${name}\n */`;
}
intermediate += type;
result.push(intermediate);
}
if (useTypescript) {
intermediate += `;`;
} else {
intermediate += `}\n */`;
}
return result;
}
result.push(intermediate);
/**
* Import types from packages that are used.
* Link of things like QueryParts
*
* @param {CodeGenContext} context
* @returns {string[]}
*/
function getStaticImportedTypesForPackages(context) {
if (context.options.useTypescript) {
return [];
}
const result = [
'/**\n * @typedef {import("@compas/code-gen").App} App\n */',
'/**\n * @typedef {import("@compas/code-gen").TypeCreator} TypeCreator\n */',
'/**\n * @typedef {import("@compas/code-gen").RouteCreator} RouteCreator\n */',
];
if (context.options.enabledGenerators.indexOf("sql") !== -1) {
result.push(
'/**\n * @typedef {import("@compas/store").Postgres} Postgres\n */',
'/**\n * @typedef import("@compas/store").QueryPart} QueryPart\n */',
'/**\n * @typedef import("@compas/store").Minio} Minio\n */',
);
}
if (context.options.enabledGenerators.indexOf("router") !== -1) {
result.push(
'/**\n * @typedef {import("@compas/insight").Logger} Logger\n */',
'/**\n * @typedef {import("@compas/insight").Event} Event\n */',
'/**\n * @typedef {import("@compas/server").Context} Context\n */',
'/**\n * @typedef {import("@compas/server").Next} Next\n */',
'/**\n * @typedef {import("@compas/server").Middleware} Middleware\n */',
);
}
return result;
}

@@ -9,4 +9,3 @@ import { isNil } from "@compas/stdlib";

/**
* @name ValidatorContext
* @typedef {object}
* @typedef ValidatorContext
* @property {CodeGenContext} context

@@ -20,7 +19,6 @@ * @property {boolean} collectErrors

/**
* @name GeneratorBuildError
* Calls generated buildError function to construct an error
*
* @typedef {function(key: string, info: string, errors: string=, errorsReturn:
* boolean=): string}
* boolean=): string} GeneratorBuildError
*/

@@ -333,42 +331,42 @@

const fn = js`
/**
* @param {*} value
* @param {string} propertyPath
* @param {{ key: string, info: any }[]} errors
* @param {string} parentType
* @returns {${generateTypeDefinition(context.context, type, {
useDefaults: true,
})}|undefined}
*/
export function anonymousValidator${hash}(value${withTypescript(
/**
* @param {*} value
* @param {string} propertyPath
* @param {{ key: string, info: any }[]} errors
* @param {string} parentType
* @returns {${generateTypeDefinition(context.context, type, {
useDefaults: true,
})}|undefined}
*/
export function anonymousValidator${hash}(value${withTypescript(
context,
": any",
)},
propertyPath${withTypescript(
context,
": string",
)},
errors${withTypescript(
context,
": { key: string, info: any }[]",
)} = [],
parentType${withTypescript(
context,
": string",
)} = "${type.type}",
) {
if (isNil(value)) {
${() => {
if (type.isOptional && type.defaultValue) {
return `return ${type.defaultValue}`;
} else if (type.isOptional) {
return `return value`;
}
propertyPath${withTypescript(
context,
": string",
)},
errors${withTypescript(
context,
": { key: string, info: any }[]",
)} = [],
parentType${withTypescript(
context,
": string",
)} = "${type.type}",
) {
if (isNil(value)) {
${() => {
if (type.isOptional && type.defaultValue) {
return `return ${type.defaultValue}`;
} else if (type.isOptional) {
return `return value`;
}
return buildError("undefined", "{ propertyPath }");
}}
return buildError("undefined", "{ propertyPath }");
}}
}
${anonymousValidatorForType(context, imports, type)}
}
${anonymousValidatorForType(context, imports, type)}
}
`;
`;

@@ -375,0 +373,0 @@ context.anonymousFunctions.push(fn);

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc