@skmtc/core 0.44.2 → 0.46.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/esm/anchors/attribute.js +3 -2
- package/esm/context/CoreContext.d.ts +1 -1
- package/esm/context/CoreContext.js +1 -1
- package/esm/context/GenerateContext.d.ts +13 -10
- package/esm/context/GenerateContext.js +36 -25
- package/esm/context/ParseContext.js +136 -34
- package/esm/context/generateTypes.d.ts +7 -6
- package/esm/context/generateTypes.js +2 -0
- package/esm/dsl/GeneratorKeys.d.ts +21 -15
- package/esm/dsl/GeneratorKeys.js +16 -11
- package/esm/dsl/Inserted.d.ts +1 -1
- package/esm/dsl/Inserted.js +1 -1
- package/esm/dsl/model/ModelDriver.d.ts +6 -5
- package/esm/dsl/model/ModelDriver.js +22 -20
- package/esm/dsl/model/toModelEntry.d.ts +18 -18
- package/esm/dsl/model/toModelEntry.js +9 -9
- package/esm/dsl/model/toModelProjectionBase.d.ts +17 -16
- package/esm/dsl/model/toModelProjectionBase.js +12 -8
- package/esm/dsl/model/types.d.ts +26 -17
- package/esm/dsl/operation/gql/toGqlOperationProjectionBase.d.ts +2 -2
- package/esm/dsl/operation/gql/toGqlOperationProjectionBase.js +2 -2
- package/esm/dsl/operation/oas/toOasOperationProjectionBase.d.ts +2 -2
- package/esm/dsl/operation/oas/toOasOperationProjectionBase.js +2 -2
- package/esm/dsl/webhook/toWebhookProjectionBase.d.ts +2 -2
- package/esm/dsl/webhook/toWebhookProjectionBase.js +2 -2
- package/esm/helpers/refFns.d.ts +108 -43
- package/esm/helpers/refFns.js +166 -57
- package/esm/oas/document/Document.d.ts +6 -0
- package/esm/oas/document/Document.js +44 -2
- package/esm/oas/header/Header.js +4 -1
- package/esm/oas/parameter/Parameter.d.ts +1 -1
- package/esm/oas/parameter/Parameter.js +5 -2
- package/esm/oas/ref/DanglingRefError.d.ts +5 -2
- package/esm/oas/ref/DanglingRefError.js +4 -3
- package/esm/oas/ref/Ref.d.ts +23 -12
- package/esm/oas/ref/Ref.js +68 -72
- package/esm/oas/schemaPath/traverseSchema.js +11 -5
- package/esm/parse/missingSchema.d.ts +30 -0
- package/esm/parse/missingSchema.js +33 -0
- package/esm/parse/v3-0/header/toHeadersV3.js +5 -0
- package/esm/parse/v3-0/parameter/toParameterV3.js +11 -0
- package/esm/parse/v3-0/ref/toRefTargetV3.d.ts +23 -0
- package/esm/parse/v3-0/ref/toRefTargetV3.js +70 -0
- package/esm/parse/v3-1/header/toHeadersV3.js +5 -0
- package/esm/parse/v3-1/parameter/toParameterV3.js +11 -0
- package/esm/parse/v3-1/ref/toRefTargetV3.d.ts +23 -0
- package/esm/parse/v3-1/ref/toRefTargetV3.js +70 -0
- package/esm/parse/v3-2/header/toHeadersV3.js +5 -0
- package/esm/parse/v3-2/parameter/toParameterV3.js +11 -0
- package/esm/parse/v3-2/ref/toRefTargetV3.d.ts +23 -0
- package/esm/parse/v3-2/ref/toRefTargetV3.js +70 -0
- package/esm/types/GeneratorType.d.ts +1 -1
- package/esm/types/ProjectionOptions.d.ts +1 -1
- package/esm/types/ProjectionOptions.js +1 -1
- package/esm/types/RefName.d.ts +3 -3
- package/esm/types/TypeSystem.d.ts +3 -3
- package/package.json +1 -1
- package/script/anchors/attribute.js +3 -2
- package/script/context/CoreContext.d.ts +1 -1
- package/script/context/CoreContext.js +1 -1
- package/script/context/GenerateContext.d.ts +13 -10
- package/script/context/GenerateContext.js +36 -25
- package/script/context/ParseContext.js +135 -33
- package/script/context/generateTypes.d.ts +7 -6
- package/script/context/generateTypes.js +2 -0
- package/script/dsl/GeneratorKeys.d.ts +21 -15
- package/script/dsl/GeneratorKeys.js +16 -11
- package/script/dsl/Inserted.d.ts +1 -1
- package/script/dsl/Inserted.js +1 -1
- package/script/dsl/model/ModelDriver.d.ts +6 -5
- package/script/dsl/model/ModelDriver.js +22 -20
- package/script/dsl/model/toModelEntry.d.ts +18 -18
- package/script/dsl/model/toModelEntry.js +9 -9
- package/script/dsl/model/toModelProjectionBase.d.ts +17 -16
- package/script/dsl/model/toModelProjectionBase.js +12 -8
- package/script/dsl/model/types.d.ts +26 -17
- package/script/dsl/operation/gql/toGqlOperationProjectionBase.d.ts +2 -2
- package/script/dsl/operation/gql/toGqlOperationProjectionBase.js +2 -2
- package/script/dsl/operation/oas/toOasOperationProjectionBase.d.ts +2 -2
- package/script/dsl/operation/oas/toOasOperationProjectionBase.js +2 -2
- package/script/dsl/webhook/toWebhookProjectionBase.d.ts +2 -2
- package/script/dsl/webhook/toWebhookProjectionBase.js +2 -2
- package/script/helpers/refFns.d.ts +108 -43
- package/script/helpers/refFns.js +180 -59
- package/script/oas/document/Document.d.ts +6 -0
- package/script/oas/document/Document.js +44 -2
- package/script/oas/header/Header.js +4 -1
- package/script/oas/parameter/Parameter.d.ts +1 -1
- package/script/oas/parameter/Parameter.js +5 -2
- package/script/oas/ref/DanglingRefError.d.ts +5 -2
- package/script/oas/ref/DanglingRefError.js +4 -3
- package/script/oas/ref/Ref.d.ts +23 -12
- package/script/oas/ref/Ref.js +67 -71
- package/script/oas/schemaPath/traverseSchema.js +10 -4
- package/script/parse/missingSchema.d.ts +30 -0
- package/script/parse/missingSchema.js +38 -0
- package/script/parse/v3-0/header/toHeadersV3.js +5 -0
- package/script/parse/v3-0/parameter/toParameterV3.js +11 -0
- package/script/parse/v3-0/ref/toRefTargetV3.d.ts +23 -0
- package/script/parse/v3-0/ref/toRefTargetV3.js +74 -0
- package/script/parse/v3-1/header/toHeadersV3.js +5 -0
- package/script/parse/v3-1/parameter/toParameterV3.js +11 -0
- package/script/parse/v3-1/ref/toRefTargetV3.d.ts +23 -0
- package/script/parse/v3-1/ref/toRefTargetV3.js +74 -0
- package/script/parse/v3-2/header/toHeadersV3.js +5 -0
- package/script/parse/v3-2/parameter/toParameterV3.js +11 -0
- package/script/parse/v3-2/ref/toRefTargetV3.d.ts +23 -0
- package/script/parse/v3-2/ref/toRefTargetV3.js +74 -0
- package/script/types/GeneratorType.d.ts +1 -1
- package/script/types/ProjectionOptions.d.ts +1 -1
- package/script/types/ProjectionOptions.js +1 -1
- package/script/types/RefName.d.ts +3 -3
- package/script/types/TypeSystem.d.ts +3 -3
package/esm/helpers/refFns.js
CHANGED
|
@@ -1,61 +1,157 @@
|
|
|
1
1
|
import "../_dnt.polyfills.js";
|
|
2
2
|
import { DanglingRefError } from '../oas/ref/DanglingRefError.js';
|
|
3
|
+
import { toJsonPointer, unescapeSegment } from '../types/JsonPointer.js';
|
|
4
|
+
import { componentsKeys } from '../oas/components/Components.js';
|
|
5
|
+
import { isPlainObject } from '../parse/isPlainObject.js';
|
|
6
|
+
import { camelCase, capitalize } from './strings.js';
|
|
3
7
|
/**
|
|
4
|
-
*
|
|
8
|
+
* Splits a `$ref` into {@link RefParts}.
|
|
5
9
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
10
|
+
* A `$ref` is a URI whose fragment holds a JSON pointer (RFC 6901), so the
|
|
11
|
+
* name is unescaped as one: `%20` is a space, and `~1` and `~0` are `/` and
|
|
12
|
+
* `~`. `#/components/schemas/Foo%20Bar` names the component `Foo Bar`.
|
|
9
13
|
*
|
|
10
|
-
* A `$ref
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
14
|
+
* A remote `$ref`, one that does not start with `#`, points outside the
|
|
15
|
+
* document, so its `refPath` does not start with `#` and it names nothing
|
|
16
|
+
* here: `other.yml#/components/schemas/A` is `refPath`
|
|
17
|
+
* `other.yml#/components/schemas` and `refName` `A`. This never throws: a
|
|
18
|
+
* `$ref` that names nothing is found out when it is resolved.
|
|
19
|
+
*/
|
|
20
|
+
export const toRefParts = ({ $ref }) => {
|
|
21
|
+
if (!$ref.startsWith('#')) {
|
|
22
|
+
const cut = $ref.lastIndexOf('/');
|
|
23
|
+
const refName = $ref.slice(cut + 1);
|
|
24
|
+
return { refName, refPath: cut === -1 ? '.' : $ref.slice(0, cut) };
|
|
25
|
+
}
|
|
26
|
+
const pointer = $ref.slice(1);
|
|
27
|
+
const segments = (pointer.startsWith('/') ? pointer.slice(1) : pointer)
|
|
28
|
+
.split('/')
|
|
29
|
+
.map(segment => unescapeSegment(toPercentDecoded(segment)));
|
|
30
|
+
return toPartsOf(segments);
|
|
31
|
+
};
|
|
32
|
+
/** The parts of a pointer's segments, each unescaped. */
|
|
33
|
+
const toPartsOf = (segments) => ({
|
|
34
|
+
refName: (segments.at(-1) ?? ''),
|
|
35
|
+
refPath: segments.length > 1 ? toJsonPointer(segments.slice(0, -1)) : '#'
|
|
36
|
+
});
|
|
37
|
+
/** The `components` bucket that holds components of each kind: `schemas`, `parameters`, and so on. */
|
|
38
|
+
export const toComponentsBucket = (refType) => {
|
|
39
|
+
switch (refType) {
|
|
40
|
+
case 'schema':
|
|
41
|
+
return 'schemas';
|
|
42
|
+
case 'requestBody':
|
|
43
|
+
return 'requestBodies';
|
|
44
|
+
case 'parameter':
|
|
45
|
+
return 'parameters';
|
|
46
|
+
case 'response':
|
|
47
|
+
return 'responses';
|
|
48
|
+
case 'example':
|
|
49
|
+
return 'examples';
|
|
50
|
+
case 'header':
|
|
51
|
+
return 'headers';
|
|
52
|
+
case 'securityScheme':
|
|
53
|
+
return 'securitySchemes';
|
|
54
|
+
case 'link':
|
|
55
|
+
return 'links';
|
|
56
|
+
default: {
|
|
57
|
+
const _exhaustive = refType;
|
|
58
|
+
throw new Error(`Unhandled ref type: ${_exhaustive}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Whether a `refPath` is that of a remote `$ref`: one that points outside the
|
|
64
|
+
* document (`other.yml#/components/schemas/A`, `./pet.yaml`, a bare id). Core
|
|
65
|
+
* does not fetch, so a remote `$ref` names nothing. The document is bundled
|
|
66
|
+
* into one before core reads it.
|
|
67
|
+
*/
|
|
68
|
+
export const isRemoteRefPath = (refPath) => !refPath.startsWith('#');
|
|
69
|
+
/** The segments of a `refPath` in the document, each unescaped. `#` has none. */
|
|
70
|
+
export const toRefPathSegments = (refPath) => {
|
|
71
|
+
const body = refPath.startsWith('#/') ? refPath.slice(2) : refPath.slice(1);
|
|
72
|
+
return body === '' ? [] : body.split('/').map(unescapeSegment);
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The `components` bucket a `refPath` is, such as `schemas` for
|
|
76
|
+
* `#/components/schemas`, or `undefined` for any other path. A ref whose path
|
|
77
|
+
* is a bucket names a component, of the bucket's kind whatever kind the ref
|
|
78
|
+
* is written for; any other ref points into a document's content, as one into
|
|
79
|
+
* an extension of `components` (`#/components/x-shared/Foo`) does. A bucket
|
|
80
|
+
* core does not read (`pathItems`) is still a bucket.
|
|
47
81
|
*/
|
|
48
|
-
export const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
throw new Error('Invalid reference');
|
|
82
|
+
export const toRefPathBucket = (refPath) => {
|
|
83
|
+
if (isRemoteRefPath(refPath)) {
|
|
84
|
+
return undefined;
|
|
52
85
|
}
|
|
53
|
-
|
|
86
|
+
const [first, bucket, ...rest] = toRefPathSegments(refPath);
|
|
87
|
+
return first === 'components' && rest.length === 0 && isBucket(bucket) ? bucket : undefined;
|
|
54
88
|
};
|
|
55
|
-
/**
|
|
56
|
-
const
|
|
57
|
-
|
|
89
|
+
/** The `components` buckets OpenAPI defines that core does not read. */
|
|
90
|
+
const unreadBuckets = ['callbacks', 'pathItems', 'mediaTypes'];
|
|
91
|
+
const isBucket = (key) => componentsKeys.includes(key) || unreadBuckets.includes(key);
|
|
92
|
+
/** Whether `refPath` is where components of `refType` live, as `toRefParts` spells it. */
|
|
93
|
+
export const isDefaultRefPath = ({ refPath, refType }) => toRefPathBucket(refPath) === toComponentsBucket(refType);
|
|
94
|
+
/**
|
|
95
|
+
* The whole pointer of a ref, with no percent escapes. Two `$ref`s that point
|
|
96
|
+
* at the same place give the same pointer, so it keys a ref that does not
|
|
97
|
+
* point at a component. A remote `$ref` is given as written.
|
|
98
|
+
*/
|
|
99
|
+
export const toRefPointer = ({ refName, refPath }) => isRemoteRefPath(refPath) ? `${refPath}/${refName}` : toJsonPointer([...toRefPathSegments(refPath), refName]);
|
|
100
|
+
/**
|
|
101
|
+
* The ref of the schema component named `refName`: `refPath`
|
|
102
|
+
* `#/components/schemas`. For code that holds a component's name but no
|
|
103
|
+
* `OasRef`, such as the engine's sweep over `components.schemas`.
|
|
104
|
+
*/
|
|
105
|
+
export const toSchemaComponentRef = (refName) => ({
|
|
106
|
+
refName,
|
|
107
|
+
refPath: `#/components/${toComponentsBucket('schema')}`
|
|
108
|
+
});
|
|
109
|
+
/**
|
|
110
|
+
* The key a model is known by in the engine: its generator key, its
|
|
111
|
+
* enrichments (`[id][key][variant]`) and its manifest entry. A component
|
|
112
|
+
* (`refPath` `#/components/schemas`) is keyed by its `refName`, as it always
|
|
113
|
+
* was. Any other target is keyed by its whole pointer ({@link toRefPointer}),
|
|
114
|
+
* so two targets that share a `refName` stay apart.
|
|
115
|
+
*/
|
|
116
|
+
export const toModelKey = ({ refName, refPath }) => isDefaultRefPath({ refPath, refType: 'schema' }) ? refName : toRefPointer({ refName, refPath });
|
|
117
|
+
/**
|
|
118
|
+
* A name for a model that differs between targets with one `refName`
|
|
119
|
+
* (`…/properties/disk` under two schemas). A component is named by its
|
|
120
|
+
* `refName`, through `toComponentName`. Any other target is named by its whole
|
|
121
|
+
* pointer, its segments joined by `_`: `Order_items`, `Pet_properties_tag`.
|
|
122
|
+
* A segment that is not alphanumeric gets a hash, so `@type` and `type` differ.
|
|
123
|
+
*/
|
|
124
|
+
export const toModelName = ({ refName, refPath }, toComponentName = name => name) => {
|
|
125
|
+
if (isDefaultRefPath({ refPath, refType: 'schema' })) {
|
|
126
|
+
return toComponentName(refName);
|
|
127
|
+
}
|
|
128
|
+
const segments = [...(isRemoteRefPath(refPath) ? [refPath] : toRefPathSegments(refPath)), refName];
|
|
129
|
+
const [first, second, ...rest] = segments;
|
|
130
|
+
const named = first === 'components' && second === 'schemas' ? rest : segments;
|
|
131
|
+
return capitalize(named.map(segment => (/^[a-zA-Z0-9]+$/.test(segment) ? segment : `${camelCase(segment)}${toShortHash(segment)}`)).join('_'));
|
|
58
132
|
};
|
|
133
|
+
/** A 32-bit FNV-1a hash, in base 36. */
|
|
134
|
+
const toShortHash = (value) => [...value]
|
|
135
|
+
.reduce((hash, char) => Math.imul(hash ^ (char.codePointAt(0) ?? 0), 0x01000193) >>> 0, 0x811c9dc5)
|
|
136
|
+
.toString(36);
|
|
137
|
+
/** Whether a model key ({@link toModelKey}) is a whole pointer, and not a component's `refName`. */
|
|
138
|
+
export const isPointerKey = (key) => key.startsWith('#/');
|
|
139
|
+
/**
|
|
140
|
+
* Reverses {@link toModelKey}, for a key read back out of a generator key.
|
|
141
|
+
* A pointer starts with `#/`; any other key is a component's `refName`. A key
|
|
142
|
+
* has no percent escapes, so a `%` in it is read as written.
|
|
143
|
+
*/
|
|
144
|
+
export const fromModelKey = (key) => isPointerKey(key) ? toPartsOf(key.slice(2).split('/').map(unescapeSegment)) : toSchemaComponentRef(key);
|
|
145
|
+
/**
|
|
146
|
+
* The value at a ref's pointer in a raw document, or `undefined` when the
|
|
147
|
+
* pointer leads nowhere, or out of the document. It walks objects by key and arrays by index.
|
|
148
|
+
*/
|
|
149
|
+
export const toRawRefTarget = (document, { refName, refPath }) => isRemoteRefPath(refPath) ? undefined : [...toRefPathSegments(refPath), refName].reduce((value, segment) => {
|
|
150
|
+
if (Array.isArray(value)) {
|
|
151
|
+
return /^\d+$/.test(segment) ? value[Number(segment)] : undefined;
|
|
152
|
+
}
|
|
153
|
+
return isPlainObject(value) && Object.hasOwn(value, segment) ? value[segment] : undefined;
|
|
154
|
+
}, document);
|
|
59
155
|
const toPercentDecoded = (segment) => {
|
|
60
156
|
try {
|
|
61
157
|
return decodeURIComponent(segment);
|
|
@@ -84,7 +180,7 @@ const toPercentDecoded = (segment) => {
|
|
|
84
180
|
* if (isRef(schemaOrRef)) {
|
|
85
181
|
* // TypeScript now knows schemaOrRef has a $ref property
|
|
86
182
|
* console.log(schemaOrRef.$ref); // Safe to access
|
|
87
|
-
* const refName =
|
|
183
|
+
* const { refName, refPath } = toRefParts({ $ref: schemaOrRef.$ref });
|
|
88
184
|
* } else {
|
|
89
185
|
* // Handle as actual schema object
|
|
90
186
|
* console.log('Direct schema object');
|
|
@@ -191,20 +287,27 @@ export const isRef = (value) => {
|
|
|
191
287
|
*/
|
|
192
288
|
export const toGetRef = (oasDocument, onDangling) => ({ $ref }) => toTargetSchema({ oasDocument, onDangling, $ref, followed: [] });
|
|
193
289
|
/**
|
|
194
|
-
* The schema at the end of the chain of `$ref`s
|
|
195
|
-
*
|
|
196
|
-
*
|
|
290
|
+
* The schema at the end of the chain of `$ref`s, each followed by its whole
|
|
291
|
+
* pointer: a component (`#/components/schemas/Pet`) or any other place in
|
|
292
|
+
* the document (`#/components/schemas/Pet/properties/tag`). A `$ref` that
|
|
293
|
+
* names nothing, one to a component that is not a schema, or a chain that
|
|
294
|
+
* comes back to a place it has passed (`A` is a `$ref` to `B`, and `B` to
|
|
295
|
+
* `A`), gives `onDangling` a `DanglingRefError`, or throws it.
|
|
197
296
|
*/
|
|
198
297
|
const toTargetSchema = ({ oasDocument, onDangling, $ref, followed }) => {
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
|
|
298
|
+
const parts = toRefParts({ $ref });
|
|
299
|
+
const bucket = toRefPathBucket(parts.refPath);
|
|
300
|
+
const namesSchema = parts.refName !== '' && (bucket === undefined || bucket === toComponentsBucket('schema'));
|
|
301
|
+
const item = namesSchema ? toRawRefTarget(oasDocument, parts) : undefined;
|
|
302
|
+
if (!isRawSchema(item)) {
|
|
303
|
+
return toDangling(onDangling, new DanglingRefError($ref, { remote: isRemoteRefPath(parts.refPath) }));
|
|
202
304
|
}
|
|
203
305
|
if (!isRef(item)) {
|
|
204
306
|
return item;
|
|
205
307
|
}
|
|
206
308
|
const passed = [...followed, $ref];
|
|
207
|
-
const
|
|
309
|
+
const toPointer = (each) => toRefPointer(toRefParts({ $ref: each }));
|
|
310
|
+
const loopStart = passed.findIndex(each => toPointer(each) === toPointer(item.$ref));
|
|
208
311
|
if (loopStart === -1) {
|
|
209
312
|
return toTargetSchema({ oasDocument, onDangling, $ref: item.$ref, followed: passed });
|
|
210
313
|
}
|
|
@@ -213,6 +316,12 @@ const toTargetSchema = ({ oasDocument, onDangling, $ref, followed }) => {
|
|
|
213
316
|
const loop = [...passed.slice(loopStart + 1), item.$ref];
|
|
214
317
|
return toDangling(onDangling, new DanglingRefError(passed[0] ?? $ref, { loop }));
|
|
215
318
|
};
|
|
319
|
+
/**
|
|
320
|
+
* Whether a value found at a pointer can be read as a schema: an object. Its
|
|
321
|
+
* keywords are read as the merge reads any member; nothing more is checked
|
|
322
|
+
* here, as for a component.
|
|
323
|
+
*/
|
|
324
|
+
const isRawSchema = (value) => isPlainObject(value);
|
|
216
325
|
const toDangling = (onDangling, error) => {
|
|
217
326
|
if (onDangling) {
|
|
218
327
|
return onDangling(error);
|
|
@@ -173,6 +173,12 @@ export declare class OasDocument {
|
|
|
173
173
|
* ```
|
|
174
174
|
*/
|
|
175
175
|
constructor(fields?: DocumentFields);
|
|
176
|
+
/** The parsed target of a ref that does not point at a component, by its whole pointer. */
|
|
177
|
+
toRefTarget(pointer: string): ComponentsEntry | undefined;
|
|
178
|
+
/** Keeps the parsed target of a ref that does not point at a component, under its whole pointer. */
|
|
179
|
+
setRefTarget(pointer: string, target: ComponentsEntry): void;
|
|
180
|
+
/** Removes a ref target, as {@link removeItem} removes a component, and returns it. */
|
|
181
|
+
removeRefTarget(pointer: string): ComponentsEntry | undefined;
|
|
176
182
|
/**
|
|
177
183
|
* Removes an item from the document based on a stack trail path.
|
|
178
184
|
*
|
|
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _OasDocument_fields;
|
|
12
|
+
var _OasDocument_fields, _OasDocument_refTargets;
|
|
13
13
|
import "../../_dnt.polyfills.js";
|
|
14
14
|
/**
|
|
15
15
|
* Represents a complete OpenAPI v3 document in the SKMTC OAS processing system.
|
|
@@ -139,8 +139,50 @@ export class OasDocument {
|
|
|
139
139
|
});
|
|
140
140
|
/** @internal Private fields storage */
|
|
141
141
|
_OasDocument_fields.set(this, void 0);
|
|
142
|
+
/**
|
|
143
|
+
* The targets of refs that do not point at a component, such as
|
|
144
|
+
* `#/components/schemas/Pet/properties/tag` or
|
|
145
|
+
* `#/paths/~1pets/get/responses/200/content/application~1json/schema`.
|
|
146
|
+
* Each is parsed once, at its own position, and kept under its whole
|
|
147
|
+
* pointer (`toRefPointer`). The model sweep does not visit them: a
|
|
148
|
+
* generator reaches one through a ref.
|
|
149
|
+
*/
|
|
150
|
+
_OasDocument_refTargets.set(this, new Map()
|
|
151
|
+
/**
|
|
152
|
+
* Creates a new OasDocument instance.
|
|
153
|
+
*
|
|
154
|
+
* The document is typically created with undefined fields and populated
|
|
155
|
+
* later during the parsing process. This allows for lazy initialization
|
|
156
|
+
* and proper error handling during document processing.
|
|
157
|
+
*
|
|
158
|
+
* @param fields - Optional document fields (usually set later during parsing)
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```typescript
|
|
162
|
+
* // Usually created without fields during parsing
|
|
163
|
+
* const document = new OasDocument();
|
|
164
|
+
*
|
|
165
|
+
* // Fields are set later by the parser
|
|
166
|
+
* document.fields = parsedDocumentFields;
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
);
|
|
142
170
|
__classPrivateFieldSet(this, _OasDocument_fields, fields, "f");
|
|
143
171
|
}
|
|
172
|
+
/** The parsed target of a ref that does not point at a component, by its whole pointer. */
|
|
173
|
+
toRefTarget(pointer) {
|
|
174
|
+
return __classPrivateFieldGet(this, _OasDocument_refTargets, "f").get(pointer);
|
|
175
|
+
}
|
|
176
|
+
/** Keeps the parsed target of a ref that does not point at a component, under its whole pointer. */
|
|
177
|
+
setRefTarget(pointer, target) {
|
|
178
|
+
__classPrivateFieldGet(this, _OasDocument_refTargets, "f").set(pointer, target);
|
|
179
|
+
}
|
|
180
|
+
/** Removes a ref target, as {@link removeItem} removes a component, and returns it. */
|
|
181
|
+
removeRefTarget(pointer) {
|
|
182
|
+
const target = __classPrivateFieldGet(this, _OasDocument_refTargets, "f").get(pointer);
|
|
183
|
+
__classPrivateFieldGet(this, _OasDocument_refTargets, "f").delete(pointer);
|
|
184
|
+
return target;
|
|
185
|
+
}
|
|
144
186
|
/**
|
|
145
187
|
* Removes an item from the document based on a stack trail path.
|
|
146
188
|
*
|
|
@@ -332,4 +374,4 @@ export class OasDocument {
|
|
|
332
374
|
};
|
|
333
375
|
}
|
|
334
376
|
}
|
|
335
|
-
_OasDocument_fields = new WeakMap();
|
|
377
|
+
_OasDocument_fields = new WeakMap(), _OasDocument_refTargets = new WeakMap();
|
package/esm/oas/header/Header.js
CHANGED
|
@@ -125,7 +125,10 @@ export class OasHeader {
|
|
|
125
125
|
if (this.schema) {
|
|
126
126
|
return this.schema;
|
|
127
127
|
}
|
|
128
|
-
|
|
128
|
+
if (this.content === undefined) {
|
|
129
|
+
throw new Error('Header has neither schema nor content');
|
|
130
|
+
}
|
|
131
|
+
const contentEntries = Object.values(this.content);
|
|
129
132
|
const onlyEntry = contentEntries.length === 1 ? contentEntries[0] : undefined;
|
|
130
133
|
const schema = toContentSchema(this.content, mediaType) ?? onlyEntry?.schema;
|
|
131
134
|
if (!schema) {
|
|
@@ -249,7 +249,7 @@ export declare class OasParameter extends OasBase {
|
|
|
249
249
|
*
|
|
250
250
|
* @param mediaType - Media type to extract schema from when using content definitions, an exact lookup. Omitted: the JSON media type (`application/json`, else the first JSON-like type, else `*\/*`).
|
|
251
251
|
* @returns The parameter's schema object
|
|
252
|
-
* @throws Error if no schema is found for the specified media type
|
|
252
|
+
* @throws Error if the parameter has neither schema nor content, or no schema is found for the specified media type
|
|
253
253
|
*/
|
|
254
254
|
toSchema(mediaType?: string): OasSchema | OasRef<'schema'>;
|
|
255
255
|
/**
|
|
@@ -306,13 +306,16 @@ export class OasParameter extends OasBase {
|
|
|
306
306
|
*
|
|
307
307
|
* @param mediaType - Media type to extract schema from when using content definitions, an exact lookup. Omitted: the JSON media type (`application/json`, else the first JSON-like type, else `*\/*`).
|
|
308
308
|
* @returns The parameter's schema object
|
|
309
|
-
* @throws Error if no schema is found for the specified media type
|
|
309
|
+
* @throws Error if the parameter has neither schema nor content, or no schema is found for the specified media type
|
|
310
310
|
*/
|
|
311
311
|
toSchema(mediaType) {
|
|
312
312
|
if (this.schema) {
|
|
313
313
|
return this.schema;
|
|
314
314
|
}
|
|
315
|
-
|
|
315
|
+
if (this.content === undefined) {
|
|
316
|
+
throw new Error(`Parameter '${this.name}' in ${this.location} has neither schema nor content`);
|
|
317
|
+
}
|
|
318
|
+
const contentEntries = Object.values(this.content);
|
|
316
319
|
const onlyEntry = contentEntries.length === 1 ? contentEntries[0] : undefined;
|
|
317
320
|
const schema = toContentSchema(this.content, mediaType) ?? onlyEntry?.schema;
|
|
318
321
|
if (!schema) {
|
|
@@ -7,10 +7,13 @@ type DanglingRefErrorOptions = {
|
|
|
7
7
|
* `['#/components/schemas/B', '#/components/schemas/A']`.
|
|
8
8
|
*/
|
|
9
9
|
loop?: string[];
|
|
10
|
+
/** The `$ref` points outside the document, which core does not fetch. */
|
|
11
|
+
remote?: boolean;
|
|
10
12
|
};
|
|
11
13
|
/**
|
|
12
14
|
* Thrown when a reference that names nothing in the document is resolved:
|
|
13
|
-
* no component has its name,
|
|
15
|
+
* no component has its name, it points outside the document (core does not
|
|
16
|
+
* fetch), or its chain of `$ref`s comes back to one it
|
|
14
17
|
* has passed (`A` is a `$ref` to `B`, and `B` to `A`), so it never reaches a
|
|
15
18
|
* component.
|
|
16
19
|
*
|
|
@@ -25,7 +28,7 @@ export declare class DanglingRefError extends Error {
|
|
|
25
28
|
ref: string;
|
|
26
29
|
/** When the chain of `$ref`s loops: see {@link DanglingRefErrorOptions.loop}. */
|
|
27
30
|
loop: string[] | undefined;
|
|
28
|
-
constructor(ref: string, { loop }?: DanglingRefErrorOptions);
|
|
31
|
+
constructor(ref: string, { loop, remote }?: DanglingRefErrorOptions);
|
|
29
32
|
/**
|
|
30
33
|
* True when `error` is a `DanglingRefError`. It reads the error's name,
|
|
31
34
|
* not its class, so it is also true for one thrown by another copy of
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import "../../_dnt.polyfills.js";
|
|
2
2
|
/**
|
|
3
3
|
* Thrown when a reference that names nothing in the document is resolved:
|
|
4
|
-
* no component has its name,
|
|
4
|
+
* no component has its name, it points outside the document (core does not
|
|
5
|
+
* fetch), or its chain of `$ref`s comes back to one it
|
|
5
6
|
* has passed (`A` is a `$ref` to `B`, and `B` to `A`), so it never reaches a
|
|
6
7
|
* component.
|
|
7
8
|
*
|
|
@@ -12,9 +13,9 @@ import "../../_dnt.polyfills.js";
|
|
|
12
13
|
* {@link DanglingRefError.is}, not `instanceof`.
|
|
13
14
|
*/
|
|
14
15
|
export class DanglingRefError extends Error {
|
|
15
|
-
constructor(ref, { loop } = {}) {
|
|
16
|
+
constructor(ref, { loop, remote = false } = {}) {
|
|
16
17
|
super(loop === undefined
|
|
17
|
-
? `Ref "${ref}" names nothing in the document`
|
|
18
|
+
? `Ref "${ref}" ${remote ? 'points outside the document; bundle the files into one document first' : 'names nothing in the document'}`
|
|
18
19
|
: `Ref "${ref}" leads into a loop of $refs (${[...loop, ...loop.slice(0, 1)].join(' → ')}) and never reaches a component`);
|
|
19
20
|
/** The `$ref`, or the component name, that names nothing. */
|
|
20
21
|
Object.defineProperty(this, "ref", {
|
package/esm/oas/ref/Ref.d.ts
CHANGED
|
@@ -92,8 +92,8 @@ export type RefFields<T extends OasRefData['refType']> = SiblingKeywords & {
|
|
|
92
92
|
* function processSchemaOrRef(schema: OasSchema | OasRef<'schema'>) {
|
|
93
93
|
* if (schema.isRef()) {
|
|
94
94
|
* // Handle reference
|
|
95
|
-
* const refName = schema
|
|
96
|
-
* console.log(`Processing reference: ${refName}`);
|
|
95
|
+
* const { refName, refPath } = schema;
|
|
96
|
+
* console.log(`Processing reference: ${refName} in ${refPath}`);
|
|
97
97
|
*
|
|
98
98
|
* // Resolve only when needed
|
|
99
99
|
* const resolved = schema.resolve();
|
|
@@ -128,8 +128,25 @@ export declare class OasRef<T extends OasRefData['refType']> extends OasBase {
|
|
|
128
128
|
type: 'ref';
|
|
129
129
|
/** The kind of component the reference points to. */
|
|
130
130
|
refType: T;
|
|
131
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* The JSON Reference, as written: a component (`#/components/<bucket>/<name>`)
|
|
133
|
+
* or any other place in the document (`#/components/schemas/Pet/properties/tag`).
|
|
134
|
+
*/
|
|
132
135
|
$ref: string;
|
|
136
|
+
/**
|
|
137
|
+
* The last segment of the `$ref`'s pointer, unescaped: `Pet` for
|
|
138
|
+
* `#/components/schemas/Pet`, `schema0` for
|
|
139
|
+
* `#/components/schemas/__sharedInput/$defs/schema0`. Empty for a `$ref`
|
|
140
|
+
* that ends in `/`, which names nothing.
|
|
141
|
+
*/
|
|
142
|
+
refName: RefName;
|
|
143
|
+
/**
|
|
144
|
+
* The pointer before {@link refName}, with no percent escapes: `#/components/schemas`
|
|
145
|
+
* for a schema component, `#/components/schemas/__sharedInput/$defs` for
|
|
146
|
+
* the `$defs` entry above. A generator that names by {@link refName} alone
|
|
147
|
+
* may ignore it.
|
|
148
|
+
*/
|
|
149
|
+
refPath: string;
|
|
133
150
|
/**
|
|
134
151
|
* Use-site nullability (see {@link SiblingKeywords.nullable}). Assigned
|
|
135
152
|
* even when `undefined`, so `'nullable' in ref` is always true and the
|
|
@@ -241,8 +258,9 @@ export declare class OasRef<T extends OasRefData['refType']> extends OasBase {
|
|
|
241
258
|
toLoop(): OasRef<T>[] | undefined;
|
|
242
259
|
/**
|
|
243
260
|
* Resolves this reference one level. Dispatches on the document's
|
|
244
|
-
* protocol — OAS reads from `document.components.<bucket
|
|
245
|
-
*
|
|
261
|
+
* protocol — OAS reads a component from `document.components.<bucket>`,
|
|
262
|
+
* and any other target from the targets the parser kept by pointer
|
|
263
|
+
* (`OasDocument.toRefTarget`); GQL reads from `document.registry.schemas`.
|
|
246
264
|
*
|
|
247
265
|
* @returns Either the resolved component or another reference in the chain
|
|
248
266
|
* @throws {DanglingRefError} when the reference names nothing in the
|
|
@@ -252,13 +270,6 @@ export declare class OasRef<T extends OasRefData['refType']> extends OasBase {
|
|
|
252
270
|
* `requestBody` ref to `#/components/schemas/…`
|
|
253
271
|
*/
|
|
254
272
|
resolveOnce(): OasRef<T> | ResolvedRef<T>;
|
|
255
|
-
/**
|
|
256
|
-
* The component name the reference looks up.
|
|
257
|
-
*
|
|
258
|
-
* @throws {DanglingRefError} when the `$ref` has no name, such as
|
|
259
|
-
* `#/components/schemas/`: it names nothing
|
|
260
|
-
*/
|
|
261
|
-
toRefName(): RefName;
|
|
262
273
|
/**
|
|
263
274
|
* Narrows this reference to a schema reference.
|
|
264
275
|
*
|