@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fedify/vocab-tools",
3
- "version": "2.1.0-dev.405+f4e278ae",
3
+ "version": "2.1.0-dev.408+5c3c9d78",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": "./src/mod.ts"
package/dist/mod.cjs CHANGED
@@ -31,7 +31,7 @@ const es_toolkit = __toESM(require("es-toolkit"));
31
31
 
32
32
  //#region deno.json
33
33
  var name = "@fedify/vocab-tools";
34
- var version = "2.1.0-dev.405+f4e278ae";
34
+ var version = "2.1.0-dev.408+5c3c9d78";
35
35
  var license = "MIT";
36
36
  var exports$1 = { ".": "./src/mod.ts" };
37
37
  var author = {
@@ -798,7 +798,7 @@ async function* generateEncoder(typeUri, types) {
798
798
  `;
799
799
  }
800
800
  yield `
801
- result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};
801
+ ${type.typeless ? "" : `result["type"] = ${JSON.stringify(type.compactName ?? type.uri)};`}
802
802
  if (this.id != null) result["id"] = this.id.href;
803
803
  result["@context"] = ${JSON.stringify(type.defaultContext)};
804
804
  return result;
@@ -853,7 +853,7 @@ async function* generateEncoder(typeUri, types) {
853
853
  `;
854
854
  }
855
855
  yield `
856
- values["@type"] = [${JSON.stringify(type.uri)}];
856
+ ${type.typeless ? "" : `values["@type"] = [${JSON.stringify(type.uri)}];`}
857
857
  if (this.id != null) values["@id"] = this.id.href;
858
858
  if (options.format === "expand") {
859
859
  return await jsonld.expand(
@@ -1413,25 +1413,41 @@ async function* generateInspector(typeUri, types) {
1413
1413
  yield `
1414
1414
  return proxy;
1415
1415
  }
1416
-
1417
- // @ts-ignore: suppressing TS4127
1418
- ${emitOverride(typeUri, types)} [Symbol.for("Deno.customInspect")](
1416
+ `;
1417
+ }
1418
+ /**
1419
+ * Generates code that must appear *after* the class closing brace: prototype
1420
+ * assignments for the Deno and Node.js custom-inspect hooks.
1421
+ *
1422
+ * These are emitted outside the class body because computed property names
1423
+ * using `Symbol.for()` are incompatible with `isolatedDeclarations` mode.
1424
+ */
1425
+ async function* generateInspectorPostClass(typeUri, types) {
1426
+ const type = types[typeUri];
1427
+ const className = type.name;
1428
+ yield `
1429
+ // deno-lint-ignore no-explicit-any
1430
+ (${className}.prototype as any)[Symbol.for("Deno.customInspect")] =
1431
+ function (
1432
+ this: ${className},
1419
1433
  inspect: typeof Deno.inspect,
1420
1434
  options: Deno.InspectOptions,
1421
1435
  ): string {
1422
1436
  const proxy = this._getCustomInspectProxy();
1423
1437
  return ${JSON.stringify(type.name + " ")} + inspect(proxy, options);
1424
- }
1438
+ };
1425
1439
 
1426
- // @ts-ignore: suppressing TS4127
1427
- ${emitOverride(typeUri, types)} [Symbol.for("nodejs.util.inspect.custom")](
1440
+ // deno-lint-ignore no-explicit-any
1441
+ (${className}.prototype as any)[Symbol.for("nodejs.util.inspect.custom")] =
1442
+ function (
1443
+ this: ${className},
1428
1444
  _depth: number,
1429
1445
  options: unknown,
1430
1446
  inspect: (value: unknown, options: unknown) => string,
1431
1447
  ): string {
1432
1448
  const proxy = this._getCustomInspectProxy();
1433
1449
  return ${JSON.stringify(type.name + " ")} + inspect(proxy, options);
1434
- }
1450
+ };
1435
1451
  `;
1436
1452
  }
1437
1453
 
@@ -1890,6 +1906,7 @@ async function* generateClass(typeUri, types) {
1890
1906
  for await (const code of generateDecoder(typeUri, types)) yield code;
1891
1907
  for await (const code of generateInspector(typeUri, types)) yield code;
1892
1908
  yield "}\n\n";
1909
+ for await (const code of generateInspectorPostClass(typeUri, types)) yield code;
1893
1910
  }
1894
1911
  /**
1895
1912
  * Generates the TypeScript classes from the given types.
package/dist/mod.d.cts 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
- * The qualified URI of a type. It is used as the range of a property.
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
- * The schema of a type. It is used to generate a class.
11
- */
10
+ * The schema of a type. It is used to generate a class.
11
+ */
12
12
  interface TypeSchema {
13
13
  /**
14
- * The type name. It is used as the name of the generated class.
15
- */
14
+ * The type name. It is used as the name of the generated class.
15
+ */
16
16
  name: string;
17
17
  /**
18
- * The qualified URI of the type.
19
- */
18
+ * The qualified URI of the type.
19
+ */
20
20
  uri: TypeUri;
21
21
  /**
22
- * The qualified URIs of the base type of the type (if any).
23
- */
22
+ * The qualified URIs of the base type of the type (if any).
23
+ */
24
24
  extends?: TypeUri;
25
25
  /**
26
- * The type name used in the compacted JSON-LD document. It is used as the
27
- * value of the `type` field.
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
- * 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
- */
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
- * The description of the type. It is used as the doc comment of
40
- * the generated class.
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
- * The possible properties of the type.
45
- */
54
+ * The possible properties of the type.
55
+ */
46
56
  properties: PropertySchema[];
47
57
  /**
48
- * The default JSON-LD context of the type. It is used as the default
49
- * context of the generated `toJsonLd()` method.
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
- * The singular form of the property name. It is used as the name of the
56
- * generated property accessors.
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
- * The qualified URI of the property.
61
- */
70
+ * The qualified URI of the property.
71
+ */
62
72
  uri: string;
63
73
  /**
64
- * The property name used in the compacted JSON-LD document. It is used as
65
- * the key of the property.
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
- * The qualified URI of the superproperty of the property (if any).
70
- * It means that the property is a specialization of the referenced property.
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
- * The description of the property. It is used as the doc comment of
75
- * the generated property accessors.
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
- * Whether the enclosed object should have its own context when the document
80
- * is compacted.
81
- */
89
+ * Whether the enclosed object should have its own context when the document
90
+ * is compacted.
91
+ */
82
92
  embedContext?: {
83
93
  /**
84
- * The compact name of the property that contains the context.
85
- */
94
+ * The compact name of the property that contains the context.
95
+ */
86
96
  compactName: string;
87
97
  /**
88
- * Whether the embedded context should be the same as the context of
89
- * the enclosing document.
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
- * Whether the property value has `@type` field. If `true`, the `range` must
97
- * have only one element.
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
- * The qualified URIs of all possible types of the property values.
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
- * Whether the property value has `@type` field. If `true`, the `range` must
107
- * have only one element.
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
- * The qualified URIs of all possible types of the property values.
112
- */
121
+ * The qualified URIs of all possible types of the property values.
122
+ */
113
123
  range: [TypeUri];
114
124
  };
115
125
  /**
116
- * The schema of a property. It is used to generate property accessors of
117
- * a class.
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
- * Marks the property that it can have only one value. Turning on this
122
- * flag will generate only singular property accessors, so `pluralName`
123
- * and `singularAccessor` should not be specified.
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
- * The plural form of the property name. It is used as the name of the
128
- * generated property accessors.
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
- * Whether to generate singular property accessors. Regardless of this
133
- * flag, plural property accessors are generated (unless `functional` is
134
- * turned on).
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
- * The container type of the property values. It can be unspecified.
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
- * Marks the property that it can have only one value. Turning on this
144
- * flag will generate only singular property accessors, so `pluralName`
145
- * and `singularAccessor` should not be specified.
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
- * If it's present, those redundant properties are also filled with
150
- * the same value altogether when the object is serialized into
151
- * JSON-LD. When it's deserialized from JSON-LD, it tries to
152
- * parse the values of the specified properties in order.
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
- * The qualified URI of the property.
157
- */
166
+ * The qualified URI of the property.
167
+ */
158
168
  uri: string;
159
169
  /**
160
- * The property name used in the compacted JSON-LD document. It is used
161
- * as the key of the property.
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
- * A JSON-LD context, which is placed in the `@context` property of a JSON-LD
168
- * document.
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
- * Type guard to check if a property is not functional (has pluralName).
176
- */
185
+ * Type guard to check if a property is not functional (has pluralName).
186
+ */
177
187
 
178
188
  /**
179
- * Loads all schema files in the directory.
180
- * @param dir The path of the directory to load schema files from.
181
- * @returns A map from the qualified URI of a type to its {@link TypeSchema}.
182
- * @throws {@link AggregateError} if any schema file is invalid. It contains
183
- * all {@link SchemaError}s of the invalid schema files.
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