@amritk/helpers 0.20.0 → 0.21.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/dist/ref-to-filename.d.ts +23 -2
- package/dist/ref-to-filename.js +12 -6
- package/dist/walk-ref-graph.js +22 -17
- package/package.json +1 -1
|
@@ -15,9 +15,28 @@ export declare const toKebabCase: (value: string) => string;
|
|
|
15
15
|
/**
|
|
16
16
|
* Converts a JSON Schema $ref to a filename.
|
|
17
17
|
*
|
|
18
|
-
*
|
|
18
|
+
* The name is built from every part of the ref that names something: the base
|
|
19
|
+
* URI a relative ref points at, then the pointer's own definition names. For the
|
|
20
|
+
* refs almost every document writes — `#/$defs/contact`,
|
|
21
|
+
* `#/components/schemas/UserProfile`, `#named` — there is exactly one, and the
|
|
22
|
+
* name is what it has always been.
|
|
23
|
+
*
|
|
24
|
+
* Deriving it from the whole ref is what makes the name a *pure function of the
|
|
25
|
+
* ref*, and that is load-bearing rather than tidy. Two definitions in different
|
|
26
|
+
* parents (`#/$defs/user/$defs/meta` and `#/$defs/order/$defs/meta`) or in
|
|
27
|
+
* different embedded resources (`first#/$defs/stuff` and `second#/$defs/stuff`)
|
|
28
|
+
* are an ordinary shape in a real document, and naming both after their last
|
|
29
|
+
* segment made them one file — so generation refused rather than emit a silently
|
|
30
|
+
* wrong type. Resolving that by *renaming* whichever was reached second would
|
|
31
|
+
* need every emitter to agree on the walk order, since each turns a `$ref` into
|
|
32
|
+
* an import name on its own; deriving a distinct name from the ref itself means
|
|
33
|
+
* they agree without having to coordinate at all.
|
|
34
|
+
*
|
|
35
|
+
* Handles these ref forms:
|
|
19
36
|
* - Internal `#/$defs/contact` → `contact`
|
|
20
37
|
* - Internal `#/definitions/ServerVariable` → `server-variable`
|
|
38
|
+
* - Nested `#/$defs/user/$defs/meta` → `user-meta`
|
|
39
|
+
* - Relative-base `second#/$defs/stuff` → `second-stuff`
|
|
21
40
|
* - URI `http://example.com/definitions/3.1.0/channel.json` → `channel`
|
|
22
41
|
* - URI with fragment `http://example.com/channel.json#/definitions/queue` → `channel-queue`
|
|
23
42
|
*
|
|
@@ -28,8 +47,10 @@ export declare const toKebabCase: (value: string) => string;
|
|
|
28
47
|
* ```ts
|
|
29
48
|
* refToFilename('#/$defs/contact') // 'contact'
|
|
30
49
|
* refToFilename('#/$defs/server-variable') // 'server-variable'
|
|
31
|
-
* refToFilename('#/definitions/ServerVariable') // 'server-variable'
|
|
32
50
|
* refToFilename('#/definitions/APIKeySecurityScheme') // 'api-key-security-scheme'
|
|
51
|
+
* refToFilename('#/components/schemas/UserProfile') // 'user-profile'
|
|
52
|
+
* refToFilename('#/$defs/user/$defs/meta') // 'user-meta'
|
|
53
|
+
* refToFilename('second#/$defs/stuff') // 'second-stuff'
|
|
33
54
|
* refToFilename('http://asyncapi.com/definitions/3.1.0/channel.json') // 'channel'
|
|
34
55
|
* refToFilename('#named') // 'named' — a plain `$anchor` ref
|
|
35
56
|
* ```
|
package/dist/ref-to-filename.js
CHANGED
|
@@ -42,16 +42,22 @@ const uriRefToFilename = (uri) => {
|
|
|
42
42
|
return baseName;
|
|
43
43
|
return `${baseName}-${toKebabCase(fragLast)}`;
|
|
44
44
|
};
|
|
45
|
+
const CONTAINER_SEGMENTS = /* @__PURE__ */ new Set(["$defs", "definitions", "properties"]);
|
|
46
|
+
const pointerNameParts = (pointer) => {
|
|
47
|
+
const segments = pointer.split("/").filter((segment) => segment !== "");
|
|
48
|
+
const named = segments.map((segment, index) => ({ segment, nested: index > 0 && CONTAINER_SEGMENTS.has(segments[index - 1]) })).filter(({ segment }) => !CONTAINER_SEGMENTS.has(segment));
|
|
49
|
+
return named.filter((part, index) => part.nested || index === named.length - 1).map(({ segment }) => segment);
|
|
50
|
+
};
|
|
45
51
|
const refToFilename = (ref) => {
|
|
46
52
|
if (ref.startsWith("http://") || ref.startsWith("https://")) {
|
|
47
53
|
return normalizeFilename(uriRefToFilename(ref), ref);
|
|
48
54
|
}
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return normalizeFilename(
|
|
55
|
+
const hashIndex = ref.indexOf("#");
|
|
56
|
+
const base = hashIndex === -1 ? "" : ref.slice(0, hashIndex);
|
|
57
|
+
const pointer = hashIndex === -1 ? ref : ref.slice(hashIndex + 1);
|
|
58
|
+
const baseParts = base.replace(/\.json$/, "").split("/").filter((segment) => segment !== "");
|
|
59
|
+
const parts = [...baseParts, ...pointerNameParts(pointer)];
|
|
60
|
+
return normalizeFilename(parts.map((part) => /[A-Z]/.test(part) ? toKebabCase(part) : part).join("-"), ref);
|
|
55
61
|
};
|
|
56
62
|
export {
|
|
57
63
|
refToFilename,
|
package/dist/walk-ref-graph.js
CHANGED
|
@@ -231,6 +231,24 @@ const cachedExtractRefs = (cache, schema) => {
|
|
|
231
231
|
cache.extractRefsCache.set(schema, refs);
|
|
232
232
|
return refs;
|
|
233
233
|
};
|
|
234
|
+
const claimNames = (ref, resolved, typeSuffix, filenameOwners, typeNameOwners) => {
|
|
235
|
+
const filename = refToFilename(ref);
|
|
236
|
+
const typeName = refToName(ref, typeSuffix);
|
|
237
|
+
const fileOwner = filenameOwners.get(filename);
|
|
238
|
+
if (fileOwner !== void 0) {
|
|
239
|
+
if (sameDefinition(fileOwner.resolved, resolved))
|
|
240
|
+
return void 0;
|
|
241
|
+
throw new Error(`"${ref}" and ${fileOwner.label} both generate the file "${filename}.ts", so only one of them can be emitted and every reference to the other would resolve to the wrong type. Rename one definition.`);
|
|
242
|
+
}
|
|
243
|
+
const typeOwner = typeNameOwners.get(typeName);
|
|
244
|
+
if (typeOwner !== void 0) {
|
|
245
|
+
throw new Error(`"${ref}" and ${typeOwner.label} both generate the type name "${typeName}" from different files, so the generated output would import that name twice and fail to parse. Rename one definition.`);
|
|
246
|
+
}
|
|
247
|
+
const owner = { label: `"${ref}"`, resolved };
|
|
248
|
+
filenameOwners.set(filename, owner);
|
|
249
|
+
typeNameOwners.set(typeName, owner);
|
|
250
|
+
return { filename, typeName };
|
|
251
|
+
};
|
|
234
252
|
const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
|
|
235
253
|
const typeSuffix = options.typeSuffix ?? "";
|
|
236
254
|
const baseCache = getRootCache(rootSchema, options.schemas);
|
|
@@ -289,25 +307,12 @@ const walkRefGraph = (rootSchema, rootTypeName, options, visit) => {
|
|
|
289
307
|
if (!resolved) {
|
|
290
308
|
throw new Error(`Could not resolve $ref "${ref}". Nothing in the document defines it, so the generated output would reference a type and a function that are never emitted. Fix the ref, or drop it from the schema.`);
|
|
291
309
|
}
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
const fileOwner = filenameOwners.get(filename);
|
|
295
|
-
if (fileOwner) {
|
|
296
|
-
if (!sameDefinition(fileOwner.resolved, resolved)) {
|
|
297
|
-
throw new Error(`"${ref}" and ${fileOwner.label} both generate the file "${filename}.ts", so only one of them can be emitted and every reference to the other would resolve to the wrong type. Rename one definition.`);
|
|
298
|
-
}
|
|
299
|
-
} else {
|
|
300
|
-
const typeOwner = typeNameOwners.get(typeName);
|
|
301
|
-
if (typeOwner) {
|
|
302
|
-
throw new Error(`"${ref}" and ${typeOwner.label} both generate the type name "${typeName}" from different files, so the generated output would import that name twice and fail to parse. Rename one definition.`);
|
|
303
|
-
}
|
|
304
|
-
const owner = { label: `"${ref}"`, resolved };
|
|
305
|
-
filenameOwners.set(filename, owner);
|
|
306
|
-
typeNameOwners.set(typeName, owner);
|
|
310
|
+
const claimed = claimNames(ref, resolved, typeSuffix, filenameOwners, typeNameOwners);
|
|
311
|
+
if (claimed !== void 0) {
|
|
307
312
|
visit({
|
|
308
313
|
ref,
|
|
309
|
-
typeName,
|
|
310
|
-
filename,
|
|
314
|
+
typeName: claimed.typeName,
|
|
315
|
+
filename: claimed.filename,
|
|
311
316
|
schema: resolveDynamicRefs(resolved, dynamicRefMap),
|
|
312
317
|
rootSchema: upgraded,
|
|
313
318
|
isRoot: false
|