@skmtc/core 0.58.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.
Files changed (2) hide show
  1. package/README.md +59 -37
  2. package/package.json +1 -1
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/`):
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skmtc/core",
3
- "version": "0.58.0",
3
+ "version": "0.58.1",
4
4
  "description": "Skmtc is a declarative code generation framework",
5
5
  "keywords": [
6
6
  "openapi",