@atcute/lex-cli 1.1.1 → 2.0.0
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/cli.mjs +1 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +77 -0
- package/dist/cli.js.map +1 -0
- package/dist/codegen.d.ts +23 -0
- package/dist/codegen.js +552 -0
- package/dist/codegen.js.map +1 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.js +3 -125
- package/dist/index.js.map +1 -1
- package/dist/schema.d.ts +817 -1945
- package/dist/schema.js +152 -113
- package/dist/schema.js.map +1 -1
- package/package.json +9 -5
- package/src/cli.ts +100 -0
- package/src/codegen.ts +726 -0
- package/src/index.ts +9 -150
- package/src/schema.ts +396 -176
- package/dist/generator.d.ts +0 -7
- package/dist/generator.js +0 -579
- package/dist/generator.js.map +0 -1
- package/src/generator.ts +0 -664
package/src/codegen.ts
ADDED
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
import { dirname as getDirname, relative as getRelativePath } from 'node:path/posix';
|
|
2
|
+
|
|
3
|
+
import * as prettier from 'prettier';
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
LexArray,
|
|
7
|
+
LexBlob,
|
|
8
|
+
LexiconDoc,
|
|
9
|
+
LexIpldType,
|
|
10
|
+
LexObject,
|
|
11
|
+
LexPrimitive,
|
|
12
|
+
LexRecord,
|
|
13
|
+
LexRefVariant,
|
|
14
|
+
LexXrpcBody,
|
|
15
|
+
LexXrpcParameters,
|
|
16
|
+
LexXrpcProcedure,
|
|
17
|
+
LexXrpcQuery,
|
|
18
|
+
LexXrpcSubscription,
|
|
19
|
+
} from './schema.js';
|
|
20
|
+
|
|
21
|
+
export interface SourceFile {
|
|
22
|
+
filename: string;
|
|
23
|
+
code: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ImportMapping {
|
|
27
|
+
nsid: string[];
|
|
28
|
+
imports: string | ((nsid: string) => { type: 'named' | 'namespace'; from: string });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface LexiconApiOptions {
|
|
32
|
+
documents: LexiconDoc[];
|
|
33
|
+
mappings: ImportMapping[];
|
|
34
|
+
prettier?: {
|
|
35
|
+
cwd?: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface LexiconApiResult {
|
|
40
|
+
files: SourceFile[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type DocumentMap = Map<string, LexiconDoc>;
|
|
44
|
+
type ImportSet = Set<string>;
|
|
45
|
+
|
|
46
|
+
type Literal = string | number | boolean;
|
|
47
|
+
|
|
48
|
+
const lit: (val: Literal | Literal[]) => string = JSON.stringify;
|
|
49
|
+
|
|
50
|
+
const resolveExternalImport = (nsid: string, mappings: ImportMapping[]): ImportMapping | undefined => {
|
|
51
|
+
return mappings.find((mapping) => {
|
|
52
|
+
return mapping.nsid.some((pattern) => {
|
|
53
|
+
if (pattern.endsWith('.*')) {
|
|
54
|
+
return nsid.startsWith(pattern.slice(0, -1));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return nsid === pattern;
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const PURE = `/*#__PURE__*/`;
|
|
63
|
+
|
|
64
|
+
export const generateLexiconApi = async (opts: LexiconApiOptions): Promise<LexiconApiResult> => {
|
|
65
|
+
const documents = opts.documents.toSorted((a, b) => {
|
|
66
|
+
if (a.id < b.id) {
|
|
67
|
+
return -1;
|
|
68
|
+
}
|
|
69
|
+
if (a.id > b.id) {
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return 0;
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const map: DocumentMap = new Map(documents.map((doc) => [doc.id, doc]));
|
|
77
|
+
const files: SourceFile[] = [];
|
|
78
|
+
|
|
79
|
+
for (const doc of documents) {
|
|
80
|
+
const filename = `types/${doc.id.replaceAll('.', '/')}.ts`;
|
|
81
|
+
const file = {
|
|
82
|
+
imports: '',
|
|
83
|
+
rawschemas: '',
|
|
84
|
+
schemadefs: '',
|
|
85
|
+
schemas: '',
|
|
86
|
+
interfaces: '',
|
|
87
|
+
exports: '',
|
|
88
|
+
ambients: '',
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
file.imports += `import type {} from '@atcute/lexicons';\n`;
|
|
92
|
+
file.imports += `import * as v from '@atcute/lexicons/validations';\n`;
|
|
93
|
+
|
|
94
|
+
const imports = new Set<string>();
|
|
95
|
+
|
|
96
|
+
const sortedDefIds = Object.keys(doc.defs).toSorted((a, b) => {
|
|
97
|
+
if (a < b) {
|
|
98
|
+
return -1;
|
|
99
|
+
}
|
|
100
|
+
if (a > b) {
|
|
101
|
+
return 1;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return 0;
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
for (const defId of sortedDefIds) {
|
|
108
|
+
const def = doc.defs[defId];
|
|
109
|
+
const defUri = `${doc.id}#${defId}`;
|
|
110
|
+
|
|
111
|
+
const camelcased = toCamelCase(defId);
|
|
112
|
+
const varname = `${camelcased}Schema`;
|
|
113
|
+
|
|
114
|
+
let result: string;
|
|
115
|
+
switch (def.type) {
|
|
116
|
+
case 'query': {
|
|
117
|
+
result = generateXrpcQuery(imports, defUri, def);
|
|
118
|
+
|
|
119
|
+
file.imports += `import type {} from '@atcute/lexicons/ambient';\n`;
|
|
120
|
+
|
|
121
|
+
file.ambients += `declare module '@atcute/lexicons/ambient' {\n`;
|
|
122
|
+
file.ambients += ` interface XRPCQueries {\n`;
|
|
123
|
+
file.ambients += ` ${lit(stripMainHash(defUri))}: ${camelcased}Schema;\n`;
|
|
124
|
+
file.ambients += ` }\n`;
|
|
125
|
+
file.ambients += `}`;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case 'procedure': {
|
|
129
|
+
result = generateXrpcProcedure(imports, defUri, def);
|
|
130
|
+
|
|
131
|
+
file.imports += `import type {} from '@atcute/lexicons/ambient';\n`;
|
|
132
|
+
|
|
133
|
+
file.ambients += `declare module '@atcute/lexicons/ambient' {\n`;
|
|
134
|
+
file.ambients += ` interface XRPCProcedures {\n`;
|
|
135
|
+
file.ambients += ` ${lit(stripMainHash(defUri))}: ${camelcased}Schema;\n`;
|
|
136
|
+
file.ambients += ` }\n`;
|
|
137
|
+
file.ambients += `}`;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case 'subscription': {
|
|
141
|
+
result = generateXrpcSubscription(imports, defUri, def);
|
|
142
|
+
|
|
143
|
+
file.imports += `import type {} from '@atcute/lexicons/ambient';\n`;
|
|
144
|
+
|
|
145
|
+
file.ambients += `declare module '@atcute/lexicons/ambient' {\n`;
|
|
146
|
+
file.ambients += ` interface XRPCSubscriptions {\n`;
|
|
147
|
+
file.ambients += ` ${lit(stripMainHash(defUri))}: ${camelcased}Schema;\n`;
|
|
148
|
+
file.ambients += ` }\n`;
|
|
149
|
+
file.ambients += `}`;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
case 'object': {
|
|
153
|
+
result = generateObject(imports, defUri, def);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
case 'record': {
|
|
157
|
+
result = generateRecord(imports, defUri, def);
|
|
158
|
+
|
|
159
|
+
file.imports += `import type {} from '@atcute/lexicons/ambient';\n`;
|
|
160
|
+
|
|
161
|
+
file.ambients += `declare module '@atcute/lexicons/ambient' {\n`;
|
|
162
|
+
file.ambients += ` interface Records {\n`;
|
|
163
|
+
file.ambients += ` ${lit(stripMainHash(defUri))}: ${camelcased}Schema;\n`;
|
|
164
|
+
file.ambients += ` }\n`;
|
|
165
|
+
file.ambients += `}`;
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case 'token': {
|
|
169
|
+
result = `${PURE} v.literal(${lit(stripMainHash(defUri))})`;
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
default: {
|
|
173
|
+
result = generateType(imports, defUri, def);
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
file.rawschemas += `const _${varname} = ${result};\n`;
|
|
179
|
+
|
|
180
|
+
file.schemadefs += `type ${camelcased}$schematype = typeof _${varname};\n`;
|
|
181
|
+
|
|
182
|
+
file.schemas += `export interface ${camelcased}Schema extends ${camelcased}$schematype {}\n`;
|
|
183
|
+
|
|
184
|
+
file.exports += `export const ${varname} = _${varname} as ${camelcased}Schema;\n`;
|
|
185
|
+
|
|
186
|
+
switch (def.type) {
|
|
187
|
+
case 'array':
|
|
188
|
+
case 'object':
|
|
189
|
+
case 'record':
|
|
190
|
+
case 'unknown': {
|
|
191
|
+
file.interfaces += `export interface ${toTitleCase(defId)} extends v.InferInput<typeof ${varname}> {}\n`;
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case 'blob':
|
|
195
|
+
case 'boolean':
|
|
196
|
+
case 'bytes':
|
|
197
|
+
case 'cid-link':
|
|
198
|
+
case 'integer':
|
|
199
|
+
case 'string':
|
|
200
|
+
case 'token': {
|
|
201
|
+
file.interfaces += `export type ${toTitleCase(defId)} = v.InferInput<typeof ${varname}>;\n`;
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
{
|
|
208
|
+
const dirname = getDirname(filename);
|
|
209
|
+
|
|
210
|
+
const sortedImports = [...imports].toSorted((a, b) => {
|
|
211
|
+
if (a < b) {
|
|
212
|
+
return -1;
|
|
213
|
+
}
|
|
214
|
+
if (a > b) {
|
|
215
|
+
return 1;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return 0;
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
for (const ns of sortedImports) {
|
|
222
|
+
const local = map.get(ns);
|
|
223
|
+
|
|
224
|
+
if (local) {
|
|
225
|
+
const target = `types/${ns.replaceAll('.', '/')}.js`;
|
|
226
|
+
|
|
227
|
+
let relative = getRelativePath(dirname, target);
|
|
228
|
+
if (!relative.startsWith('.')) {
|
|
229
|
+
relative = `./${relative}`;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
file.imports += `import * as ${toTitleCase(ns)} from ${lit(relative)};\n`;
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const external = resolveExternalImport(ns, opts.mappings);
|
|
237
|
+
|
|
238
|
+
if (external) {
|
|
239
|
+
if (typeof external.imports === 'function') {
|
|
240
|
+
const res = external.imports(ns);
|
|
241
|
+
|
|
242
|
+
if (res.type === 'named') {
|
|
243
|
+
file.imports += `import { ${toTitleCase(ns)} } from ${lit(res.from)};\n`;
|
|
244
|
+
} else if (res.type === 'namespace') {
|
|
245
|
+
file.imports += `import * as ${toTitleCase(ns)} from ${lit(res.from)};\n`;
|
|
246
|
+
}
|
|
247
|
+
} else {
|
|
248
|
+
file.imports += `import { ${toTitleCase(ns)} } from ${lit(external.imports)};\n`;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
throw new Error(`'${doc.id}' referenced non-existent '${ns}' namespace`);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
files.push({
|
|
259
|
+
filename: filename,
|
|
260
|
+
code:
|
|
261
|
+
file.imports +
|
|
262
|
+
`\n\n` +
|
|
263
|
+
file.rawschemas +
|
|
264
|
+
`\n\n` +
|
|
265
|
+
file.schemadefs +
|
|
266
|
+
`\n\n` +
|
|
267
|
+
file.schemas +
|
|
268
|
+
`\n\n` +
|
|
269
|
+
file.exports +
|
|
270
|
+
`\n\n` +
|
|
271
|
+
file.interfaces +
|
|
272
|
+
`\n\n` +
|
|
273
|
+
file.ambients,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
{
|
|
278
|
+
let code = ``;
|
|
279
|
+
|
|
280
|
+
for (const doc of map.values()) {
|
|
281
|
+
code += `export * as ${toTitleCase(doc.id)} from ${lit(`./types/${doc.id.replaceAll('.', '/')}.js`)};\n`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
files.push({
|
|
285
|
+
filename: 'index.ts',
|
|
286
|
+
code: code,
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (opts.prettier) {
|
|
291
|
+
const config = await prettier.resolveConfig(opts.prettier.cwd ?? process.cwd(), { editorconfig: true });
|
|
292
|
+
|
|
293
|
+
for (const file of files) {
|
|
294
|
+
const formatted = await prettier.format(file.code, { ...config, parser: 'typescript' });
|
|
295
|
+
file.code = formatted;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
return { files };
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
const generateXrpcQuery = (imports: ImportSet, defUri: string, spec: LexXrpcQuery): string => {
|
|
303
|
+
const params = generateXrpcParameters(imports, defUri, spec.parameters);
|
|
304
|
+
const output = generateXrpcBody(imports, defUri, spec.output);
|
|
305
|
+
|
|
306
|
+
return `${PURE} v.query(${lit(stripMainHash(defUri))}, {\n"params": ${params}, "output": ${output} })`;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const generateXrpcProcedure = (imports: ImportSet, defUri: string, spec: LexXrpcProcedure): string => {
|
|
310
|
+
const params = generateXrpcParameters(imports, defUri, spec.parameters);
|
|
311
|
+
const input = generateXrpcBody(imports, defUri, spec.input);
|
|
312
|
+
const output = generateXrpcBody(imports, defUri, spec.output);
|
|
313
|
+
|
|
314
|
+
return `${PURE} v.procedure(${lit(stripMainHash(defUri))}, {\n"params": ${params}, "input": ${input}, "output": ${output} })`;
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const generateXrpcSubscription = (imports: ImportSet, defUri: string, spec: LexXrpcSubscription): string => {
|
|
318
|
+
const schema = spec.message?.schema;
|
|
319
|
+
|
|
320
|
+
const params = generateXrpcParameters(imports, defUri, spec.parameters);
|
|
321
|
+
|
|
322
|
+
let inner = ``;
|
|
323
|
+
|
|
324
|
+
inner += `"params": ${params},`;
|
|
325
|
+
|
|
326
|
+
if (schema) {
|
|
327
|
+
if (schema.type === 'object') {
|
|
328
|
+
const res = generateObject(imports, defUri, schema, 'none');
|
|
329
|
+
|
|
330
|
+
inner += `"message": ${res},`;
|
|
331
|
+
} else {
|
|
332
|
+
const res = generateType(imports, defUri, schema);
|
|
333
|
+
|
|
334
|
+
inner += `get "message" () { return ${res} },`;
|
|
335
|
+
}
|
|
336
|
+
} else {
|
|
337
|
+
inner += `"message": null,`;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return `${PURE} v.subscription(${lit(stripMainHash(defUri))}, {\n${inner}})`;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const generateXrpcBody = (imports: ImportSet, defUri: string, spec: LexXrpcBody | undefined): string => {
|
|
344
|
+
if (spec === undefined) {
|
|
345
|
+
return `null`;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const schema = spec.schema;
|
|
349
|
+
const encoding = spec.encoding;
|
|
350
|
+
|
|
351
|
+
if (schema) {
|
|
352
|
+
let inner = ``;
|
|
353
|
+
|
|
354
|
+
inner += `"type": "lex",`;
|
|
355
|
+
|
|
356
|
+
if (schema.type === 'object') {
|
|
357
|
+
const res = generateObject(imports, defUri, schema, 'none');
|
|
358
|
+
|
|
359
|
+
inner += `"schema": ${res},`;
|
|
360
|
+
} else {
|
|
361
|
+
const res = generateType(imports, defUri, schema);
|
|
362
|
+
|
|
363
|
+
inner += `get "schema" () { return ${res} },`;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
return `{\n${inner}}`;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (encoding) {
|
|
370
|
+
return `{\n"type": "blob" }`;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return `null`;
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
const generateXrpcParameters = (
|
|
377
|
+
imports: ImportSet,
|
|
378
|
+
defUri: string,
|
|
379
|
+
spec: LexXrpcParameters | undefined,
|
|
380
|
+
): string => {
|
|
381
|
+
if (spec === undefined) {
|
|
382
|
+
return `null`;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const mask: LexObject = {
|
|
386
|
+
type: 'object',
|
|
387
|
+
description: spec.description,
|
|
388
|
+
required: spec.required,
|
|
389
|
+
properties: spec.properties,
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
return generateObject(imports, defUri, mask, 'none');
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const generateRecord = (imports: ImportSet, defUri: string, spec: LexRecord): string => {
|
|
396
|
+
const schema = generateObject(imports, defUri, spec.record, 'required');
|
|
397
|
+
|
|
398
|
+
let key = `${PURE} v.string()`;
|
|
399
|
+
if (spec.key) {
|
|
400
|
+
if (spec.key === 'tid') {
|
|
401
|
+
key = `${PURE} v.tidString()`;
|
|
402
|
+
} else if (spec.key === 'nsid') {
|
|
403
|
+
key = `${PURE} v.nsidString()`;
|
|
404
|
+
} else if (spec.key.startsWith('literal:')) {
|
|
405
|
+
key = `${PURE} v.literal(${lit(spec.key.slice('literal:'.length))})`;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
return `${PURE} v.record(${key}, ${schema})`;
|
|
410
|
+
};
|
|
411
|
+
|
|
412
|
+
const generateObject = (
|
|
413
|
+
imports: ImportSet,
|
|
414
|
+
defUri: string,
|
|
415
|
+
spec: LexObject,
|
|
416
|
+
writeType: 'required' | 'optional' | 'none' = 'optional',
|
|
417
|
+
): string => {
|
|
418
|
+
const required = new Set(spec.required);
|
|
419
|
+
const nullable = new Set(spec.nullable);
|
|
420
|
+
|
|
421
|
+
let inner = ``;
|
|
422
|
+
|
|
423
|
+
switch (writeType) {
|
|
424
|
+
case 'optional': {
|
|
425
|
+
inner += `"$type": ${PURE} v.optional(${PURE} v.literal(${lit(stripMainHash(defUri))})),`;
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
case 'required': {
|
|
429
|
+
inner += `"$type": ${PURE} v.literal(${lit(stripMainHash(defUri))}),`;
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
for (const [prop, propSpec] of Object.entries(spec.properties ?? {})) {
|
|
435
|
+
const lazy = isRefVariant(propSpec.type === 'array' ? propSpec.items : propSpec);
|
|
436
|
+
const optional = !required.has(prop) && !('default' in propSpec);
|
|
437
|
+
const nulled = nullable.has(prop);
|
|
438
|
+
|
|
439
|
+
let call = generateType(imports, defUri, propSpec, lazy);
|
|
440
|
+
|
|
441
|
+
if (nulled) {
|
|
442
|
+
call = `${PURE} v.nullable(${call})`;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
if (optional) {
|
|
446
|
+
call = `${PURE} v.optional(${call})`;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
if (lazy) {
|
|
450
|
+
inner += `get ${lit(prop)} () { return ${call} },`;
|
|
451
|
+
} else {
|
|
452
|
+
inner += `${lit(prop)}: ${call},`;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
return `${PURE} v.object({\n${inner}})`;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
const generateType = (
|
|
460
|
+
imports: ImportSet,
|
|
461
|
+
defUri: string,
|
|
462
|
+
spec: LexArray | LexPrimitive | LexIpldType | LexRefVariant | LexBlob,
|
|
463
|
+
lazy = false,
|
|
464
|
+
): string => {
|
|
465
|
+
switch (spec.type) {
|
|
466
|
+
// LexRefVariant
|
|
467
|
+
case 'ref': {
|
|
468
|
+
const ref = spec.ref;
|
|
469
|
+
|
|
470
|
+
if (ref.startsWith('#')) {
|
|
471
|
+
const id = ref.slice(1);
|
|
472
|
+
|
|
473
|
+
return `${toCamelCase(id)}Schema`;
|
|
474
|
+
} else {
|
|
475
|
+
const [ns, id = 'main'] = ref.split('#');
|
|
476
|
+
if (ns === stripHash(defUri)) {
|
|
477
|
+
return `${toCamelCase(id)}Schema`;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
imports.add(ns);
|
|
481
|
+
|
|
482
|
+
return `${toTitleCase(ns)}.${toCamelCase(id)}Schema`;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
case 'union': {
|
|
486
|
+
const refs = spec.refs.map((ref): string => {
|
|
487
|
+
if (ref.startsWith('#')) {
|
|
488
|
+
const id = ref.slice(1);
|
|
489
|
+
|
|
490
|
+
return `${toCamelCase(id)}Schema`;
|
|
491
|
+
} else {
|
|
492
|
+
const [ns, id = 'main'] = ref.split('#');
|
|
493
|
+
if (ns === stripHash(defUri)) {
|
|
494
|
+
return `${toCamelCase(id)}Schema`;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
imports.add(ns);
|
|
498
|
+
|
|
499
|
+
return `${toTitleCase(ns)}.${toCamelCase(id)}Schema`;
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
return `${PURE} v.variant([${refs.join(', ')}]${spec.closed ? `, true` : ``})`;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// LexArray
|
|
507
|
+
case 'array': {
|
|
508
|
+
let item = generateType(imports, defUri, spec.items);
|
|
509
|
+
if (!lazy && (spec.items.type === 'ref' || spec.items.type === 'union')) {
|
|
510
|
+
item = `(() => { return ${item}; })`;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
let pipe: string[] = [];
|
|
514
|
+
|
|
515
|
+
if ((spec.minLength ?? 0) > 0 || spec.maxLength !== undefined) {
|
|
516
|
+
if (spec.maxLength === undefined) {
|
|
517
|
+
pipe.push(`${PURE} v.arrayLength(${lit(spec.minLength ?? 0)})`);
|
|
518
|
+
} else {
|
|
519
|
+
pipe.push(`${PURE} v.arrayLength(${lit(spec.minLength ?? 0)}, ${lit(spec.maxLength)})`);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
if (pipe.length === 0) {
|
|
524
|
+
return `${PURE} v.array(${item})`;
|
|
525
|
+
} else {
|
|
526
|
+
return `${PURE} v.constrain(v.array(${item}), [ ${pipe.join(', ')} ])`;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// LexPrimitive
|
|
531
|
+
case 'boolean': {
|
|
532
|
+
if (spec.const !== undefined) {
|
|
533
|
+
return `${PURE} v.literal(${spec.const})`;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
let call = `${PURE} v.boolean()`;
|
|
537
|
+
|
|
538
|
+
if (spec.default !== undefined) {
|
|
539
|
+
call = `${PURE} v.optional(${call}, ${lit(spec.default)})`;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
return call;
|
|
543
|
+
}
|
|
544
|
+
case 'integer': {
|
|
545
|
+
if (spec.const !== undefined) {
|
|
546
|
+
return `${PURE} v.literal(${lit(spec.const)})`;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
if (spec.enum !== undefined) {
|
|
550
|
+
return `${PURE} v.literalEnum(${lit(spec.enum)})`;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
let pipe: string[] = [];
|
|
554
|
+
|
|
555
|
+
if ((spec.minimum ?? 0) > 0 || spec.maximum !== undefined) {
|
|
556
|
+
if (spec.maximum === undefined) {
|
|
557
|
+
pipe.push(`${PURE} v.integerRange(${lit(spec.minimum ?? 0)})`);
|
|
558
|
+
} else {
|
|
559
|
+
pipe.push(`${PURE} v.integerRange(${lit(spec.minimum ?? 0)}, ${lit(spec.maximum)})`);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
let call = `${PURE} v.integer()`;
|
|
564
|
+
|
|
565
|
+
if (pipe.length !== 0) {
|
|
566
|
+
call = `${PURE} v.constrain(${call}, [ ${pipe.join(', ')} ])`;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
if (spec.default !== undefined) {
|
|
570
|
+
call = `${PURE} v.optional(${call}, ${lit(spec.default)})`;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
return call;
|
|
574
|
+
}
|
|
575
|
+
case 'string': {
|
|
576
|
+
if (spec.const !== undefined) {
|
|
577
|
+
return `${PURE} v.literal(${lit(spec.const)})`;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
if (spec.enum !== undefined) {
|
|
581
|
+
return `${PURE} v.literalEnum(${lit(spec.enum)})`;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
let pipe: string[] = [];
|
|
585
|
+
|
|
586
|
+
if ((spec.minLength ?? 0) > 0 || spec.maxLength !== undefined) {
|
|
587
|
+
if (spec.maxLength === undefined) {
|
|
588
|
+
pipe.push(`${PURE} v.stringLength(${lit(spec.minLength ?? 0)})`);
|
|
589
|
+
} else {
|
|
590
|
+
pipe.push(`${PURE} v.stringLength(${lit(spec.minLength ?? 0)}, ${lit(spec.maxLength)})`);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
if ((spec.minGraphemes ?? 0) > 0 || spec.maxGraphemes !== undefined) {
|
|
595
|
+
if (spec.maxGraphemes === undefined) {
|
|
596
|
+
pipe.push(`${PURE} v.stringGraphemes(${lit(spec.minGraphemes ?? 0)})`);
|
|
597
|
+
} else {
|
|
598
|
+
pipe.push(`${PURE} v.stringGraphemes(${lit(spec.minGraphemes ?? 0)}, ${lit(spec.maxGraphemes)})`);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
let call = `${PURE} v.string()`;
|
|
603
|
+
|
|
604
|
+
if (spec.knownValues?.length) {
|
|
605
|
+
call = `${PURE} v.string<${spec.knownValues.map(lit).join(' | ')} | (string & {})>()`;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
switch (spec.format) {
|
|
609
|
+
case 'at-identifier': {
|
|
610
|
+
call = `${PURE} v.actorIdentifierString()`;
|
|
611
|
+
break;
|
|
612
|
+
}
|
|
613
|
+
case 'at-uri': {
|
|
614
|
+
call = `${PURE} v.resourceUriString()`;
|
|
615
|
+
break;
|
|
616
|
+
}
|
|
617
|
+
case 'datetime': {
|
|
618
|
+
call = `${PURE} v.datetimeString()`;
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
case 'did': {
|
|
622
|
+
call = `${PURE} v.didString()`;
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
case 'handle': {
|
|
626
|
+
call = `${PURE} v.handleString()`;
|
|
627
|
+
break;
|
|
628
|
+
}
|
|
629
|
+
case 'language': {
|
|
630
|
+
call = `${PURE} v.languageCodeString()`;
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
633
|
+
case 'nsid': {
|
|
634
|
+
call = `${PURE} v.nsidString()`;
|
|
635
|
+
break;
|
|
636
|
+
}
|
|
637
|
+
case 'record-key': {
|
|
638
|
+
call = `${PURE} v.recordKeyString()`;
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
case 'tid': {
|
|
642
|
+
call = `${PURE} v.tidString()`;
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
case 'uri': {
|
|
646
|
+
call = `${PURE} v.genericUriString()`;
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
if (pipe.length !== 0) {
|
|
652
|
+
call = `${PURE} v.constrain(${call}, [ ${pipe.join(', ')} ])`;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
if (spec.default !== undefined) {
|
|
656
|
+
call = `${PURE} v.optional(${call}, ${lit(spec.default)})`;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
return call;
|
|
660
|
+
}
|
|
661
|
+
case 'unknown': {
|
|
662
|
+
return `${PURE} v.unknown()`;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// LexBlob
|
|
666
|
+
case 'blob': {
|
|
667
|
+
return `${PURE} v.blob()`;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// LexIpldType
|
|
671
|
+
case 'bytes': {
|
|
672
|
+
let pipe: string[] = [];
|
|
673
|
+
|
|
674
|
+
if ((spec.minLength ?? 0) > 0 || spec.maxLength !== undefined) {
|
|
675
|
+
if (spec.maxLength === undefined) {
|
|
676
|
+
pipe.push(`${PURE} v.bytesSize(${lit(spec.minLength ?? 0)})`);
|
|
677
|
+
} else {
|
|
678
|
+
pipe.push(`${PURE} v.bytesSize(${lit(spec.minLength ?? 0)}, ${lit(spec.maxLength)})`);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
let call = `${PURE} v.bytes()`;
|
|
683
|
+
|
|
684
|
+
if (pipe.length !== 0) {
|
|
685
|
+
call = `${PURE} v.constrain(${call}, [ ${pipe.join(', ')} ])`;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
return call;
|
|
689
|
+
}
|
|
690
|
+
case 'cid-link': {
|
|
691
|
+
return `${PURE} v.cidLink()`;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
|
|
696
|
+
const isRefVariant = (
|
|
697
|
+
spec: LexArray | LexPrimitive | LexIpldType | LexRefVariant | LexBlob,
|
|
698
|
+
): spec is LexRefVariant => {
|
|
699
|
+
const type = spec.type;
|
|
700
|
+
return type === 'ref' || type === 'union';
|
|
701
|
+
};
|
|
702
|
+
|
|
703
|
+
const stripHash = (defUri: string): string => {
|
|
704
|
+
const index = defUri.indexOf('#');
|
|
705
|
+
if (index === -1) {
|
|
706
|
+
return defUri;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
return defUri.slice(0, index);
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
const stripMainHash = (defUri: string): string => {
|
|
713
|
+
return defUri.endsWith('#main') ? defUri.slice(0, -'#main'.length) : defUri;
|
|
714
|
+
};
|
|
715
|
+
|
|
716
|
+
const toTitleCase = (v: string): string => {
|
|
717
|
+
v = v.replace(/^([a-z])/gi, (_, g) => g.toUpperCase());
|
|
718
|
+
v = v.replace(/[.#-]([a-z])/gi, (_, g) => g.toUpperCase());
|
|
719
|
+
return v.replace(/[.-]/g, '');
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
const toCamelCase = (v: string): string => {
|
|
723
|
+
v = v.replace(/^([A-Z])/gi, (_, g) => g.toLowerCase());
|
|
724
|
+
v = v.replace(/[.#-]([a-z])/gi, (_, g) => g.toUpperCase());
|
|
725
|
+
return v.replace(/[.-]/g, '');
|
|
726
|
+
};
|