@skmtc/core 0.52.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.
Files changed (39) hide show
  1. package/esm/context/ParseContext.d.ts +7 -0
  2. package/esm/context/ParseContext.js +16 -16
  3. package/esm/dsl/DeclaredProjection.d.ts +48 -0
  4. package/esm/dsl/DeclaredProjection.js +1 -0
  5. package/esm/dsl/model/toModelEntry.d.ts +10 -1
  6. package/esm/dsl/model/toModelEntry.js +2 -1
  7. package/esm/dsl/operation/gql/toGqlOperationEntry.d.ts +10 -1
  8. package/esm/dsl/operation/gql/toGqlOperationEntry.js +2 -1
  9. package/esm/dsl/operation/oas/toOasOperationEntry.d.ts +10 -1
  10. package/esm/dsl/operation/oas/toOasOperationEntry.js +2 -1
  11. package/esm/dsl/webhook/toWebhookEntry.d.ts +10 -1
  12. package/esm/dsl/webhook/toWebhookEntry.js +2 -1
  13. package/esm/mod.d.ts +1 -0
  14. package/esm/parse/Reach.d.ts +56 -0
  15. package/esm/parse/Reach.js +131 -0
  16. package/esm/parse/toCyclicRefs.d.ts +3 -0
  17. package/esm/parse/toCyclicRefs.js +1 -1
  18. package/esm/parse/toRefKey.d.ts +11 -0
  19. package/esm/parse/toRefKey.js +16 -0
  20. package/package.json +1 -1
  21. package/script/context/ParseContext.d.ts +7 -0
  22. package/script/context/ParseContext.js +18 -18
  23. package/script/dsl/DeclaredProjection.d.ts +48 -0
  24. package/script/dsl/DeclaredProjection.js +2 -0
  25. package/script/dsl/model/toModelEntry.d.ts +10 -1
  26. package/script/dsl/model/toModelEntry.js +2 -1
  27. package/script/dsl/operation/gql/toGqlOperationEntry.d.ts +10 -1
  28. package/script/dsl/operation/gql/toGqlOperationEntry.js +2 -1
  29. package/script/dsl/operation/oas/toOasOperationEntry.d.ts +10 -1
  30. package/script/dsl/operation/oas/toOasOperationEntry.js +2 -1
  31. package/script/dsl/webhook/toWebhookEntry.d.ts +10 -1
  32. package/script/dsl/webhook/toWebhookEntry.js +2 -1
  33. package/script/mod.d.ts +1 -0
  34. package/script/parse/Reach.d.ts +56 -0
  35. package/script/parse/Reach.js +135 -0
  36. package/script/parse/toCyclicRefs.d.ts +3 -0
  37. package/script/parse/toCyclicRefs.js +3 -2
  38. package/script/parse/toRefKey.d.ts +11 -0
  39. 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, toRefPointer } from '../helpers/refFns.js';
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,48 @@
1
+ import type { ToModelEnrichmentsArgs, ToModelExportPathArgs } from './model/types.js';
2
+ import type { ToOasOperationEnrichmentsArgs, ToOasOperationExportPathArgs } from './operation/oas/types.js';
3
+ import type { ToGqlOperationEnrichmentsArgs, ToGqlOperationExportPathArgs } from './operation/gql/types.js';
4
+ import type { ToWebhookEnrichmentsArgs, ToWebhookExportPathArgs } from './webhook/types.js';
5
+ /**
6
+ * A model projection class as a generator's entry declares it: the statics
7
+ * core can read before it constructs anything.
8
+ *
9
+ * The statics are methods, not function properties. TypeScript compares a
10
+ * method's parameters both ways, so projection classes with different
11
+ * enrichment and option types fit in one list.
12
+ */
13
+ export type DeclaredModelProjection = {
14
+ id: string;
15
+ type: 'model';
16
+ toEnrichments(args: ToModelEnrichmentsArgs): unknown;
17
+ toExportPath(args: ToModelExportPathArgs<unknown, unknown>): string;
18
+ };
19
+ /** An OpenAPI operation projection class as an entry declares it. See {@link DeclaredModelProjection}. */
20
+ export type DeclaredOasOperationProjection = {
21
+ id: string;
22
+ type: 'oasOperation';
23
+ toEnrichments(args: ToOasOperationEnrichmentsArgs): unknown;
24
+ toExportPath(args: ToOasOperationExportPathArgs<unknown, unknown>): string;
25
+ };
26
+ /** A webhook projection class as an entry declares it. See {@link DeclaredModelProjection}. */
27
+ export type DeclaredWebhookProjection = {
28
+ id: string;
29
+ type: 'webhook';
30
+ toEnrichments(args: ToWebhookEnrichmentsArgs): unknown;
31
+ toExportPath(args: ToWebhookExportPathArgs<unknown, unknown>): string;
32
+ };
33
+ /** A GraphQL operation projection class as an entry declares it. See {@link DeclaredModelProjection}. */
34
+ export type DeclaredGqlOperationProjection = {
35
+ id: string;
36
+ type: 'gqlOperation';
37
+ toEnrichments(args: ToGqlOperationEnrichmentsArgs): unknown;
38
+ toExportPath(args: ToGqlOperationExportPathArgs<unknown, unknown>): string;
39
+ };
40
+ /**
41
+ * A projection class in an entry's `projections` list.
42
+ *
43
+ * An entry lists every projection class its generator can insert: its own,
44
+ * and each one of another generator that it inserts, such as the model
45
+ * projection an operation generator takes its schemas from. From the list,
46
+ * core can know before a run which files the generator can write.
47
+ */
48
+ export type DeclaredProjection = DeclaredModelProjection | DeclaredOasOperationProjection | DeclaredWebhookProjection | DeclaredGqlOperationProjection;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
+ import type { DeclaredProjection } from '../DeclaredProjection.js';
1
2
  import type { RefParts } from '../../helpers/refFns.js';
