@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,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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.53.0",
3
+ "version": "0.54.1",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -11,11 +11,10 @@ import type { BuildModelSettingsArgs, GenerateContextType, GenerateResult, Inser
11
11
  import type { ClientSettings } from '../types/Settings.js';
12
12
  import { type Method } from '../types/Method.js';
13
13
  import type { OasOperation } from '../oas/operation/Operation.js';
14
- import type { OasWebhook } from '../oas/webhook/Webhook.js';
15
14
  import { type ProjectionOptionsRest } from '../types/ProjectionOptions.js';
16
15
  import type { GeneratedValue } from '../dsl/GeneratedValue.js';
17
16
  import { ContentSettings } from '../dsl/ContentSettings.js';
18
- import type { RefName } from '../types/RefName.js';
17
+ export { toGqlOperationSource, toModelSource, toOasOperationSource, toWebhookSource } from './Subject.js';
19
18
  import type * as log from '../deps/jsr.io/@std/log/0.224.14/mod.js';
20
19
  import type { Logger } from '../types/Logger.js';
21
20
  import type { ResultType } from '../types/Results.js';
@@ -26,8 +25,6 @@ import { Inserted } from '../dsl/Inserted.js';
26
25
  import type { CaptureChannel, CaptureSink } from '../anchors/CaptureSink.js';
27
26
  import type { FileBase } from '../dsl/FileBase.js';
28
27
  import type { GeneratorsMapContainer } from '../types/GeneratorType.js';
29
- import type { OasOperationSource, WebhookSource, GqlOperationSource, ModelSource } from '../types/Preview.js';
30
- import type { GqlOperation } from '../gql/operation/GqlOperation.js';
31
28
  import type { OasVoid } from '../oas/void/Void.js';
32
29
  import type { ModelProjection, NormalizedModelProjection } from '../dsl/model/types.js';
33
30
  import { type EnrichmentWarning } from '../enrichments/EnrichmentWarning.js';
@@ -366,60 +363,3 @@ export declare class GenerateContext implements GenerateContextType {
366
363
  */
367
364
  findDefinition({ name, exportPath, into }: FindDefinitionArgs): DefinitionBase | undefined;
368
365
  }
369
- type ToOasOperationSourceArgs = {
370
- operation: OasOperation;
371
- generatorId: string;
372
- /** Operation variant the artifact was emitted for (see {@link Variant}) */
373
- variant: string;
374
- };
375
- /**
376
- * Creates an OasOperationSource from an operation, generator ID, and variant.
377
- *
378
- * Transforms operation and generator information into a source descriptor
379
- * that can be used for tracking operation origins in the generation pipeline.
380
- *
381
- * @param args - Arguments containing operation, generator ID, and variant
382
- * @returns OasOperationSource descriptor for the operation
383
- */
384
- export declare const toOasOperationSource: ({ operation, generatorId, variant }: ToOasOperationSourceArgs) => OasOperationSource;
385
- type ToWebhookSourceArgs = {
386
- webhook: OasWebhook;
387
- generatorId: string;
388
- /** Webhook variant the artifact was emitted for (see {@link Variant}) */
389
- variant: string;
390
- };
391
- /**
392
- * Creates a {@link WebhookSource} from a webhook, generator ID, and variant.
393
- * Sibling of {@link toOasOperationSource} for the 3.1 webhook subject.
394
- */
395
- export declare const toWebhookSource: ({ webhook, generatorId, variant }: ToWebhookSourceArgs) => WebhookSource;
396
- type ToGqlOperationSourceArgs = {
397
- operation: GqlOperation;
398
- generatorId: string;
399
- /** Operation variant the artifact was emitted for (see {@link Variant}) */
400
- variant: string;
401
- };
402
- /**
403
- * Creates a GraphQL operation source descriptor.
404
- *
405
- * Sibling to {@link toOasOperationSource} for the GraphQL protocol — encodes
406
- * `rootKind` and `fieldName` instead of `path` / `method`.
407
- */
408
- export declare const toGqlOperationSource: ({ operation, generatorId, variant }: ToGqlOperationSourceArgs) => GqlOperationSource;
409
- type ToModelSourceArgs = {
410
- refName: RefName;
411
- generatorId: string;
412
- /** Model variant the artifact was emitted for (see {@link Variant}) */
413
- variant: string;
414
- };
415
- /**
416
- * Creates a ModelSource from a reference name, generator ID, and variant.
417
- *
418
- * Transforms model reference and generator information into a source descriptor
419
- * that can be used for tracking model origins in the generation pipeline.
420
- *
421
- * @param args - Arguments containing reference name, generator ID, and variant
422
- * @returns ModelSource descriptor for the model
423
- */
424
- export declare const toModelSource: ({ refName, generatorId, variant }: ToModelSourceArgs) => ModelSource;
425
- export {};