@skmtc/core 0.53.0 → 0.54.1

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.
@@ -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;