tsondb 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/lib/ModelContainer.d.ts +17 -0
- package/lib/ModelContainer.js +63 -0
- package/lib/Schema.d.ts +8 -0
- package/lib/Schema.js +72 -0
- package/lib/client/api.d.ts +11 -0
- package/lib/client/api.js +83 -0
- package/lib/client/components/Layout.d.ts +10 -0
- package/lib/client/components/Layout.js +4 -0
- package/lib/client/components/Select.d.ts +3 -0
- package/lib/client/components/Select.js +2 -0
- package/lib/client/components/typeInputs/ArrayTypeInput.d.ts +13 -0
- package/lib/client/components/typeInputs/ArrayTypeInput.js +10 -0
- package/lib/client/components/typeInputs/BooleanTypeInput.d.ts +9 -0
- package/lib/client/components/typeInputs/BooleanTypeInput.js +6 -0
- package/lib/client/components/typeInputs/DateTypeInput.d.ts +9 -0
- package/lib/client/components/typeInputs/DateTypeInput.js +9 -0
- package/lib/client/components/typeInputs/FloatTypeInput.d.ts +9 -0
- package/lib/client/components/typeInputs/FloatTypeInput.js +15 -0
- package/lib/client/components/typeInputs/GenericTypeArgumentIdentifierTypeInput.d.ts +7 -0
- package/lib/client/components/typeInputs/GenericTypeArgumentIdentifierTypeInput.js +4 -0
- package/lib/client/components/typeInputs/IncludeIdentifierTypeInput.d.ts +13 -0
- package/lib/client/components/typeInputs/IncludeIdentifierTypeInput.js +18 -0
- package/lib/client/components/typeInputs/IntegerTypeInput.d.ts +9 -0
- package/lib/client/components/typeInputs/IntegerTypeInput.js +15 -0
- package/lib/client/components/typeInputs/NestedEntityMapTypeInput.d.ts +13 -0
- package/lib/client/components/typeInputs/NestedEntityMapTypeInput.js +25 -0
- package/lib/client/components/typeInputs/ObjectTypeInput.d.ts +13 -0
- package/lib/client/components/typeInputs/ObjectTypeInput.js +20 -0
- package/lib/client/components/typeInputs/ReferenceIdentifierTypeInput.d.ts +11 -0
- package/lib/client/components/typeInputs/ReferenceIdentifierTypeInput.js +9 -0
- package/lib/client/components/typeInputs/StringTypeInput.d.ts +9 -0
- package/lib/client/components/typeInputs/StringTypeInput.js +10 -0
- package/lib/client/components/typeInputs/TypeInput.d.ts +13 -0
- package/lib/client/components/typeInputs/TypeInput.js +87 -0
- package/lib/client/components/typeInputs/utils/EnumDeclField.d.ts +13 -0
- package/lib/client/components/typeInputs/utils/EnumDeclField.js +38 -0
- package/lib/client/components/typeInputs/utils/MismatchingTypeError.d.ts +7 -0
- package/lib/client/components/typeInputs/utils/MismatchingTypeError.js +4 -0
- package/lib/client/components/typeInputs/utils/ValidationErrors.d.ts +6 -0
- package/lib/client/components/typeInputs/utils/ValidationErrors.js +4 -0
- package/lib/client/hooks/useEntityFromRoute.d.ts +5 -0
- package/lib/client/hooks/useEntityFromRoute.js +20 -0
- package/lib/client/hooks/useInstanceNamesByEntity.d.ts +3 -0
- package/lib/client/hooks/useInstanceNamesByEntity.js +18 -0
- package/lib/client/hooks/useSecondaryDeclarations.d.ts +3 -0
- package/lib/client/hooks/useSecondaryDeclarations.js +18 -0
- package/lib/client/index.d.ts +1 -0
- package/lib/client/index.js +11 -0
- package/lib/client/routes/CreateInstance.d.ts +2 -0
- package/lib/client/routes/CreateInstance.js +68 -0
- package/lib/client/routes/Entity.d.ts +2 -0
- package/lib/client/routes/Entity.js +47 -0
- package/lib/client/routes/Home.d.ts +2 -0
- package/lib/client/routes/Home.js +18 -0
- package/lib/client/routes/Instance.d.ts +2 -0
- package/lib/client/routes/Instance.js +73 -0
- package/lib/client/routes/NotFound.d.ts +2 -0
- package/lib/client/routes/NotFound.js +5 -0
- package/lib/client/utils/typeSkeleton.d.ts +3 -0
- package/lib/client/utils/typeSkeleton.js +51 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/renderers/Output.d.ts +4 -0
- package/lib/renderers/Output.js +1 -0
- package/lib/renderers/jsonschema/index.d.ts +6 -0
- package/lib/renderers/jsonschema/index.js +12 -0
- package/lib/renderers/jsonschema/render.d.ts +5 -0
- package/lib/renderers/jsonschema/render.js +156 -0
- package/lib/renderers/ts/index.d.ts +6 -0
- package/lib/renderers/ts/index.js +11 -0
- package/lib/renderers/ts/render.d.ts +5 -0
- package/lib/renderers/ts/render.js +109 -0
- package/lib/schema/Node.d.ts +37 -0
- package/lib/schema/Node.js +79 -0
- package/lib/schema/declarations/Declaration.d.ts +44 -0
- package/lib/schema/declarations/Declaration.js +98 -0
- package/lib/schema/declarations/EntityDecl.d.ts +73 -0
- package/lib/schema/declarations/EntityDecl.js +57 -0
- package/lib/schema/declarations/EnumDecl.d.ts +33 -0
- package/lib/schema/declarations/EnumDecl.js +104 -0
- package/lib/schema/declarations/TypeAliasDecl.d.ts +33 -0
- package/lib/schema/declarations/TypeAliasDecl.js +49 -0
- package/lib/schema/index.d.ts +20 -0
- package/lib/schema/index.js +20 -0
- package/lib/schema/parameters/TypeParameter.d.ts +14 -0
- package/lib/schema/parameters/TypeParameter.js +11 -0
- package/lib/schema/types/Type.d.ts +42 -0
- package/lib/schema/types/Type.js +177 -0
- package/lib/schema/types/generic/ArrayType.d.ts +30 -0
- package/lib/schema/types/generic/ArrayType.js +38 -0
- package/lib/schema/types/generic/ObjectType.d.ts +47 -0
- package/lib/schema/types/generic/ObjectType.js +70 -0
- package/lib/schema/types/primitives/BooleanType.d.ts +15 -0
- package/lib/schema/types/primitives/BooleanType.js +15 -0
- package/lib/schema/types/primitives/DateType.d.ts +16 -0
- package/lib/schema/types/primitives/DateType.js +17 -0
- package/lib/schema/types/primitives/FloatType.d.ts +26 -0
- package/lib/schema/types/primitives/FloatType.js +17 -0
- package/lib/schema/types/primitives/IntegerType.d.ts +26 -0
- package/lib/schema/types/primitives/IntegerType.js +21 -0
- package/lib/schema/types/primitives/NumericType.d.ts +6 -0
- package/lib/schema/types/primitives/NumericType.js +2 -0
- package/lib/schema/types/primitives/PrimitiveType.d.ts +6 -0
- package/lib/schema/types/primitives/PrimitiveType.js +1 -0
- package/lib/schema/types/primitives/StringType.d.ts +25 -0
- package/lib/schema/types/primitives/StringType.js +20 -0
- package/lib/schema/types/references/GenericArgumentIdentifierType.d.ts +21 -0
- package/lib/schema/types/references/GenericArgumentIdentifierType.js +18 -0
- package/lib/schema/types/references/IncludeIdentifierType.d.ts +28 -0
- package/lib/schema/types/references/IncludeIdentifierType.js +25 -0
- package/lib/schema/types/references/NestedEntityMapType.d.ts +36 -0
- package/lib/schema/types/references/NestedEntityMapType.js +67 -0
- package/lib/schema/types/references/ReferenceIdentifierType.d.ts +23 -0
- package/lib/schema/types/references/ReferenceIdentifierType.js +21 -0
- package/lib/schema/validation/options.d.ts +4 -0
- package/lib/schema/validation/options.js +12 -0
- package/lib/schema/validation/type.d.ts +4 -0
- package/lib/schema/validation/type.js +1 -0
- package/lib/server/index.d.ts +8 -0
- package/lib/server/index.js +207 -0
- package/lib/server/instanceOperations.d.ts +7 -0
- package/lib/server/instanceOperations.js +67 -0
- package/lib/shared/api.d.ts +42 -0
- package/lib/shared/api.js +1 -0
- package/lib/shared/enum.d.ts +1 -0
- package/lib/shared/enum.js +1 -0
- package/lib/shared/utils/compare.d.ts +13 -0
- package/lib/shared/utils/compare.js +24 -0
- package/lib/shared/utils/displayName.d.ts +2 -0
- package/lib/shared/utils/displayName.js +31 -0
- package/lib/shared/utils/instances.d.ts +6 -0
- package/lib/shared/utils/instances.js +1 -0
- package/lib/shared/utils/object.d.ts +2 -0
- package/lib/shared/utils/object.js +2 -0
- package/lib/shared/utils/string.d.ts +6 -0
- package/lib/shared/utils/string.js +52 -0
- package/lib/shared/utils/typeSafety.d.ts +1 -0
- package/lib/shared/utils/typeSafety.js +3 -0
- package/lib/shared/utils/validation.d.ts +3 -0
- package/lib/shared/utils/validation.js +14 -0
- package/lib/shared/validation/array.d.ts +6 -0
- package/lib/shared/validation/array.js +29 -0
- package/lib/shared/validation/date.d.ts +4 -0
- package/lib/shared/validation/date.js +13 -0
- package/lib/shared/validation/identifier.d.ts +1 -0
- package/lib/shared/validation/identifier.js +7 -0
- package/lib/shared/validation/number.d.ts +12 -0
- package/lib/shared/validation/number.js +34 -0
- package/lib/shared/validation/object.d.ts +6 -0
- package/lib/shared/validation/object.js +13 -0
- package/lib/shared/validation/string.d.ts +6 -0
- package/lib/shared/validation/string.js +15 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/utils/enum.d.ts +6 -0
- package/lib/utils/enum.js +1 -0
- package/lib/utils/error.d.ts +2 -0
- package/lib/utils/error.js +18 -0
- package/lib/utils/instances.d.ts +4 -0
- package/lib/utils/instances.js +12 -0
- package/lib/utils/lazy.d.ts +16 -0
- package/lib/utils/lazy.js +32 -0
- package/lib/utils/object.d.ts +3 -0
- package/lib/utils/object.js +1 -0
- package/lib/utils/render.d.ts +4 -0
- package/lib/utils/render.js +8 -0
- package/lib/utils/result.d.ts +57 -0
- package/lib/utils/result.js +48 -0
- package/package.json +46 -0
- package/public/css/styles.css +418 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { addEphemeralUUIDToType, createEntityIdentifierTypeAsDecl, isEntityDecl, } from "../../schema/declarations/EntityDecl.js";
|
|
2
|
+
import { TypeAliasDecl } from "../../schema/declarations/TypeAliasDecl.js";
|
|
3
|
+
import { flatMapAuxiliaryDecls, NodeKind } from "../../schema/Node.js";
|
|
4
|
+
import { isNestedEntityMapType, } from "../../schema/types/references/NestedEntityMapType.js";
|
|
5
|
+
import { getParentDecl } from "../../schema/types/Type.js";
|
|
6
|
+
import { discriminatorKey } from "../../shared/enum.js";
|
|
7
|
+
import { assertExhaustive } from "../../shared/utils/typeSafety.js";
|
|
8
|
+
const defaultOptions = {
|
|
9
|
+
indentation: 2,
|
|
10
|
+
};
|
|
11
|
+
const renderArrayType = (options, type) => ({
|
|
12
|
+
type: "array",
|
|
13
|
+
items: renderType(options, type.items),
|
|
14
|
+
minItems: type.minItems,
|
|
15
|
+
maxItems: type.maxItems,
|
|
16
|
+
uniqueItems: type.uniqueItems,
|
|
17
|
+
});
|
|
18
|
+
const renderObjectType = (options, type) => ({
|
|
19
|
+
type: "object",
|
|
20
|
+
properties: Object.fromEntries(Object.entries(type.properties).map(([name, config]) => [
|
|
21
|
+
name,
|
|
22
|
+
{ description: config.comment, ...renderType(options, config.type) },
|
|
23
|
+
])),
|
|
24
|
+
required: Object.entries(type.properties)
|
|
25
|
+
.filter(([, config]) => config.isRequired)
|
|
26
|
+
.map(([name]) => name),
|
|
27
|
+
minProperties: type.minProperties,
|
|
28
|
+
maxProperties: type.maxProperties,
|
|
29
|
+
additionalProperties: type.additionalProperties,
|
|
30
|
+
});
|
|
31
|
+
const renderBooleanType = (_options, _type) => ({ type: "boolean" });
|
|
32
|
+
const renderDateType = (_options, _type) => ({
|
|
33
|
+
type: "string",
|
|
34
|
+
format: "date",
|
|
35
|
+
});
|
|
36
|
+
const renderNumericRangeBounds = (inclusiveKey, exclusiveKey, rangeBound) => rangeBound === undefined
|
|
37
|
+
? undefined
|
|
38
|
+
: typeof rangeBound === "number"
|
|
39
|
+
? { [inclusiveKey]: rangeBound }
|
|
40
|
+
: rangeBound.isExclusive
|
|
41
|
+
? { [exclusiveKey]: rangeBound.value }
|
|
42
|
+
: { [inclusiveKey]: rangeBound.value };
|
|
43
|
+
const renderMultipleOf = (multipleOf) => multipleOf === undefined ? undefined : { multipleOf };
|
|
44
|
+
const renderFloatType = (_options, type) => ({
|
|
45
|
+
type: "number",
|
|
46
|
+
...renderNumericRangeBounds("minimum", "exclusiveMinimum", type.minimum),
|
|
47
|
+
...renderNumericRangeBounds("maximum", "exclusiveMaximum", type.maximum),
|
|
48
|
+
...renderMultipleOf(type.multipleOf),
|
|
49
|
+
});
|
|
50
|
+
const renderIntegerType = (_options, type) => ({
|
|
51
|
+
type: "integer",
|
|
52
|
+
...renderNumericRangeBounds("minimum", "exclusiveMinimum", type.minimum),
|
|
53
|
+
...renderNumericRangeBounds("maximum", "exclusiveMaximum", type.maximum),
|
|
54
|
+
...renderMultipleOf(type.multipleOf),
|
|
55
|
+
});
|
|
56
|
+
const renderStringType = (_options, type) => ({
|
|
57
|
+
type: "string",
|
|
58
|
+
minLength: type.minLength,
|
|
59
|
+
maxLength: type.maxLength,
|
|
60
|
+
pattern: type.pattern?.source,
|
|
61
|
+
});
|
|
62
|
+
const renderGenericArgumentIdentifierType = (_options, _type) => {
|
|
63
|
+
throw new TypeError("GenericArgumentIdentifierType is not supported in JSON Schema.");
|
|
64
|
+
};
|
|
65
|
+
const renderReferenceIdentifierType = (_options, type) => ({
|
|
66
|
+
$ref: `#/$defs/${type.entity.name}_ID`,
|
|
67
|
+
});
|
|
68
|
+
const renderIncludeIdentifierType = (_options, type) => ({
|
|
69
|
+
$ref: `#/$defs/${type.reference.name}`,
|
|
70
|
+
});
|
|
71
|
+
const renderNestedEntityMapType = (_options, type) => ({
|
|
72
|
+
type: "object",
|
|
73
|
+
additionalProperties: {
|
|
74
|
+
$ref: `#/$defs/${type.name}`,
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
const renderType = (options, type) => {
|
|
78
|
+
switch (type.kind) {
|
|
79
|
+
case NodeKind.ArrayType:
|
|
80
|
+
return renderArrayType(options, type);
|
|
81
|
+
case NodeKind.ObjectType:
|
|
82
|
+
return renderObjectType(options, type);
|
|
83
|
+
case NodeKind.BooleanType:
|
|
84
|
+
return renderBooleanType(options, type);
|
|
85
|
+
case NodeKind.DateType:
|
|
86
|
+
return renderDateType(options, type);
|
|
87
|
+
case NodeKind.FloatType:
|
|
88
|
+
return renderFloatType(options, type);
|
|
89
|
+
case NodeKind.IntegerType:
|
|
90
|
+
return renderIntegerType(options, type);
|
|
91
|
+
case NodeKind.StringType:
|
|
92
|
+
return renderStringType(options, type);
|
|
93
|
+
case NodeKind.GenericArgumentIdentifierType:
|
|
94
|
+
return renderGenericArgumentIdentifierType(options, type);
|
|
95
|
+
case NodeKind.ReferenceIdentifierType:
|
|
96
|
+
return renderReferenceIdentifierType(options, type);
|
|
97
|
+
case NodeKind.IncludeIdentifierType:
|
|
98
|
+
return renderIncludeIdentifierType(options, type);
|
|
99
|
+
case NodeKind.NestedEntityMapType:
|
|
100
|
+
return renderNestedEntityMapType(options, type);
|
|
101
|
+
default:
|
|
102
|
+
return assertExhaustive(type, "Unknown type");
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const renderEntityDecl = (options, decl) => ({
|
|
106
|
+
description: decl.comment,
|
|
107
|
+
...renderType(options, addEphemeralUUIDToType(decl)),
|
|
108
|
+
});
|
|
109
|
+
const renderEnumDecl = (options, decl) => ({
|
|
110
|
+
description: decl.comment,
|
|
111
|
+
oneOf: Object.entries(decl.values.value).map(([caseName, caseDef]) => ({
|
|
112
|
+
type: "object",
|
|
113
|
+
properties: {
|
|
114
|
+
[discriminatorKey]: {
|
|
115
|
+
const: caseName,
|
|
116
|
+
},
|
|
117
|
+
...(caseDef === null ? {} : { [caseName]: renderType(options, caseDef) }),
|
|
118
|
+
},
|
|
119
|
+
required: [discriminatorKey, ...(caseDef === null ? [] : [caseName])],
|
|
120
|
+
})),
|
|
121
|
+
});
|
|
122
|
+
const renderTypeAliasDecl = (options, decl) => ({
|
|
123
|
+
description: decl.comment,
|
|
124
|
+
...renderType(options, decl.type.value),
|
|
125
|
+
});
|
|
126
|
+
const renderDecl = (options, decl) => {
|
|
127
|
+
switch (decl.kind) {
|
|
128
|
+
case NodeKind.EntityDecl:
|
|
129
|
+
return renderEntityDecl(options, decl);
|
|
130
|
+
case NodeKind.EnumDecl:
|
|
131
|
+
return renderEnumDecl(options, decl);
|
|
132
|
+
case NodeKind.TypeAliasDecl:
|
|
133
|
+
return renderTypeAliasDecl(options, decl);
|
|
134
|
+
default:
|
|
135
|
+
return assertExhaustive(decl, "Unknown declaration");
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
const renderDeclarations = (options, declarations) => Object.fromEntries(declarations.map(decl => [decl.name, renderDecl(options, decl)]));
|
|
139
|
+
export const render = (options = defaultOptions, declarations) => {
|
|
140
|
+
const finalOptions = { ...defaultOptions, ...options };
|
|
141
|
+
return JSON.stringify({
|
|
142
|
+
$defs: renderDeclarations(finalOptions, flatMapAuxiliaryDecls(node => {
|
|
143
|
+
if (isNestedEntityMapType(node)) {
|
|
144
|
+
return TypeAliasDecl(getParentDecl(node)?.sourceUrl ?? "", {
|
|
145
|
+
name: node.name,
|
|
146
|
+
comment: node.comment,
|
|
147
|
+
type: () => node.type.value,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else if (isEntityDecl(node)) {
|
|
151
|
+
return createEntityIdentifierTypeAsDecl(node);
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}, declarations)),
|
|
155
|
+
}, undefined, finalOptions.indentation);
|
|
156
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
2
|
+
import { dirname } from "path";
|
|
3
|
+
import { render } from "./render.js";
|
|
4
|
+
export const TypeScriptOutput = (options) => ({
|
|
5
|
+
run: async (schema) => {
|
|
6
|
+
await mkdir(dirname(options.targetPath), { recursive: true });
|
|
7
|
+
await writeFile(options.targetPath, render(options.rendererOptions, schema.declarations), {
|
|
8
|
+
encoding: "utf-8",
|
|
9
|
+
});
|
|
10
|
+
},
|
|
11
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { EOL } from "node:os";
|
|
2
|
+
import { addEphemeralUUIDToType, createEntityIdentifierTypeAsDecl, isEntityDecl, } from "../../schema/declarations/EntityDecl.js";
|
|
3
|
+
import { TypeAliasDecl } from "../../schema/declarations/TypeAliasDecl.js";
|
|
4
|
+
import { flatMapAuxiliaryDecls, NodeKind } from "../../schema/Node.js";
|
|
5
|
+
import { isObjectType } from "../../schema/types/generic/ObjectType.js";
|
|
6
|
+
import { isNestedEntityMapType, } from "../../schema/types/references/NestedEntityMapType.js";
|
|
7
|
+
import { getParentDecl } from "../../schema/types/Type.js";
|
|
8
|
+
import { discriminatorKey } from "../../shared/enum.js";
|
|
9
|
+
import { toCamelCase } from "../../shared/utils/string.js";
|
|
10
|
+
import { assertExhaustive } from "../../shared/utils/typeSafety.js";
|
|
11
|
+
import { applyIndentation, joinSyntax, prefixLines, syntax } from "../../utils/render.js";
|
|
12
|
+
const defaultOptions = {
|
|
13
|
+
indentation: 2,
|
|
14
|
+
};
|
|
15
|
+
const renderDocumentation = (comment) => comment === undefined
|
|
16
|
+
? ""
|
|
17
|
+
: syntax `/**
|
|
18
|
+
${prefixLines(" * ", comment, true)}
|
|
19
|
+
*/
|
|
20
|
+
`;
|
|
21
|
+
const renderTypeParameters = (options, params) => params.length === 0
|
|
22
|
+
? ""
|
|
23
|
+
: `<${params
|
|
24
|
+
.map(param => param.constraint === undefined
|
|
25
|
+
? param.name
|
|
26
|
+
: joinSyntax(param.name, " extends ", renderType(options, param.constraint)))
|
|
27
|
+
.join(", ")}>`;
|
|
28
|
+
const renderArrayType = (options, type) => `${renderType(options, type.items)}[]`;
|
|
29
|
+
const wrapAsObject = (options, syntax) => joinSyntax("{", EOL, applyIndentation(1, syntax, options.indentation), EOL, "}");
|
|
30
|
+
const renderObjectType = (options, type) => {
|
|
31
|
+
return wrapAsObject(options, Object.entries(type.properties)
|
|
32
|
+
.map(([name, config]) => joinSyntax(renderDocumentation(config.comment), name, config.isRequired ? "" : "?", ": ", renderType(options, config.type)))
|
|
33
|
+
.join(Object.values(type.properties).some(prop => prop.comment !== undefined) ? EOL + EOL : EOL));
|
|
34
|
+
};
|
|
35
|
+
const renderBooleanType = (_options, _type) => "boolean";
|
|
36
|
+
const renderDateType = (_options, _type) => "Date";
|
|
37
|
+
const renderNumericType = (_options, _type) => "number";
|
|
38
|
+
const renderStringType = (_options, _type) => "string";
|
|
39
|
+
const renderGenericArgumentIdentifierType = (_options, type) => type.argument.name;
|
|
40
|
+
const renderReferenceIdentifierType = (_options, type) => type.entity.name + "_ID";
|
|
41
|
+
const renderIncludeIdentifierType = (_options, type) => type.reference.name;
|
|
42
|
+
const renderNestedEntityMapType = (options, type) => wrapAsObject(options, syntax `[${toCamelCase(type.secondaryEntity.name)}Id: string]: ${type.name}`);
|
|
43
|
+
const renderType = (options, type) => {
|
|
44
|
+
switch (type.kind) {
|
|
45
|
+
case NodeKind.ArrayType:
|
|
46
|
+
return renderArrayType(options, type);
|
|
47
|
+
case NodeKind.ObjectType:
|
|
48
|
+
return renderObjectType(options, type);
|
|
49
|
+
case NodeKind.BooleanType:
|
|
50
|
+
return renderBooleanType(options, type);
|
|
51
|
+
case NodeKind.DateType:
|
|
52
|
+
return renderDateType(options, type);
|
|
53
|
+
case NodeKind.FloatType:
|
|
54
|
+
return renderNumericType(options, type);
|
|
55
|
+
case NodeKind.IntegerType:
|
|
56
|
+
return renderNumericType(options, type);
|
|
57
|
+
case NodeKind.StringType:
|
|
58
|
+
return renderStringType(options, type);
|
|
59
|
+
case NodeKind.GenericArgumentIdentifierType:
|
|
60
|
+
return renderGenericArgumentIdentifierType(options, type);
|
|
61
|
+
case NodeKind.ReferenceIdentifierType:
|
|
62
|
+
return renderReferenceIdentifierType(options, type);
|
|
63
|
+
case NodeKind.IncludeIdentifierType:
|
|
64
|
+
return renderIncludeIdentifierType(options, type);
|
|
65
|
+
case NodeKind.NestedEntityMapType:
|
|
66
|
+
return renderNestedEntityMapType(options, type);
|
|
67
|
+
default:
|
|
68
|
+
return assertExhaustive(type, "Unknown type");
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const renderEntityDecl = (options, decl) => joinSyntax(renderDocumentation(decl.comment), "export interface ", decl.name, " ", renderType(options, addEphemeralUUIDToType(decl)));
|
|
72
|
+
const renderEnumDecl = (options, decl) => joinSyntax(renderDocumentation(decl.comment), "export type ", decl.name, renderTypeParameters(options, decl.parameters), " =", ...Object.entries(decl.values.value).map(([caseName, caseDef]) => applyIndentation(1, joinSyntax(EOL, "| {", EOL, applyIndentation(1, joinSyntax(`${discriminatorKey}: "${caseName}"`, caseDef === null
|
|
73
|
+
? ""
|
|
74
|
+
: joinSyntax(EOL, caseName + ": ", renderType(options, caseDef))), options.indentation), EOL, "}"), options.indentation)));
|
|
75
|
+
const renderTypeAliasDecl = (options, decl) => {
|
|
76
|
+
const type = decl.type.value;
|
|
77
|
+
return isObjectType(type)
|
|
78
|
+
? joinSyntax(renderDocumentation(decl.comment), "export interface ", decl.name, renderTypeParameters(options, decl.parameters), " ", renderType(options, type))
|
|
79
|
+
: joinSyntax(renderDocumentation(decl.comment), "export type ", decl.name, renderTypeParameters(options, decl.parameters), " = ", renderType(options, type));
|
|
80
|
+
};
|
|
81
|
+
const renderDecl = (options, decl) => {
|
|
82
|
+
switch (decl.kind) {
|
|
83
|
+
case NodeKind.EntityDecl:
|
|
84
|
+
return renderEntityDecl(options, decl);
|
|
85
|
+
case NodeKind.EnumDecl:
|
|
86
|
+
return renderEnumDecl(options, decl);
|
|
87
|
+
case NodeKind.TypeAliasDecl:
|
|
88
|
+
return renderTypeAliasDecl(options, decl);
|
|
89
|
+
default:
|
|
90
|
+
return assertExhaustive(decl, "Unknown declaration");
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const renderDeclarations = (options, declarations) => declarations.map(decl => renderDecl(options, decl)).join(EOL + EOL);
|
|
94
|
+
export const render = (options = defaultOptions, declarations) => {
|
|
95
|
+
const finalOptions = { ...defaultOptions, ...options };
|
|
96
|
+
return renderDeclarations(finalOptions, flatMapAuxiliaryDecls(node => {
|
|
97
|
+
if (isNestedEntityMapType(node)) {
|
|
98
|
+
return TypeAliasDecl(getParentDecl(node)?.sourceUrl ?? "", {
|
|
99
|
+
name: node.name,
|
|
100
|
+
comment: node.comment,
|
|
101
|
+
type: () => node.type.value,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else if (isEntityDecl(node)) {
|
|
105
|
+
return createEntityIdentifierTypeAsDecl(node);
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
}, declarations));
|
|
109
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { InstancesByEntityName } from "../shared/utils/instances.js";
|
|
2
|
+
import { Decl } from "./declarations/Declaration.js";
|
|
3
|
+
import { Type } from "./types/Type.js";
|
|
4
|
+
export interface NodeKind {
|
|
5
|
+
EntityDecl: "EntityDecl";
|
|
6
|
+
EnumDecl: "EnumDecl";
|
|
7
|
+
TypeAliasDecl: "TypeAliasDecl";
|
|
8
|
+
MemberDecl: "MemberDecl";
|
|
9
|
+
ArrayType: "ArrayType";
|
|
10
|
+
ObjectType: "ObjectType";
|
|
11
|
+
BooleanType: "BooleanType";
|
|
12
|
+
FloatType: "FloatType";
|
|
13
|
+
IntegerType: "IntegerType";
|
|
14
|
+
StringType: "StringType";
|
|
15
|
+
DateType: "DateType";
|
|
16
|
+
GenericArgumentIdentifierType: "GenericArgumentIdentifierType";
|
|
17
|
+
GenericParameter: "GenericParameter";
|
|
18
|
+
ReferenceIdentifierType: "ReferenceIdentifierType";
|
|
19
|
+
IncludeIdentifierType: "IncludeIdentifierType";
|
|
20
|
+
NestedEntityMapType: "NestedEntityMapType";
|
|
21
|
+
}
|
|
22
|
+
export declare const NodeKind: NodeKind;
|
|
23
|
+
export interface BaseNode {
|
|
24
|
+
kind: (typeof NodeKind)[keyof typeof NodeKind];
|
|
25
|
+
}
|
|
26
|
+
export type Node = Decl | Type;
|
|
27
|
+
export declare const flatMapAuxiliaryDecls: (callbackFn: (node: Node) => (Decl | undefined)[] | Decl | undefined, declarations: readonly Decl[]) => Decl[];
|
|
28
|
+
export type IdentifierToCheck = {
|
|
29
|
+
name: string;
|
|
30
|
+
value: unknown;
|
|
31
|
+
};
|
|
32
|
+
export interface Validators {
|
|
33
|
+
checkReferentialIntegrity: (identifier: IdentifierToCheck) => Error[];
|
|
34
|
+
}
|
|
35
|
+
export declare const createValidators: (instancesByEntityName: InstancesByEntityName) => Validators;
|
|
36
|
+
export type Serializer<T, U> = (node: T) => U;
|
|
37
|
+
export type GetReferences<T extends Node> = (node: T, value: unknown) => string[];
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { assertExhaustive } from "../shared/utils/typeSafety.js";
|
|
2
|
+
import { enumOfObject } from "../utils/enum.js";
|
|
3
|
+
export const NodeKind = enumOfObject({
|
|
4
|
+
EntityDecl: null,
|
|
5
|
+
EnumDecl: null,
|
|
6
|
+
TypeAliasDecl: null,
|
|
7
|
+
MemberDecl: null,
|
|
8
|
+
ArrayType: null,
|
|
9
|
+
ObjectType: null,
|
|
10
|
+
BooleanType: null,
|
|
11
|
+
FloatType: null,
|
|
12
|
+
IntegerType: null,
|
|
13
|
+
StringType: null,
|
|
14
|
+
DateType: null,
|
|
15
|
+
GenericArgumentIdentifierType: null,
|
|
16
|
+
GenericParameter: null,
|
|
17
|
+
ReferenceIdentifierType: null,
|
|
18
|
+
IncludeIdentifierType: null,
|
|
19
|
+
NestedEntityMapType: null,
|
|
20
|
+
});
|
|
21
|
+
export const flatMapAuxiliaryDecls = (callbackFn, declarations) => {
|
|
22
|
+
const mapNodeTree = (callbackFn, node, decls) => {
|
|
23
|
+
switch (node.kind) {
|
|
24
|
+
case NodeKind.EntityDecl: {
|
|
25
|
+
const newDecls = callbackFn(node, decls);
|
|
26
|
+
return mapNodeTree(callbackFn, node.type.value, newDecls);
|
|
27
|
+
}
|
|
28
|
+
case NodeKind.EnumDecl: {
|
|
29
|
+
const newDecls = callbackFn(node, decls);
|
|
30
|
+
return Object.values(node.values.value).reduce((newDeclsAcc, caseDef) => caseDef === null ? newDecls : mapNodeTree(callbackFn, caseDef, newDeclsAcc), newDecls);
|
|
31
|
+
}
|
|
32
|
+
case NodeKind.TypeAliasDecl: {
|
|
33
|
+
const newDecls = callbackFn(node, decls);
|
|
34
|
+
return mapNodeTree(callbackFn, node.type.value, newDecls);
|
|
35
|
+
}
|
|
36
|
+
case NodeKind.ArrayType: {
|
|
37
|
+
const newDecls = callbackFn(node, decls);
|
|
38
|
+
return mapNodeTree(callbackFn, node.items, newDecls);
|
|
39
|
+
}
|
|
40
|
+
case NodeKind.ObjectType: {
|
|
41
|
+
const newDecls = callbackFn(node, decls);
|
|
42
|
+
return Object.values(node.properties).reduce((newDeclsAcc, prop) => mapNodeTree(callbackFn, prop.type, newDeclsAcc), newDecls);
|
|
43
|
+
}
|
|
44
|
+
case NodeKind.BooleanType:
|
|
45
|
+
case NodeKind.DateType:
|
|
46
|
+
case NodeKind.FloatType:
|
|
47
|
+
case NodeKind.IntegerType:
|
|
48
|
+
case NodeKind.StringType:
|
|
49
|
+
case NodeKind.GenericArgumentIdentifierType:
|
|
50
|
+
case NodeKind.ReferenceIdentifierType:
|
|
51
|
+
case NodeKind.IncludeIdentifierType:
|
|
52
|
+
case NodeKind.NestedEntityMapType:
|
|
53
|
+
return callbackFn(node, decls);
|
|
54
|
+
default:
|
|
55
|
+
return assertExhaustive(node);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const reducer = (node, decls) => {
|
|
59
|
+
const result = callbackFn(node);
|
|
60
|
+
const normalizedResult = (Array.isArray(result) ? result : [result]).filter(decl => decl !== undefined);
|
|
61
|
+
normalizedResult.forEach(decl => {
|
|
62
|
+
if (decls.some(existingDecl => existingDecl.name === decl.name)) {
|
|
63
|
+
throw new Error(`Duplicate declaration name: "${decl.name}". Make sure declaration names are globally unique.`);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
return decls.concat(normalizedResult);
|
|
67
|
+
};
|
|
68
|
+
return declarations.reduce((decls, node) => mapNodeTree(reducer, node, [...decls, node]), []);
|
|
69
|
+
};
|
|
70
|
+
export const createValidators = (instancesByEntityName) => ({
|
|
71
|
+
checkReferentialIntegrity: ({ name, value }) => instancesByEntityName[name].some(instance => typeof instance.content === "object" &&
|
|
72
|
+
instance.content !== null &&
|
|
73
|
+
!Array.isArray(instance.content) &&
|
|
74
|
+
instance.id === value)
|
|
75
|
+
? []
|
|
76
|
+
: [
|
|
77
|
+
ReferenceError(`Invalid reference to instance of entity "${name}" with identifier ${JSON.stringify(value)}`),
|
|
78
|
+
],
|
|
79
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { BaseNode, GetReferences, Node, Serializer } from "../Node.js";
|
|
2
|
+
import { SerializedTypeParameter, TypeParameter } from "../parameters/TypeParameter.js";
|
|
3
|
+
import { ObjectType, SerializedObjectType } from "../types/generic/ObjectType.js";
|
|
4
|
+
import { SerializedType, Type } from "../types/Type.js";
|
|
5
|
+
import { ValidatorHelpers } from "../validation/type.js";
|
|
6
|
+
import { EntityDecl, SerializedEntityDecl } from "./EntityDecl.js";
|
|
7
|
+
import { EnumDecl, SerializedEnumDecl } from "./EnumDecl.js";
|
|
8
|
+
import { SerializedTypeAliasDecl, TypeAliasDecl } from "./TypeAliasDecl.js";
|
|
9
|
+
export type TypeArguments<Params extends TypeParameter[]> = {
|
|
10
|
+
[K in keyof Params]: Params[K] extends TypeParameter<string, infer T> ? T : Type;
|
|
11
|
+
};
|
|
12
|
+
export type SerializedTypeArguments<Params extends SerializedTypeParameter[]> = {
|
|
13
|
+
[K in keyof Params]: Params[K] extends SerializedTypeParameter<string, infer T> ? T : Type;
|
|
14
|
+
};
|
|
15
|
+
export declare const getParameterNames: (decl: Decl) => string[];
|
|
16
|
+
export declare const getTypeArgumentsRecord: <Params extends TypeParameter[]>(decl: DeclP<Params>, args: TypeArguments<Params>) => Record<string, Type>;
|
|
17
|
+
export type Decl = EntityDecl | EnumDecl | TypeAliasDecl;
|
|
18
|
+
export type SerializedDecl = SerializedEntityDecl | SerializedEnumDecl | SerializedTypeAliasDecl;
|
|
19
|
+
export type DeclP<Params extends TypeParameter[] = TypeParameter[]> = EntityDecl<string, ObjectType> | EnumDecl<string, Record<string, Type | null>, Params> | TypeAliasDecl<string, Type, Params>;
|
|
20
|
+
export type SerializedDeclP<Params extends SerializedTypeParameter[] = SerializedTypeParameter[]> = SerializedEntityDecl<string, SerializedObjectType> | SerializedEnumDecl<string, Record<string, SerializedType | null>, Params> | SerializedTypeAliasDecl<string, SerializedType, Params>;
|
|
21
|
+
export type SecondaryDecl = EnumDecl | TypeAliasDecl;
|
|
22
|
+
export type SerializedSecondaryDecl = SerializedEnumDecl | SerializedTypeAliasDecl;
|
|
23
|
+
export declare const getNestedDeclarations: GetNestedDeclarations;
|
|
24
|
+
export type GetNestedDeclarations<T extends Node = Node> = (isDeclarationAdded: (decl: Decl) => boolean, node: T) => Decl[];
|
|
25
|
+
export declare const isDecl: (node: Node) => node is Decl;
|
|
26
|
+
export interface BaseDecl<Name extends string = string, Params extends TypeParameter[] = TypeParameter[]> extends BaseNode {
|
|
27
|
+
sourceUrl: string;
|
|
28
|
+
name: Name;
|
|
29
|
+
comment?: string;
|
|
30
|
+
parameters: Params;
|
|
31
|
+
}
|
|
32
|
+
export interface SerializedBaseDecl<Name extends string = string, Params extends SerializedTypeParameter[] = SerializedTypeParameter[]> extends BaseNode {
|
|
33
|
+
sourceUrl: string;
|
|
34
|
+
name: Name;
|
|
35
|
+
comment?: string;
|
|
36
|
+
parameters: Params;
|
|
37
|
+
}
|
|
38
|
+
export declare const validateDecl: (helpers: ValidatorHelpers, decl: Decl, args: Type[], value: unknown) => Error[];
|
|
39
|
+
export declare const validateDeclName: (name: string) => void;
|
|
40
|
+
export declare const resolveTypeArgumentsInDecl: <Params extends TypeParameter[]>(decl: DeclP<Params>, args: TypeArguments<Params>) => DeclP<[]>;
|
|
41
|
+
export declare const isDeclWithoutTypeParameters: (decl: Decl) => decl is DeclP<[]>;
|
|
42
|
+
export declare const resolveTypeArgumentsInDecls: (decls: readonly Decl[]) => DeclP<[]>[];
|
|
43
|
+
export declare const serializeDecl: Serializer<Decl, SerializedDecl>;
|
|
44
|
+
export declare const getReferencesForDecl: GetReferences<Decl>;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { assertExhaustive } from "../../shared/utils/typeSafety.js";
|
|
2
|
+
import { NodeKind } from "../Node.js";
|
|
3
|
+
import { getNestedDeclarationsInArrayType } from "../types/generic/ArrayType.js";
|
|
4
|
+
import { getNestedDeclarationsInObjectType, } from "../types/generic/ObjectType.js";
|
|
5
|
+
import { getNestedDeclarationsInIncludeIdentifierType } from "../types/references/IncludeIdentifierType.js";
|
|
6
|
+
import { getNestedDeclarationsInNestedEntityMapType } from "../types/references/NestedEntityMapType.js";
|
|
7
|
+
import { getNestedDeclarationsInReferenceIdentifierType } from "../types/references/ReferenceIdentifierType.js";
|
|
8
|
+
import { getNestedDeclarationsInEntityDecl, getReferencesForEntityDecl, isEntityDecl, resolveTypeArgumentsInEntityDecl, serializeEntityDecl, validateEntityDecl, } from "./EntityDecl.js";
|
|
9
|
+
import { getNestedDeclarationsInEnumDecl, getReferencesForEnumDecl, isEnumDecl, resolveTypeArgumentsInEnumDecl, serializeEnumDecl, validateEnumDecl, } from "./EnumDecl.js";
|
|
10
|
+
import { getNestedDeclarationsInTypeAliasDecl, getReferencesForTypeAliasDecl, isTypeAliasDecl, resolveTypeArgumentsInTypeAliasDecl, serializeTypeAliasDecl, validateTypeAliasDecl, } from "./TypeAliasDecl.js";
|
|
11
|
+
export const getParameterNames = (decl) => decl.parameters.map(param => param.name);
|
|
12
|
+
export const getTypeArgumentsRecord = (decl, args) => Object.fromEntries(args.map((arg, i) => [decl.parameters[i].name, arg]));
|
|
13
|
+
export const getNestedDeclarations = (isDeclAdded, node) => {
|
|
14
|
+
switch (node.kind) {
|
|
15
|
+
case NodeKind.EntityDecl:
|
|
16
|
+
return isDeclAdded(node) ? [] : getNestedDeclarationsInEntityDecl(isDeclAdded, node);
|
|
17
|
+
case NodeKind.EnumDecl:
|
|
18
|
+
return isDeclAdded(node) ? [] : getNestedDeclarationsInEnumDecl(isDeclAdded, node);
|
|
19
|
+
case NodeKind.TypeAliasDecl:
|
|
20
|
+
return isDeclAdded(node) ? [] : getNestedDeclarationsInTypeAliasDecl(isDeclAdded, node);
|
|
21
|
+
case NodeKind.ArrayType:
|
|
22
|
+
return getNestedDeclarationsInArrayType(isDeclAdded, node);
|
|
23
|
+
case NodeKind.ObjectType:
|
|
24
|
+
return getNestedDeclarationsInObjectType(isDeclAdded, node);
|
|
25
|
+
case NodeKind.BooleanType:
|
|
26
|
+
case NodeKind.DateType:
|
|
27
|
+
case NodeKind.FloatType:
|
|
28
|
+
case NodeKind.IntegerType:
|
|
29
|
+
case NodeKind.StringType:
|
|
30
|
+
case NodeKind.GenericArgumentIdentifierType:
|
|
31
|
+
return [];
|
|
32
|
+
case NodeKind.ReferenceIdentifierType:
|
|
33
|
+
return getNestedDeclarationsInReferenceIdentifierType(isDeclAdded, node);
|
|
34
|
+
case NodeKind.IncludeIdentifierType:
|
|
35
|
+
return getNestedDeclarationsInIncludeIdentifierType(isDeclAdded, node);
|
|
36
|
+
case NodeKind.NestedEntityMapType:
|
|
37
|
+
return getNestedDeclarationsInNestedEntityMapType(isDeclAdded, node);
|
|
38
|
+
default:
|
|
39
|
+
return assertExhaustive(node);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
export const isDecl = (node) => isEntityDecl(node) || isEnumDecl(node) || isTypeAliasDecl(node);
|
|
43
|
+
export const validateDecl = (helpers, decl, args, value) => {
|
|
44
|
+
switch (decl.kind) {
|
|
45
|
+
case NodeKind.EntityDecl:
|
|
46
|
+
return validateEntityDecl(helpers, decl, value);
|
|
47
|
+
case NodeKind.EnumDecl:
|
|
48
|
+
return validateEnumDecl(helpers, decl, args, value);
|
|
49
|
+
case NodeKind.TypeAliasDecl:
|
|
50
|
+
return validateTypeAliasDecl(helpers, decl, args, value);
|
|
51
|
+
default:
|
|
52
|
+
return assertExhaustive(decl);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const declNamePattern = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
56
|
+
export const validateDeclName = (name) => {
|
|
57
|
+
if (!declNamePattern.test(name)) {
|
|
58
|
+
throw new Error(`Invalid declaration name "${name}". Declaration names must match the pattern ${declNamePattern.toString()}.`);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
export const resolveTypeArgumentsInDecl = (decl, args) => {
|
|
62
|
+
switch (decl.kind) {
|
|
63
|
+
case NodeKind.EntityDecl:
|
|
64
|
+
return resolveTypeArgumentsInEntityDecl(decl);
|
|
65
|
+
case NodeKind.EnumDecl:
|
|
66
|
+
return resolveTypeArgumentsInEnumDecl(decl, args);
|
|
67
|
+
case NodeKind.TypeAliasDecl:
|
|
68
|
+
return resolveTypeArgumentsInTypeAliasDecl(decl, args);
|
|
69
|
+
default:
|
|
70
|
+
return assertExhaustive(decl);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
export const isDeclWithoutTypeParameters = (decl) => decl.parameters.length === 0;
|
|
74
|
+
export const resolveTypeArgumentsInDecls = (decls) => decls.filter(isDeclWithoutTypeParameters).map(decl => resolveTypeArgumentsInDecl(decl, []));
|
|
75
|
+
export const serializeDecl = decl => {
|
|
76
|
+
switch (decl.kind) {
|
|
77
|
+
case NodeKind.EntityDecl:
|
|
78
|
+
return serializeEntityDecl(decl);
|
|
79
|
+
case NodeKind.EnumDecl:
|
|
80
|
+
return serializeEnumDecl(decl);
|
|
81
|
+
case NodeKind.TypeAliasDecl:
|
|
82
|
+
return serializeTypeAliasDecl(decl);
|
|
83
|
+
default:
|
|
84
|
+
return assertExhaustive(decl);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
export const getReferencesForDecl = (decl, value) => {
|
|
88
|
+
switch (decl.kind) {
|
|
89
|
+
case NodeKind.EntityDecl:
|
|
90
|
+
return getReferencesForEntityDecl(decl, value);
|
|
91
|
+
case NodeKind.EnumDecl:
|
|
92
|
+
return getReferencesForEnumDecl(decl, value);
|
|
93
|
+
case NodeKind.TypeAliasDecl:
|
|
94
|
+
return getReferencesForTypeAliasDecl(decl, value);
|
|
95
|
+
default:
|
|
96
|
+
return assertExhaustive(decl);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Lazy } from "../../utils/lazy.js";
|
|
2
|
+
import { Leaves } from "../../utils/object.js";
|
|
3
|
+
import { GetReferences, Node, NodeKind, Serializer } from "../Node.js";
|
|
4
|
+
import { MemberDecl, ObjectType, SerializedObjectType } from "../types/generic/ObjectType.js";
|
|
5
|
+
import { StringType } from "../types/primitives/StringType.js";
|
|
6
|
+
import { AsType, SerializedAsType, Type } from "../types/Type.js";
|
|
7
|
+
import { ValidatorHelpers } from "../validation/type.js";
|
|
8
|
+
import { BaseDecl, GetNestedDeclarations, SerializedBaseDecl } from "./Declaration.js";
|
|
9
|
+
import { TypeAliasDecl } from "./TypeAliasDecl.js";
|
|
10
|
+
export interface EntityDecl<Name extends string = string, T extends ObjectType = ObjectType> extends BaseDecl<Name, []> {
|
|
11
|
+
kind: NodeKind["EntityDecl"];
|
|
12
|
+
type: Lazy<T>;
|
|
13
|
+
/**
|
|
14
|
+
* @default "name"
|
|
15
|
+
*/
|
|
16
|
+
displayName?: Leaves<AsType<T>> | {
|
|
17
|
+
/**
|
|
18
|
+
* @default "translations"
|
|
19
|
+
*/
|
|
20
|
+
pathToLocaleMap?: Leaves<AsType<T>>;
|
|
21
|
+
/**
|
|
22
|
+
* @default "name"
|
|
23
|
+
*/
|
|
24
|
+
pathInLocaleMap?: string;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export interface SerializedEntityDecl<Name extends string = string, T extends SerializedObjectType = SerializedObjectType> extends SerializedBaseDecl<Name, []> {
|
|
28
|
+
kind: NodeKind["EntityDecl"];
|
|
29
|
+
type: T;
|
|
30
|
+
/**
|
|
31
|
+
* @default "name"
|
|
32
|
+
*/
|
|
33
|
+
displayName?: Leaves<SerializedAsType<T>> | {
|
|
34
|
+
/**
|
|
35
|
+
* @default "translations"
|
|
36
|
+
*/
|
|
37
|
+
pathToLocaleMap?: Leaves<SerializedAsType<T>>;
|
|
38
|
+
/**
|
|
39
|
+
* @default "name"
|
|
40
|
+
*/
|
|
41
|
+
pathInLocaleMap?: string;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export declare const EntityDecl: <Name extends string, T extends ObjectType>(sourceUrl: string, options: {
|
|
45
|
+
name: Name;
|
|
46
|
+
comment?: string;
|
|
47
|
+
type: () => T;
|
|
48
|
+
/**
|
|
49
|
+
* @default "name"
|
|
50
|
+
*/
|
|
51
|
+
displayName?: Leaves<AsType<T>> | {
|
|
52
|
+
/**
|
|
53
|
+
* @default "translations"
|
|
54
|
+
*/
|
|
55
|
+
pathToLocaleMap?: Leaves<AsType<T>>;
|
|
56
|
+
/**
|
|
57
|
+
* @default "name"
|
|
58
|
+
*/
|
|
59
|
+
pathInLocaleMap?: string;
|
|
60
|
+
};
|
|
61
|
+
}) => EntityDecl<Name, T>;
|
|
62
|
+
export { EntityDecl as Entity };
|
|
63
|
+
export declare const isEntityDecl: (node: Node) => node is EntityDecl;
|
|
64
|
+
export declare const getNestedDeclarationsInEntityDecl: GetNestedDeclarations<EntityDecl>;
|
|
65
|
+
export declare const validateEntityDecl: (helpers: ValidatorHelpers, decl: EntityDecl, value: unknown) => Error[];
|
|
66
|
+
export declare const resolveTypeArgumentsInEntityDecl: (decl: EntityDecl) => EntityDecl;
|
|
67
|
+
export declare const addEphemeralUUIDToType: <T extends Record<string, MemberDecl<Type, boolean>>>(decl: EntityDecl<string, ObjectType<T>>) => ObjectType<Omit<T, "id"> & {
|
|
68
|
+
id: MemberDecl<StringType, true>;
|
|
69
|
+
}>;
|
|
70
|
+
export declare const createEntityIdentifierType: () => StringType;
|
|
71
|
+
export declare const createEntityIdentifierTypeAsDecl: <Name extends string>(decl: EntityDecl<Name, ObjectType>) => TypeAliasDecl<`${Name}_ID`, StringType, []>;
|
|
72
|
+
export declare const serializeEntityDecl: Serializer<EntityDecl, SerializedEntityDecl>;
|
|
73
|
+
export declare const getReferencesForEntityDecl: GetReferences<EntityDecl>;
|