@skmtc/core 0.53.0 → 0.54.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/context/ParseContext.d.ts +7 -0
- package/esm/context/ParseContext.js +16 -16
- package/esm/parse/Reach.d.ts +56 -0
- package/esm/parse/Reach.js +131 -0
- package/esm/parse/toCyclicRefs.d.ts +3 -0
- package/esm/parse/toCyclicRefs.js +1 -1
- package/esm/parse/toRefKey.d.ts +11 -0
- package/esm/parse/toRefKey.js +16 -0
- package/package.json +1 -1
- package/script/context/ParseContext.d.ts +7 -0
- package/script/context/ParseContext.js +18 -18
- package/script/parse/Reach.d.ts +56 -0
- package/script/parse/Reach.js +135 -0
- package/script/parse/toCyclicRefs.d.ts +3 -0
- package/script/parse/toCyclicRefs.js +3 -2
- package/script/parse/toRefKey.d.ts +11 -0
- package/script/parse/toRefKey.js +20 -0
|
@@ -32,6 +32,7 @@ import type { ParseIssue } from './ParseIssue.js';
|
|
|
32
32
|
import type { OasRef } from '../oas/ref/Ref.js';
|
|
33
33
|
import type { OasRefData } from '../oas/ref/ref-types.js';
|
|
34
34
|
import { DanglingRefError } from '../oas/ref/DanglingRefError.js';
|
|
35
|
+
import { Reach } from '../parse/Reach.js';
|
|
35
36
|
export type { ParseIssue, GqlIssueType } from './ParseIssue.js';
|
|
36
37
|
export type { GqlParseOptions, LogAtArgs, LogIssueArgs, LogIssueAtArgs, LogIssueNoKeyArgs, LogSkippedValuesArgs, ParseContextType, ParseErrorInput, ParseIssueInput, ParseWarningInput } from './parseTypes.js';
|
|
37
38
|
type OasProtocolState = {
|
|
@@ -163,6 +164,12 @@ export declare class ParseContext {
|
|
|
163
164
|
* ```
|
|
164
165
|
*/
|
|
165
166
|
withStackTrail<T>(stackTrail: StackTrail, fn: () => T): T;
|
|
167
|
+
/**
|
|
168
|
+
* @internal
|
|
169
|
+
* What each subject of the parsed document can reach through `$ref`s, read
|
|
170
|
+
* from the refs this parse read. Call it when the document is parsed.
|
|
171
|
+
*/
|
|
172
|
+
toReach(): Reach;
|
|
166
173
|
/**
|
|
167
174
|
* Register a `$ref`-style consumer site. The key encoding is up to
|
|
168
175
|
* the caller — OAS uses `#/components/schemas/User` strings, GQL
|
|
@@ -45,25 +45,12 @@ import { toSerializedError } from '../types/SerializedError.js';
|
|
|
45
45
|
import { LogLineIssueArg } from '../types/LogLine.js';
|
|
46
46
|
import { tryParseAt } from './tryParseAt.js';
|
|
47
47
|
import { DanglingRefError } from '../oas/ref/DanglingRefError.js';
|
|
48
|
-
import { toRawRefTarget, toRefParts, toRefPathBucket, toRefPathSegments
|
|
48
|
+
import { toRawRefTarget, toRefParts, toRefPathBucket, toRefPathSegments } from '../helpers/refFns.js';
|
|
49
|
+
import { Reach } from '../parse/Reach.js';
|
|
50
|
+
import { toRefKey } from '../parse/toRefKey.js';
|
|
49
51
|
import { toRefTargetV3 } from '../parse/v3-0/ref/toRefTargetV3.js';
|
|
50
52
|
import { toRefTargetV3 as toRefTargetV31 } from '../parse/v3-1/ref/toRefTargetV3.js';
|
|
51
53
|
import { toRefTargetV3 as toRefTargetV32 } from '../parse/v3-2/ref/toRefTargetV3.js';
|
|
52
|
-
/**
|
|
53
|
-
* The key a `$ref`'s consumers and errors are kept under. A component ref
|
|
54
|
-
* (`#/components/<bucket>/<name>`) is keyed by the name it resolves,
|
|
55
|
-
* unescaped, which is the key `StackTrail.toStackRef()` gives that
|
|
56
|
-
* component's own errors: `#/components/schemas/Foo%20Bar` is kept under
|
|
57
|
-
* `#/components/schemas/Foo Bar`. Any other ref is keyed by its whole
|
|
58
|
-
* pointer in one spelling (`toRefPointer`), which is also the key its
|
|
59
|
-
* parsed target is kept under.
|
|
60
|
-
*/
|
|
61
|
-
const toRefKey = ({ refName, refPath }) => {
|
|
62
|
-
const bucket = toRefPathBucket(refPath);
|
|
63
|
-
return bucket === undefined
|
|
64
|
-
? toRefPointer({ refName, refPath })
|
|
65
|
-
: `#/components/${bucket}/${refName}`;
|
|
66
|
-
};
|
|
67
54
|
/** The issue logged when a ref target's parser throws. */
|
|
68
55
|
const toInvalidType = (refType) => {
|
|
69
56
|
switch (refType) {
|
|
@@ -432,6 +419,19 @@ export class ParseContext {
|
|
|
432
419
|
this.currentStackTrail = prev;
|
|
433
420
|
}
|
|
434
421
|
}
|
|
422
|
+
/**
|
|
423
|
+
* @internal
|
|
424
|
+
* What each subject of the parsed document can reach through `$ref`s, read
|
|
425
|
+
* from the refs this parse read. Call it when the document is parsed.
|
|
426
|
+
*/
|
|
427
|
+
toReach() {
|
|
428
|
+
const rootDepth = __classPrivateFieldGet(this, _ParseContext_rootFrames, "f").length;
|
|
429
|
+
return new Reach(__classPrivateFieldGet(this, _ParseContext_oasRefs, "f").map(({ consumer, ref, refTarget }) => ({
|
|
430
|
+
ref,
|
|
431
|
+
place: consumer.stackTrail.slice(rootDepth),
|
|
432
|
+
refTarget
|
|
433
|
+
})));
|
|
434
|
+
}
|
|
435
435
|
/**
|
|
436
436
|
* Register a `$ref`-style consumer site. The key encoding is up to
|
|
437
437
|
* the caller — OAS uses `#/components/schemas/User` strings, GQL
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type RefParts } from '../helpers/refFns.js';
|
|
2
|
+
import type { OasRefData } from '../oas/ref/ref-types.js';
|
|
3
|
+
/** A ref the parser read, and where it is. */
|
|
4
|
+
export type ReadRef = {
|
|
5
|
+
/** The ref, with the kind of object the parser read it as: only a `schema` ref can name a model. */
|
|
6
|
+
ref: RefParts & {
|
|
7
|
+
refType: OasRefData['refType'];
|
|
8
|
+
};
|
|
9
|
+
/** The frames of the ref's place in the document: `['paths', '/pets', 'get', …]`. */
|
|
10
|
+
place: readonly string[];
|
|
11
|
+
/** The key of the ref target the parser read it for; `undefined` in the document's own walk. */
|
|
12
|
+
refTarget: string | undefined;
|
|
13
|
+
};
|
|
14
|
+
/** Where an operation is in a document. An `OasOperation` fits it. */
|
|
15
|
+
type OperationPlace = {
|
|
16
|
+
path: string;
|
|
17
|
+
method: string;
|
|
18
|
+
};
|
|
19
|
+
/** Where a webhook is in a document. An `OasWebhook` fits it. */
|
|
20
|
+
type WebhookPlace = {
|
|
21
|
+
name: string;
|
|
22
|
+
method: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* What a subject of a document can reach through `$ref`s: the models a
|
|
26
|
+
* generator can get to from it, each by its model key (`toModelKey`).
|
|
27
|
+
*
|
|
28
|
+
* It is read from the refs the parser itself read, each with its place
|
|
29
|
+
* (`ParseContext.toReach`). So it has what a generator can follow in the
|
|
30
|
+
* parsed document, and no more: a `$ref` the parser took for data is not in
|
|
31
|
+
* it, and neither is the parent of an `allOf` that the parser merged into
|
|
32
|
+
* its child. It needs no walk of the document of its own.
|
|
33
|
+
*
|
|
34
|
+
* A place is a model, an operation, the parameters of a path item, or a
|
|
35
|
+
* component that is no schema. The models a place names are those its own
|
|
36
|
+
* refs give, and those behind each such component it refers to. A model
|
|
37
|
+
* reaches the models it names, and the models those name.
|
|
38
|
+
*/
|
|
39
|
+
export declare class Reach {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(refs: Iterable<ReadRef>);
|
|
42
|
+
/** The keys of the models a model reaches, its own included. */
|
|
43
|
+
ofModel(ref: RefParts): readonly string[];
|
|
44
|
+
/**
|
|
45
|
+
* The keys of the models an operation names: `paths[path][method]`, with
|
|
46
|
+
* what every operation of the path has, which is the path item's
|
|
47
|
+
* `parameters`, and any ref kept at the path item itself. The parser does
|
|
48
|
+
* not read a path item's own `$ref` yet, so that last place is empty. A
|
|
49
|
+
* path item parameter the operation overrides is still named: one model too
|
|
50
|
+
* many is safe for a plan, one too few is not.
|
|
51
|
+
*/
|
|
52
|
+
namedByOperation({ path, method }: OperationPlace): readonly string[];
|
|
53
|
+
/** The keys of the models a webhook names: `webhooks[name][method]`, as {@link namedByOperation}. */
|
|
54
|
+
namedByWebhook({ name, method }: WebhookPlace): readonly string[];
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
+
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");
|
|
4
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
+
};
|
|
6
|
+
var _Reach_instances, _Reach_named, _Reach_reached, _Reach_namedByModel, _Reach_toNamedByModel, _Reach_namedAt, _Reach_toNamed;
|
|
7
|
+
import { fromModelKey, toModelKey } from '../helpers/refFns.js';
|
|
8
|
+
import { toRefKey } from './toRefKey.js';
|
|
9
|
+
import { canNameSchema } from './toCyclicRefs.js';
|
|
10
|
+
/**
|
|
11
|
+
* What a subject of a document can reach through `$ref`s: the models a
|
|
12
|
+
* generator can get to from it, each by its model key (`toModelKey`).
|
|
13
|
+
*
|
|
14
|
+
* It is read from the refs the parser itself read, each with its place
|
|
15
|
+
* (`ParseContext.toReach`). So it has what a generator can follow in the
|
|
16
|
+
* parsed document, and no more: a `$ref` the parser took for data is not in
|
|
17
|
+
* it, and neither is the parent of an `allOf` that the parser merged into
|
|
18
|
+
* its child. It needs no walk of the document of its own.
|
|
19
|
+
*
|
|
20
|
+
* A place is a model, an operation, the parameters of a path item, or a
|
|
21
|
+
* component that is no schema. The models a place names are those its own
|
|
22
|
+
* refs give, and those behind each such component it refers to. A model
|
|
23
|
+
* reaches the models it names, and the models those name.
|
|
24
|
+
*/
|
|
25
|
+
export class Reach {
|
|
26
|
+
constructor(refs) {
|
|
27
|
+
_Reach_instances.add(this);
|
|
28
|
+
_Reach_named.set(this, new Map()
|
|
29
|
+
/** By a model's key: the keys of the models it reaches, its own first, read one time. */
|
|
30
|
+
);
|
|
31
|
+
/** By a model's key: the keys of the models it reaches, its own first, read one time. */
|
|
32
|
+
_Reach_reached.set(this, new Map()
|
|
33
|
+
/** By a model's key: the keys of the models it names, read one time. */
|
|
34
|
+
);
|
|
35
|
+
/** By a model's key: the keys of the models it names, read one time. */
|
|
36
|
+
_Reach_namedByModel.set(this, new Map());
|
|
37
|
+
for (const { ref, place, refTarget } of refs) {
|
|
38
|
+
const named = __classPrivateFieldGet(this, _Reach_instances, "m", _Reach_toNamed).call(this, refTarget ?? toPlaceKey(place));
|
|
39
|
+
// A pointer into `paths` passes `canNameSchema` whatever it points at,
|
|
40
|
+
// so a parameter or a response ref is told apart by its `refType`.
|
|
41
|
+
if (ref.refType === 'schema' && canNameSchema(ref)) {
|
|
42
|
+
named.models.add(toModelKey(ref));
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
named.components.add(toRefKey(ref));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The keys of the models a model reaches, its own included. */
|
|
50
|
+
ofModel(ref) {
|
|
51
|
+
const key = toModelKey(ref);
|
|
52
|
+
const found = __classPrivateFieldGet(this, _Reach_reached, "f").get(key);
|
|
53
|
+
if (found !== undefined) {
|
|
54
|
+
return found;
|
|
55
|
+
}
|
|
56
|
+
const seen = new Set([key]);
|
|
57
|
+
const reached = [key];
|
|
58
|
+
// `reached` grows while it is walked: each model may name more models.
|
|
59
|
+
for (const next of reached) {
|
|
60
|
+
__classPrivateFieldGet(this, _Reach_instances, "m", _Reach_toNamedByModel).call(this, next).forEach(model => {
|
|
61
|
+
if (!seen.has(model)) {
|
|
62
|
+
seen.add(model);
|
|
63
|
+
reached.push(model);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
__classPrivateFieldGet(this, _Reach_reached, "f").set(key, reached);
|
|
68
|
+
return reached;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The keys of the models an operation names: `paths[path][method]`, with
|
|
72
|
+
* what every operation of the path has, which is the path item's
|
|
73
|
+
* `parameters`, and any ref kept at the path item itself. The parser does
|
|
74
|
+
* not read a path item's own `$ref` yet, so that last place is empty. A
|
|
75
|
+
* path item parameter the operation overrides is still named: one model too
|
|
76
|
+
* many is safe for a plan, one too few is not.
|
|
77
|
+
*/
|
|
78
|
+
namedByOperation({ path, method }) {
|
|
79
|
+
return __classPrivateFieldGet(this, _Reach_instances, "m", _Reach_namedAt).call(this, [
|
|
80
|
+
toPlaceKey(['paths', path, method]),
|
|
81
|
+
toPlaceKey(['paths', path, 'parameters']),
|
|
82
|
+
toPlaceKey(['paths', path])
|
|
83
|
+
]);
|
|
84
|
+
}
|
|
85
|
+
/** The keys of the models a webhook names: `webhooks[name][method]`, as {@link namedByOperation}. */
|
|
86
|
+
namedByWebhook({ name, method }) {
|
|
87
|
+
return __classPrivateFieldGet(this, _Reach_instances, "m", _Reach_namedAt).call(this, [
|
|
88
|
+
toPlaceKey(['webhooks', name, method]),
|
|
89
|
+
toPlaceKey(['webhooks', name, 'parameters']),
|
|
90
|
+
toPlaceKey(['webhooks', name])
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
_Reach_named = new WeakMap(), _Reach_reached = new WeakMap(), _Reach_namedByModel = new WeakMap(), _Reach_instances = new WeakSet(), _Reach_toNamedByModel = function _Reach_toNamedByModel(key) {
|
|
95
|
+
const found = __classPrivateFieldGet(this, _Reach_namedByModel, "f").get(key);
|
|
96
|
+
if (found !== undefined) {
|
|
97
|
+
return found;
|
|
98
|
+
}
|
|
99
|
+
const named = __classPrivateFieldGet(this, _Reach_instances, "m", _Reach_namedAt).call(this, [toRefKey(fromModelKey(key))]);
|
|
100
|
+
__classPrivateFieldGet(this, _Reach_namedByModel, "f").set(key, named);
|
|
101
|
+
return named;
|
|
102
|
+
}, _Reach_namedAt = function _Reach_namedAt(places) {
|
|
103
|
+
const seen = new Set(places);
|
|
104
|
+
const models = new Set();
|
|
105
|
+
// `places` grows while it is walked: each component may refer to more components.
|
|
106
|
+
for (const place of places) {
|
|
107
|
+
const named = __classPrivateFieldGet(this, _Reach_named, "f").get(place);
|
|
108
|
+
named?.models.forEach(model => models.add(model));
|
|
109
|
+
named?.components.forEach(component => {
|
|
110
|
+
if (!seen.has(component)) {
|
|
111
|
+
seen.add(component);
|
|
112
|
+
places.push(component);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return [...models];
|
|
117
|
+
}, _Reach_toNamed = function _Reach_toNamed(place) {
|
|
118
|
+
const found = __classPrivateFieldGet(this, _Reach_named, "f").get(place);
|
|
119
|
+
if (found !== undefined) {
|
|
120
|
+
return found;
|
|
121
|
+
}
|
|
122
|
+
const named = { models: new Set(), components: new Set() };
|
|
123
|
+
__classPrivateFieldGet(this, _Reach_named, "f").set(place, named);
|
|
124
|
+
return named;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* The key of the place a ref is in. A component is keyed as a ref to it is
|
|
128
|
+
* (`toRefKey`), so a ref leads to the refs inside its target. An operation
|
|
129
|
+
* is keyed by its first three frames, and a ref at a path item itself by two.
|
|
130
|
+
*/
|
|
131
|
+
const toPlaceKey = (place) => place[0] === 'components' ? `#/components/${place[1]}/${place[2]}` : place.slice(0, 3).join('\n');
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { type RefParts } from '../helpers/refFns.js';
|
|
2
|
+
/** Whether a ref can point at a schema: not a remote one, and not one to another kind of component. */
|
|
3
|
+
export declare const canNameSchema: ({ refName, refPath }: RefParts) => boolean;
|
|
1
4
|
/**
|
|
2
5
|
* The `$ref` objects of a raw document that are in a cycle: the ref's target
|
|
3
6
|
* leads back, through any refs, to a schema that holds the ref. A ref inside a
|
|
@@ -45,7 +45,7 @@ const toDocumentRefs = (value, found = []) => {
|
|
|
45
45
|
return found;
|
|
46
46
|
};
|
|
47
47
|
/** Whether a ref can point at a schema: not a remote one, and not one to another kind of component. */
|
|
48
|
-
const canNameSchema = ({ refName, refPath }) => {
|
|
48
|
+
export const canNameSchema = ({ refName, refPath }) => {
|
|
49
49
|
const bucket = toRefPathBucket(refPath);
|
|
50
50
|
return refName !== '' && !isRemoteRefPath(refPath) && (bucket === undefined || bucket === 'schemas');
|
|
51
51
|
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type RefParts } from '../helpers/refFns.js';
|
|
2
|
+
/**
|
|
3
|
+
* The key a `$ref`'s consumers and errors are kept under. A component ref
|
|
4
|
+
* (`#/components/<bucket>/<name>`) is keyed by the name it resolves,
|
|
5
|
+
* unescaped, which is the key `StackTrail.toStackRef()` gives that
|
|
6
|
+
* component's own errors: `#/components/schemas/Foo%20Bar` is kept under
|
|
7
|
+
* `#/components/schemas/Foo Bar`. Any other ref is keyed by its whole
|
|
8
|
+
* pointer in one spelling (`toRefPointer`), which is also the key its
|
|
9
|
+
* parsed target is kept under.
|
|
10
|
+
*/
|
|
11
|
+
export declare const toRefKey: ({ refName, refPath }: RefParts) => string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { toRefPathBucket, toRefPointer } from '../helpers/refFns.js';
|
|
2
|
+
/**
|
|
3
|
+
* The key a `$ref`'s consumers and errors are kept under. A component ref
|
|
4
|
+
* (`#/components/<bucket>/<name>`) is keyed by the name it resolves,
|
|
5
|
+
* unescaped, which is the key `StackTrail.toStackRef()` gives that
|
|
6
|
+
* component's own errors: `#/components/schemas/Foo%20Bar` is kept under
|
|
7
|
+
* `#/components/schemas/Foo Bar`. Any other ref is keyed by its whole
|
|
8
|
+
* pointer in one spelling (`toRefPointer`), which is also the key its
|
|
9
|
+
* parsed target is kept under.
|
|
10
|
+
*/
|
|
11
|
+
export const toRefKey = ({ refName, refPath }) => {
|
|
12
|
+
const bucket = toRefPathBucket(refPath);
|
|
13
|
+
return bucket === undefined
|
|
14
|
+
? toRefPointer({ refName, refPath })
|
|
15
|
+
: `#/components/${bucket}/${refName}`;
|
|
16
|
+
};
|
package/package.json
CHANGED
|
@@ -32,6 +32,7 @@ import type { ParseIssue } from './ParseIssue.js';
|
|
|
32
32
|
import type { OasRef } from '../oas/ref/Ref.js';
|
|
33
33
|
import type { OasRefData } from '../oas/ref/ref-types.js';
|
|
34
34
|
import { DanglingRefError } from '../oas/ref/DanglingRefError.js';
|
|
35
|
+
import { Reach } from '../parse/Reach.js';
|
|
35
36
|
export type { ParseIssue, GqlIssueType } from './ParseIssue.js';
|
|
36
37
|
export type { GqlParseOptions, LogAtArgs, LogIssueArgs, LogIssueAtArgs, LogIssueNoKeyArgs, LogSkippedValuesArgs, ParseContextType, ParseErrorInput, ParseIssueInput, ParseWarningInput } from './parseTypes.js';
|
|
37
38
|
type OasProtocolState = {
|
|
@@ -163,6 +164,12 @@ export declare class ParseContext {
|
|
|
163
164
|
* ```
|
|
164
165
|
*/
|
|
165
166
|
withStackTrail<T>(stackTrail: StackTrail, fn: () => T): T;
|
|
167
|
+
/**
|
|
168
|
+
* @internal
|
|
169
|
+
* What each subject of the parsed document can reach through `$ref`s, read
|
|
170
|
+
* from the refs this parse read. Call it when the document is parsed.
|
|
171
|
+
*/
|
|
172
|
+
toReach(): Reach;
|
|
166
173
|
/**
|
|
167
174
|
* Register a `$ref`-style consumer site. The key encoding is up to
|
|
168
175
|
* the caller — OAS uses `#/components/schemas/User` strings, GQL
|
|
@@ -49,24 +49,11 @@ const LogLine_js_1 = require("../types/LogLine.js");
|
|
|
49
49
|
const tryParseAt_js_1 = require("./tryParseAt.js");
|
|
50
50
|
const DanglingRefError_js_1 = require("../oas/ref/DanglingRefError.js");
|
|
51
51
|
const refFns_js_1 = require("../helpers/refFns.js");
|
|
52
|
+
const Reach_js_1 = require("../parse/Reach.js");
|
|
53
|
+
const toRefKey_js_1 = require("../parse/toRefKey.js");
|
|
52
54
|
const toRefTargetV3_js_1 = require("../parse/v3-0/ref/toRefTargetV3.js");
|
|
53
55
|
const toRefTargetV3_js_2 = require("../parse/v3-1/ref/toRefTargetV3.js");
|
|
54
56
|
const toRefTargetV3_js_3 = require("../parse/v3-2/ref/toRefTargetV3.js");
|
|
55
|
-
/**
|
|
56
|
-
* The key a `$ref`'s consumers and errors are kept under. A component ref
|
|
57
|
-
* (`#/components/<bucket>/<name>`) is keyed by the name it resolves,
|
|
58
|
-
* unescaped, which is the key `StackTrail.toStackRef()` gives that
|
|
59
|
-
* component's own errors: `#/components/schemas/Foo%20Bar` is kept under
|
|
60
|
-
* `#/components/schemas/Foo Bar`. Any other ref is keyed by its whole
|
|
61
|
-
* pointer in one spelling (`toRefPointer`), which is also the key its
|
|
62
|
-
* parsed target is kept under.
|
|
63
|
-
*/
|
|
64
|
-
const toRefKey = ({ refName, refPath }) => {
|
|
65
|
-
const bucket = (0, refFns_js_1.toRefPathBucket)(refPath);
|
|
66
|
-
return bucket === undefined
|
|
67
|
-
? (0, refFns_js_1.toRefPointer)({ refName, refPath })
|
|
68
|
-
: `#/components/${bucket}/${refName}`;
|
|
69
|
-
};
|
|
70
57
|
/** The issue logged when a ref target's parser throws. */
|
|
71
58
|
const toInvalidType = (refType) => {
|
|
72
59
|
switch (refType) {
|
|
@@ -435,6 +422,19 @@ class ParseContext {
|
|
|
435
422
|
this.currentStackTrail = prev;
|
|
436
423
|
}
|
|
437
424
|
}
|
|
425
|
+
/**
|
|
426
|
+
* @internal
|
|
427
|
+
* What each subject of the parsed document can reach through `$ref`s, read
|
|
428
|
+
* from the refs this parse read. Call it when the document is parsed.
|
|
429
|
+
*/
|
|
430
|
+
toReach() {
|
|
431
|
+
const rootDepth = __classPrivateFieldGet(this, _ParseContext_rootFrames, "f").length;
|
|
432
|
+
return new Reach_js_1.Reach(__classPrivateFieldGet(this, _ParseContext_oasRefs, "f").map(({ consumer, ref, refTarget }) => ({
|
|
433
|
+
ref,
|
|
434
|
+
place: consumer.stackTrail.slice(rootDepth),
|
|
435
|
+
refTarget
|
|
436
|
+
})));
|
|
437
|
+
}
|
|
438
438
|
/**
|
|
439
439
|
* Register a `$ref`-style consumer site. The key encoding is up to
|
|
440
440
|
* the caller — OAS uses `#/components/schemas/User` strings, GQL
|
|
@@ -457,7 +457,7 @@ class ParseContext {
|
|
|
457
457
|
* resolve it there.
|
|
458
458
|
*/
|
|
459
459
|
registerOasRef(consumer, ref) {
|
|
460
|
-
this.registerRef(consumer, toRefKey(ref));
|
|
460
|
+
this.registerRef(consumer, (0, toRefKey_js_1.toRefKey)(ref));
|
|
461
461
|
__classPrivateFieldGet(this, _ParseContext_oasRefs, "f").push({ consumer, ref, refTarget: __classPrivateFieldGet(this, _ParseContext_refTarget, "f") });
|
|
462
462
|
}
|
|
463
463
|
/**
|
|
@@ -673,7 +673,7 @@ _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMa
|
|
|
673
673
|
}
|
|
674
674
|
const { oasDocument, documentObject } = this.protocol;
|
|
675
675
|
for (const { ref } of __classPrivateFieldGet(this, _ParseContext_oasRefs, "f")) {
|
|
676
|
-
const key = toRefKey(ref);
|
|
676
|
+
const key = (0, toRefKey_js_1.toRefKey)(ref);
|
|
677
677
|
// A ref to a component, of its own kind or not, is resolved in
|
|
678
678
|
// `components`; `#checkRefs` reports one of the wrong kind.
|
|
679
679
|
if (ref.refName === '' ||
|
|
@@ -828,7 +828,7 @@ _ParseContext_refConsumers = new WeakMap(), _ParseContext_refErrors = new WeakMa
|
|
|
828
828
|
return startsWithRoot ? new StackTrail_js_1.StackTrail(frames.slice(rootFrames.length)) : stackTrail;
|
|
829
829
|
}, _ParseContext_checkRefs = function _ParseContext_checkRefs() {
|
|
830
830
|
__classPrivateFieldGet(this, _ParseContext_oasRefs, "f").forEach(({ consumer, ref, refTarget }) => __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_inRefTarget).call(this, refTarget, () => {
|
|
831
|
-
if (__classPrivateFieldGet(this, _ParseContext_refErrors, "f").has(toRefKey(ref))) {
|
|
831
|
+
if (__classPrivateFieldGet(this, _ParseContext_refErrors, "f").has((0, toRefKey_js_1.toRefKey)(ref))) {
|
|
832
832
|
return;
|
|
833
833
|
}
|
|
834
834
|
const error = __classPrivateFieldGet(this, _ParseContext_instances, "m", _ParseContext_toRefError).call(this, ref);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type RefParts } from '../helpers/refFns.js';
|
|
2
|
+
import type { OasRefData } from '../oas/ref/ref-types.js';
|
|
3
|
+
/** A ref the parser read, and where it is. */
|
|
4
|
+
export type ReadRef = {
|
|
5
|
+
/** The ref, with the kind of object the parser read it as: only a `schema` ref can name a model. */
|
|
6
|
+
ref: RefParts & {
|
|
7
|
+
refType: OasRefData['refType'];
|
|
8
|
+
};
|
|
9
|
+
/** The frames of the ref's place in the document: `['paths', '/pets', 'get', …]`. */
|
|
10
|
+
place: readonly string[];
|
|
11
|
+
/** The key of the ref target the parser read it for; `undefined` in the document's own walk. */
|
|
12
|
+
refTarget: string | undefined;
|
|
13
|
+
};
|
|
14
|
+
/** Where an operation is in a document. An `OasOperation` fits it. */
|
|
15
|
+
type OperationPlace = {
|
|
16
|
+
path: string;
|
|
17
|
+
method: string;
|
|
18
|
+
};
|
|
19
|
+
/** Where a webhook is in a document. An `OasWebhook` fits it. */
|
|
20
|
+
type WebhookPlace = {
|
|
21
|
+
name: string;
|
|
22
|
+
method: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* What a subject of a document can reach through `$ref`s: the models a
|
|
26
|
+
* generator can get to from it, each by its model key (`toModelKey`).
|
|
27
|
+
*
|
|
28
|
+
* It is read from the refs the parser itself read, each with its place
|
|
29
|
+
* (`ParseContext.toReach`). So it has what a generator can follow in the
|
|
30
|
+
* parsed document, and no more: a `$ref` the parser took for data is not in
|
|
31
|
+
* it, and neither is the parent of an `allOf` that the parser merged into
|
|
32
|
+
* its child. It needs no walk of the document of its own.
|
|
33
|
+
*
|
|
34
|
+
* A place is a model, an operation, the parameters of a path item, or a
|
|
35
|
+
* component that is no schema. The models a place names are those its own
|
|
36
|
+
* refs give, and those behind each such component it refers to. A model
|
|
37
|
+
* reaches the models it names, and the models those name.
|
|
38
|
+
*/
|
|
39
|
+
export declare class Reach {
|
|
40
|
+
#private;
|
|
41
|
+
constructor(refs: Iterable<ReadRef>);
|
|
42
|
+
/** The keys of the models a model reaches, its own included. */
|
|
43
|
+
ofModel(ref: RefParts): readonly string[];
|
|
44
|
+
/**
|
|
45
|
+
* The keys of the models an operation names: `paths[path][method]`, with
|
|
46
|
+
* what every operation of the path has, which is the path item's
|
|
47
|
+
* `parameters`, and any ref kept at the path item itself. The parser does
|
|
48
|
+
* not read a path item's own `$ref` yet, so that last place is empty. A
|
|
49
|
+
* path item parameter the operation overrides is still named: one model too
|
|
50
|
+
* many is safe for a plan, one too few is not.
|
|
51
|
+
*/
|
|
52
|
+
namedByOperation({ path, method }: OperationPlace): readonly string[];
|
|
53
|
+
/** The keys of the models a webhook names: `webhooks[name][method]`, as {@link namedByOperation}. */
|
|
54
|
+
namedByWebhook({ name, method }: WebhookPlace): readonly string[];
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var _Reach_instances, _Reach_named, _Reach_reached, _Reach_namedByModel, _Reach_toNamedByModel, _Reach_namedAt, _Reach_toNamed;
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.Reach = void 0;
|
|
10
|
+
const refFns_js_1 = require("../helpers/refFns.js");
|
|
11
|
+
const toRefKey_js_1 = require("./toRefKey.js");
|
|
12
|
+
const toCyclicRefs_js_1 = require("./toCyclicRefs.js");
|
|
13
|
+
/**
|
|
14
|
+
* What a subject of a document can reach through `$ref`s: the models a
|
|
15
|
+
* generator can get to from it, each by its model key (`toModelKey`).
|
|
16
|
+
*
|
|
17
|
+
* It is read from the refs the parser itself read, each with its place
|
|
18
|
+
* (`ParseContext.toReach`). So it has what a generator can follow in the
|
|
19
|
+
* parsed document, and no more: a `$ref` the parser took for data is not in
|
|
20
|
+
* it, and neither is the parent of an `allOf` that the parser merged into
|
|
21
|
+
* its child. It needs no walk of the document of its own.
|
|
22
|
+
*
|
|
23
|
+
* A place is a model, an operation, the parameters of a path item, or a
|
|
24
|
+
* component that is no schema. The models a place names are those its own
|
|
25
|
+
* refs give, and those behind each such component it refers to. A model
|
|
26
|
+
* reaches the models it names, and the models those name.
|
|
27
|
+
*/
|
|
28
|
+
class Reach {
|
|
29
|
+
constructor(refs) {
|
|
30
|
+
_Reach_instances.add(this);
|
|
31
|
+
_Reach_named.set(this, new Map()
|
|
32
|
+
/** By a model's key: the keys of the models it reaches, its own first, read one time. */
|
|
33
|
+
);
|
|
34
|
+
/** By a model's key: the keys of the models it reaches, its own first, read one time. */
|
|
35
|
+
_Reach_reached.set(this, new Map()
|
|
36
|
+
/** By a model's key: the keys of the models it names, read one time. */
|
|
37
|
+
);
|
|
38
|
+
/** By a model's key: the keys of the models it names, read one time. */
|
|
39
|
+
_Reach_namedByModel.set(this, new Map());
|
|
40
|
+
for (const { ref, place, refTarget } of refs) {
|
|
41
|
+
const named = __classPrivateFieldGet(this, _Reach_instances, "m", _Reach_toNamed).call(this, refTarget ?? toPlaceKey(place));
|
|
42
|
+
// A pointer into `paths` passes `canNameSchema` whatever it points at,
|
|
43
|
+
// so a parameter or a response ref is told apart by its `refType`.
|
|
44
|
+
if (ref.refType === 'schema' && (0, toCyclicRefs_js_1.canNameSchema)(ref)) {
|
|
45
|
+
named.models.add((0, refFns_js_1.toModelKey)(ref));
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
named.components.add((0, toRefKey_js_1.toRefKey)(ref));
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** The keys of the models a model reaches, its own included. */
|
|
53
|
+
ofModel(ref) {
|
|
54
|
+
const key = (0, refFns_js_1.toModelKey)(ref);
|
|
55
|
+
const found = __classPrivateFieldGet(this, _Reach_reached, "f").get(key);
|
|
56
|
+
if (found !== undefined) {
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
const seen = new Set([key]);
|
|
60
|
+
const reached = [key];
|
|
61
|
+
// `reached` grows while it is walked: each model may name more models.
|
|
62
|
+
for (const next of reached) {
|
|
63
|
+
__classPrivateFieldGet(this, _Reach_instances, "m", _Reach_toNamedByModel).call(this, next).forEach(model => {
|
|
64
|
+
if (!seen.has(model)) {
|
|
65
|
+
seen.add(model);
|
|
66
|
+
reached.push(model);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
__classPrivateFieldGet(this, _Reach_reached, "f").set(key, reached);
|
|
71
|
+
return reached;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The keys of the models an operation names: `paths[path][method]`, with
|
|
75
|
+
* what every operation of the path has, which is the path item's
|
|
76
|
+
* `parameters`, and any ref kept at the path item itself. The parser does
|
|
77
|
+
* not read a path item's own `$ref` yet, so that last place is empty. A
|
|
78
|
+
* path item parameter the operation overrides is still named: one model too
|
|
79
|
+
* many is safe for a plan, one too few is not.
|
|
80
|
+
*/
|
|
81
|
+
namedByOperation({ path, method }) {
|
|
82
|
+
return __classPrivateFieldGet(this, _Reach_instances, "m", _Reach_namedAt).call(this, [
|
|
83
|
+
toPlaceKey(['paths', path, method]),
|
|
84
|
+
toPlaceKey(['paths', path, 'parameters']),
|
|
85
|
+
toPlaceKey(['paths', path])
|
|
86
|
+
]);
|
|
87
|
+
}
|
|
88
|
+
/** The keys of the models a webhook names: `webhooks[name][method]`, as {@link namedByOperation}. */
|
|
89
|
+
namedByWebhook({ name, method }) {
|
|
90
|
+
return __classPrivateFieldGet(this, _Reach_instances, "m", _Reach_namedAt).call(this, [
|
|
91
|
+
toPlaceKey(['webhooks', name, method]),
|
|
92
|
+
toPlaceKey(['webhooks', name, 'parameters']),
|
|
93
|
+
toPlaceKey(['webhooks', name])
|
|
94
|
+
]);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.Reach = Reach;
|
|
98
|
+
_Reach_named = new WeakMap(), _Reach_reached = new WeakMap(), _Reach_namedByModel = new WeakMap(), _Reach_instances = new WeakSet(), _Reach_toNamedByModel = function _Reach_toNamedByModel(key) {
|
|
99
|
+
const found = __classPrivateFieldGet(this, _Reach_namedByModel, "f").get(key);
|
|
100
|
+
if (found !== undefined) {
|
|
101
|
+
return found;
|
|
102
|
+
}
|
|
103
|
+
const named = __classPrivateFieldGet(this, _Reach_instances, "m", _Reach_namedAt).call(this, [(0, toRefKey_js_1.toRefKey)((0, refFns_js_1.fromModelKey)(key))]);
|
|
104
|
+
__classPrivateFieldGet(this, _Reach_namedByModel, "f").set(key, named);
|
|
105
|
+
return named;
|
|
106
|
+
}, _Reach_namedAt = function _Reach_namedAt(places) {
|
|
107
|
+
const seen = new Set(places);
|
|
108
|
+
const models = new Set();
|
|
109
|
+
// `places` grows while it is walked: each component may refer to more components.
|
|
110
|
+
for (const place of places) {
|
|
111
|
+
const named = __classPrivateFieldGet(this, _Reach_named, "f").get(place);
|
|
112
|
+
named?.models.forEach(model => models.add(model));
|
|
113
|
+
named?.components.forEach(component => {
|
|
114
|
+
if (!seen.has(component)) {
|
|
115
|
+
seen.add(component);
|
|
116
|
+
places.push(component);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return [...models];
|
|
121
|
+
}, _Reach_toNamed = function _Reach_toNamed(place) {
|
|
122
|
+
const found = __classPrivateFieldGet(this, _Reach_named, "f").get(place);
|
|
123
|
+
if (found !== undefined) {
|
|
124
|
+
return found;
|
|
125
|
+
}
|
|
126
|
+
const named = { models: new Set(), components: new Set() };
|
|
127
|
+
__classPrivateFieldGet(this, _Reach_named, "f").set(place, named);
|
|
128
|
+
return named;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* The key of the place a ref is in. A component is keyed as a ref to it is
|
|
132
|
+
* (`toRefKey`), so a ref leads to the refs inside its target. An operation
|
|
133
|
+
* is keyed by its first three frames, and a ref at a path item itself by two.
|
|
134
|
+
*/
|
|
135
|
+
const toPlaceKey = (place) => place[0] === 'components' ? `#/components/${place[1]}/${place[2]}` : place.slice(0, 3).join('\n');
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { type RefParts } from '../helpers/refFns.js';
|
|
2
|
+
/** Whether a ref can point at a schema: not a remote one, and not one to another kind of component. */
|
|
3
|
+
export declare const canNameSchema: ({ refName, refPath }: RefParts) => boolean;
|
|
1
4
|
/**
|
|
2
5
|
* The `$ref` objects of a raw document that are in a cycle: the ref's target
|
|
3
6
|
* leads back, through any refs, to a schema that holds the ref. A ref inside a
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toCyclicRefs = void 0;
|
|
3
|
+
exports.toCyclicRefs = exports.canNameSchema = void 0;
|
|
4
4
|
const isPlainObject_js_1 = require("./isPlainObject.js");
|
|
5
5
|
const refFns_js_1 = require("../helpers/refFns.js");
|
|
6
6
|
/** Keywords whose value is data, so a `$ref` inside it is not a reference. */
|
|
@@ -52,6 +52,7 @@ const canNameSchema = ({ refName, refPath }) => {
|
|
|
52
52
|
const bucket = (0, refFns_js_1.toRefPathBucket)(refPath);
|
|
53
53
|
return refName !== '' && !(0, refFns_js_1.isRemoteRefPath)(refPath) && (bucket === undefined || bucket === 'schemas');
|
|
54
54
|
};
|
|
55
|
+
exports.canNameSchema = canNameSchema;
|
|
55
56
|
/** The document's schema ref graph: each schema component and each other ref target, by `toRefPointer`, with the refs inside it. */
|
|
56
57
|
const toRefGraph = (document) => {
|
|
57
58
|
const components = (0, isPlainObject_js_1.isPlainObject)(document) ? document.components : undefined;
|
|
@@ -65,7 +66,7 @@ const toRefGraph = (document) => {
|
|
|
65
66
|
// `pending` grows while it is walked: each target may hold refs to more targets.
|
|
66
67
|
for (const parts of pending) {
|
|
67
68
|
const key = (0, refFns_js_1.toRefPointer)(parts);
|
|
68
|
-
if (graph.has(key) || !canNameSchema(parts)) {
|
|
69
|
+
if (graph.has(key) || !(0, exports.canNameSchema)(parts)) {
|
|
69
70
|
continue;
|
|
70
71
|
}
|
|
71
72
|
const edges = toSchemaRefs((0, refFns_js_1.toRawRefTarget)(document, parts)).map(ref => ({
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type RefParts } from '../helpers/refFns.js';
|
|
2
|
+
/**
|
|
3
|
+
* The key a `$ref`'s consumers and errors are kept under. A component ref
|
|
4
|
+
* (`#/components/<bucket>/<name>`) is keyed by the name it resolves,
|
|
5
|
+
* unescaped, which is the key `StackTrail.toStackRef()` gives that
|
|
6
|
+
* component's own errors: `#/components/schemas/Foo%20Bar` is kept under
|
|
7
|
+
* `#/components/schemas/Foo Bar`. Any other ref is keyed by its whole
|
|
8
|
+
* pointer in one spelling (`toRefPointer`), which is also the key its
|
|
9
|
+
* parsed target is kept under.
|
|
10
|
+
*/
|
|
11
|
+
export declare const toRefKey: ({ refName, refPath }: RefParts) => string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toRefKey = void 0;
|
|
4
|
+
const refFns_js_1 = require("../helpers/refFns.js");
|
|
5
|
+
/**
|
|
6
|
+
* The key a `$ref`'s consumers and errors are kept under. A component ref
|
|
7
|
+
* (`#/components/<bucket>/<name>`) is keyed by the name it resolves,
|
|
8
|
+
* unescaped, which is the key `StackTrail.toStackRef()` gives that
|
|
9
|
+
* component's own errors: `#/components/schemas/Foo%20Bar` is kept under
|
|
10
|
+
* `#/components/schemas/Foo Bar`. Any other ref is keyed by its whole
|
|
11
|
+
* pointer in one spelling (`toRefPointer`), which is also the key its
|
|
12
|
+
* parsed target is kept under.
|
|
13
|
+
*/
|
|
14
|
+
const toRefKey = ({ refName, refPath }) => {
|
|
15
|
+
const bucket = (0, refFns_js_1.toRefPathBucket)(refPath);
|
|
16
|
+
return bucket === undefined
|
|
17
|
+
? (0, refFns_js_1.toRefPointer)({ refName, refPath })
|
|
18
|
+
: `#/components/${bucket}/${refName}`;
|
|
19
|
+
};
|
|
20
|
+
exports.toRefKey = toRefKey;
|