@fedify/vocab-tools 2.0.0-pr.458.1785
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/LICENSE +20 -0
- package/README.md +26 -0
- package/deno.json +28 -0
- package/dist/mod.cjs +1941 -0
- package/dist/mod.d.cts +190 -0
- package/dist/mod.d.ts +190 -0
- package/dist/mod.js +1916 -0
- package/package.json +49 -0
- package/src/__snapshots__/class.test.ts.deno.snap +81555 -0
- package/src/__snapshots__/class.test.ts.node.snap +81553 -0
- package/src/__snapshots__/class.test.ts.snap +81555 -0
- package/src/class.test.ts +122 -0
- package/src/class.ts +140 -0
- package/src/codec.ts +485 -0
- package/src/constructor.ts +337 -0
- package/src/field.ts +46 -0
- package/src/fs.test.ts +31 -0
- package/src/fs.ts +21 -0
- package/src/generate.ts +24 -0
- package/src/inspector.ts +96 -0
- package/src/mod.ts +7 -0
- package/src/property.ts +397 -0
- package/src/schema.test.ts +203 -0
- package/src/schema.ts +321 -0
- package/src/schema.yaml +247 -0
- package/src/type.ts +643 -0
- package/tsdown.config.ts +9 -0
package/src/type.ts
ADDED
|
@@ -0,0 +1,643 @@
|
|
|
1
|
+
import type { PropertySchema, TypeSchema } from "./schema.ts";
|
|
2
|
+
|
|
3
|
+
// The list of JSON-LD contexts to apply heuristics that bypass the proper
|
|
4
|
+
// JSON-LD processor.
|
|
5
|
+
const HEURISTICS_CONTEXTS: string[] = [
|
|
6
|
+
"https://www.w3.org/ns/activitystreams",
|
|
7
|
+
"https://w3id.org/security/v1",
|
|
8
|
+
"https://w3id.org/security/data-integrity/v1",
|
|
9
|
+
"https://www.w3.org/ns/did/v1",
|
|
10
|
+
"https://w3id.org/security/multikey/v1",
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
interface ScalarType {
|
|
14
|
+
name: string;
|
|
15
|
+
typeGuard(variable: string): string;
|
|
16
|
+
encoder(variable: string): string;
|
|
17
|
+
compactEncoder?: (variable: string) => string;
|
|
18
|
+
dataCheck(variable: string): string;
|
|
19
|
+
decoder(variable: string, baseUrlVar: string): string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const scalarTypes: Record<string, ScalarType> = {
|
|
23
|
+
"http://www.w3.org/2001/XMLSchema#boolean": {
|
|
24
|
+
name: "boolean",
|
|
25
|
+
typeGuard(v) {
|
|
26
|
+
return `typeof ${v} === "boolean"`;
|
|
27
|
+
},
|
|
28
|
+
encoder(v) {
|
|
29
|
+
return `{ "@value": ${v} }`;
|
|
30
|
+
},
|
|
31
|
+
compactEncoder(v) {
|
|
32
|
+
return v;
|
|
33
|
+
},
|
|
34
|
+
dataCheck(v) {
|
|
35
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
36
|
+
&& typeof ${v}["@value"] === "boolean"`;
|
|
37
|
+
},
|
|
38
|
+
decoder(v) {
|
|
39
|
+
return `${v}["@value"]`;
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
"http://www.w3.org/2001/XMLSchema#integer": {
|
|
43
|
+
name: "number",
|
|
44
|
+
typeGuard(v) {
|
|
45
|
+
return `typeof ${v} === "number" && Number.isInteger(${v})`;
|
|
46
|
+
},
|
|
47
|
+
encoder(v) {
|
|
48
|
+
return `{
|
|
49
|
+
"@type": "http://www.w3.org/2001/XMLSchema#integer",
|
|
50
|
+
"@value": ${v},
|
|
51
|
+
}`;
|
|
52
|
+
},
|
|
53
|
+
compactEncoder(v) {
|
|
54
|
+
return v;
|
|
55
|
+
},
|
|
56
|
+
dataCheck(v) {
|
|
57
|
+
return `typeof ${v} === "object" && "@type" in ${v}
|
|
58
|
+
&& ${v}["@type"] === "http://www.w3.org/2001/XMLSchema#integer"
|
|
59
|
+
&& "@value" in ${v} && typeof ${v}["@value"] === "number"`;
|
|
60
|
+
},
|
|
61
|
+
decoder(v) {
|
|
62
|
+
return `${v}["@value"] as number`;
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
"http://www.w3.org/2001/XMLSchema#nonNegativeInteger": {
|
|
66
|
+
name: "number",
|
|
67
|
+
typeGuard(v) {
|
|
68
|
+
return `typeof ${v} === "number" && Number.isInteger(${v}) && ${v} >= 0`;
|
|
69
|
+
},
|
|
70
|
+
encoder(v) {
|
|
71
|
+
return `{
|
|
72
|
+
"@type": "http://www.w3.org/2001/XMLSchema#nonNegativeInteger",
|
|
73
|
+
"@value": ${v},
|
|
74
|
+
}`;
|
|
75
|
+
},
|
|
76
|
+
compactEncoder(v) {
|
|
77
|
+
return v;
|
|
78
|
+
},
|
|
79
|
+
dataCheck(v) {
|
|
80
|
+
return `typeof ${v} === "object" && "@type" in ${v}
|
|
81
|
+
&& ${v}["@type"] === "http://www.w3.org/2001/XMLSchema#nonNegativeInteger"
|
|
82
|
+
&& "@value" in ${v} && typeof ${v}["@value"] === "number"`;
|
|
83
|
+
},
|
|
84
|
+
decoder(v) {
|
|
85
|
+
return `${v}["@value"]`;
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
"http://www.w3.org/2001/XMLSchema#float": {
|
|
89
|
+
name: "number",
|
|
90
|
+
typeGuard(v) {
|
|
91
|
+
return `typeof ${v} === "number" && !Number.isNaN(${v})`;
|
|
92
|
+
},
|
|
93
|
+
encoder(v) {
|
|
94
|
+
return `{
|
|
95
|
+
"@type": "http://www.w3.org/2001/XMLSchema#float",
|
|
96
|
+
"@value": ${v},
|
|
97
|
+
}`;
|
|
98
|
+
},
|
|
99
|
+
compactEncoder(v) {
|
|
100
|
+
return v;
|
|
101
|
+
},
|
|
102
|
+
dataCheck(v) {
|
|
103
|
+
return `typeof ${v} === "object" && "@type" in ${v}
|
|
104
|
+
&& ${v}["@type"] === "http://www.w3.org/2001/XMLSchema#float"
|
|
105
|
+
&& "@value" in ${v} && typeof ${v}["@value"] === "number"`;
|
|
106
|
+
},
|
|
107
|
+
decoder(v) {
|
|
108
|
+
return `${v}["@value"]`;
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
"http://www.w3.org/2001/XMLSchema#string": {
|
|
112
|
+
name: "string",
|
|
113
|
+
typeGuard(v) {
|
|
114
|
+
return `typeof ${v} === "string"`;
|
|
115
|
+
},
|
|
116
|
+
encoder(v) {
|
|
117
|
+
return `{ "@value": ${v} }`;
|
|
118
|
+
},
|
|
119
|
+
compactEncoder(v) {
|
|
120
|
+
return v;
|
|
121
|
+
},
|
|
122
|
+
dataCheck(v) {
|
|
123
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
124
|
+
&& typeof ${v}["@value"] === "string" && !("@language" in ${v})`;
|
|
125
|
+
},
|
|
126
|
+
decoder(v) {
|
|
127
|
+
return `${v}["@value"]`;
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
"http://www.w3.org/2001/XMLSchema#anyURI": {
|
|
131
|
+
name: "URL",
|
|
132
|
+
typeGuard(v) {
|
|
133
|
+
return `${v} instanceof URL`;
|
|
134
|
+
},
|
|
135
|
+
encoder(v) {
|
|
136
|
+
return `{ "@id": ${v}.href }`;
|
|
137
|
+
},
|
|
138
|
+
compactEncoder(v) {
|
|
139
|
+
return `${v}.href`;
|
|
140
|
+
},
|
|
141
|
+
dataCheck(v) {
|
|
142
|
+
return `typeof ${v} === "object" && "@id" in ${v}
|
|
143
|
+
&& typeof ${v}["@id"] === "string"
|
|
144
|
+
&& ${v}["@id"] !== ""`;
|
|
145
|
+
},
|
|
146
|
+
decoder(v, baseUrlVar) {
|
|
147
|
+
return `${v}["@id"].startsWith("at://")
|
|
148
|
+
? new URL("at://" +
|
|
149
|
+
encodeURIComponent(
|
|
150
|
+
${v}["@id"].includes("/", 5)
|
|
151
|
+
? ${v}["@id"].slice(5, ${v}["@id"].indexOf("/", 5))
|
|
152
|
+
: ${v}["@id"].slice(5)
|
|
153
|
+
) +
|
|
154
|
+
(
|
|
155
|
+
${v}["@id"].includes("/", 5)
|
|
156
|
+
? ${v}["@id"].slice(${v}["@id"].indexOf("/", 5))
|
|
157
|
+
: ""
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
: URL.canParse(${v}["@id"]) && ${baseUrlVar}
|
|
161
|
+
? new URL(${v}["@id"])
|
|
162
|
+
: new URL(${v}["@id"], ${baseUrlVar})`;
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString": {
|
|
166
|
+
name: "LanguageString",
|
|
167
|
+
typeGuard(v) {
|
|
168
|
+
return `${v} instanceof LanguageString`;
|
|
169
|
+
},
|
|
170
|
+
encoder(v) {
|
|
171
|
+
return `{
|
|
172
|
+
"@value": ${v}.toString(),
|
|
173
|
+
"@language": ${v}.locale.baseName,
|
|
174
|
+
}`;
|
|
175
|
+
},
|
|
176
|
+
dataCheck(v) {
|
|
177
|
+
return `typeof ${v} === "object" && "@language" in ${v} && "@value" in ${v}
|
|
178
|
+
&& typeof ${v}["@language"] === "string"
|
|
179
|
+
&& typeof ${v}["@value"] === "string"`;
|
|
180
|
+
},
|
|
181
|
+
decoder(v) {
|
|
182
|
+
return `new LanguageString(${v}["@value"], ${v}["@language"])`;
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
"http://www.w3.org/2001/XMLSchema#dateTime": {
|
|
186
|
+
name: "Temporal.Instant",
|
|
187
|
+
typeGuard(v) {
|
|
188
|
+
return `${v} instanceof Temporal.Instant`;
|
|
189
|
+
},
|
|
190
|
+
encoder(v) {
|
|
191
|
+
return `{
|
|
192
|
+
"@type": "http://www.w3.org/2001/XMLSchema#dateTime",
|
|
193
|
+
"@value": ${v}.toString(),
|
|
194
|
+
}`;
|
|
195
|
+
},
|
|
196
|
+
compactEncoder(v) {
|
|
197
|
+
return `${v}.toString()`;
|
|
198
|
+
},
|
|
199
|
+
dataCheck(v) {
|
|
200
|
+
return `typeof ${v} === "object" && "@type" in ${v}
|
|
201
|
+
&& "@value" in ${v} && typeof ${v}["@value"] === "string"
|
|
202
|
+
&& ${v}["@type"] === "http://www.w3.org/2001/XMLSchema#dateTime"
|
|
203
|
+
// Check if the value is a valid RFC 3339 date-time string
|
|
204
|
+
&& new Date(${v}["@value"]).toString() !== "Invalid Date"
|
|
205
|
+
`;
|
|
206
|
+
},
|
|
207
|
+
decoder(v) {
|
|
208
|
+
return `Temporal.Instant.from(
|
|
209
|
+
${v}["@value"].substring(19).match(/[Z+-]/)
|
|
210
|
+
? ${v}["@value"]
|
|
211
|
+
: ${v}["@value"] + "Z"
|
|
212
|
+
)`;
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
"http://www.w3.org/2001/XMLSchema#duration": {
|
|
216
|
+
name: "Temporal.Duration",
|
|
217
|
+
typeGuard(v) {
|
|
218
|
+
return `${v} instanceof Temporal.Duration`;
|
|
219
|
+
},
|
|
220
|
+
encoder(v) {
|
|
221
|
+
return `{
|
|
222
|
+
"@type": "http://www.w3.org/2001/XMLSchema#duration",
|
|
223
|
+
"@value": ${v}.toString(),
|
|
224
|
+
}`;
|
|
225
|
+
},
|
|
226
|
+
compactEncoder(v) {
|
|
227
|
+
return `${v}.toString()`;
|
|
228
|
+
},
|
|
229
|
+
dataCheck(v) {
|
|
230
|
+
return `typeof ${v} === "object" && "@type" in ${v}
|
|
231
|
+
&& "@value" in ${v} && typeof ${v}["@value"] === "string"
|
|
232
|
+
&& ${v}["@type"] === "http://www.w3.org/2001/XMLSchema#duration"`;
|
|
233
|
+
},
|
|
234
|
+
decoder(v) {
|
|
235
|
+
return `Temporal.Duration.from(${v}["@value"])`;
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
"https://w3id.org/security#cryptosuiteString": {
|
|
239
|
+
name: '"eddsa-jcs-2022"',
|
|
240
|
+
typeGuard(v) {
|
|
241
|
+
return `${v} == "eddsa-jcs-2022"`;
|
|
242
|
+
},
|
|
243
|
+
encoder(v) {
|
|
244
|
+
return `{ "@value": ${v} }`;
|
|
245
|
+
},
|
|
246
|
+
compactEncoder(v) {
|
|
247
|
+
return v;
|
|
248
|
+
},
|
|
249
|
+
dataCheck(v) {
|
|
250
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
251
|
+
&& !("@language" in ${v}) && ${v}["@value"] === "eddsa-jcs-2022"`;
|
|
252
|
+
},
|
|
253
|
+
decoder(v) {
|
|
254
|
+
return `${v}["@value"]`;
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
"https://w3id.org/security#multibase": {
|
|
258
|
+
name: "Uint8Array",
|
|
259
|
+
typeGuard(v) {
|
|
260
|
+
return `${v} instanceof Uint8Array`;
|
|
261
|
+
},
|
|
262
|
+
encoder(v) {
|
|
263
|
+
return `{
|
|
264
|
+
"@type": "https://w3id.org/security#multibase",
|
|
265
|
+
"@value": new TextDecoder().decode(encodeMultibase("base58btc", ${v})),
|
|
266
|
+
}`;
|
|
267
|
+
},
|
|
268
|
+
compactEncoder(v) {
|
|
269
|
+
return `new TextDecoder().decode(encodeMultibase("base58btc", ${v}))`;
|
|
270
|
+
},
|
|
271
|
+
dataCheck(v) {
|
|
272
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
273
|
+
&& typeof ${v}["@value"] === "string"`;
|
|
274
|
+
},
|
|
275
|
+
decoder(v) {
|
|
276
|
+
return `decodeMultibase(${v}["@value"])`;
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
"fedify:langTag": {
|
|
280
|
+
name: "Intl.Locale",
|
|
281
|
+
typeGuard(v) {
|
|
282
|
+
return `${v} instanceof Intl.Locale`;
|
|
283
|
+
},
|
|
284
|
+
encoder(v) {
|
|
285
|
+
return `{ "@value": ${v}.baseName }`;
|
|
286
|
+
},
|
|
287
|
+
compactEncoder(v) {
|
|
288
|
+
return `${v}.baseName`;
|
|
289
|
+
},
|
|
290
|
+
dataCheck(v) {
|
|
291
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
292
|
+
&& typeof ${v}["@value"] === "string" && !("@language" in ${v})`;
|
|
293
|
+
},
|
|
294
|
+
decoder(v) {
|
|
295
|
+
return `new Intl.Locale(${v}["@value"])`;
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
"fedify:url": {
|
|
299
|
+
name: "URL",
|
|
300
|
+
typeGuard(v) {
|
|
301
|
+
return `${v} instanceof URL`;
|
|
302
|
+
},
|
|
303
|
+
encoder(v) {
|
|
304
|
+
return `{ "@value": ${v}.href }`;
|
|
305
|
+
},
|
|
306
|
+
compactEncoder(v) {
|
|
307
|
+
return `${v}.href`;
|
|
308
|
+
},
|
|
309
|
+
dataCheck(v) {
|
|
310
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
311
|
+
&& typeof ${v}["@value"] === "string"
|
|
312
|
+
&& ${v}["@value"] !== "" && ${v}["@value"] !== "/"`;
|
|
313
|
+
},
|
|
314
|
+
decoder(v) {
|
|
315
|
+
return `new URL(${v}["@value"])`;
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
"fedify:publicKey": {
|
|
319
|
+
name: "CryptoKey",
|
|
320
|
+
typeGuard(v) {
|
|
321
|
+
return `
|
|
322
|
+
// @ts-ignore: CryptoKey exists in the global scope.
|
|
323
|
+
${v} instanceof CryptoKey
|
|
324
|
+
`;
|
|
325
|
+
},
|
|
326
|
+
encoder(v) {
|
|
327
|
+
return `{ "@value": await exportSpki(${v}) }`;
|
|
328
|
+
},
|
|
329
|
+
compactEncoder(v) {
|
|
330
|
+
return `await exportSpki(${v})`;
|
|
331
|
+
},
|
|
332
|
+
dataCheck(v) {
|
|
333
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
334
|
+
&& typeof ${v}["@value"] === "string"`;
|
|
335
|
+
},
|
|
336
|
+
decoder(v) {
|
|
337
|
+
return `await importPem(${v}["@value"])`;
|
|
338
|
+
},
|
|
339
|
+
},
|
|
340
|
+
"fedify:multibaseKey": {
|
|
341
|
+
name: "CryptoKey",
|
|
342
|
+
typeGuard(v) {
|
|
343
|
+
return `
|
|
344
|
+
// @ts-ignore: CryptoKey exists in the global scope.
|
|
345
|
+
${v} instanceof CryptoKey
|
|
346
|
+
`;
|
|
347
|
+
},
|
|
348
|
+
encoder(v) {
|
|
349
|
+
return `{
|
|
350
|
+
"@type": "https://w3id.org/security#multibase",
|
|
351
|
+
"@value": await exportMultibaseKey(${v}),
|
|
352
|
+
}`;
|
|
353
|
+
},
|
|
354
|
+
compactEncoder(v) {
|
|
355
|
+
return `await exportMultibaseKey(${v})`;
|
|
356
|
+
},
|
|
357
|
+
dataCheck(v) {
|
|
358
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
359
|
+
&& typeof ${v}["@value"] === "string"`;
|
|
360
|
+
},
|
|
361
|
+
decoder(v) {
|
|
362
|
+
return `await importMultibaseKey(${v}["@value"])`;
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
"fedify:proofPurpose": {
|
|
366
|
+
name: '"assertionMethod" | "authentication" | "capabilityInvocation" | ' +
|
|
367
|
+
'"capabilityDelegation" | "keyAgreement"',
|
|
368
|
+
typeGuard(v) {
|
|
369
|
+
return `${v} === "assertionMethod" || ${v} === "authentication" ||
|
|
370
|
+
${v} === "capabilityInvocation" || ${v} === "capabilityDelegation" ||
|
|
371
|
+
${v} === "keyAgreement"`;
|
|
372
|
+
},
|
|
373
|
+
encoder(v) {
|
|
374
|
+
return `{
|
|
375
|
+
"@id": "https://w3id.org/security#" + ${v},
|
|
376
|
+
}`;
|
|
377
|
+
},
|
|
378
|
+
compactEncoder(v) {
|
|
379
|
+
return v;
|
|
380
|
+
},
|
|
381
|
+
dataCheck(v) {
|
|
382
|
+
return `typeof ${v} === "object" && "@id" in ${v}
|
|
383
|
+
&& typeof ${v}["@id"] === "string"
|
|
384
|
+
&& ${v}["@id"].startsWith("https://w3id.org/security#")
|
|
385
|
+
&& [
|
|
386
|
+
"assertionMethod", "authentication", "capabilityInvocation",
|
|
387
|
+
"capabilityDelegation", "keyAgreement",
|
|
388
|
+
].includes(${v}["@id"].substring(26))`;
|
|
389
|
+
},
|
|
390
|
+
decoder(v) {
|
|
391
|
+
return `${v}["@id"].substring(26)`;
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
"fedify:units": {
|
|
395
|
+
name: '"cm" | "feet" | "inches" | "km" | "m" | "miles"',
|
|
396
|
+
typeGuard(v) {
|
|
397
|
+
return `${v} == "cm" || ${v} == "feet" || ${v} == "inches" ` +
|
|
398
|
+
`|| ${v} == "km" || ${v} == "m" || ${v} == "miles"`;
|
|
399
|
+
},
|
|
400
|
+
encoder(v) {
|
|
401
|
+
return `{ "@value": ${v} }`;
|
|
402
|
+
},
|
|
403
|
+
compactEncoder(v) {
|
|
404
|
+
return v;
|
|
405
|
+
},
|
|
406
|
+
dataCheck(v) {
|
|
407
|
+
return `typeof ${v} === "object" && "@value" in ${v}
|
|
408
|
+
&& (${v}["@value"] == "cm" || ${v}["@value"] == "feet" ` +
|
|
409
|
+
`|| ${v}["@value"] == "inches" || ${v}["@value"] == "km" ` +
|
|
410
|
+
`|| ${v}["@value"] == "m" || ${v}["@value"] == "miles")`;
|
|
411
|
+
},
|
|
412
|
+
decoder(v) {
|
|
413
|
+
return `${v}["@value"]`;
|
|
414
|
+
},
|
|
415
|
+
},
|
|
416
|
+
};
|
|
417
|
+
|
|
418
|
+
export function getTypeName(
|
|
419
|
+
typeUri: string,
|
|
420
|
+
types: Record<string, TypeSchema>,
|
|
421
|
+
): string {
|
|
422
|
+
if (typeUri in types) return types[typeUri].name;
|
|
423
|
+
if (typeUri in scalarTypes) return scalarTypes[typeUri].name;
|
|
424
|
+
throw new Error(`Unknown type: ${typeUri}`);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
export function getTypeNames(
|
|
428
|
+
typeUris: string[],
|
|
429
|
+
types: Record<string, TypeSchema>,
|
|
430
|
+
parentheses = false,
|
|
431
|
+
): string {
|
|
432
|
+
if (typeUris.length < 1) return "never";
|
|
433
|
+
else if (typeUris.length === 1) return getTypeName(typeUris[0], types);
|
|
434
|
+
let typeNames = typeUris.map((typeUri) => getTypeName(typeUri, types));
|
|
435
|
+
typeNames = typeNames.filter((t, i) => typeNames.indexOf(t) === i);
|
|
436
|
+
const t = typeNames.join(" | ");
|
|
437
|
+
return parentheses && typeNames.length > 1 ? `(${t})` : t;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
export function isScalarType(
|
|
441
|
+
typeUri: string,
|
|
442
|
+
types: Record<string, TypeSchema>,
|
|
443
|
+
): boolean {
|
|
444
|
+
if (typeUri in scalarTypes) return true;
|
|
445
|
+
else if (typeUri in types) return !types[typeUri].entity;
|
|
446
|
+
throw new Error(`Unknown type: ${typeUri}`);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
export function areAllScalarTypes(
|
|
450
|
+
typeUris: string[],
|
|
451
|
+
types: Record<string, TypeSchema>,
|
|
452
|
+
): boolean {
|
|
453
|
+
return typeUris.every((typeUri) => isScalarType(typeUri, types));
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
export function isCompactableType(
|
|
457
|
+
typeUri: string,
|
|
458
|
+
types: Record<string, TypeSchema>,
|
|
459
|
+
): boolean {
|
|
460
|
+
if (typeUri in scalarTypes) {
|
|
461
|
+
return scalarTypes[typeUri].compactEncoder != null;
|
|
462
|
+
} else if (typeUri in types) {
|
|
463
|
+
const type = types[typeUri];
|
|
464
|
+
if (type.compactName == null) return false;
|
|
465
|
+
else if (type.extends != null && !isCompactableType(type.extends, types)) {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
const defaultContext = type.defaultContext;
|
|
469
|
+
return defaultContext != null &&
|
|
470
|
+
HEURISTICS_CONTEXTS.includes(defaultContext as string) ||
|
|
471
|
+
Array.isArray(defaultContext) &&
|
|
472
|
+
(defaultContext as string[]).some(
|
|
473
|
+
HEURISTICS_CONTEXTS.includes.bind(HEURISTICS_CONTEXTS),
|
|
474
|
+
);
|
|
475
|
+
}
|
|
476
|
+
throw new Error(`Unknown type: ${typeUri}`);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export function getSubtypes(
|
|
480
|
+
typeUri: string,
|
|
481
|
+
types: Record<string, TypeSchema>,
|
|
482
|
+
excludeSelf = false,
|
|
483
|
+
): string[] {
|
|
484
|
+
const subtypes: string[] = excludeSelf ? [] : [typeUri];
|
|
485
|
+
for (const uri in types) {
|
|
486
|
+
const type = types[uri];
|
|
487
|
+
if (type.extends === typeUri) subtypes.push(...getSubtypes(uri, types));
|
|
488
|
+
}
|
|
489
|
+
return subtypes.filter((t, i) => subtypes.indexOf(t) === i);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
export function getSupertypes(
|
|
493
|
+
typeUri: string,
|
|
494
|
+
types: Record<string, TypeSchema>,
|
|
495
|
+
excludeSelf = false,
|
|
496
|
+
): string[] {
|
|
497
|
+
const supertypes: string[] = excludeSelf ? [] : [typeUri];
|
|
498
|
+
const type = types[typeUri];
|
|
499
|
+
if (type.extends) supertypes.push(...getSupertypes(type.extends, types));
|
|
500
|
+
return supertypes;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export function* getAllProperties(
|
|
504
|
+
typeUri: string,
|
|
505
|
+
types: Record<string, TypeSchema>,
|
|
506
|
+
excludeSelf = false,
|
|
507
|
+
): Iterable<PropertySchema> {
|
|
508
|
+
for (const t of getSupertypes(typeUri, types, excludeSelf)) {
|
|
509
|
+
if (!(t in types)) continue;
|
|
510
|
+
for (const prop of types[t].properties) yield prop;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export function getEncoder(
|
|
515
|
+
typeUri: string,
|
|
516
|
+
types: Record<string, TypeSchema>,
|
|
517
|
+
variable: string,
|
|
518
|
+
optionsVariable: string,
|
|
519
|
+
compact = false,
|
|
520
|
+
): string {
|
|
521
|
+
if (typeUri in scalarTypes) {
|
|
522
|
+
return compact
|
|
523
|
+
? scalarTypes[typeUri].compactEncoder?.(variable) ??
|
|
524
|
+
scalarTypes[typeUri].encoder(variable)
|
|
525
|
+
: scalarTypes[typeUri].encoder(variable);
|
|
526
|
+
}
|
|
527
|
+
if (typeUri in types) {
|
|
528
|
+
return compact
|
|
529
|
+
? `await ${variable}.toJsonLd({
|
|
530
|
+
...(${optionsVariable}),
|
|
531
|
+
format: undefined,
|
|
532
|
+
context: undefined,
|
|
533
|
+
})`
|
|
534
|
+
: `await ${variable}.toJsonLd(${optionsVariable})`;
|
|
535
|
+
}
|
|
536
|
+
throw new Error(`Unknown type: ${typeUri}`);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export function getTypeGuard(
|
|
540
|
+
typeUri: string,
|
|
541
|
+
types: Record<string, TypeSchema>,
|
|
542
|
+
variable: string,
|
|
543
|
+
): string {
|
|
544
|
+
if (typeUri in scalarTypes) return scalarTypes[typeUri].typeGuard(variable);
|
|
545
|
+
if (typeUri in types) return `${variable} instanceof ${types[typeUri].name}`;
|
|
546
|
+
throw new Error(`Unknown type: ${typeUri}`);
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
export function getTypeGuards(
|
|
550
|
+
typeUris: string[],
|
|
551
|
+
types: Record<string, TypeSchema>,
|
|
552
|
+
variable: string,
|
|
553
|
+
): string {
|
|
554
|
+
return typeUris.map((t) => getTypeGuard(t, types, variable)).join(" || ");
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
export function* getEncoders(
|
|
558
|
+
typeUris: string[],
|
|
559
|
+
types: Record<string, TypeSchema>,
|
|
560
|
+
variable: string,
|
|
561
|
+
optionsVariable: string,
|
|
562
|
+
compact = false,
|
|
563
|
+
): Iterable<string> {
|
|
564
|
+
let i = typeUris.length;
|
|
565
|
+
for (const typeUri of typeUris) {
|
|
566
|
+
if (--i > 0) {
|
|
567
|
+
yield getTypeGuard(typeUri, types, variable);
|
|
568
|
+
yield " ? ";
|
|
569
|
+
}
|
|
570
|
+
yield getEncoder(typeUri, types, variable, optionsVariable, compact);
|
|
571
|
+
if (i > 0) yield " : ";
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
export function getDecoder(
|
|
576
|
+
typeUri: string,
|
|
577
|
+
types: Record<string, TypeSchema>,
|
|
578
|
+
variable: string,
|
|
579
|
+
optionsVariable: string,
|
|
580
|
+
baseUrlVariable: string,
|
|
581
|
+
): string {
|
|
582
|
+
if (typeUri in scalarTypes) {
|
|
583
|
+
return scalarTypes[typeUri].decoder(
|
|
584
|
+
variable,
|
|
585
|
+
baseUrlVariable,
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
if (typeUri in types) {
|
|
589
|
+
return `await ${types[typeUri].name}.fromJsonLd(
|
|
590
|
+
${variable},
|
|
591
|
+
{ ...${optionsVariable}, baseUrl: ${baseUrlVariable} }
|
|
592
|
+
)`;
|
|
593
|
+
}
|
|
594
|
+
throw new Error(`Unknown type: ${typeUri}`);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
export function getDataCheck(
|
|
598
|
+
typeUri: string,
|
|
599
|
+
types: Record<string, TypeSchema>,
|
|
600
|
+
variable: string,
|
|
601
|
+
): string {
|
|
602
|
+
if (typeUri in scalarTypes) return scalarTypes[typeUri].dataCheck(variable);
|
|
603
|
+
if (typeUri in types) {
|
|
604
|
+
const subtypes = getSubtypes(typeUri, types);
|
|
605
|
+
return `typeof ${variable} === "object" && "@type" in ${variable}
|
|
606
|
+
&& Array.isArray(${variable}["@type"])` +
|
|
607
|
+
(subtypes.length > 1
|
|
608
|
+
? `&& ${JSON.stringify(subtypes)}.some(
|
|
609
|
+
t => ${variable}["@type"].includes(t))`
|
|
610
|
+
: `&& ${variable}["@type"].includes(${JSON.stringify(typeUri)})`);
|
|
611
|
+
}
|
|
612
|
+
throw new Error(`Unknown type: ${typeUri}`);
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
export function* getDecoders(
|
|
616
|
+
typeUris: string[],
|
|
617
|
+
types: Record<string, TypeSchema>,
|
|
618
|
+
variable: string,
|
|
619
|
+
optionsVariable: string,
|
|
620
|
+
baseUrlVariable: string,
|
|
621
|
+
): Iterable<string> {
|
|
622
|
+
for (const typeUri of typeUris) {
|
|
623
|
+
yield getDataCheck(typeUri, types, variable);
|
|
624
|
+
yield " ? ";
|
|
625
|
+
yield getDecoder(
|
|
626
|
+
typeUri,
|
|
627
|
+
types,
|
|
628
|
+
variable,
|
|
629
|
+
optionsVariable,
|
|
630
|
+
baseUrlVariable,
|
|
631
|
+
);
|
|
632
|
+
yield " : ";
|
|
633
|
+
}
|
|
634
|
+
yield "undefined";
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
export function emitOverride(
|
|
638
|
+
typeUri: string,
|
|
639
|
+
types: Record<string, TypeSchema>,
|
|
640
|
+
): string {
|
|
641
|
+
if (types[typeUri].extends == null) return "";
|
|
642
|
+
return "override";
|
|
643
|
+
}
|