diff --git a/packages/code-gen/src/database/generator.js b/packages/code-gen/src/database/generator.js index 47960bf233..cf7ce6b164 100644 --- a/packages/code-gen/src/database/generator.js +++ b/packages/code-gen/src/database/generator.js @@ -1,10 +1,9 @@ import { AppError, noop } from "@compas/stdlib"; import { structureModels } from "../processors/models.js"; -import { stringFormatNameForError } from "../string-format.js"; import { targetCustomSwitch } from "../target/switcher.js"; import { typesCacheGet } from "../types/cache.js"; import { typesGeneratorUseTypeName } from "../types/generator.js"; -import { upperCaseFirst } from "../utils.js"; +import { stringFormatNameForError, upperCaseFirst } from "../utils.js"; import { validatorGeneratorGenerateValidator, validatorGetNameAndImport, diff --git a/packages/code-gen/src/database/postgres.js b/packages/code-gen/src/database/postgres.js index 1687e3efe4..0eaa696f9c 100644 --- a/packages/code-gen/src/database/postgres.js +++ b/packages/code-gen/src/database/postgres.js @@ -16,9 +16,8 @@ import { import { structureModels } from "../processors/models.js"; import { referenceUtilsGetProperty } from "../processors/reference-utils.js"; import { structureResolveReference } from "../processors/structure.js"; -import { stringFormatNameForError } from "../string-format.js"; import { typesOptionalityIsOptional } from "../types/optionality.js"; -import { upperCaseFirst } from "../utils.js"; +import { stringFormatNameForError, upperCaseFirst } from "../utils.js"; /** * Write the DDL out for Postgres diff --git a/packages/code-gen/src/errors.d.ts b/packages/code-gen/src/errors.d.ts deleted file mode 100644 index 5b81855989..0000000000 --- a/packages/code-gen/src/errors.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -/** - * Combine the messages of the provided errors and throw a new error. - * - * Early returns if an empty array is provided. - * - * Other supported properties: - * - messages: expected to be a string[] - * - * @param {import("@compas/stdlib").AppError[]} errors - * @returns {void} - */ -export function errorsThrowCombinedError( - errors: import("@compas/stdlib").AppError[], -): void; -//# sourceMappingURL=errors.d.ts.map diff --git a/packages/code-gen/src/errors.js b/packages/code-gen/src/errors.js deleted file mode 100644 index 0c9f2bfa3a..0000000000 --- a/packages/code-gen/src/errors.js +++ /dev/null @@ -1,34 +0,0 @@ -import { AppError } from "@compas/stdlib"; - -/** - * Combine the messages of the provided errors and throw a new error. - * - * Early returns if an empty array is provided. - * - * Other supported properties: - * - messages: expected to be a string[] - * - * @param {import("@compas/stdlib").AppError[]} errors - * @returns {void} - */ -export function errorsThrowCombinedError(errors) { - if (errors.length === 0) { - return; - } - - const messages = []; - - for (const err of errors) { - if (err.info?.message) { - messages.push(err.info.message); - } else if (err.info?.messages) { - messages.push(...err.info.messages); - } else { - messages.push(AppError.format(err)); - } - } - - throw AppError.serverError({ - messages, - }); -} diff --git a/packages/code-gen/src/processors/any-of.js b/packages/code-gen/src/processors/any-of.js index 533c5bc5a9..30cdfb5270 100644 --- a/packages/code-gen/src/processors/any-of.js +++ b/packages/code-gen/src/processors/any-of.js @@ -1,6 +1,6 @@ import { AppError, isNil } from "@compas/stdlib"; -import { stringFormatNameForError } from "../string-format.js"; import { typesOptionalityIsOptional } from "../types/optionality.js"; +import { stringFormatNameForError } from "../utils.js"; import { referenceUtilsGetProperty } from "./reference-utils.js"; import { structureNamedTypes, structureResolveReference } from "./structure.js"; import { typeDefinitionTraverse } from "./type-definition-traverse.js"; diff --git a/packages/code-gen/src/processors/crud-validation.js b/packages/code-gen/src/processors/crud-validation.js index b3ee3da19d..75376fffe9 100644 --- a/packages/code-gen/src/processors/crud-validation.js +++ b/packages/code-gen/src/processors/crud-validation.js @@ -1,6 +1,8 @@ import { AppError, isNil } from "@compas/stdlib"; -import { errorsThrowCombinedError } from "../errors.js"; -import { stringFormatNameForError } from "../string-format.js"; +import { + errorsThrowCombinedError, + stringFormatNameForError, +} from "../utils.js"; import { crudInformationGetHasCustomReadableType, crudInformationGetModel, diff --git a/packages/code-gen/src/processors/model-keys.js b/packages/code-gen/src/processors/model-keys.js index 8cdcf0d310..cd1239e64c 100644 --- a/packages/code-gen/src/processors/model-keys.js +++ b/packages/code-gen/src/processors/model-keys.js @@ -1,7 +1,9 @@ import { AppError, isNil } from "@compas/stdlib"; import { DateType, UuidType } from "../builders/index.js"; -import { errorsThrowCombinedError } from "../errors.js"; -import { stringFormatNameForError } from "../string-format.js"; +import { + errorsThrowCombinedError, + stringFormatNameForError, +} from "../utils.js"; import { structureModels } from "./models.js"; import { referenceUtilsGetProperty } from "./reference-utils.js"; diff --git a/packages/code-gen/src/processors/model-name.js b/packages/code-gen/src/processors/model-name.js index 613cab64be..b56da6720d 100644 --- a/packages/code-gen/src/processors/model-name.js +++ b/packages/code-gen/src/processors/model-name.js @@ -1,6 +1,8 @@ import { AppError } from "@compas/stdlib"; -import { errorsThrowCombinedError } from "../errors.js"; -import { stringFormatNameForError } from "../string-format.js"; +import { + errorsThrowCombinedError, + stringFormatNameForError, +} from "../utils.js"; import { structureModels } from "./models.js"; /** diff --git a/packages/code-gen/src/processors/model-relation.js b/packages/code-gen/src/processors/model-relation.js index 2655bc2db6..05152a17ec 100644 --- a/packages/code-gen/src/processors/model-relation.js +++ b/packages/code-gen/src/processors/model-relation.js @@ -1,11 +1,11 @@ import { AppError } from "@compas/stdlib"; import { ReferenceType, RelationType } from "../builders/index.js"; -import { errorsThrowCombinedError } from "../errors.js"; +import { typesOptionalityIsOptional } from "../types/optionality.js"; import { + errorsThrowCombinedError, stringFormatNameForError, stringFormatRelation, -} from "../string-format.js"; -import { typesOptionalityIsOptional } from "../types/optionality.js"; +} from "../utils.js"; import { modelKeyGetPrimary } from "./model-keys.js"; import { structureModels } from "./models.js"; import { structureNamedTypes, structureResolveReference } from "./structure.js"; diff --git a/packages/code-gen/src/processors/model-where.js b/packages/code-gen/src/processors/model-where.js index 439a443d75..ecb39b3db4 100644 --- a/packages/code-gen/src/processors/model-where.js +++ b/packages/code-gen/src/processors/model-where.js @@ -7,8 +7,7 @@ import { ObjectType, ReferenceType, } from "../builders/index.js"; -import { stringFormatNameForError } from "../string-format.js"; -import { upperCaseFirst } from "../utils.js"; +import { stringFormatNameForError, upperCaseFirst } from "../utils.js"; import { modelKeyGetSearchable } from "./model-keys.js"; import { modelRelationGetInformation, diff --git a/packages/code-gen/src/processors/object-expansion.js b/packages/code-gen/src/processors/object-expansion.js index 18a36a7a33..c27d5674a3 100644 --- a/packages/code-gen/src/processors/object-expansion.js +++ b/packages/code-gen/src/processors/object-expansion.js @@ -1,6 +1,8 @@ import { AppError } from "@compas/stdlib"; -import { errorsThrowCombinedError } from "../errors.js"; -import { stringFormatNameForError } from "../string-format.js"; +import { + errorsThrowCombinedError, + stringFormatNameForError, +} from "../utils.js"; import { structureNamedTypes, structureResolveReference } from "./structure.js"; import { typeDefinitionTraverse } from "./type-definition-traverse.js"; diff --git a/packages/code-gen/src/processors/route-invalidation.js b/packages/code-gen/src/processors/route-invalidation.js index 08e2e0266e..f819cee15f 100644 --- a/packages/code-gen/src/processors/route-invalidation.js +++ b/packages/code-gen/src/processors/route-invalidation.js @@ -1,6 +1,8 @@ import { AppError, isNil } from "@compas/stdlib"; -import { errorsThrowCombinedError } from "../errors.js"; -import { stringFormatNameForError } from "../string-format.js"; +import { + errorsThrowCombinedError, + stringFormatNameForError, +} from "../utils.js"; import { structureRoutes } from "./routes.js"; import { structureResolveReference } from "./structure.js"; diff --git a/packages/code-gen/src/processors/route-trie.js b/packages/code-gen/src/processors/route-trie.js index 7f3ee403e5..00f150caf5 100644 --- a/packages/code-gen/src/processors/route-trie.js +++ b/packages/code-gen/src/processors/route-trie.js @@ -1,6 +1,8 @@ import { AppError } from "@compas/stdlib"; -import { errorsThrowCombinedError } from "../errors.js"; -import { stringFormatNameForError } from "../string-format.js"; +import { + errorsThrowCombinedError, + stringFormatNameForError, +} from "../utils.js"; import { structureRoutes } from "./routes.js"; /** diff --git a/packages/code-gen/src/processors/structure-name-checks.js b/packages/code-gen/src/processors/structure-name-checks.js index 78d0e6b83e..db868209cb 100644 --- a/packages/code-gen/src/processors/structure-name-checks.js +++ b/packages/code-gen/src/processors/structure-name-checks.js @@ -1,6 +1,8 @@ import { AppError } from "@compas/stdlib"; -import { errorsThrowCombinedError } from "../errors.js"; -import { stringFormatNameForError } from "../string-format.js"; +import { + errorsThrowCombinedError, + stringFormatNameForError, +} from "../utils.js"; import { structureNamedTypes } from "./structure.js"; import { typeDefinitionTraverse } from "./type-definition-traverse.js"; diff --git a/packages/code-gen/src/processors/structure.js b/packages/code-gen/src/processors/structure.js index b6e04cbddf..2463abf048 100644 --- a/packages/code-gen/src/processors/structure.js +++ b/packages/code-gen/src/processors/structure.js @@ -1,7 +1,9 @@ import { AppError, isNil } from "@compas/stdlib"; import { isNamedTypeBuilderLike } from "../builders/index.js"; -import { errorsThrowCombinedError } from "../errors.js"; -import { stringFormatNameForError } from "../string-format.js"; +import { + errorsThrowCombinedError, + stringFormatNameForError, +} from "../utils.js"; import { typeDefinitionTraverse } from "./type-definition-traverse.js"; /** diff --git a/packages/code-gen/src/string-format.d.ts b/packages/code-gen/src/string-format.d.ts deleted file mode 100644 index d880be7316..0000000000 --- a/packages/code-gen/src/string-format.d.ts +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Format a type name for error messages - * - * @param {undefined|{ group?: string, name?: string, type?: string }} type - * @returns {string} - */ -export function stringFormatNameForError( - type: - | undefined - | { - group?: string; - name?: string; - type?: string; - }, -): string; -/** - * Format a full relation name - * - * @param {string} ownName - * @param {string} inverseName - * @param {string} ownKey - * @param {string} inverseKey - * @returns {string} - */ -export function stringFormatRelation( - ownName: string, - inverseName: string, - ownKey: string, - inverseKey: string, -): string; -//# sourceMappingURL=string-format.d.ts.map diff --git a/packages/code-gen/src/string-format.js b/packages/code-gen/src/string-format.js deleted file mode 100644 index 39c67f9478..0000000000 --- a/packages/code-gen/src/string-format.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Format a type name for error messages - * - * @param {undefined|{ group?: string, name?: string, type?: string }} type - * @returns {string} - */ -export function stringFormatNameForError(type) { - if (type?.group && type?.name) { - return `('${type.group}', '${type.name}')`; - } - - if (type?.type) { - return `(${type.type})`; - } - - return `(anonymous)`; -} - -/** - * Format a full relation name - * - * @param {string} ownName - * @param {string} inverseName - * @param {string} ownKey - * @param {string} inverseKey - * @returns {string} - */ -export function stringFormatRelation(ownName, inverseName, ownKey, inverseKey) { - return `from '${ownName}' to '${inverseName}' via '${ownName}#${ownKey}' to '${inverseName}#${inverseKey}'`; -} diff --git a/packages/code-gen/src/string-format.test.js b/packages/code-gen/src/string-format.test.js deleted file mode 100644 index eb27340b01..0000000000 --- a/packages/code-gen/src/string-format.test.js +++ /dev/null @@ -1,43 +0,0 @@ -import { mainTestFn, test } from "@compas/cli"; -import { stringFormatNameForError } from "./string-format.js"; - -mainTestFn(import.meta); - -test("code-gen/string-format", (t) => { - t.test("stringFormatNameForError", (t) => { - t.test("format group and name", (t) => { - const result = stringFormatNameForError({ - group: "foo", - name: "bar", - }); - - t.equal(result, "('foo', 'bar')"); - }); - - t.test("format type property", (t) => { - const result = stringFormatNameForError({ - type: "boolean", - }); - - t.equal(result, "(boolean)"); - }); - - t.test("default to anonymous", (t) => { - const result = stringFormatNameForError({}); - - t.equal(result, "(anonymous)"); - }); - - t.test("return default value if only group is provided", (t) => { - const result = stringFormatNameForError({ group: "foo" }); - - t.equal(result, "(anonymous)"); - }); - - t.test("return default value if only name is provided", (t) => { - const result = stringFormatNameForError({ name: "foo" }); - - t.equal(result, "(anonymous)"); - }); - }); -}); diff --git a/packages/code-gen/src/utils.d.ts b/packages/code-gen/src/utils.d.ts index 4f57890900..e2743d002b 100644 --- a/packages/code-gen/src/utils.d.ts +++ b/packages/code-gen/src/utils.d.ts @@ -12,4 +12,48 @@ export function upperCaseFirst(str?: string | undefined): string; * @returns {string} */ export function lowerCaseFirst(str?: string | undefined): string; +/** + * Format a type name for error messages + * + * @param {undefined|{ group?: string, name?: string, type?: string }} type + * @returns {string} + */ +export function stringFormatNameForError( + type: + | undefined + | { + group?: string; + name?: string; + type?: string; + }, +): string; +/** + * Format a full relation name + * + * @param {string} ownName + * @param {string} inverseName + * @param {string} ownKey + * @param {string} inverseKey + * @returns {string} + */ +export function stringFormatRelation( + ownName: string, + inverseName: string, + ownKey: string, + inverseKey: string, +): string; +/** + * Combine the messages of the provided errors and throw a new error. + * + * Early returns if an empty array is provided. + * + * Other supported properties: + * - messages: expected to be a string[] + * + * @param {import("@compas/stdlib").AppError[]} errors + * @returns {void} + */ +export function errorsThrowCombinedError( + errors: import("@compas/stdlib").AppError[], +): void; //# sourceMappingURL=utils.d.ts.map diff --git a/packages/code-gen/src/utils.js b/packages/code-gen/src/utils.js index b7f7bb0105..7e1a96867b 100644 --- a/packages/code-gen/src/utils.js +++ b/packages/code-gen/src/utils.js @@ -1,3 +1,5 @@ +import { AppError } from "@compas/stdlib"; + /** * Uppercase first character of the input string * @@ -17,3 +19,67 @@ export function upperCaseFirst(str = "") { export function lowerCaseFirst(str = "") { return str.length > 0 ? str[0].toLowerCase() + str.substring(1) : ""; } + +/** + * Format a type name for error messages + * + * @param {undefined|{ group?: string, name?: string, type?: string }} type + * @returns {string} + */ +export function stringFormatNameForError(type) { + if (type?.group && type?.name) { + return `('${type.group}', '${type.name}')`; + } + + if (type?.type) { + return `(${type.type})`; + } + + return `(anonymous)`; +} + +/** + * Format a full relation name + * + * @param {string} ownName + * @param {string} inverseName + * @param {string} ownKey + * @param {string} inverseKey + * @returns {string} + */ +export function stringFormatRelation(ownName, inverseName, ownKey, inverseKey) { + return `from '${ownName}' to '${inverseName}' via '${ownName}#${ownKey}' to '${inverseName}#${inverseKey}'`; +} + +/** + * Combine the messages of the provided errors and throw a new error. + * + * Early returns if an empty array is provided. + * + * Other supported properties: + * - messages: expected to be a string[] + * + * @param {import("@compas/stdlib").AppError[]} errors + * @returns {void} + */ +export function errorsThrowCombinedError(errors) { + if (errors.length === 0) { + return; + } + + const messages = []; + + for (const err of errors) { + if (err.info?.message) { + messages.push(err.info.message); + } else if (err.info?.messages) { + messages.push(...err.info.messages); + } else { + messages.push(AppError.format(err)); + } + } + + throw AppError.serverError({ + messages, + }); +} diff --git a/packages/code-gen/src/errors.test.js b/packages/code-gen/src/utils.test.js similarity index 59% rename from packages/code-gen/src/errors.test.js rename to packages/code-gen/src/utils.test.js index cd6f661944..276220c284 100644 --- a/packages/code-gen/src/errors.test.js +++ b/packages/code-gen/src/utils.test.js @@ -1,10 +1,47 @@ import { mainTestFn, test } from "@compas/cli"; import { AppError } from "@compas/stdlib"; -import { errorsThrowCombinedError } from "./errors.js"; +import { errorsThrowCombinedError, stringFormatNameForError } from "./utils.js"; mainTestFn(import.meta); -test("code-gen/errors", (t) => { +test("code-gen/utils", (t) => { + t.test("stringFormatNameForError", (t) => { + t.test("format group and name", (t) => { + const result = stringFormatNameForError({ + group: "foo", + name: "bar", + }); + + t.equal(result, "('foo', 'bar')"); + }); + + t.test("format type property", (t) => { + const result = stringFormatNameForError({ + type: "boolean", + }); + + t.equal(result, "(boolean)"); + }); + + t.test("default to anonymous", (t) => { + const result = stringFormatNameForError({}); + + t.equal(result, "(anonymous)"); + }); + + t.test("return default value if only group is provided", (t) => { + const result = stringFormatNameForError({ group: "foo" }); + + t.equal(result, "(anonymous)"); + }); + + t.test("return default value if only name is provided", (t) => { + const result = stringFormatNameForError({ name: "foo" }); + + t.equal(result, "(anonymous)"); + }); + }); + t.test("errorsThrowCombinedError", (t) => { t.test("returns with empty error array", (t) => { errorsThrowCombinedError([]); diff --git a/packages/code-gen/src/validators/generator.js b/packages/code-gen/src/validators/generator.js index 832151c9e3..ce799acc3c 100644 --- a/packages/code-gen/src/validators/generator.js +++ b/packages/code-gen/src/validators/generator.js @@ -4,11 +4,11 @@ import { structureNamedTypes, structureResolveReference, } from "../processors/structure.js"; -import { stringFormatNameForError } from "../string-format.js"; import { targetLanguageSwitch } from "../target/switcher.js"; import { typesCacheGet } from "../types/cache.js"; import { typesGeneratorGenerateNamedType } from "../types/generator.js"; import { typesOptionalityIsOptional } from "../types/optionality.js"; +import { stringFormatNameForError } from "../utils.js"; import { validatorJavascriptAny, validatorJavascriptAnyOf,