@skmtc/core 0.57.0 → 0.58.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.
package/README.md CHANGED
@@ -42,60 +42,82 @@ skmtc generate petstore https://petstore3.swagger.io/api/v3/openapi.json
42
42
 
43
43
  ### Example generator code
44
44
 
45
- ```typescript
45
+ A whole generator, in one file. It writes one `fetch` function for each
46
+ operation of the document. It is short on purpose: the function sends the
47
+ path as the document writes it, with no path parameters and no body.
48
+
49
+ ```ts
46
50
  import {
47
- toOasOperationEntry,
48
- toOasOperationProjectionBase,
49
- Identifier
51
+ decapitalize,
52
+ type EmptyEnrichments,
53
+ emptyEnrichmentSchema,
54
+ type OasOperationProjectionConstructorArgs,
55
+ toEndpointName,
56
+ toOasOperationEntry
50
57
  } from '@skmtc/core'
51
- import { ZodProjection } from '@skmtc/gen-zod'
52
- import denoJson from '../deno.json' with { type: 'json' }
53
-
54
- // Build a per-generator projection base via the factory.
55
- const ZodFetchBase = toOasOperationProjectionBase({
56
- id: denoJson.name,
57
- toIdentifier: ({ operation }) =>
58
- Identifier.createVariable(`fetch${operation.operationId}`),
59
- toExportPath: ({ operation }) =>
60
- `@/services/${operation.operationId}.generated.ts`
58
+ import { toStringLiteral, toTsOasOperationProjectionBase } from '@skmtc/lang-typescript'
59
+
60
+ const ID = '@acme/gen-fetch'
61
+
62
+ // This generator takes no settings from its consumer.
63
+ const toEnrichmentSchema = () => emptyEnrichmentSchema
64
+
65
+ // The name and the export path of each definition are decided here, once.
66
+ const FetchBase = toTsOasOperationProjectionBase<EmptyEnrichments>({
67
+ id: ID,
68
+
69
+ // Method and path, never operationId: `POST /search` is `createApiSearch`.
70
+ toIdentifierName: ({ operation }) => decapitalize(toEndpointName(operation)),
71
+
72
+ toIdentifierType: () => ({ type: 'variable' }),
73
+
74
+ toExportPath({ operation, enrichments, variant }) {
75
+ return `@/api/${this.toIdentifierName({ operation, enrichments, variant })}.generated.ts`
76
+ },
77
+
78
+ toEnrichmentSchema
61
79
  })
62
80
 
63
- class ZodFetch extends ZodFetchBase {
64
- zodName: string
81
+ // A producer: the constructor stores the parts, and toString() arranges them.
82
+ class FetchEndpoint extends FetchBase {
83
+ method: string
84
+ path: string
65
85
 
66
- constructor(args: ConstructorParameters<typeof ZodFetchBase>[0]) {
86
+ constructor(args: OasOperationProjectionConstructorArgs<EmptyEnrichments>) {
67
87
  super(args)
68
88
 
69
- // Insert a Zod schema for the success response into the same file.
70
- const response = this.operation.toSuccessResponse()?.resolve().toSchema()
71
- const zodResponse = this.insertNormalizedModel(ZodProjection, {
72
- schema: response,
73
- fallbackName: `${this.operation.operationId}Response`
74
- })
75
-
76
- // Grab the Zod identifier so toString() can reference it.
77
- this.zodName = zodResponse.identifier.name
89
+ this.method = toStringLiteral(this.operation.method.toUpperCase())
90
+ this.path = toStringLiteral(this.operation.path)
78
91
  }
79
92
 
80
93
  override toString(): string {
81
- return `async () => {
82
- const res = await fetch('${this.operation.path}')
83
- const data = await res.json()
84
- return ${this.zodName}.parse(data)
85
- }`
94
+ return `() => fetch(${this.path}, { method: ${this.method} })`
86
95
  }
87
96
  }
88
97
 
89
- // Wire it up so the engine dispatches against OAS operations.
90
- export const zodFetchEntry = toOasOperationEntry({
91
- id: denoJson.name,
92
- isSupported: () => true,
93
- transform: ({ context, operation }) => {
94
- context.insertOperation(ZodFetch, operation)
98
+ // The engine runs the entry once for each operation of the document.
99
+ export default toOasOperationEntry<EmptyEnrichments>({
100
+ id: ID,
101
+ toEnrichmentSchema,
102
+ transform({ context, operation, variant }) {
103
+ context.insertOperation({ projection: FetchEndpoint, operation, variant })
95
104
  }
96
105
  })
97
106
  ```
98
107
 
108
+ For a document with `GET /pets` and `POST /search`, it writes two files:
109
+
110
+ ```ts
111
+ // api/getApiPets.generated.ts
112
+ export const getApiPets = () => fetch('/pets', { method: 'GET' });
113
+
114
+ // api/createApiSearch.generated.ts
115
+ export const createApiSearch = () => fetch('/search', { method: 'POST' });
116
+ ```
117
+
118
+ `deno task verify-docs` type-checks this example against the packages in
119
+ this repository, so it compiles with the version you read it beside.
120
+
99
121
  ## 📦 Available Generators
100
122
 
101
123
  Stock generators on JSR (under `@skmtc/`):
@@ -98,8 +98,6 @@ export type CreateAndRegisterDefinition<Schema extends SchemaType> = {
98
98
  destinationPath: string;
99
99
  /** Function to transform the schema into a generated value */
100
100
  schemaToValueFn: SchemaToValueFn;
101
- /** The ref of the model whose value tree holds the schema */
102
- rootRef?: RefParts;
103
101
  /** Whether to exclude this definition from exports */
104
102
  noExport?: boolean;
105
103
  };
@@ -764,7 +764,6 @@ export class GenerateContext {
764
764
  context: this,
765
765
  projection,
766
766
  ref,
767
- rootRef: ref,
768
767
  ...toInsertOptions(rest)
769
768
  });
770
769
  return new Inserted({ settings, definition });
@@ -15,7 +15,6 @@ type CreateModelArgs<V extends GeneratedValue, EnrichmentType, ProjectionOptions
15
15
  projection: ModelProjection<V, EnrichmentType, ProjectionOptions>;
16
16
  /** The model's ref; an `OasRef` fits it. */
17
17
  ref: RefParts;
18
- rootRef?: RefParts;
19
18
  } & InsertModelOptions<ProjectionOptions>;
20
19
  /**
21
20
  * Driver for the model insertion lifecycle.
@@ -33,7 +32,6 @@ export declare class ModelDriver<V extends GeneratedValue, EnrichmentType, Proje
33
32
  settings: ContentSettings<EnrichmentType>;
34
33
  destinationPath?: string;
35
34
  definition: GeneratedDefinition<V>;
36
- rootRef?: RefParts;
37
35
  noExport: boolean;
38
36
  variant: string;
39
37
  /** The caller's options, as passed to `insertModel`. */
@@ -57,12 +57,6 @@ export class ModelDriver {
57
57
  writable: true,
58
58
  value: void 0
59
59
  });
60
- Object.defineProperty(this, "rootRef", {
61
- enumerable: true,
62
- configurable: true,
63
- writable: true,
64
- value: void 0
65
- });
66
60
  Object.defineProperty(this, "noExport", {
67
61
  enumerable: true,
68
62
  configurable: true,
@@ -82,12 +76,11 @@ export class ModelDriver {
82
76
  writable: true,
83
77
  value: void 0
84
78
  });
85
- const { context, projection, ref, destinationPath, rootRef, noExport = false, variant = DEFAULT_VARIANT } = args;
79
+ const { context, projection, ref, destinationPath, noExport = false, variant = DEFAULT_VARIANT } = args;
86
80
  this.context = context;
87
81
  this.projection = projection;
88
82
  this.ref = ref;
89
83
  this.destinationPath = destinationPath;
90
- this.rootRef = rootRef;
91
84
  this.noExport = noExport;
92
85
  this.variant = variant;
93
86
  this.options = readProjectionOptions(args);
@@ -158,7 +151,6 @@ export class ModelDriver {
158
151
  context: this.context,
159
152
  settings: this.settings,
160
153
  destinationPath: this.settings.exportPath,
161
- rootRef: this.rootRef,
162
154
  options: this.options
163
155
  }));
164
156
  const definition = this.projection.lang.toDefinition({
@@ -20,7 +20,6 @@ export type ModelProjectionConstructorArgs<EnrichmentType = undefined, Projectio
20
20
  ref: RefParts;
21
21
  settings: ContentSettings<EnrichmentType>;
22
22
  destinationPath: string;
23
- rootRef?: RefParts;
24
23
  } & ProjectionOptionsArg<ProjectionOptions>;
25
24
  export type WithTransformModel = {
26
25
  transformModel: (ref: RefParts) => void;
@@ -104,7 +103,7 @@ export type ToModelExportPathArgs<EnrichmentType = undefined, ProjectionOptions
104
103
  export type ModelProjection<V extends GeneratedValue, EnrichmentType = undefined, ProjectionOptions = undefined> = {
105
104
  prototype: V;
106
105
  } & {
107
- new ({ context, ref, settings, destinationPath, rootRef, options }: ModelProjectionConstructorArgs<EnrichmentType, ProjectionOptions>): V;
106
+ new ({ context, ref, settings, destinationPath, options }: ModelProjectionConstructorArgs<EnrichmentType, ProjectionOptions>): V;
108
107
  id: string;
109
108
  type: 'model';
110
109
  /**
@@ -7,7 +7,6 @@ import type { GenerateContextType } from '../context/generateTypes.js';
7
7
  import type { CustomValue } from '../dsl/CustomValue.js';
8
8
  import type { OasVoid } from '../oas/void/Void.js';
9
9
  import type { GeneratorKey } from '../dsl/GeneratorKeys.js';
10
- import type { RefParts } from '../helpers/refFns.js';
11
10
  /**
12
11
  * Union type representing all possible type system values in the SKMTC code generation system.
13
12
  *
@@ -554,8 +553,6 @@ export type TypeSystemArgs<Schema extends SchemaType> = {
554
553
  destinationPath: string;
555
554
  /** The schema to transform */
556
555
  schema: Schema;
557
- /** The ref of the model whose value tree holds the schema */
558
- rootRef?: RefParts;
559
556
  /** Whether the schema field is required */
560
557
  required: boolean | undefined;
561
558
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.57.0",
3
+ "version": "0.58.1",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",
@@ -98,8 +98,6 @@ export type CreateAndRegisterDefinition<Schema extends SchemaType> = {
98
98
  destinationPath: string;
99
99
  /** Function to transform the schema into a generated value */
100
100
  schemaToValueFn: SchemaToValueFn;
101
- /** The ref of the model whose value tree holds the schema */
102
- rootRef?: RefParts;
103
101
  /** Whether to exclude this definition from exports */
104
102
  noExport?: boolean;
105
103
  };
@@ -774,7 +774,6 @@ class GenerateContext {
774
774
  context: this,
775
775
  projection,
776
776
  ref,
777
- rootRef: ref,
778
777
  ...(0, InsertOptions_js_1.toInsertOptions)(rest)
779
778
  });
780
779
  return new Inserted_js_1.Inserted({ settings, definition });
@@ -15,7 +15,6 @@ type CreateModelArgs<V extends GeneratedValue, EnrichmentType, ProjectionOptions
15
15
  projection: ModelProjection<V, EnrichmentType, ProjectionOptions>;
16
16
  /** The model's ref; an `OasRef` fits it. */
17
17
  ref: RefParts;
18
- rootRef?: RefParts;
19
18
  } & InsertModelOptions<ProjectionOptions>;
20
19
  /**
21
20
  * Driver for the model insertion lifecycle.
@@ -33,7 +32,6 @@ export declare class ModelDriver<V extends GeneratedValue, EnrichmentType, Proje
33
32
  settings: ContentSettings<EnrichmentType>;
34
33
  destinationPath?: string;
35
34
  definition: GeneratedDefinition<V>;
36
- rootRef?: RefParts;
37
35
  noExport: boolean;
38
36
  variant: string;
39
37
  /** The caller's options, as passed to `insertModel`. */
@@ -63,12 +63,6 @@ class ModelDriver {
63
63
  writable: true,
64
64
  value: void 0
65
65
  });
66
- Object.defineProperty(this, "rootRef", {
67
- enumerable: true,
68
- configurable: true,
69
- writable: true,
70
- value: void 0
71
- });
72
66
  Object.defineProperty(this, "noExport", {
73
67
  enumerable: true,
74
68
  configurable: true,
@@ -88,12 +82,11 @@ class ModelDriver {
88
82
  writable: true,
89
83
  value: void 0
90
84
  });
91
- const { context, projection, ref, destinationPath, rootRef, noExport = false, variant = Variant_js_1.DEFAULT_VARIANT } = args;
85
+ const { context, projection, ref, destinationPath, noExport = false, variant = Variant_js_1.DEFAULT_VARIANT } = args;
92
86
  this.context = context;
93
87
  this.projection = projection;
94
88
  this.ref = ref;
95
89
  this.destinationPath = destinationPath;
96
- this.rootRef = rootRef;
97
90
  this.noExport = noExport;
98
91
  this.variant = variant;
99
92
  this.options = (0, ProjectionOptions_js_1.readProjectionOptions)(args);
@@ -164,7 +157,6 @@ class ModelDriver {
164
157
  context: this.context,
165
158
  settings: this.settings,
166
159
  destinationPath: this.settings.exportPath,
167
- rootRef: this.rootRef,
168
160
  options: this.options
169
161
  }));
170
162
  const definition = this.projection.lang.toDefinition({
@@ -20,7 +20,6 @@ export type ModelProjectionConstructorArgs<EnrichmentType = undefined, Projectio
20
20
  ref: RefParts;
21
21
  settings: ContentSettings<EnrichmentType>;
22
22
  destinationPath: string;
23
- rootRef?: RefParts;
24
23
  } & ProjectionOptionsArg<ProjectionOptions>;
25
24
  export type WithTransformModel = {
26
25
  transformModel: (ref: RefParts) => void;
@@ -104,7 +103,7 @@ export type ToModelExportPathArgs<EnrichmentType = undefined, ProjectionOptions
104
103
  export type ModelProjection<V extends GeneratedValue, EnrichmentType = undefined, ProjectionOptions = undefined> = {
105
104
  prototype: V;
106
105
  } & {
107
- new ({ context, ref, settings, destinationPath, rootRef, options }: ModelProjectionConstructorArgs<EnrichmentType, ProjectionOptions>): V;
106
+ new ({ context, ref, settings, destinationPath, options }: ModelProjectionConstructorArgs<EnrichmentType, ProjectionOptions>): V;
108
107
  id: string;
109
108
  type: 'model';
110
109
  /**
@@ -7,7 +7,6 @@ import type { GenerateContextType } from '../context/generateTypes.js';
7
7
  import type { CustomValue } from '../dsl/CustomValue.js';
8
8
  import type { OasVoid } from '../oas/void/Void.js';
9
9
  import type { GeneratorKey } from '../dsl/GeneratorKeys.js';
10
- import type { RefParts } from '../helpers/refFns.js';
11
10
  /**
12
11
  * Union type representing all possible type system values in the SKMTC code generation system.
13
12
  *
@@ -554,8 +553,6 @@ export type TypeSystemArgs<Schema extends SchemaType> = {
554
553
  destinationPath: string;
555
554
  /** The schema to transform */
556
555
  schema: Schema;
557
- /** The ref of the model whose value tree holds the schema */
558
- rootRef?: RefParts;
559
556
  /** Whether the schema field is required */
560
557
  required: boolean | undefined;
561
558
  };