@pylonts/dsl 1.1.4 → 1.1.6
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/dist/controller.d.ts +27 -0
- package/dist/controller.js +11 -0
- package/dist/convert.d.ts +18 -6
- package/dist/convert.js +12 -2
- package/dist/curd.d.ts +0 -2
- package/dist/curd.js +7 -0
- package/dist/dao.d.ts +111 -2
- package/dist/dao.js +28 -2
- package/dist/db.js +3 -0
- package/dist/dsl.d.ts +24 -1
- package/dist/dsl.js +16 -0
- package/dist/dto.d.ts +6 -2
- package/dist/dto.js +20 -9
- package/dist/endpoint.d.ts +15 -0
- package/dist/endpoint.js +3 -0
- package/dist/exception.d.ts +14 -0
- package/dist/exception.js +9 -0
- package/dist/field-rule.d.ts +20 -0
- package/dist/field-rule.js +19 -0
- package/dist/flow.d.ts +24 -2
- package/dist/flow.js +16 -4
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/dist/mermaid-driver.js +32 -3
- package/dist/method.d.ts +11 -0
- package/dist/method.js +3 -0
- package/dist/mysql-driver.js +8 -1
- package/dist/provider.d.ts +6 -11
- package/dist/provider.js +2 -2
- package/dist/service.d.ts +16 -6
- package/dist/service.js +13 -2
- package/dist/third-service.d.ts +75 -0
- package/dist/third-service.js +96 -0
- package/dist/typebox-driver.d.ts +6 -0
- package/dist/typebox-driver.js +76 -14
- package/dist/utils.d.ts +25 -10
- package/dist/utils.js +28 -11
- package/docs/curd.md +110 -110
- package/docs/dto.md +9 -2
- package/docs/table.md +135 -135
- package/docs/third-service.md +122 -0
- package/package.json +4 -1
- package/src/action.ts +10 -10
- package/src/asset.ts +62 -62
- package/src/bases.ts +29 -29
- package/src/component.ts +21 -21
- package/src/controller.ts +40 -0
- package/src/convert.ts +34 -7
- package/src/curd.ts +7 -2
- package/src/dao.ts +162 -3
- package/src/db.ts +5 -0
- package/src/dsl.ts +49 -1
- package/src/dto.ts +25 -9
- package/src/endpoint.ts +18 -0
- package/src/event.ts +12 -12
- package/src/exception.ts +28 -0
- package/src/field-rule.ts +47 -0
- package/src/flow.ts +143 -103
- package/src/index.ts +11 -1
- package/src/mermaid-driver.ts +31 -3
- package/src/method.ts +20 -0
- package/src/mock.ts +12 -12
- package/src/mysql-driver.ts +8 -2
- package/src/navigation.ts +28 -28
- package/src/page-def.ts +79 -79
- package/src/page-flow.ts +153 -153
- package/src/page.ts +76 -76
- package/src/popup.ts +25 -25
- package/src/project.ts +97 -97
- package/src/provider.ts +7 -12
- package/src/ref.ts +18 -18
- package/src/route.ts +11 -11
- package/src/service.ts +28 -6
- package/src/third-service.ts +186 -0
- package/src/typebox-driver.ts +264 -192
- package/src/utils.ts +54 -17
- package/dist/check-inheritance.d.ts +0 -9
- package/dist/check-inheritance.js +0 -58
package/src/typebox-driver.ts
CHANGED
|
@@ -1,193 +1,265 @@
|
|
|
1
|
-
import { DtoArrayField, DtoField, DtoMessage, DtoObjectField, ImportBase, ImportRef } from './dto.js';
|
|
2
|
-
import { Field } from './dsl.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
// export
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
case '
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
case '
|
|
56
|
-
case '
|
|
57
|
-
case '
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
)
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
)
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const
|
|
192
|
-
|
|
1
|
+
import { DtoArrayField, DtoField, DtoMessage, DtoObjectField, ImportBase, ImportRef } from './dto.js';
|
|
2
|
+
import { EnumField, Field } from './dsl.js';
|
|
3
|
+
import type { ThirdMethodSchema } from './third-service.js';
|
|
4
|
+
|
|
5
|
+
// TypeBox driver: renders a DtoMessage into TypeBox TypeScript source.
|
|
6
|
+
// Shape matches the codegen product consumed by fastify v5 TypeBoxTypeProvider:
|
|
7
|
+
//
|
|
8
|
+
// export const RegisterUserInput = Type.Object({...});
|
|
9
|
+
// export type RegisterUserInput = Static<typeof RegisterUserInput>;
|
|
10
|
+
//
|
|
11
|
+
// ENUM fields reference a generated enum (see enum-driver) by its import
|
|
12
|
+
// location — the resolver maps a jsName to its product import.
|
|
13
|
+
// DTO bases (.include()) render as Type.Intersect([...bases, Type.Object({...})]).
|
|
14
|
+
|
|
15
|
+
export type EnumResolver = (enumName: string) => ImportBase | undefined;
|
|
16
|
+
|
|
17
|
+
function renderString(s: string): string {
|
|
18
|
+
return `'${s.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function renderDefault(v: unknown): string {
|
|
22
|
+
if (typeof v === 'string') return renderString(v);
|
|
23
|
+
return JSON.stringify(v);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function renderBasic(
|
|
27
|
+
field: Field,
|
|
28
|
+
pattern: string | undefined,
|
|
29
|
+
defaultValue: unknown,
|
|
30
|
+
resolver: EnumResolver | undefined,
|
|
31
|
+
indent = 0,
|
|
32
|
+
): string {
|
|
33
|
+
if (pattern !== undefined && field.type !== 'string') {
|
|
34
|
+
throw new Error(`pattern is only supported on string fields, got ${field.type} (${field.name})`);
|
|
35
|
+
}
|
|
36
|
+
const def = defaultValue !== undefined ? `default: ${renderDefault(defaultValue)}` : undefined;
|
|
37
|
+
switch (field.type) {
|
|
38
|
+
case 'string': {
|
|
39
|
+
const opts: string[] = [];
|
|
40
|
+
if (field.minLength !== undefined) opts.push(`minLength: ${field.minLength}`);
|
|
41
|
+
if (field.maxLength !== undefined) opts.push(`maxLength: ${field.maxLength}`);
|
|
42
|
+
if (pattern !== undefined) opts.push(`pattern: ${renderString(pattern)}`);
|
|
43
|
+
if (def !== undefined) opts.push(def);
|
|
44
|
+
return opts.length > 0 ? `Type.String({ ${opts.join(', ')} })` : 'Type.String()';
|
|
45
|
+
}
|
|
46
|
+
case 'text':
|
|
47
|
+
return def !== undefined ? `Type.String({ ${def} })` : 'Type.String()';
|
|
48
|
+
case 'integer': {
|
|
49
|
+
const opts: string[] = [];
|
|
50
|
+
if (field.min !== undefined) opts.push(`minimum: ${field.min}`);
|
|
51
|
+
if (field.max !== undefined) opts.push(`maximum: ${field.max}`);
|
|
52
|
+
if (def !== undefined) opts.push(def);
|
|
53
|
+
return opts.length > 0 ? `Type.Integer({ ${opts.join(', ')} })` : 'Type.Integer()';
|
|
54
|
+
}
|
|
55
|
+
case 'bigint':
|
|
56
|
+
case 'decimal':
|
|
57
|
+
case 'rate':
|
|
58
|
+
case 'time':
|
|
59
|
+
case 'date':
|
|
60
|
+
case 'datetime':
|
|
61
|
+
// Transmitted as string over HTTP: bigint/decimal/rate keep full
|
|
62
|
+
// precision, date/time serialize to string.
|
|
63
|
+
return def !== undefined ? `Type.String({ ${def} })` : 'Type.String()';
|
|
64
|
+
case 'boolean':
|
|
65
|
+
return def !== undefined ? `Type.Boolean({ ${def} })` : 'Type.Boolean()';
|
|
66
|
+
case 'json':
|
|
67
|
+
return def !== undefined ? `Type.Unknown({ ${def} })` : 'Type.Unknown()';
|
|
68
|
+
case 'enum': {
|
|
69
|
+
const ref = resolver?.(field.enum.jsName);
|
|
70
|
+
if (!ref) throw new Error(`enum field ${field.name}: no import ref for ${field.enum.jsName} — pass an EnumResolver`);
|
|
71
|
+
if (def !== undefined) {
|
|
72
|
+
const member = field.enum.values.find((v) => v.value === defaultValue);
|
|
73
|
+
if (!member) {
|
|
74
|
+
throw new Error(
|
|
75
|
+
`enum field ${field.name}: default ${renderDefault(defaultValue)} is not a member of ${field.enum.jsName}`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
return `Type.Enum(${ref.name}, { default: ${ref.name}.${member.symbol} })`;
|
|
79
|
+
}
|
|
80
|
+
return `Type.Enum(${ref.name})`;
|
|
81
|
+
}
|
|
82
|
+
case 'array':
|
|
83
|
+
return `Type.Array(${renderFieldValue(field.items, indent, resolver)})`;
|
|
84
|
+
case 'object':
|
|
85
|
+
return renderFieldObject(field.properties, indent + 1, resolver);
|
|
86
|
+
default:
|
|
87
|
+
// Field union is exhaustive; this branch is unreachable at runtime.
|
|
88
|
+
throw new Error(`unsupported field type: ${String((field as Field).type)}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Render a plain Field value (wire-format nested fields), wrapping optional. */
|
|
93
|
+
function renderFieldValue(field: Field, indent: number, resolver: EnumResolver | undefined): string {
|
|
94
|
+
const base = renderBasic(field, undefined, undefined, resolver, indent);
|
|
95
|
+
return field.optional ? `Type.Optional(${base})` : base;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Render a plain Field object (wire-format nested object). */
|
|
99
|
+
function renderFieldObject(properties: Record<string, Field>, indent: number, resolver: EnumResolver | undefined): string {
|
|
100
|
+
const pad = ' '.repeat(indent);
|
|
101
|
+
const entries = Object.entries(properties).map(([name, f]) => `${pad}${name}: ${renderFieldValue(f, indent, resolver)}`);
|
|
102
|
+
return `Type.Object({\n${entries.join(',\n')}\n${' '.repeat(indent - 1)}})`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function renderObject(fields: Record<string, DtoField | Field>, indent: number, resolver: EnumResolver | undefined): string {
|
|
106
|
+
const pad = ' '.repeat(indent);
|
|
107
|
+
const entries = Object.entries(fields).map(([name, f]) => {
|
|
108
|
+
const rendered = isDtoField(f) ? renderField(f, indent, resolver) : renderFieldValue(f, indent, resolver);
|
|
109
|
+
return `${pad}${name}: ${rendered}`;
|
|
110
|
+
});
|
|
111
|
+
return `Type.Object({\n${entries.join(',\n')}\n${' '.repeat(indent - 1)}})`;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function renderField(f: DtoField, indent: number, resolver: EnumResolver | undefined): string {
|
|
115
|
+
const base = renderValue(f, indent, resolver);
|
|
116
|
+
return f.isOptional() ? `Type.Optional(${base})` : base;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function renderValue(f: DtoField, indent: number, resolver: EnumResolver | undefined): string {
|
|
120
|
+
if (f.field.type === 'array') {
|
|
121
|
+
const items = f.field.items;
|
|
122
|
+
// Referenced DTO element — render by name (same-file export), not expanded.
|
|
123
|
+
if (isDtoMessage(items)) return `Type.Array(${items.name})`;
|
|
124
|
+
if (isDtoField(items)) return `Type.Array(${renderField(items, indent + 1, resolver)})`;
|
|
125
|
+
return `Type.Array(${renderFieldValue(items, indent, resolver)})`;
|
|
126
|
+
}
|
|
127
|
+
if (f.field.type === 'object') {
|
|
128
|
+
return renderObject(f.field.properties, indent + 1, resolver);
|
|
129
|
+
}
|
|
130
|
+
// DtoField only wraps a database Field; array/object defs live in the subclasses.
|
|
131
|
+
// Only DTO-level defaults (setDefault) are emitted as TypeBox default
|
|
132
|
+
// annotations; DB field defaults are not carried into the API contract.
|
|
133
|
+
return renderBasic(f.field as Field, f.pattern, f.default, resolver, indent);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Structural check — DtoMessage instances may come from a different module copy, so instanceof is unreliable. */
|
|
137
|
+
function isDtoMessage(v: unknown): v is DtoMessage {
|
|
138
|
+
if (typeof v !== 'object' || v === null) return false;
|
|
139
|
+
return (v as Record<string, unknown>).type === 'dto';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Structural check — a DtoField wraps a Field in a .field property and has no .type of its own. */
|
|
143
|
+
function isDtoField(v: unknown): v is DtoField {
|
|
144
|
+
if (typeof v !== 'object' || v === null) return false;
|
|
145
|
+
return 'field' in v && !('type' in v);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function collectEnumImports(
|
|
149
|
+
f: DtoField,
|
|
150
|
+
resolver: EnumResolver | undefined,
|
|
151
|
+
out: Map<string, ImportBase>,
|
|
152
|
+
): void {
|
|
153
|
+
if (f.field.type === 'array') {
|
|
154
|
+
const items = f.field.items;
|
|
155
|
+
if (isDtoMessage(items)) return;
|
|
156
|
+
if (isDtoField(items)) {
|
|
157
|
+
collectEnumImports(items, resolver, out);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
collectFieldEnumImports(items, resolver, out);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (f.field.type === 'object') {
|
|
164
|
+
for (const child of Object.values(f.field.properties)) {
|
|
165
|
+
if (isDtoField(child)) collectEnumImports(child, resolver, out);
|
|
166
|
+
else collectFieldEnumImports(child, resolver, out);
|
|
167
|
+
}
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (f.field.type === 'enum') collectEnumRef(f.field, resolver, out);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Enum import collection over a plain Field (wire-format nested fields). */
|
|
174
|
+
function collectFieldEnumImports(
|
|
175
|
+
field: Field,
|
|
176
|
+
resolver: EnumResolver | undefined,
|
|
177
|
+
out: Map<string, ImportBase>,
|
|
178
|
+
): void {
|
|
179
|
+
if (field.type === 'array') {
|
|
180
|
+
collectFieldEnumImports(field.items, resolver, out);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (field.type === 'object') {
|
|
184
|
+
for (const child of Object.values(field.properties)) collectFieldEnumImports(child, resolver, out);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (field.type === 'enum') collectEnumRef(field, resolver, out);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function collectEnumRef(field: EnumField, resolver: EnumResolver | undefined, out: Map<string, ImportBase>): void {
|
|
191
|
+
const ref = resolver?.(field.enum.jsName);
|
|
192
|
+
if (!ref) throw new Error(`enum field ${field.name}: no import ref for ${field.enum.jsName} — pass an EnumResolver`);
|
|
193
|
+
out.set(`${ref.from}#${ref.name}`, ref);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Collect all imports needed to render a DTO: include() bases + enum references. */
|
|
197
|
+
export function collectDtoImports(
|
|
198
|
+
schema: DtoMessage,
|
|
199
|
+
resolver: EnumResolver | undefined,
|
|
200
|
+
out: Map<string, ImportBase>,
|
|
201
|
+
): void {
|
|
202
|
+
for (const base of schema.bases ?? []) out.set(`${base.from}#${base.name}`, base);
|
|
203
|
+
for (const f of Object.values(schema.fields)) collectEnumImports(f, resolver, out);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Render one DTO export (const + type) — no file header, for file-level generation. */
|
|
207
|
+
export function renderDtoExport(schema: DtoMessage, resolver: EnumResolver | undefined): string {
|
|
208
|
+
const object = renderObject(schema.fields, 1, resolver);
|
|
209
|
+
const bases = schema.bases ?? [];
|
|
210
|
+
const body =
|
|
211
|
+
bases.length > 0
|
|
212
|
+
? `Type.Intersect([${bases.map(renderBase).join(', ')}, ${object}])`
|
|
213
|
+
: object;
|
|
214
|
+
return `export const ${schema.name} = ${body};`;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Render the Static type export for a DTO. */
|
|
218
|
+
export function renderDtoTypeExport(name: string): string {
|
|
219
|
+
return `export type ${name} = Static<typeof ${name}>;`;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function renderDtoMessage(
|
|
223
|
+
schema: DtoMessage,
|
|
224
|
+
options: { resolver?: EnumResolver; source?: string } = {},
|
|
225
|
+
): string {
|
|
226
|
+
const { resolver, source } = options;
|
|
227
|
+
const imports = new Map<string, ImportBase>();
|
|
228
|
+
collectDtoImports(schema, resolver, imports);
|
|
229
|
+
|
|
230
|
+
const header = [
|
|
231
|
+
'// AUTO-GENERATED by typebox-driver — DO NOT EDIT',
|
|
232
|
+
...(source !== undefined ? [`// Source: ${source}`] : []),
|
|
233
|
+
"import { Type, Static } from '@sinclair/typebox';",
|
|
234
|
+
...[...imports.values()].map((r) => `import${r.type ? ' type' : ''} { ${r.name} } from '${r.from}';`),
|
|
235
|
+
];
|
|
236
|
+
|
|
237
|
+
return [
|
|
238
|
+
...header,
|
|
239
|
+
'',
|
|
240
|
+
renderDtoExport(schema, resolver),
|
|
241
|
+
renderDtoTypeExport(schema.name),
|
|
242
|
+
'',
|
|
243
|
+
].join('\n');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function renderBase(base: ImportRef): string {
|
|
247
|
+
if (base.args === undefined || base.args.length === 0) return base.name;
|
|
248
|
+
const args = base.args.map((a) => (typeof a === 'string' ? a : a.name));
|
|
249
|
+
return `${base.name}(${args.join(', ')})`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Render one third-party method message export (const only — pair with
|
|
253
|
+
* renderDtoTypeExport for the Static type). */
|
|
254
|
+
export function renderThirdMethodExport(schema: ThirdMethodSchema, resolver: EnumResolver | undefined): string {
|
|
255
|
+
return `export const ${schema.name} = ${renderFieldObject(schema.fields, 1, resolver)};`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Collect all imports needed to render a third-party method message: enum references. */
|
|
259
|
+
export function collectThirdMethodImports(
|
|
260
|
+
schema: ThirdMethodSchema,
|
|
261
|
+
resolver: EnumResolver | undefined,
|
|
262
|
+
out: Map<string, ImportBase>,
|
|
263
|
+
): void {
|
|
264
|
+
for (const f of Object.values(schema.fields)) collectFieldEnumImports(f, resolver, out);
|
|
193
265
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,27 +1,64 @@
|
|
|
1
|
-
|
|
1
|
+
import type { Field, SchemaBase } from './dsl.js';
|
|
2
|
+
import type { FrontAppSchema } from './project.js';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
4
|
+
// Base utility modules — business-agnostic helpers with full method
|
|
5
|
+
// signatures (e.g. DateTimeUtils.format). Called at the service layer;
|
|
6
|
+
// their args/results are own wire types, independent of business schemas.
|
|
7
7
|
|
|
8
|
-
/**
|
|
9
|
-
export
|
|
10
|
-
|
|
8
|
+
/** A utility method with a full signature. */
|
|
9
|
+
export interface UtilsMethodSchema extends SchemaBase {
|
|
10
|
+
type: 'utilsMethod';
|
|
11
|
+
/** The utility module this method belongs to. */
|
|
12
|
+
schema: UtilsSchema;
|
|
13
|
+
/** Input fields. */
|
|
14
|
+
args: Record<string, Field>;
|
|
15
|
+
/** Output field. */
|
|
16
|
+
result: Field;
|
|
11
17
|
}
|
|
12
18
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
import { SchemaBase } from './dsl.js';
|
|
16
|
-
import { FrontAppSchema } from './project.js';
|
|
19
|
+
/** Method input for defineUtils: type/schema/name are set by the builder. */
|
|
20
|
+
export type UtilsMethodDef = Omit<UtilsMethodSchema, 'type' | 'schema' | 'name'>;
|
|
17
21
|
|
|
18
|
-
/** A utility module
|
|
22
|
+
/** A base utility module (e.g. DateTimeUtils). */
|
|
19
23
|
export interface UtilsSchema extends SchemaBase {
|
|
20
24
|
type: 'utils';
|
|
21
|
-
/**
|
|
22
|
-
app
|
|
25
|
+
/** Optional binding — empty means a shared public module. */
|
|
26
|
+
app?: FrontAppSchema;
|
|
27
|
+
/** Methods keyed by name — the map key is written back as the method name. */
|
|
28
|
+
methods: Record<string, UtilsMethodSchema>;
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
export function defineUtils(
|
|
26
|
-
|
|
31
|
+
export function defineUtils(options: {
|
|
32
|
+
name: string;
|
|
33
|
+
app?: FrontAppSchema;
|
|
34
|
+
methods: Record<string, UtilsMethodDef>;
|
|
35
|
+
description?: string;
|
|
36
|
+
}): UtilsSchema {
|
|
37
|
+
const schema: UtilsSchema = {
|
|
38
|
+
type: 'utils',
|
|
39
|
+
name: options.name,
|
|
40
|
+
description: options.description,
|
|
41
|
+
app: options.app,
|
|
42
|
+
methods: {},
|
|
43
|
+
};
|
|
44
|
+
for (const key of Object.keys(options.methods)) {
|
|
45
|
+
const method = options.methods[key] as UtilsMethodDef;
|
|
46
|
+
const methodSchema: UtilsMethodSchema = {
|
|
47
|
+
type: 'utilsMethod',
|
|
48
|
+
name: key,
|
|
49
|
+
description: method.description,
|
|
50
|
+
schema,
|
|
51
|
+
args: method.args,
|
|
52
|
+
result: method.result,
|
|
53
|
+
};
|
|
54
|
+
for (const argKey of Object.keys(methodSchema.args)) {
|
|
55
|
+
const field = methodSchema.args[argKey] as Field;
|
|
56
|
+
field.name = argKey;
|
|
57
|
+
field.schema = methodSchema;
|
|
58
|
+
}
|
|
59
|
+
methodSchema.result.name = key;
|
|
60
|
+
methodSchema.result.schema = methodSchema;
|
|
61
|
+
schema.methods[key] = methodSchema;
|
|
62
|
+
}
|
|
63
|
+
return schema;
|
|
27
64
|
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export interface InheritanceIssue {
|
|
2
|
-
file: string;
|
|
3
|
-
container: string;
|
|
4
|
-
field: string;
|
|
5
|
-
/** e.g. ["t_order.order_no"] or ["t_order.id", "t_merchant.id"] when several inferred tables share the name */
|
|
6
|
-
candidates: string[];
|
|
7
|
-
}
|
|
8
|
-
/** Check one loaded DSL module (its exports) for inheritance gaps. */
|
|
9
|
-
export declare function checkInheritance(mod: Record<string, unknown>, file: string): InheritanceIssue[];
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/** snake_case → camelCase: order_no → orderNo; names without underscores are unchanged */
|
|
2
|
-
function toCamelCase(name) {
|
|
3
|
-
return name.replace(/_([a-z])/g, (_, c) => c.toUpperCase());
|
|
4
|
-
}
|
|
5
|
-
function isDtoMessage(v) {
|
|
6
|
-
if (typeof v !== 'object' || v === null)
|
|
7
|
-
return false;
|
|
8
|
-
const o = v;
|
|
9
|
-
return o.type === 'dto' && typeof o.name === 'string' && typeof o.fields === 'object' && o.fields !== null;
|
|
10
|
-
}
|
|
11
|
-
function collectFields(mod) {
|
|
12
|
-
const out = [];
|
|
13
|
-
for (const [name, v] of Object.entries(mod)) {
|
|
14
|
-
if (isDtoMessage(v)) {
|
|
15
|
-
const container = v;
|
|
16
|
-
for (const [fname, f] of Object.entries(container.fields)) {
|
|
17
|
-
out.push({ name: fname, field: f.field, container: name });
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return out;
|
|
22
|
-
}
|
|
23
|
-
/** Check one loaded DSL module (its exports) for inheritance gaps. */
|
|
24
|
-
export function checkInheritance(mod, file) {
|
|
25
|
-
const fields = collectFields(mod);
|
|
26
|
-
if (fields.length === 0)
|
|
27
|
-
return [];
|
|
28
|
-
// Infer the tables this file is about from the referenced fields' schema identity.
|
|
29
|
-
const tables = new Map(); // table -> camelCol -> origCol
|
|
30
|
-
for (const f of fields) {
|
|
31
|
-
const schema = f.field.schema;
|
|
32
|
-
if (schema?.type !== 'table')
|
|
33
|
-
continue;
|
|
34
|
-
const table = schema;
|
|
35
|
-
if (!tables.has(table.name))
|
|
36
|
-
tables.set(table.name, new Map());
|
|
37
|
-
tables.get(table.name).set(toCamelCase(f.name), f.name);
|
|
38
|
-
}
|
|
39
|
-
if (tables.size === 0)
|
|
40
|
-
return [];
|
|
41
|
-
const issues = [];
|
|
42
|
-
for (const f of fields) {
|
|
43
|
-
if (f.field.schema?.type === 'table')
|
|
44
|
-
continue;
|
|
45
|
-
if (f.field.semantic !== undefined || f.field.type === 'enum')
|
|
46
|
-
continue;
|
|
47
|
-
const candidates = [];
|
|
48
|
-
for (const [t, cols] of tables) {
|
|
49
|
-
const orig = cols.get(f.name);
|
|
50
|
-
if (orig !== undefined)
|
|
51
|
-
candidates.push(`${t}.${orig}`);
|
|
52
|
-
}
|
|
53
|
-
if (candidates.length > 0) {
|
|
54
|
-
issues.push({ file, container: f.container, field: f.name, candidates });
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return issues;
|
|
58
|
-
}
|