2
3
  import type { EnrichmentRequest } from '../../types/EnrichmentRequest.js';
3
4
  import type { TransformModelArgs, ToModelPreviewModuleArgs, ToModelEnrichmentsArgs, IsSupportedModelArgs } from './types.js';
@@ -5,6 +6,12 @@ import type { PreviewModule } from '../../types/Preview.js';
5
6
  import type * as v from 'valibot';
6
7
  type ToModelEntryArgs<EnrichmentType = undefined> = {
7
8
  id: string;
9
+ /**
10
+ * Every projection class this generator can insert: its own, and each one
11
+ * of another generator that it inserts. Core reads only their statics. See
12
+ * {@link DeclaredProjection}. Optional.
13
+ */
14
+ projections?: readonly DeclaredProjection[];
8
15
  transform: ({ context, ref, variant }: TransformModelArgs) => void;
9
16
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
10
17
  isSupported?: (args: IsSupportedModelArgs) => boolean;
@@ -20,6 +27,8 @@ type ToModelEntryArgs<EnrichmentType = undefined> = {
20
27
  export type ModelEntry<EnrichmentType = undefined> = {
21
28
  id: string;
22
29
  type: 'model';
30
+ /** The projection classes the entry declared; absent when it declared none. See {@link DeclaredProjection}. */
31
+ projections?: readonly DeclaredProjection[];
23
32
  transform: ({ context, ref, variant }: TransformModelArgs) => void;
24
33
  isSupported: (args: IsSupportedModelArgs) => boolean;
25
34
  supportsVariant: () => boolean;
@@ -86,5 +95,5 @@ export type ModelEntry<EnrichmentType = undefined> = {
86
95
  * });
87
96
  * ```
88
97
  */
89
- export declare const toModelEntry: <EnrichmentType = undefined>({ id, transform, isSupported, supportsVariant, toPreviewModule, toEnrichmentSchema, toEnrichmentRequest, toEnrichmentDefaults }: ToModelEntryArgs<EnrichmentType>) => ModelEntry<EnrichmentType>;
98
+ export declare const toModelEntry: <EnrichmentType = undefined>({ id, projections, transform, isSupported, supportsVariant, toPreviewModule, toEnrichmentSchema, toEnrichmentRequest, toEnrichmentDefaults }: ToModelEntryArgs<EnrichmentType>) => ModelEntry<EnrichmentType>;
90
99
  export {};
@@ -56,10 +56,11 @@
56
56
  * });
57
57
  * ```
58
58
  */
59
- export const toModelEntry = ({ id, transform, isSupported, supportsVariant, toPreviewModule, toEnrichmentSchema, toEnrichmentRequest, toEnrichmentDefaults }) => {
59
+ export const toModelEntry = ({ id, projections, transform, isSupported, supportsVariant, toPreviewModule, toEnrichmentSchema, toEnrichmentRequest, toEnrichmentDefaults }) => {
60
60
  return {
61
61
  id,
62
62
  type: 'model',
63
+ projections,
63
64
  transform,
64
65
  isSupported: isSupported ?? (() => true),
65
66
  supportsVariant: supportsVariant ?? (() => false),
@@ -1,3 +1,4 @@
1
+ import type { DeclaredProjection } from '../../DeclaredProjection.js';
1
2
  import type * as v from 'valibot';
2
3
  import type { GqlOperation } from '../../../gql/operation/GqlOperation.js';
3
4
  import type { EnrichmentRequest } from '../../../types/EnrichmentRequest.js';
@@ -13,6 +14,12 @@ import type { PreviewModule } from '../../../types/Preview.js';
13
14
  */
14
15
  export type ToGqlOperationConfigArgs<EnrichmentType = undefined> = {
15
16
  id: string;
17
+ /**
18
+ * Every projection class this generator can insert: its own, and each one
19
+ * of another generator that it inserts. Core reads only their statics. See
20
+ * {@link DeclaredProjection}. Optional.
21
+ */
22
+ projections?: readonly DeclaredProjection[];
16
23
  transform: ({ context, operation, variant }: TransformGqlOperationArgs) => void;
17
24
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
18
25
  isSupported?: (args: IsSupportedGqlOperationArgs) => boolean;
@@ -27,6 +34,8 @@ export type ToGqlOperationConfigArgs<EnrichmentType = undefined> = {
27
34
  export type GqlOperationEntry<EnrichmentType = undefined> = {
28
35
  id: string;
29
36
  type: 'gqlOperation';
37
+ /** The projection classes the entry declared; absent when it declared none. See {@link DeclaredProjection}. */
38
+ projections?: readonly DeclaredProjection[];
30
39
  transform: ({ context, operation, variant }: TransformGqlOperationArgs) => void;
31
40
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
32
41
  isSupported: (args: IsSupportedGqlOperationArgs) => boolean;
@@ -45,4 +54,4 @@ export type GqlOperationEntry<EnrichmentType = undefined> = {
45
54
  * @param config - Configuration object defining operation behavior
46
55
  * @returns Configured operation generator entry ready for pipeline integration
47
56
  */
48
- export declare const toGqlOperationEntry: <EnrichmentType = undefined>({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest }: ToGqlOperationConfigArgs<EnrichmentType>) => GqlOperationEntry<EnrichmentType>;
57
+ export declare const toGqlOperationEntry: <EnrichmentType = undefined>({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest }: ToGqlOperationConfigArgs<EnrichmentType>) => GqlOperationEntry<EnrichmentType>;
@@ -9,10 +9,11 @@
9
9
  * @param config - Configuration object defining operation behavior
10
10
  * @returns Configured operation generator entry ready for pipeline integration
11
11
  */
12
- export const toGqlOperationEntry = ({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest }) => {
12
+ export const toGqlOperationEntry = ({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest }) => {
13
13
  return {
14
14
  id,
15
15
  type: 'gqlOperation',
16
+ projections,
16
17
  transform,
17
18
  toEnrichmentSchema,
18
19
  isSupported: isSupported ?? (() => true),
@@ -1,3 +1,4 @@
1
+ import type { DeclaredProjection } from '../../DeclaredProjection.js';
1
2
  import type * as v from 'valibot';
2
3
  import type { OasOperation } from '../../../oas/operation/Operation.js';
3
4
  import type { EnrichmentRequest } from '../../../types/EnrichmentRequest.js';
@@ -13,6 +14,12 @@ import type { PreviewModule } from '../../../types/Preview.js';
13
14
  */
14
15
  export type ToOasOperationConfigArgs<EnrichmentType = undefined> = {
15
16
  id: string;
17
+ /**
18
+ * Every projection class this generator can insert: its own, and each one
19
+ * of another generator that it inserts. Core reads only their statics. See
20
+ * {@link DeclaredProjection}. Optional.
21
+ */
22
+ projections?: readonly DeclaredProjection[];
16
23
  transform: ({ context, operation, variant }: TransformOasOperationArgs) => void;
17
24
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
18
25
  isSupported?: (args: IsSupportedOasOperationArgs) => boolean;
@@ -36,6 +43,8 @@ export type ToOasOperationConfigArgs<EnrichmentType = undefined> = {
36
43
  export type OasOperationEntry<EnrichmentType = undefined> = {
37
44
  id: string;
38
45
  type: 'oasOperation';
46
+ /** The projection classes the entry declared; absent when it declared none. See {@link DeclaredProjection}. */
47
+ projections?: readonly DeclaredProjection[];
39
48
  transform: ({ context, operation, variant }: TransformOasOperationArgs) => void;
40
49
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
41
50
  isSupported: (args: IsSupportedOasOperationArgs) => boolean;
@@ -70,4 +79,4 @@ export type OasOperationEntry<EnrichmentType = undefined> = {
70
79
  * });
71
80
  * ```
72
81
  */
73
- export declare const toOasOperationEntry: <EnrichmentType = undefined>({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }: ToOasOperationConfigArgs<EnrichmentType>) => OasOperationEntry<EnrichmentType>;
82
+ export declare const toOasOperationEntry: <EnrichmentType = undefined>({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }: ToOasOperationConfigArgs<EnrichmentType>) => OasOperationEntry<EnrichmentType>;
@@ -24,10 +24,11 @@
24
24
  * });
25
25
  * ```
26
26
  */
27
- export const toOasOperationEntry = ({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }) => {
27
+ export const toOasOperationEntry = ({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }) => {
28
28
  return {
29
29
  id,
30
30
  type: 'oasOperation',
31
+ projections,
31
32
  transform,
32
33
  toEnrichmentSchema,
33
34
  isSupported: isSupported ?? (() => true),
@@ -1,3 +1,4 @@
1
+ import type { DeclaredProjection } from '../DeclaredProjection.js';
1
2
  import type * as v from 'valibot';
2
3
  import type { OasWebhook } from '../../oas/webhook/Webhook.js';
3
4
  import type { EnrichmentRequest } from '../../types/EnrichmentRequest.js';
@@ -14,6 +15,12 @@ import type { PreviewModule } from '../../types/Preview.js';
14
15
  */
15
16
  export type ToWebhookConfigArgs<EnrichmentType = undefined> = {
16
17
  id: string;
18
+ /**
19
+ * Every projection class this generator can insert: its own, and each one
20
+ * of another generator that it inserts. Core reads only their statics. See
21
+ * {@link DeclaredProjection}. Optional.
22
+ */
23
+ projections?: readonly DeclaredProjection[];
17
24
  transform: ({ context, webhook, variant }: TransformWebhookArgs) => void;
18
25
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
19
26
  isSupported?: (args: IsSupportedWebhookArgs) => boolean;
@@ -29,6 +36,8 @@ export type ToWebhookConfigArgs<EnrichmentType = undefined> = {
29
36
  export type WebhookEntry<EnrichmentType = undefined> = {
30
37
  id: string;
31
38
  type: 'webhook';
39
+ /** The projection classes the entry declared; absent when it declared none. See {@link DeclaredProjection}. */
40
+ projections?: readonly DeclaredProjection[];
32
41
  transform: ({ context, webhook, variant }: TransformWebhookArgs) => void;
33
42
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
34
43
  isSupported: (args: IsSupportedWebhookArgs) => boolean;
@@ -59,4 +68,4 @@ export type WebhookEntry<EnrichmentType = undefined> = {
59
68
  * });
60
69
  * ```
61
70
  */
62
- export declare const toWebhookEntry: <EnrichmentType = undefined>({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }: ToWebhookConfigArgs<EnrichmentType>) => WebhookEntry<EnrichmentType>;
71
+ export declare const toWebhookEntry: <EnrichmentType = undefined>({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }: ToWebhookConfigArgs<EnrichmentType>) => WebhookEntry<EnrichmentType>;
@@ -20,10 +20,11 @@
20
20
  * });
21
21
  * ```
22
22
  */
23
- export const toWebhookEntry = ({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }) => {
23
+ export const toWebhookEntry = ({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }) => {
24
24
  return {
25
25
  id,
26
26
  type: 'webhook',
27
+ projections,
27
28
  transform,
28
29
  toEnrichmentSchema,
29
30
  isSupported: isSupported ?? (() => true),
package/esm/mod.d.ts CHANGED
@@ -92,6 +92,7 @@ export * from './dsl/IdentifierType.js';
92
92
  export * from './dsl/Inserted.js';
93
93
  export * from './dsl/model/ModelDriver.js';
94
94
  export * from './dsl/model/toModelProjectionBase.js';
95
+ export type * from './dsl/DeclaredProjection.js';
95
96
  export * from './dsl/model/toModelEntry.js';
96
97
  export * from './dsl/model/types.js';
97
98
  export * from './dsl/operation/oas/OasOperationDriver.js';
@@ -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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.52.0",
3
+ "version": "0.54.0",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -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,48 @@
1
+ import type { ToModelEnrichmentsArgs, ToModelExportPathArgs } from './model/types.js';
2
+ import type { ToOasOperationEnrichmentsArgs, ToOasOperationExportPathArgs } from './operation/oas/types.js';
3
+ import type { ToGqlOperationEnrichmentsArgs, ToGqlOperationExportPathArgs } from './operation/gql/types.js';
4
+ import type { ToWebhookEnrichmentsArgs, ToWebhookExportPathArgs } from './webhook/types.js';
5
+ /**
6
+ * A model projection class as a generator's entry declares it: the statics
7
+ * core can read before it constructs anything.
8
+ *
9
+ * The statics are methods, not function properties. TypeScript compares a
10
+ * method's parameters both ways, so projection classes with different
11
+ * enrichment and option types fit in one list.
12
+ */
13
+ export type DeclaredModelProjection = {
14
+ id: string;
15
+ type: 'model';
16
+ toEnrichments(args: ToModelEnrichmentsArgs): unknown;
17
+ toExportPath(args: ToModelExportPathArgs<unknown, unknown>): string;
18
+ };
19
+ /** An OpenAPI operation projection class as an entry declares it. See {@link DeclaredModelProjection}. */
20
+ export type DeclaredOasOperationProjection = {
21
+ id: string;
22
+ type: 'oasOperation';
23
+ toEnrichments(args: ToOasOperationEnrichmentsArgs): unknown;
24
+ toExportPath(args: ToOasOperationExportPathArgs<unknown, unknown>): string;
25
+ };
26
+ /** A webhook projection class as an entry declares it. See {@link DeclaredModelProjection}. */
27
+ export type DeclaredWebhookProjection = {
28
+ id: string;
29
+ type: 'webhook';
30
+ toEnrichments(args: ToWebhookEnrichmentsArgs): unknown;
31
+ toExportPath(args: ToWebhookExportPathArgs<unknown, unknown>): string;
32
+ };
33
+ /** A GraphQL operation projection class as an entry declares it. See {@link DeclaredModelProjection}. */
34
+ export type DeclaredGqlOperationProjection = {
35
+ id: string;
36
+ type: 'gqlOperation';
37
+ toEnrichments(args: ToGqlOperationEnrichmentsArgs): unknown;
38
+ toExportPath(args: ToGqlOperationExportPathArgs<unknown, unknown>): string;
39
+ };
40
+ /**
41
+ * A projection class in an entry's `projections` list.
42
+ *
43
+ * An entry lists every projection class its generator can insert: its own,
44
+ * and each one of another generator that it inserts, such as the model
45
+ * projection an operation generator takes its schemas from. From the list,
46
+ * core can know before a run which files the generator can write.
47
+ */
48
+ export type DeclaredProjection = DeclaredModelProjection | DeclaredOasOperationProjection | DeclaredWebhookProjection | DeclaredGqlOperationProjection;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
+ import type { DeclaredProjection } from '../DeclaredProjection.js';
1
2
  import type { RefParts } from '../../helpers/refFns.js';
2
3
  import type { EnrichmentRequest } from '../../types/EnrichmentRequest.js';
3
4
  import type { TransformModelArgs, ToModelPreviewModuleArgs, ToModelEnrichmentsArgs, IsSupportedModelArgs } from './types.js';
@@ -5,6 +6,12 @@ import type { PreviewModule } from '../../types/Preview.js';
5
6
  import type * as v from 'valibot';
6
7
  type ToModelEntryArgs<EnrichmentType = undefined> = {
7
8
  id: string;
9
+ /**
10
+ * Every projection class this generator can insert: its own, and each one
11
+ * of another generator that it inserts. Core reads only their statics. See
12
+ * {@link DeclaredProjection}. Optional.
13
+ */
14
+ projections?: readonly DeclaredProjection[];
8
15
  transform: ({ context, ref, variant }: TransformModelArgs) => void;
9
16
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
10
17
  isSupported?: (args: IsSupportedModelArgs) => boolean;
@@ -20,6 +27,8 @@ type ToModelEntryArgs<EnrichmentType = undefined> = {
20
27
  export type ModelEntry<EnrichmentType = undefined> = {
21
28
  id: string;
22
29
  type: 'model';
30
+ /** The projection classes the entry declared; absent when it declared none. See {@link DeclaredProjection}. */
31
+ projections?: readonly DeclaredProjection[];
23
32
  transform: ({ context, ref, variant }: TransformModelArgs) => void;
24
33
  isSupported: (args: IsSupportedModelArgs) => boolean;
25
34
  supportsVariant: () => boolean;
@@ -86,5 +95,5 @@ export type ModelEntry<EnrichmentType = undefined> = {
86
95
  * });
87
96
  * ```
88
97
  */
89
- export declare const toModelEntry: <EnrichmentType = undefined>({ id, transform, isSupported, supportsVariant, toPreviewModule, toEnrichmentSchema, toEnrichmentRequest, toEnrichmentDefaults }: ToModelEntryArgs<EnrichmentType>) => ModelEntry<EnrichmentType>;
98
+ export declare const toModelEntry: <EnrichmentType = undefined>({ id, projections, transform, isSupported, supportsVariant, toPreviewModule, toEnrichmentSchema, toEnrichmentRequest, toEnrichmentDefaults }: ToModelEntryArgs<EnrichmentType>) => ModelEntry<EnrichmentType>;
90
99
  export {};
@@ -59,10 +59,11 @@ exports.toModelEntry = void 0;
59
59
  * });
60
60
  * ```
61
61
  */
62
- const toModelEntry = ({ id, transform, isSupported, supportsVariant, toPreviewModule, toEnrichmentSchema, toEnrichmentRequest, toEnrichmentDefaults }) => {
62
+ const toModelEntry = ({ id, projections, transform, isSupported, supportsVariant, toPreviewModule, toEnrichmentSchema, toEnrichmentRequest, toEnrichmentDefaults }) => {
63
63
  return {
64
64
  id,
65
65
  type: 'model',
66
+ projections,
66
67
  transform,
67
68
  isSupported: isSupported ?? (() => true),
68
69
  supportsVariant: supportsVariant ?? (() => false),
@@ -1,3 +1,4 @@
1
+ import type { DeclaredProjection } from '../../DeclaredProjection.js';
1
2
  import type * as v from 'valibot';
2
3
  import type { GqlOperation } from '../../../gql/operation/GqlOperation.js';
3
4
  import type { EnrichmentRequest } from '../../../types/EnrichmentRequest.js';
@@ -13,6 +14,12 @@ import type { PreviewModule } from '../../../types/Preview.js';
13
14
  */
14
15
  export type ToGqlOperationConfigArgs<EnrichmentType = undefined> = {
15
16
  id: string;
17
+ /**
18
+ * Every projection class this generator can insert: its own, and each one
19
+ * of another generator that it inserts. Core reads only their statics. See
20
+ * {@link DeclaredProjection}. Optional.
21
+ */
22
+ projections?: readonly DeclaredProjection[];
16
23
  transform: ({ context, operation, variant }: TransformGqlOperationArgs) => void;
17
24
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
18
25
  isSupported?: (args: IsSupportedGqlOperationArgs) => boolean;
@@ -27,6 +34,8 @@ export type ToGqlOperationConfigArgs<EnrichmentType = undefined> = {
27
34
  export type GqlOperationEntry<EnrichmentType = undefined> = {
28
35
  id: string;
29
36
  type: 'gqlOperation';
37
+ /** The projection classes the entry declared; absent when it declared none. See {@link DeclaredProjection}. */
38
+ projections?: readonly DeclaredProjection[];
30
39
  transform: ({ context, operation, variant }: TransformGqlOperationArgs) => void;
31
40
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
32
41
  isSupported: (args: IsSupportedGqlOperationArgs) => boolean;
@@ -45,4 +54,4 @@ export type GqlOperationEntry<EnrichmentType = undefined> = {
45
54
  * @param config - Configuration object defining operation behavior
46
55
  * @returns Configured operation generator entry ready for pipeline integration
47
56
  */
48
- export declare const toGqlOperationEntry: <EnrichmentType = undefined>({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest }: ToGqlOperationConfigArgs<EnrichmentType>) => GqlOperationEntry<EnrichmentType>;
57
+ export declare const toGqlOperationEntry: <EnrichmentType = undefined>({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest }: ToGqlOperationConfigArgs<EnrichmentType>) => GqlOperationEntry<EnrichmentType>;
@@ -12,10 +12,11 @@ exports.toGqlOperationEntry = void 0;
12
12
  * @param config - Configuration object defining operation behavior
13
13
  * @returns Configured operation generator entry ready for pipeline integration
14
14
  */
15
- const toGqlOperationEntry = ({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest }) => {
15
+ const toGqlOperationEntry = ({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest }) => {
16
16
  return {
17
17
  id,
18
18
  type: 'gqlOperation',
19
+ projections,
19
20
  transform,
20
21
  toEnrichmentSchema,
21
22
  isSupported: isSupported ?? (() => true),
@@ -1,3 +1,4 @@
1
+ import type { DeclaredProjection } from '../../DeclaredProjection.js';
1
2
  import type * as v from 'valibot';
2
3
  import type { OasOperation } from '../../../oas/operation/Operation.js';
3
4
  import type { EnrichmentRequest } from '../../../types/EnrichmentRequest.js';
@@ -13,6 +14,12 @@ import type { PreviewModule } from '../../../types/Preview.js';
13
14
  */
14
15
  export type ToOasOperationConfigArgs<EnrichmentType = undefined> = {
15
16
  id: string;
17
+ /**
18
+ * Every projection class this generator can insert: its own, and each one
19
+ * of another generator that it inserts. Core reads only their statics. See
20
+ * {@link DeclaredProjection}. Optional.
21
+ */
22
+ projections?: readonly DeclaredProjection[];
16
23
  transform: ({ context, operation, variant }: TransformOasOperationArgs) => void;
17
24
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
18
25
  isSupported?: (args: IsSupportedOasOperationArgs) => boolean;
@@ -36,6 +43,8 @@ export type ToOasOperationConfigArgs<EnrichmentType = undefined> = {
36
43
  export type OasOperationEntry<EnrichmentType = undefined> = {
37
44
  id: string;
38
45
  type: 'oasOperation';
46
+ /** The projection classes the entry declared; absent when it declared none. See {@link DeclaredProjection}. */
47
+ projections?: readonly DeclaredProjection[];
39
48
  transform: ({ context, operation, variant }: TransformOasOperationArgs) => void;
40
49
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
41
50
  isSupported: (args: IsSupportedOasOperationArgs) => boolean;
@@ -70,4 +79,4 @@ export type OasOperationEntry<EnrichmentType = undefined> = {
70
79
  * });
71
80
  * ```
72
81
  */
73
- export declare const toOasOperationEntry: <EnrichmentType = undefined>({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }: ToOasOperationConfigArgs<EnrichmentType>) => OasOperationEntry<EnrichmentType>;
82
+ export declare const toOasOperationEntry: <EnrichmentType = undefined>({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }: ToOasOperationConfigArgs<EnrichmentType>) => OasOperationEntry<EnrichmentType>;
@@ -27,10 +27,11 @@ exports.toOasOperationEntry = void 0;
27
27
  * });
28
28
  * ```
29
29
  */
30
- const toOasOperationEntry = ({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }) => {
30
+ const toOasOperationEntry = ({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }) => {
31
31
  return {
32
32
  id,
33
33
  type: 'oasOperation',
34
+ projections,
34
35
  transform,
35
36
  toEnrichmentSchema,
36
37
  isSupported: isSupported ?? (() => true),
@@ -1,3 +1,4 @@
1
+ import type { DeclaredProjection } from '../DeclaredProjection.js';
1
2
  import type * as v from 'valibot';
2
3
  import type { OasWebhook } from '../../oas/webhook/Webhook.js';
3
4
  import type { EnrichmentRequest } from '../../types/EnrichmentRequest.js';
@@ -14,6 +15,12 @@ import type { PreviewModule } from '../../types/Preview.js';
14
15
  */
15
16
  export type ToWebhookConfigArgs<EnrichmentType = undefined> = {
16
17
  id: string;
18
+ /**
19
+ * Every projection class this generator can insert: its own, and each one
20
+ * of another generator that it inserts. Core reads only their statics. See
21
+ * {@link DeclaredProjection}. Optional.
22
+ */
23
+ projections?: readonly DeclaredProjection[];
17
24
  transform: ({ context, webhook, variant }: TransformWebhookArgs) => void;
18
25
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
19
26
  isSupported?: (args: IsSupportedWebhookArgs) => boolean;
@@ -29,6 +36,8 @@ export type ToWebhookConfigArgs<EnrichmentType = undefined> = {
29
36
  export type WebhookEntry<EnrichmentType = undefined> = {
30
37
  id: string;
31
38
  type: 'webhook';
39
+ /** The projection classes the entry declared; absent when it declared none. See {@link DeclaredProjection}. */
40
+ projections?: readonly DeclaredProjection[];
32
41
  transform: ({ context, webhook, variant }: TransformWebhookArgs) => void;
33
42
  toEnrichmentSchema: () => v.GenericSchema<EnrichmentType>;
34
43
  isSupported: (args: IsSupportedWebhookArgs) => boolean;
@@ -59,4 +68,4 @@ export type WebhookEntry<EnrichmentType = undefined> = {
59
68
  * });
60
69
  * ```
61
70
  */
62
- export declare const toWebhookEntry: <EnrichmentType = undefined>({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }: ToWebhookConfigArgs<EnrichmentType>) => WebhookEntry<EnrichmentType>;
71
+ export declare const toWebhookEntry: <EnrichmentType = undefined>({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }: ToWebhookConfigArgs<EnrichmentType>) => WebhookEntry<EnrichmentType>;
@@ -23,10 +23,11 @@ exports.toWebhookEntry = void 0;
23
23
  * });
24
24
  * ```
25
25
  */
26
- const toWebhookEntry = ({ id, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }) => {
26
+ const toWebhookEntry = ({ id, projections, transform, toEnrichmentSchema, isSupported, supportsVariant, toPreviewModule, toEnrichmentRequest, toEnrichmentDefaults }) => {
27
27
  return {
28
28
  id,
29
29
  type: 'webhook',
30
+ projections,
30
31
  transform,
31
32
  toEnrichmentSchema,
32
33
  isSupported: isSupported ?? (() => true),
package/script/mod.d.ts CHANGED
@@ -92,6 +92,7 @@ export * from './dsl/IdentifierType.js';
92
92
  export * from './dsl/Inserted.js';
93
93
  export * from './dsl/model/ModelDriver.js';
94
94
  export * from './dsl/model/toModelProjectionBase.js';
95
+ export type * from './dsl/DeclaredProjection.js';
95
96
  export * from './dsl/model/toModelEntry.js';
96
97
  export * from './dsl/model/types.js';
97
98
  export * from './dsl/operation/oas/OasOperationDriver.js';
@@ -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;