@ir-kit/openapi 0.1.0 → 0.3.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/README.md +1 -1
- package/dist/index.d.ts +9 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/operation/body.d.ts +23 -31
- package/dist/operation/body.d.ts.map +1 -1
- package/dist/operation/body.js +24 -17
- package/dist/operation/body.js.map +1 -1
- package/dist/operation/iter.d.ts +1 -1
- package/dist/operation/iter.d.ts.map +1 -1
- package/dist/operation/iter.js +2 -1
- package/dist/operation/iter.js.map +1 -1
- package/dist/operation/responses.d.ts +14 -34
- package/dist/operation/responses.d.ts.map +1 -1
- package/dist/operation/responses.js +21 -21
- package/dist/operation/responses.js.map +1 -1
- package/dist/operation/signature.d.ts +1 -1
- package/dist/operation/signature.js +1 -1
- package/dist/spec/constants.d.ts +20 -0
- package/dist/spec/constants.d.ts.map +1 -0
- package/dist/spec/constants.js +37 -0
- package/dist/spec/constants.js.map +1 -0
- package/dist/spec/normalize/allOf.d.ts +3 -0
- package/dist/spec/normalize/allOf.d.ts.map +1 -0
- package/dist/spec/normalize/allOf.js +42 -0
- package/dist/spec/normalize/allOf.js.map +1 -0
- package/dist/spec/normalize/enums.d.ts +10 -0
- package/dist/spec/normalize/enums.d.ts.map +1 -0
- package/dist/spec/normalize/enums.js +121 -0
- package/dist/spec/normalize/enums.js.map +1 -0
- package/dist/spec/normalize/index.d.ts +20 -0
- package/dist/spec/normalize/index.d.ts.map +1 -0
- package/dist/spec/normalize/index.js +37 -0
- package/dist/spec/normalize/index.js.map +1 -0
- package/dist/spec/normalize/metadata.d.ts +12 -0
- package/dist/spec/normalize/metadata.d.ts.map +1 -0
- package/dist/spec/normalize/metadata.js +49 -0
- package/dist/spec/normalize/metadata.js.map +1 -0
- package/dist/spec/normalize/name.d.ts +8 -0
- package/dist/spec/normalize/name.d.ts.map +1 -0
- package/dist/spec/normalize/name.js +24 -0
- package/dist/spec/normalize/name.js.map +1 -0
- package/dist/spec/normalize/objects.d.ts +14 -0
- package/dist/spec/normalize/objects.d.ts.map +1 -0
- package/dist/spec/normalize/objects.js +229 -0
- package/dist/spec/normalize/objects.js.map +1 -0
- package/dist/spec/normalize/prune.d.ts +15 -0
- package/dist/spec/normalize/prune.d.ts.map +1 -0
- package/dist/spec/normalize/prune.js +87 -0
- package/dist/spec/normalize/prune.js.map +1 -0
- package/dist/spec/normalize/walk.d.ts +21 -0
- package/dist/spec/normalize/walk.d.ts.map +1 -0
- package/dist/spec/normalize/walk.js +91 -0
- package/dist/spec/normalize/walk.js.map +1 -0
- package/dist/spec/ref.d.ts +7 -0
- package/dist/spec/ref.d.ts.map +1 -0
- package/dist/spec/ref.js +18 -0
- package/dist/spec/ref.js.map +1 -0
- package/dist/spec/security.d.ts +22 -0
- package/dist/spec/security.d.ts.map +1 -0
- package/dist/spec/security.js +47 -0
- package/dist/spec/security.js.map +1 -0
- package/dist/type/dispatch.d.ts +36 -64
- package/dist/type/dispatch.d.ts.map +1 -1
- package/dist/type/dispatch.js +31 -54
- package/dist/type/dispatch.js.map +1 -1
- package/package.json +2 -2
- package/dist/type/enum.d.ts +0 -42
- package/dist/type/enum.d.ts.map +0 -1
- package/dist/type/enum.js +0 -49
- package/dist/type/enum.js.map +0 -1
- package/dist/type/object.d.ts +0 -53
- package/dist/type/object.d.ts.map +0 -1
- package/dist/type/object.js +0 -36
- package/dist/type/object.js.map +0 -1
- package/dist/type/union.d.ts +0 -35
- package/dist/type/union.d.ts.map +0 -1
- package/dist/type/union.js +0 -29
- package/dist/type/union.js.map +0 -1
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Algorithm adapted from @hey-api/openapi-ts (MIT) —
|
|
3
|
+
* `packages/shared/src/openApi/shared/transforms/enums.ts` `rootMode`.
|
|
4
|
+
* Deviations: signature includes `format`; only the `'root'` direction
|
|
5
|
+
* is supported; metadata is reconciled across hits (see `metadata.ts`).
|
|
6
|
+
*/
|
|
7
|
+
import { preserveMetadata } from "./metadata.js";
|
|
8
|
+
import { applyNaming, uniqueComponentName } from "./name.js";
|
|
9
|
+
import { getSchemasObject, isRootSchemaPath, schemasPointerPrefix, walkSchemas, } from "./walk.js";
|
|
10
|
+
const DEFAULT_NAMING = {
|
|
11
|
+
case: "PascalCase",
|
|
12
|
+
name: "{{name}}Enum",
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Hoist identical inline enums to `components.schemas` and rewrite each
|
|
16
|
+
* occurrence as a `$ref`. Returns the set of newly-added schema names.
|
|
17
|
+
*/
|
|
18
|
+
export function dedupeInlineEnums(spec, opts = {}) {
|
|
19
|
+
const hoisted = new Set();
|
|
20
|
+
const schemas = getSchemasObject(spec);
|
|
21
|
+
if (!schemas)
|
|
22
|
+
return hoisted;
|
|
23
|
+
const pointerPrefix = schemasPointerPrefix(spec);
|
|
24
|
+
if (!pointerPrefix)
|
|
25
|
+
return hoisted;
|
|
26
|
+
const naming = opts.naming ?? DEFAULT_NAMING;
|
|
27
|
+
const existingBySignature = {};
|
|
28
|
+
for (const [name, schema] of Object.entries(schemas)) {
|
|
29
|
+
const signature = enumSignature(schema);
|
|
30
|
+
if (signature)
|
|
31
|
+
existingBySignature[signature] = name;
|
|
32
|
+
}
|
|
33
|
+
const hits = [];
|
|
34
|
+
const hitsBySignature = new Map();
|
|
35
|
+
walkSchemas(spec, ({ key, node, parent, path }) => {
|
|
36
|
+
if (key === null || parent === null)
|
|
37
|
+
return;
|
|
38
|
+
if (isRootSchemaPath(path))
|
|
39
|
+
return;
|
|
40
|
+
const signature = enumSignature(node);
|
|
41
|
+
if (!signature)
|
|
42
|
+
return;
|
|
43
|
+
const hit = {
|
|
44
|
+
parent: parent,
|
|
45
|
+
key: key,
|
|
46
|
+
signature,
|
|
47
|
+
schema: node,
|
|
48
|
+
};
|
|
49
|
+
hits.push(hit);
|
|
50
|
+
const arr = hitsBySignature.get(signature);
|
|
51
|
+
if (arr)
|
|
52
|
+
arr.push(hit);
|
|
53
|
+
else
|
|
54
|
+
hitsBySignature.set(signature, [hit]);
|
|
55
|
+
});
|
|
56
|
+
if (hits.length === 0)
|
|
57
|
+
return hoisted;
|
|
58
|
+
const nameBySignature = new Map();
|
|
59
|
+
const schemaBySignature = new Map();
|
|
60
|
+
const reservedNames = new Set();
|
|
61
|
+
for (const { schema, signature, key } of hits) {
|
|
62
|
+
if (nameBySignature.has(signature))
|
|
63
|
+
continue;
|
|
64
|
+
const existingName = existingBySignature[signature];
|
|
65
|
+
if (existingName) {
|
|
66
|
+
nameBySignature.set(signature, existingName);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const base = applyNaming(canonicalBase(schema, key), naming);
|
|
70
|
+
const name = uniqueComponentName(base, schemas, reservedNames);
|
|
71
|
+
reservedNames.add(name);
|
|
72
|
+
nameBySignature.set(signature, name);
|
|
73
|
+
schemaBySignature.set(signature, schema);
|
|
74
|
+
}
|
|
75
|
+
for (const [signature, name] of nameBySignature) {
|
|
76
|
+
if (name in schemas)
|
|
77
|
+
continue;
|
|
78
|
+
const schema = schemaBySignature.get(signature);
|
|
79
|
+
if (schema && typeof schema === "object") {
|
|
80
|
+
const target = schema;
|
|
81
|
+
preserveMetadata(hitsBySignature.get(signature) ?? [], target);
|
|
82
|
+
schemas[name] = target;
|
|
83
|
+
hoisted.add(name);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
for (const { parent, key, signature } of hits) {
|
|
87
|
+
const name = nameBySignature.get(signature);
|
|
88
|
+
if (!name)
|
|
89
|
+
continue;
|
|
90
|
+
const ref = { $ref: `${pointerPrefix}${name}` };
|
|
91
|
+
if (Array.isArray(parent)) {
|
|
92
|
+
parent[key] = ref;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
parent[key] = ref;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return hoisted;
|
|
99
|
+
}
|
|
100
|
+
/** Signature key: `(type, format, sorted enum values)`. Differs in `format` matters for typed languages. */
|
|
101
|
+
function enumSignature(schema) {
|
|
102
|
+
if (!schema || typeof schema !== "object")
|
|
103
|
+
return undefined;
|
|
104
|
+
const s = schema;
|
|
105
|
+
if (!Array.isArray(s.enum))
|
|
106
|
+
return undefined;
|
|
107
|
+
return JSON.stringify({
|
|
108
|
+
type: s.type ?? "",
|
|
109
|
+
format: s.format ?? "",
|
|
110
|
+
values: [...s.enum].sort(),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function canonicalBase(schema, key) {
|
|
114
|
+
if (schema && typeof schema === "object" && "title" in schema) {
|
|
115
|
+
const title = schema.title;
|
|
116
|
+
if (typeof title === "string" && title)
|
|
117
|
+
return title;
|
|
118
|
+
}
|
|
119
|
+
return String(key);
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=enums.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enums.js","sourceRoot":"","sources":["../../../src/spec/normalize/enums.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,oBAAoB,EACpB,WAAW,GACZ,MAAM,WAAW,CAAC;AAMnB,MAAM,cAAc,GAAiB;IACnC,IAAI,EAAE,YAAY;IAClB,IAAI,EAAE,cAAc;CACrB,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAC/B,IAAa,EACb,OAAwB,EAAE;IAE1B,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,MAAM,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACvC,IAAI,CAAC,OAAO;QAAE,OAAO,OAAO,CAAC;IAC7B,MAAM,aAAa,GAAG,oBAAoB,C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,OAAO,IAAI,MAAM,EAAE,CAAC;QAC9D,MAAM,KAAK,GAAI,MAAkC,CAAC,KAAK,CAAC;QACxD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK;YAAE,OAAO,KAAK,CAAC;IACvD,CAAC;IACD,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;AACrB,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { EnumPassOptions } from "./enums.js";
|
|
2
|
+
import type { ObjectPassOptions } from "./objects.js";
|
|
3
|
+
export type { EnumPassOptions } from "./enums.js";
|
|
4
|
+
export type { Casing, NamingConfig, NamingRule } from "./name.js";
|
|
5
|
+
export type { ObjectPassOptions } from "./objects.js";
|
|
6
|
+
export interface NormalizeOptions {
|
|
7
|
+
/** Hoist identical inline enums to `components.schemas`. */
|
|
8
|
+
enums?: boolean | EnumPassOptions;
|
|
9
|
+
/** Collapse single-element `allOf` compositions. */
|
|
10
|
+
allOf?: boolean;
|
|
11
|
+
/** Hoist identical inline objects. Opt-in: structural equality ≠ domain equality. */
|
|
12
|
+
objects?: boolean | ObjectPassOptions;
|
|
13
|
+
/** Drop unreferenced schemas. Scoped to pipeline-hoisted entries; authored schemas are never touched. */
|
|
14
|
+
prune?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/** Apply enabled passes to `spec` in place. Returns the same reference. */
|
|
17
|
+
export declare function normalizeSpec<T>(spec: T, opts?: NormalizeOptions): T;
|
|
18
|
+
/** Every pass except `objects` (kept opt-in for its false-merge risk). */
|
|
19
|
+
export declare const SAFE_NORMALIZE: NormalizeOptions;
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/spec/normalize/index.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAItD,YAAY,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,YAAY,EAAE,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAClE,YAAY,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEtD,MAAM,WAAW,gBAAgB;IAC/B,4DAA4D;IAC5D,KAAK,CAAC,EAAE,OAAO,GAAG,eAAe,CAAC;IAClC,oDAAoD;IACpD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,qFAAqF;IACrF,OAAO,CAAC,EAAE,OAAO,GAAG,iBAAiB,CAAC;IACtC,yGAAyG;IACzG,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,2EAA2E;AAC3E,wBAAgB,aAAa,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAE,gBAAqB,GAAG,CAAC,CAmBxE;AAED,0EAA0E;AAC1E,eAAO,MAAM,cAAc,EAAE,gBAI5B,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI spec normalization pipeline. Pure spec → spec passes
|
|
3
|
+
* applied to the raw bundled doc before IR codegen.
|
|
4
|
+
*
|
|
5
|
+
* Order: allOf collapse → enum dedup → object dedup (opt-in) → prune.
|
|
6
|
+
* Pattern adapted from @hey-api/openapi-ts (MIT) `transformOpenApiSpec`.
|
|
7
|
+
*/
|
|
8
|
+
import { collapseSingleAllOf } from "./allOf.js";
|
|
9
|
+
import { dedupeInlineEnums } from "./enums.js";
|
|
10
|
+
import { dedupeInlineObjects } from "./objects.js";
|
|
11
|
+
import { pruneUnusedSchemas } from "./prune.js";
|
|
12
|
+
/** Apply enabled passes to `spec` in place. Returns the same reference. */
|
|
13
|
+
export function normalizeSpec(spec, opts = {}) {
|
|
14
|
+
if (opts.allOf)
|
|
15
|
+
collapseSingleAllOf(spec);
|
|
16
|
+
const hoisted = new Set();
|
|
17
|
+
if (opts.enums) {
|
|
18
|
+
const added = dedupeInlineEnums(spec, opts.enums === true ? {} : opts.enums);
|
|
19
|
+
for (const name of added)
|
|
20
|
+
hoisted.add(name);
|
|
21
|
+
}
|
|
22
|
+
if (opts.objects) {
|
|
23
|
+
const added = dedupeInlineObjects(spec, opts.objects === true ? {} : opts.objects);
|
|
24
|
+
for (const name of added)
|
|
25
|
+
hoisted.add(name);
|
|
26
|
+
}
|
|
27
|
+
if (opts.prune)
|
|
28
|
+
pruneUnusedSchemas(spec, { targets: hoisted });
|
|
29
|
+
return spec;
|
|
30
|
+
}
|
|
31
|
+
/** Every pass except `objects` (kept opt-in for its false-merge risk). */
|
|
32
|
+
export const SAFE_NORMALIZE = {
|
|
33
|
+
allOf: true,
|
|
34
|
+
enums: true,
|
|
35
|
+
prune: true,
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/spec/normalize/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAiBhD,2EAA2E;AAC3E,MAAM,UAAU,aAAa,CAAI,IAAO,EAAE,OAAyB,EAAE;IACnE,IAAI,IAAI,CAAC,KAAK;QAAE,mBAAmB,CAAC,IAAI,CAAC,CAAC;IAC1C,MAAM,OAAO,GAAG,IAAI,GAAG,EAAU,CAAC;IAClC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;QACf,MAAM,KAAK,GAAG,iBAAiB,CAC7B,IAAI,EACJ,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CACtC,CAAC;QACF,KAAK,MAAM,IAAI,IAAI,KAAK;YAAE,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9C,CAAC;IACD,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;QACjB,MAAM,KAAK,GAAG,mBAAmB,CAC/B,IAAI,EACJ,IAAI,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAC1C,CAAC;QACF,KAAK,MAAM,IAAI,IAAI,KAAK;YAAE,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAC9C,CAAC;IACD,IAAI,IAAI,CAAC,KAAK;QAAE,kBAAkB,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC;IAC/D,OAAO,IAAI,CAAC;AACd,CAAC;AAED,0EAA0E;AAC1E,MAAM,CAAC,MAAM,cAAc,GAAqB;IAC9C,KAAK,EAAE,IAAI;IACX,KAAK,EAAE,IAAI;IACX,KAAK,EAAE,IAAI;CACZ,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reconcile presentation metadata across hits in a hoist group so the
|
|
3
|
+
* canonical body keeps the most informative authored context.
|
|
4
|
+
*
|
|
5
|
+
* Rules: longest non-empty `description` / `title` wins; first
|
|
6
|
+
* non-undefined `example` / `examples` wins; any `deprecated: true`
|
|
7
|
+
* propagates.
|
|
8
|
+
*/
|
|
9
|
+
export declare function preserveMetadata(hits: ReadonlyArray<{
|
|
10
|
+
schema: unknown;
|
|
11
|
+
}>, target: Record<string, unknown>): void;
|
|
12
|
+
//# sourceMappingURL=metadata.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metadata.d.ts","sourceRoot":"","sources":["../../../src/spec/normalize/metadata.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,aAAa,CAAC;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC,EACxC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC9B,IAAI,CA8BN"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reconcile presentation metadata across hits in a hoist group so the
|
|
3
|
+
* canonical body keeps the most informative authored context.
|
|
4
|
+
*
|
|
5
|
+
* Rules: longest non-empty `description` / `title` wins; first
|
|
6
|
+
* non-undefined `example` / `examples` wins; any `deprecated: true`
|
|
7
|
+
* propagates.
|
|
8
|
+
*/
|
|
9
|
+
export function preserveMetadata(hits, target) {
|
|
10
|
+
let bestDescription = stringField(target.description);
|
|
11
|
+
let bestTitle = stringField(target.title);
|
|
12
|
+
let example = target.example;
|
|
13
|
+
let examples = target.examples;
|
|
14
|
+
let deprecated = target.deprecated === true;
|
|
15
|
+
for (const hit of hits) {
|
|
16
|
+
if (!hit.schema || typeof hit.schema !== "object")
|
|
17
|
+
continue;
|
|
18
|
+
if (hit.schema === target)
|
|
19
|
+
continue;
|
|
20
|
+
const s = hit.schema;
|
|
21
|
+
const desc = stringField(s.description);
|
|
22
|
+
if (desc && desc.length > bestDescription.length)
|
|
23
|
+
bestDescription = desc;
|
|
24
|
+
const title = stringField(s.title);
|
|
25
|
+
if (title && title.length > bestTitle.length)
|
|
26
|
+
bestTitle = title;
|
|
27
|
+
if (example === undefined && s.example !== undefined)
|
|
28
|
+
example = s.example;
|
|
29
|
+
if (examples === undefined && s.examples !== undefined) {
|
|
30
|
+
examples = s.examples;
|
|
31
|
+
}
|
|
32
|
+
if (s.deprecated === true)
|
|
33
|
+
deprecated = true;
|
|
34
|
+
}
|
|
35
|
+
if (bestDescription)
|
|
36
|
+
target.description = bestDescription;
|
|
37
|
+
if (bestTitle)
|
|
38
|
+
target.title = bestTitle;
|
|
39
|
+
if (example !== undefined)
|
|
40
|
+
target.example = example;
|
|
41
|
+
if (examples !== undefined)
|
|
42
|
+
target.examples = examples;
|
|
43
|
+
if (deprecated)
|
|
44
|
+
target.deprecated = true;
|
|
45
|
+
}
|
|
46
|
+
function stringField(value) {
|
|
47
|
+
return typeof value === "string" ? value : "";
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=metadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metadata.js","sourceRoot":"","sources":["../../../src/spec/normalize/metadata.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,gBAAgB,CAC9B,IAAwC,EACxC,MAA+B;IAE/B,IAAI,eAAe,GAAG,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACtD,IAAI,SAAS,GAAG,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1C,IAAI,OAAO,GAAY,MAAM,CAAC,OAAO,CAAC;IACtC,IAAI,QAAQ,GAAY,MAAM,CAAC,QAAQ,CAAC;IACxC,IAAI,UAAU,GAAG,MAAM,CAAC,UAAU,KAAK,IAAI,CAAC;IAE5C,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;QACvB,IAAI,CAAC,GAAG,CAAC,MAAM,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,QAAQ;YAAE,SAAS;QAC5D,IAAI,GAAG,CAAC,MAAM,KAAK,MAAM;YAAE,SAAS;QACpC,MAAM,CAAC,GAAG,GAAG,CAAC,MAAiC,CAAC;QAEhD,MAAM,IAAI,GAAG,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;QACxC,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,MAAM;YAAE,eAAe,GAAG,IAAI,CAAC;QAEzE,MAAM,KAAK,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QACnC,IAAI,KAAK,IAAI,KAAK,CAAC,MAAM,GAAG,SAAS,CAAC,MAAM;YAAE,SAAS,GAAG,KAAK,CAAC;QAEhE,IAAI,OAAO,KAAK,SAAS,IAAI,CAAC,CAAC,OAAO,KAAK,SAAS;YAAE,OAAO,GAAG,CAAC,CAAC,OAAO,CAAC;QAC1E,IAAI,QAAQ,KAAK,SAAS,IAAI,CAAC,CAAC,QAAQ,KAAK,SAAS,EAAE,CAAC;YACvD,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC;QACxB,CAAC;QACD,IAAI,CAAC,CAAC,UAAU,KAAK,IAAI;YAAE,UAAU,GAAG,IAAI,CAAC;IAC/C,CAAC;IAED,IAAI,eAAe;QAAE,MAAM,CAAC,WAAW,GAAG,eAAe,CAAC;IAC1D,IAAI,SAAS;QAAE,MAAM,CAAC,KAAK,GAAG,SAAS,CAAC;IACxC,IAAI,OAAO,KAAK,SAAS;QAAE,MAAM,CAAC,OAAO,GAAG,OAAO,CAAC;IACpD,IAAI,QAAQ,KAAK,SAAS;QAAE,MAAM,CAAC,QAAQ,GAAG,QAAQ,CAAC;IACvD,IAAI,UAAU;QAAE,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;AAC3C,CAAC;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export type { Casing, NameTransformer, NamingConfig, NamingRule, } from "@hey-api/shared";
|
|
2
|
+
export { applyNaming, resolveNaming, toCase, } from "@hey-api/shared";
|
|
3
|
+
/**
|
|
4
|
+
* Pick a name not already taken, suffixing `2`, `3`, … on collision.
|
|
5
|
+
* Mirrors hey-api's private `getUniqueComponentName`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function uniqueComponentName(base: string, components: Record<string, unknown>, taken?: ReadonlyArray<string> | ReadonlySet<string>): string;
|
|
8
|
+
//# sourceMappingURL=name.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"name.d.ts","sourceRoot":"","sources":["../../../src/spec/normalize/name.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,MAAM,EACN,eAAe,EACf,YAAY,EACZ,UAAU,GACX,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,WAAW,EACX,aAAa,EACb,MAAM,GACP,MAAM,iBAAiB,CAAC;AAEzB;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,KAAK,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,GAClD,MAAM,CAMR"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export { applyNaming, resolveNaming, toCase, } from "@hey-api/shared";
|
|
2
|
+
/**
|
|
3
|
+
* Pick a name not already taken, suffixing `2`, `3`, … on collision.
|
|
4
|
+
* Mirrors hey-api's private `getUniqueComponentName`.
|
|
5
|
+
*/
|
|
6
|
+
export function uniqueComponentName(base, components, taken) {
|
|
7
|
+
if (!isTaken(base, components, taken))
|
|
8
|
+
return base;
|
|
9
|
+
for (let i = 2;; i++) {
|
|
10
|
+
const candidate = `${base}${i}`;
|
|
11
|
+
if (!isTaken(candidate, components, taken))
|
|
12
|
+
return candidate;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function isTaken(name, components, taken) {
|
|
16
|
+
if (name in components)
|
|
17
|
+
return true;
|
|
18
|
+
if (!taken)
|
|
19
|
+
return false;
|
|
20
|
+
if (Array.isArray(taken))
|
|
21
|
+
return taken.includes(name);
|
|
22
|
+
return taken.has(name);
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=name.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"name.js","sourceRoot":"","sources":["../../../src/spec/normalize/name.ts"],"names":[],"mappings":"AAMA,OAAO,EACL,WAAW,EACX,aAAa,EACb,MAAM,GACP,MAAM,iBAAiB,CAAC;AAEzB;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CACjC,IAAY,EACZ,UAAmC,EACnC,KAAmD;IAEnD,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACnD,KAAK,IAAI,CAAC,GAAG,CAAC,GAAI,CAAC,EAAE,EAAE,CAAC;QACtB,MAAM,SAAS,GAAG,GAAG,IAAI,GAAG,CAAC,EAAE,CAAC;QAChC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,UAAU,EAAE,KAAK,CAAC;YAAE,OAAO,SAAS,CAAC;IAC/D,CAAC;AACH,CAAC;AAED,SAAS,OAAO,CACd,IAAY,EACZ,UAAmC,EACnC,KAA8D;IAE9D,IAAI,IAAI,IAAI,UAAU;QAAE,OAAO,IAAI,CAAC;IACpC,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAC;IACzB,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACtD,OAAQ,KAA6B,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { NamingConfig } from "./name.js";
|
|
2
|
+
export interface ObjectPassOptions {
|
|
3
|
+
naming?: NamingConfig;
|
|
4
|
+
/** Skip shapes with fewer than this many properties. Default: 3. */
|
|
5
|
+
minProperties?: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Hoist structurally identical inline objects to `components.schemas`.
|
|
9
|
+
* Inline-only — never merges with existing roots, since structural
|
|
10
|
+
* equality is too weak a signal for object identity. Returns the set
|
|
11
|
+
* of newly-added schema names.
|
|
12
|
+
*/
|
|
13
|
+
export declare function dedupeInlineObjects(spec: unknown, opts?: ObjectPassOptions): Set<string>;
|
|
14
|
+
//# sourceMappingURL=objects.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objects.d.ts","sourceRoot":"","sources":["../../../src/spec/normalize/objects.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAS9C,MAAM,WAAW,iBAAiB;IAChC,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,oEAAoE;IACpE,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAOD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,OAAO,EACb,IAAI,GAAE,iBAAsB,GAC3B,GAAG,CAAC,MAAM,CAAC,CAyEb"}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { preserveMetadata } from "./metadata.js";
|
|
2
|
+
import { applyNaming, uniqueComponentName } from "./name.js";
|
|
3
|
+
import { getSchemasObject, isRootSchemaPath, schemasPointerPrefix, walkSchemas, } from "./walk.js";
|
|
4
|
+
const DEFAULT_NAMING = {
|
|
5
|
+
case: "PascalCase",
|
|
6
|
+
name: "{{name}}Object",
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Hoist structurally identical inline objects to `components.schemas`.
|
|
10
|
+
* Inline-only — never merges with existing roots, since structural
|
|
11
|
+
* equality is too weak a signal for object identity. Returns the set
|
|
12
|
+
* of newly-added schema names.
|
|
13
|
+
*/
|
|
14
|
+
export function dedupeInlineObjects(spec, opts = {}) {
|
|
15
|
+
const hoisted = new Set();
|
|
16
|
+
const schemas = getSchemasObject(spec);
|
|
17
|
+
if (!schemas)
|
|
18
|
+
return hoisted;
|
|
19
|
+
const pointerPrefix = schemasPointerPrefix(spec);
|
|
20
|
+
if (!pointerPrefix)
|
|
21
|
+
return hoisted;
|
|
22
|
+
const naming = opts.naming ?? DEFAULT_NAMING;
|
|
23
|
+
const minProperties = Math.max(1, opts.minProperties ?? 3);
|
|
24
|
+
const groups = new Map();
|
|
25
|
+
walkSchemas(spec, ({ key, node, parent, path }) => {
|
|
26
|
+
if (key === null || parent === null)
|
|
27
|
+
return;
|
|
28
|
+
if (isRootSchemaPath(path))
|
|
29
|
+
return;
|
|
30
|
+
const signature = objectSignature(node, minProperties);
|
|
31
|
+
if (!signature)
|
|
32
|
+
return;
|
|
33
|
+
const hit = {
|
|
34
|
+
parent: parent,
|
|
35
|
+
key: key,
|
|
36
|
+
signature,
|
|
37
|
+
schema: node,
|
|
38
|
+
};
|
|
39
|
+
const arr = groups.get(signature);
|
|
40
|
+
if (arr)
|
|
41
|
+
arr.push(hit);
|
|
42
|
+
else
|
|
43
|
+
groups.set(signature, [hit]);
|
|
44
|
+
});
|
|
45
|
+
if (groups.size === 0)
|
|
46
|
+
return hoisted;
|
|
47
|
+
const nameBySignature = new Map();
|
|
48
|
+
const schemaBySignature = new Map();
|
|
49
|
+
const reservedNames = new Set();
|
|
50
|
+
for (const [signature, hits] of groups) {
|
|
51
|
+
if (hits.length < 2)
|
|
52
|
+
continue;
|
|
53
|
+
const first = hits[0];
|
|
54
|
+
if (!first)
|
|
55
|
+
continue;
|
|
56
|
+
const baseSource = canonicalBase(first.schema, first.key);
|
|
57
|
+
if (!hasUsableName(first.schema, baseSource))
|
|
58
|
+
continue;
|
|
59
|
+
const base = applyNaming(baseSource, naming);
|
|
60
|
+
const name = uniqueComponentName(base, schemas, reservedNames);
|
|
61
|
+
reservedNames.add(name);
|
|
62
|
+
nameBySignature.set(signature, name);
|
|
63
|
+
schemaBySignature.set(signature, first.schema);
|
|
64
|
+
}
|
|
65
|
+
for (const [signature, name] of nameBySignature) {
|
|
66
|
+
if (name in schemas)
|
|
67
|
+
continue;
|
|
68
|
+
const schema = schemaBySignature.get(signature);
|
|
69
|
+
if (schema && typeof schema === "object") {
|
|
70
|
+
const target = schema;
|
|
71
|
+
preserveMetadata(groups.get(signature) ?? [], target);
|
|
72
|
+
schemas[name] = target;
|
|
73
|
+
hoisted.add(name);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (const [signature, hits] of groups) {
|
|
77
|
+
const name = nameBySignature.get(signature);
|
|
78
|
+
if (!name)
|
|
79
|
+
continue;
|
|
80
|
+
for (const { parent, key } of hits) {
|
|
81
|
+
const ref = { $ref: `${pointerPrefix}${name}` };
|
|
82
|
+
if (Array.isArray(parent))
|
|
83
|
+
parent[key] = ref;
|
|
84
|
+
else
|
|
85
|
+
parent[key] = ref;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return hoisted;
|
|
89
|
+
}
|
|
90
|
+
function objectSignature(schema, minProperties) {
|
|
91
|
+
if (!schema || typeof schema !== "object")
|
|
92
|
+
return undefined;
|
|
93
|
+
const s = schema;
|
|
94
|
+
if (s.$ref)
|
|
95
|
+
return undefined;
|
|
96
|
+
if (Array.isArray(s.enum))
|
|
97
|
+
return undefined;
|
|
98
|
+
if (s.type !== undefined && s.type !== "object")
|
|
99
|
+
return undefined;
|
|
100
|
+
// Composition / conditional keywords change semantics if folded.
|
|
101
|
+
if ("allOf" in s ||
|
|
102
|
+
"anyOf" in s ||
|
|
103
|
+
"oneOf" in s ||
|
|
104
|
+
"not" in s ||
|
|
105
|
+
"if" in s ||
|
|
106
|
+
"then" in s ||
|
|
107
|
+
"else" in s) {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
const properties = s.properties;
|
|
111
|
+
if (!properties ||
|
|
112
|
+
typeof properties !== "object" ||
|
|
113
|
+
Array.isArray(properties)) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
const propNames = Object.keys(properties).sort();
|
|
117
|
+
if (propNames.length < minProperties)
|
|
118
|
+
return undefined;
|
|
119
|
+
const required = Array.isArray(s.required)
|
|
120
|
+
? [...s.required].sort()
|
|
121
|
+
: [];
|
|
122
|
+
const propsForSig = propNames.map((name) => [
|
|
123
|
+
name,
|
|
124
|
+
propertySignature(properties[name]),
|
|
125
|
+
]);
|
|
126
|
+
return JSON.stringify({
|
|
127
|
+
properties: propsForSig,
|
|
128
|
+
required,
|
|
129
|
+
additionalProperties: additionalPropertiesSignature(s.additionalProperties),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/** Recursive structural hash of a property's shape. */
|
|
133
|
+
function propertySignature(node) {
|
|
134
|
+
if (!node || typeof node !== "object")
|
|
135
|
+
return "?";
|
|
136
|
+
const s = node;
|
|
137
|
+
if (typeof s.$ref === "string")
|
|
138
|
+
return `ref:${s.$ref}`;
|
|
139
|
+
if (Array.isArray(s.enum)) {
|
|
140
|
+
return `enum:${JSON.stringify({
|
|
141
|
+
type: s.type ?? "",
|
|
142
|
+
format: s.format ?? "",
|
|
143
|
+
values: [...s.enum].sort(),
|
|
144
|
+
})}`;
|
|
145
|
+
}
|
|
146
|
+
const type = (s.type ?? "any");
|
|
147
|
+
if (type === "array") {
|
|
148
|
+
const items = s.items;
|
|
149
|
+
return `array<${propertySignature(items ?? null)}>`;
|
|
150
|
+
}
|
|
151
|
+
if (type === "object" || s.properties) {
|
|
152
|
+
const props = s.properties;
|
|
153
|
+
const propPairs = props
|
|
154
|
+
? Object.keys(props)
|
|
155
|
+
.sort()
|
|
156
|
+
.map((k) => [k, propertySignature(props[k])])
|
|
157
|
+
: [];
|
|
158
|
+
const required = Array.isArray(s.required)
|
|
159
|
+
? [...s.required].sort()
|
|
160
|
+
: [];
|
|
161
|
+
return `object:${JSON.stringify({
|
|
162
|
+
properties: propPairs,
|
|
163
|
+
required,
|
|
164
|
+
additionalProperties: additionalPropertiesSignature(s.additionalProperties),
|
|
165
|
+
})}`;
|
|
166
|
+
}
|
|
167
|
+
const format = s.format ? `:${s.format}` : "";
|
|
168
|
+
return `${type}${format}`;
|
|
169
|
+
}
|
|
170
|
+
function additionalPropertiesSignature(value) {
|
|
171
|
+
if (value === undefined)
|
|
172
|
+
return "u";
|
|
173
|
+
if (value === false)
|
|
174
|
+
return "false";
|
|
175
|
+
if (value === true)
|
|
176
|
+
return "true";
|
|
177
|
+
return propertySignature(value);
|
|
178
|
+
}
|
|
179
|
+
function canonicalBase(schema, key) {
|
|
180
|
+
if (schema && typeof schema === "object" && "title" in schema) {
|
|
181
|
+
const title = schema.title;
|
|
182
|
+
if (typeof title === "string" && title)
|
|
183
|
+
return title;
|
|
184
|
+
}
|
|
185
|
+
return String(key);
|
|
186
|
+
}
|
|
187
|
+
/** Reject numeric / generic keys that would yield meaningless type names like `0Object` or `DataObject`. Title overrides. */
|
|
188
|
+
const GENERIC_KEYS = new Set([
|
|
189
|
+
"attributes",
|
|
190
|
+
"body",
|
|
191
|
+
"config",
|
|
192
|
+
"content",
|
|
193
|
+
"data",
|
|
194
|
+
"details",
|
|
195
|
+
"fields",
|
|
196
|
+
"info",
|
|
197
|
+
"item",
|
|
198
|
+
"items",
|
|
199
|
+
"key",
|
|
200
|
+
"metadata",
|
|
201
|
+
"name",
|
|
202
|
+
"options",
|
|
203
|
+
"params",
|
|
204
|
+
"parameters",
|
|
205
|
+
"payload",
|
|
206
|
+
"properties",
|
|
207
|
+
"props",
|
|
208
|
+
"request",
|
|
209
|
+
"response",
|
|
210
|
+
"result",
|
|
211
|
+
"results",
|
|
212
|
+
"schema",
|
|
213
|
+
"settings",
|
|
214
|
+
"value",
|
|
215
|
+
"values",
|
|
216
|
+
]);
|
|
217
|
+
function hasUsableName(schema, base) {
|
|
218
|
+
if (schema && typeof schema === "object" && "title" in schema) {
|
|
219
|
+
const title = schema.title;
|
|
220
|
+
if (typeof title === "string" && title)
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
if (!base)
|
|
224
|
+
return false;
|
|
225
|
+
if (/^\d+$/.test(base))
|
|
226
|
+
return false;
|
|
227
|
+
return !GENERIC_KEYS.has(base.toLowerCase());
|
|
228
|
+
}
|
|
229
|
+
//# sourceMappingURL=objects.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objects.js","sourceRoot":"","sources":["../../../src/spec/normalize/objects.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe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O;IACP,KAAK;IACL,UAAU;IACV,MAAM;IACN,SAAS;IACT,QAAQ;IACR,YAAY;IACZ,SAAS;IACT,YAAY;IACZ,OAAO;IACP,SAAS;IACT,UAAU;IACV,QAAQ;IACR,SAAS;IACT,QAAQ;IACR,UAAU;IACV,OAAO;IACP,QAAQ;CACT,CAAC,CAAC;AAEH,SAAS,aAAa,CAAC,MAAe,EAAE,IAAY;IAClD,IAAI,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,IAAI,MAAM,EAAE,CAAC;QAC9D,MAAM,KAAK,GAAI,MAAkC,CAAC,KAAK,CAAC;QACxD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK;YAAE,OAAO,IAAI,CAAC;IACtD,CAAC;IACD,IAAI,CAAC,IAAI;QAAE,OAAO,KAAK,CAAC;IACxB,IAAI,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IACrC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface PruneOptions {
|
|
2
|
+
/**
|
|
3
|
+
* When set, only schemas whose name is in this set are eligible for
|
|
4
|
+
* deletion. Pass the union of names hoisted by earlier passes to
|
|
5
|
+
* keep user-authored schemas untouched. Omit for a global sweep.
|
|
6
|
+
*/
|
|
7
|
+
targets?: ReadonlySet<string>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Drop unreferenced `components.schemas` entries. Scoped to `targets`
|
|
11
|
+
* when supplied; otherwise performs a global reachability sweep
|
|
12
|
+
* (operations + non-schema components, then transitive closure).
|
|
13
|
+
*/
|
|
14
|
+
export declare function pruneUnusedSchemas(spec: unknown, opts?: PruneOptions): void;
|
|
15
|
+
//# sourceMappingURL=prune.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prune.d.ts","sourceRoot":"","sources":["../../../src/spec/normalize/prune.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,OAAO,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CAC/B;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,EACb,IAAI,GAAE,YAAiB,GACtB,IAAI,CAuDN"}
|