@fedify/vocab-tools 2.1.0-dev.405 → 2.1.0-dev.408
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/deno.json +1 -1
- package/dist/mod.cjs +27 -10
- package/dist/mod.d.cts +110 -100
- package/dist/mod.d.ts +110 -100
- package/dist/mod.js +27 -10
- package/dist/schema.yaml +11 -0
- package/package.json +1 -1
- package/src/__snapshots__/class.test.ts.deno.snap +862 -598
- package/src/__snapshots__/class.test.ts.node.snap +862 -598
- package/src/__snapshots__/class.test.ts.snap +862 -598
- package/src/class.ts +4 -1
- package/src/codec.ts +6 -2
- package/src/generate.ts +1 -1
- package/src/inspector.ts +26 -6
- package/src/schema.ts +11 -0
- package/src/schema.yaml +11 -0
- package/tsdown.config.ts +1 -1
package/dist/mod.d.ts
CHANGED
|
@@ -3,185 +3,195 @@ declare function generateVocab(schemaDir: string, generatedPath: string): Promis
|
|
|
3
3
|
//#endregion
|
|
4
4
|
//#region src/schema.d.ts
|
|
5
5
|
/**
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
* The qualified URI of a type. It is used as the range of a property.
|
|
7
|
+
*/
|
|
8
8
|
type TypeUri = `https://${string}` | `http://${string}` | `fedify:${string}`;
|
|
9
9
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
* The schema of a type. It is used to generate a class.
|
|
11
|
+
*/
|
|
12
12
|
interface TypeSchema {
|
|
13
13
|
/**
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
* The type name. It is used as the name of the generated class.
|
|
15
|
+
*/
|
|
16
16
|
name: string;
|
|
17
17
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
* The qualified URI of the type.
|
|
19
|
+
*/
|
|
20
20
|
uri: TypeUri;
|
|
21
21
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
* The qualified URIs of the base type of the type (if any).
|
|
23
|
+
*/
|
|
24
24
|
extends?: TypeUri;
|
|
25
25
|
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
* The type name used in the compacted JSON-LD document. It is used as the
|
|
27
|
+
* value of the `type` field.
|
|
28
|
+
*/
|
|
29
29
|
compactName?: string;
|
|
30
30
|
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
* Marks the type an entity type rather than a value type. Turning on this
|
|
32
|
+
* flag will make property accessors for the type asynchronous, so that they
|
|
33
|
+
* can load the values of the properties from the remote server.
|
|
34
|
+
*
|
|
35
|
+
* The extended subtypes must have the consistent value of this flag.
|
|
36
|
+
*/
|
|
37
37
|
entity: boolean;
|
|
38
38
|
/**
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
* Whether the type omits `@type` in JSON-LD serialization. When `true`,
|
|
40
|
+
* the generated `toJsonLd()` method will not emit `@type` (or `type` in
|
|
41
|
+
* compact form) in the serialized JSON-LD. The generated `fromJsonLd()`
|
|
42
|
+
* method will still accept `@type` if present for backward compatibility.
|
|
43
|
+
*
|
|
44
|
+
* This is useful for types that are not real vocabulary types but rather
|
|
45
|
+
* anonymous object structures (e.g., `Endpoints`, `Source` in ActivityStreams).
|
|
46
|
+
*/
|
|
47
|
+
typeless?: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* The description of the type. It is used as the doc comment of
|
|
50
|
+
* the generated class.
|
|
51
|
+
*/
|
|
42
52
|
description: string;
|
|
43
53
|
/**
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
* The possible properties of the type.
|
|
55
|
+
*/
|
|
46
56
|
properties: PropertySchema[];
|
|
47
57
|
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
* The default JSON-LD context of the type. It is used as the default
|
|
59
|
+
* context of the generated `toJsonLd()` method.
|
|
60
|
+
*/
|
|
51
61
|
defaultContext: Context;
|
|
52
62
|
}
|
|
53
63
|
interface PropertySchemaBase {
|
|
54
64
|
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
65
|
+
* The singular form of the property name. It is used as the name of the
|
|
66
|
+
* generated property accessors.
|
|
67
|
+
*/
|
|
58
68
|
singularName: string;
|
|
59
69
|
/**
|
|
60
|
-
|
|
61
|
-
|
|
70
|
+
* The qualified URI of the property.
|
|
71
|
+
*/
|
|
62
72
|
uri: string;
|
|
63
73
|
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
74
|
+
* The property name used in the compacted JSON-LD document. It is used as
|
|
75
|
+
* the key of the property.
|
|
76
|
+
*/
|
|
67
77
|
compactName?: string;
|
|
68
78
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
79
|
+
* The qualified URI of the superproperty of the property (if any).
|
|
80
|
+
* It means that the property is a specialization of the referenced property.
|
|
81
|
+
*/
|
|
72
82
|
subpropertyOf?: string;
|
|
73
83
|
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
* The description of the property. It is used as the doc comment of
|
|
85
|
+
* the generated property accessors.
|
|
86
|
+
*/
|
|
77
87
|
description: string;
|
|
78
88
|
/**
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
* Whether the enclosed object should have its own context when the document
|
|
90
|
+
* is compacted.
|
|
91
|
+
*/
|
|
82
92
|
embedContext?: {
|
|
83
93
|
/**
|
|
84
|
-
|
|
85
|
-
|
|
94
|
+
* The compact name of the property that contains the context.
|
|
95
|
+
*/
|
|
86
96
|
compactName: string;
|
|
87
97
|
/**
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
98
|
+
* Whether the embedded context should be the same as the context of
|
|
99
|
+
* the enclosing document.
|
|
100
|
+
*/
|
|
91
101
|
inherit: true;
|
|
92
102
|
};
|
|
93
103
|
}
|
|
94
104
|
type PropertySchemaTyping = {
|
|
95
105
|
/**
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
106
|
+
* Whether the property value has `@type` field. If `true`, the `range` must
|
|
107
|
+
* have only one element.
|
|
108
|
+
*/
|
|
99
109
|
untyped?: false;
|
|
100
110
|
/**
|
|
101
|
-
|
|
102
|
-
|
|
111
|
+
* The qualified URIs of all possible types of the property values.
|
|
112
|
+
*/
|
|
103
113
|
range: [TypeUri] | [TypeUri, ...TypeUri[]];
|
|
104
114
|
} | {
|
|
105
115
|
/**
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
116
|
+
* Whether the property value has `@type` field. If `true`, the `range` must
|
|
117
|
+
* have only one element.
|
|
118
|
+
*/
|
|
109
119
|
untyped: true;
|
|
110
120
|
/**
|
|
111
|
-
|
|
112
|
-
|
|
121
|
+
* The qualified URIs of all possible types of the property values.
|
|
122
|
+
*/
|
|
113
123
|
range: [TypeUri];
|
|
114
124
|
};
|
|
115
125
|
/**
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
126
|
+
* The schema of a property. It is used to generate property accessors of
|
|
127
|
+
* a class.
|
|
128
|
+
*/
|
|
119
129
|
type PropertySchema = PropertySchemaBase & PropertySchemaTyping & {
|
|
120
130
|
/**
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
131
|
+
* Marks the property that it can have only one value. Turning on this
|
|
132
|
+
* flag will generate only singular property accessors, so `pluralName`
|
|
133
|
+
* and `singularAccessor` should not be specified.
|
|
134
|
+
*/
|
|
125
135
|
functional?: false;
|
|
126
136
|
/**
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
137
|
+
* The plural form of the property name. It is used as the name of the
|
|
138
|
+
* generated property accessors.
|
|
139
|
+
*/
|
|
130
140
|
pluralName: string;
|
|
131
141
|
/**
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
142
|
+
* Whether to generate singular property accessors. Regardless of this
|
|
143
|
+
* flag, plural property accessors are generated (unless `functional` is
|
|
144
|
+
* turned on).
|
|
145
|
+
*/
|
|
136
146
|
singularAccessor?: boolean;
|
|
137
147
|
/**
|
|
138
|
-
|
|
139
|
-
|
|
148
|
+
* The container type of the property values. It can be unspecified.
|
|
149
|
+
*/
|
|
140
150
|
container?: "graph" | "list";
|
|
141
151
|
} | PropertySchemaBase & PropertySchemaTyping & {
|
|
142
152
|
/**
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
153
|
+
* Marks the property that it can have only one value. Turning on this
|
|
154
|
+
* flag will generate only singular property accessors, so `pluralName`
|
|
155
|
+
* and `singularAccessor` should not be specified.
|
|
156
|
+
*/
|
|
147
157
|
functional: true;
|
|
148
158
|
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
159
|
+
* If it's present, those redundant properties are also filled with
|
|
160
|
+
* the same value altogether when the object is serialized into
|
|
161
|
+
* JSON-LD. When it's deserialized from JSON-LD, it tries to
|
|
162
|
+
* parse the values of the specified properties in order.
|
|
163
|
+
*/
|
|
154
164
|
redundantProperties?: {
|
|
155
165
|
/**
|
|
156
|
-
|
|
157
|
-
|
|
166
|
+
* The qualified URI of the property.
|
|
167
|
+
*/
|
|
158
168
|
uri: string;
|
|
159
169
|
/**
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
170
|
+
* The property name used in the compacted JSON-LD document. It is used
|
|
171
|
+
* as the key of the property.
|
|
172
|
+
*/
|
|
163
173
|
compactName?: string;
|
|
164
174
|
}[];
|
|
165
175
|
};
|
|
166
176
|
/**
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
177
|
+
* A JSON-LD context, which is placed in the `@context` property of a JSON-LD
|
|
178
|
+
* document.
|
|
179
|
+
*/
|
|
170
180
|
type Context = Uri | EmbeddedContext | (Uri | EmbeddedContext)[];
|
|
171
181
|
type Uri = "http://{string}" | "https://{string}";
|
|
172
182
|
type EmbeddedContext = Record<string, TermDefinition>;
|
|
173
183
|
type TermDefinition = Uri | Record<string, Uri | "@id">;
|
|
174
184
|
/**
|
|
175
|
-
|
|
176
|
-
|
|
185
|
+
* Type guard to check if a property is not functional (has pluralName).
|
|
186
|
+
*/
|
|
177
187
|
|
|
178
188
|
/**
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
189
|
+
* Loads all schema files in the directory.
|
|
190
|
+
* @param dir The path of the directory to load schema files from.
|
|
191
|
+
* @returns A map from the qualified URI of a type to its {@link TypeSchema}.
|
|
192
|
+
* @throws {@link AggregateError} if any schema file is invalid. It contains
|
|
193
|
+
* all {@link SchemaError}s of the invalid schema files.
|
|
194
|
+
*/
|
|
185
195
|
declare function loadSchemaFiles(dir: string): Promise<Record<string, TypeSchema>>;
|
|
186
196
|
//#endregion
|
|
187
197
|
//#region src/type.d.ts
|
package/dist/mod.js
CHANGED
|
@@ -8,7 +8,7 @@ import { pascalCase } from "es-toolkit";
|
|
|
8
8
|
|
|
9
9
|
//#region deno.json
|
|
10
10
|
var name = "@fedify/vocab-tools";
|
|
11
|
-
var version = "2.1.0-dev.
|
|
11
|
+
var version = "2.1.0-dev.408+5c3c9d78";
|
|
12
12
|
var license = "MIT";
|
|
13
13
|
var exports = { ".": "./src/mod.ts" };
|
|
14
14
|
var author = {
|
|
@@ -775,7 +775,7 @@ async function* generateEncoder(typeUri, types) {
|
|
|
775
775
|
`;
|
|
776
776
|
}
|
|
777
777
|
yield `
|
|
778
|
-
result["type"] = ${JSON.stringify(type.compactName ?? type.uri)}
|
|
778
|
+
${type.typeless ? "" : `result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};`}
|
|
779
779
|
if (this.id != null) result["id"] = this.id.href;
|
|
780
780
|
result["@context"] = ${JSON.stringify(type.defaultContext)};
|
|
781
781
|
return result;
|
|
@@ -830,7 +830,7 @@ async function* generateEncoder(typeUri, types) {
|
|
|
830
830
|
`;
|
|
831
831
|
}
|
|
832
832
|
yield `
|
|
833
|
-
values["@type"] = [${JSON.stringify(type.uri)}]
|
|
833
|
+
${type.typeless ? "" : `values["@type"] = [${JSON.stringify(type.uri)}];`}
|
|
834
834
|
if (this.id != null) values["@id"] = this.id.href;
|
|
835
835
|
if (options.format === "expand") {
|
|
836
836
|
return await jsonld.expand(
|
|
@@ -1390,25 +1390,41 @@ async function* generateInspector(typeUri, types) {
|
|
|
1390
1390
|
yield `
|
|
1391
1391
|
return proxy;
|
|
1392
1392
|
}
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1393
|
+
`;
|
|
1394
|
+
}
|
|
1395
|
+
/**
|
|
1396
|
+
* Generates code that must appear *after* the class closing brace: prototype
|
|
1397
|
+
* assignments for the Deno and Node.js custom-inspect hooks.
|
|
1398
|
+
*
|
|
1399
|
+
* These are emitted outside the class body because computed property names
|
|
1400
|
+
* using `Symbol.for()` are incompatible with `isolatedDeclarations` mode.
|
|
1401
|
+
*/
|
|
1402
|
+
async function* generateInspectorPostClass(typeUri, types) {
|
|
1403
|
+
const type = types[typeUri];
|
|
1404
|
+
const className = type.name;
|
|
1405
|
+
yield `
|
|
1406
|
+
// deno-lint-ignore no-explicit-any
|
|
1407
|
+
(${className}.prototype as any)[Symbol.for("Deno.customInspect")] =
|
|
1408
|
+
function (
|
|
1409
|
+
this: ${className},
|
|
1396
1410
|
inspect: typeof Deno.inspect,
|
|
1397
1411
|
options: Deno.InspectOptions,
|
|
1398
1412
|
): string {
|
|
1399
1413
|
const proxy = this._getCustomInspectProxy();
|
|
1400
1414
|
return ${JSON.stringify(type.name + " ")} + inspect(proxy, options);
|
|
1401
|
-
}
|
|
1415
|
+
};
|
|
1402
1416
|
|
|
1403
|
-
|
|
1404
|
-
|
|
1417
|
+
// deno-lint-ignore no-explicit-any
|
|
1418
|
+
(${className}.prototype as any)[Symbol.for("nodejs.util.inspect.custom")] =
|
|
1419
|
+
function (
|
|
1420
|
+
this: ${className},
|
|
1405
1421
|
_depth: number,
|
|
1406
1422
|
options: unknown,
|
|
1407
1423
|
inspect: (value: unknown, options: unknown) => string,
|
|
1408
1424
|
): string {
|
|
1409
1425
|
const proxy = this._getCustomInspectProxy();
|
|
1410
1426
|
return ${JSON.stringify(type.name + " ")} + inspect(proxy, options);
|
|
1411
|
-
}
|
|
1427
|
+
};
|
|
1412
1428
|
`;
|
|
1413
1429
|
}
|
|
1414
1430
|
|
|
@@ -1867,6 +1883,7 @@ async function* generateClass(typeUri, types) {
|
|
|
1867
1883
|
for await (const code of generateDecoder(typeUri, types)) yield code;
|
|
1868
1884
|
for await (const code of generateInspector(typeUri, types)) yield code;
|
|
1869
1885
|
yield "}\n\n";
|
|
1886
|
+
for await (const code of generateInspectorPostClass(typeUri, types)) yield code;
|
|
1870
1887
|
}
|
|
1871
1888
|
/**
|
|
1872
1889
|
* Generates the TypeScript classes from the given types.
|
package/dist/schema.yaml
CHANGED
|
@@ -223,6 +223,17 @@ properties:
|
|
|
223
223
|
|
|
224
224
|
The extended subtypes must have the consistent value of this flag.
|
|
225
225
|
type: boolean
|
|
226
|
+
typeless:
|
|
227
|
+
description: >-
|
|
228
|
+
Whether the type omits @type in JSON-LD serialization. When true,
|
|
229
|
+
the generated toJsonLd() method will not emit @type (or type in
|
|
230
|
+
compact form) in the serialized JSON-LD. The generated fromJsonLd()
|
|
231
|
+
method will still accept @type if present for backward compatibility.
|
|
232
|
+
|
|
233
|
+
This is useful for types that are not real vocabulary types but rather
|
|
234
|
+
anonymous object structures (e.g., Endpoints, Source in
|
|
235
|
+
ActivityStreams).
|
|
236
|
+
type: boolean
|
|
226
237
|
description:
|
|
227
238
|
description: >-
|
|
228
239
|
The description of the type. It is used as the doc comment of
|