@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.
- package/README.md +59 -37
- 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
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
decapitalize,
|
|
52
|
+
type EmptyEnrichments,
|
|
53
|
+
emptyEnrichmentSchema,
|
|
54
|
+
type OasOperationProjectionConstructorArgs,
|
|
55
|
+
toEndpointName,
|
|
56
|
+
toOasOperationEntry
|
|
50
57
|
} from '@skmtc/core'
|
|
51
|
-
import {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
-
|
|
64
|
-
|
|
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:
|
|
86
|
+
constructor(args: OasOperationProjectionConstructorArgs<EmptyEnrichments>) {
|
|
67
87
|
super(args)
|
|
68
88
|
|
|
69
|
-
|
|
70
|
-
|
|
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 `
|
|
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
|
-
//
|
|
90
|
-
export
|
|
91
|
-
id:
|
|
92
|
-
|
|
93
|
-
transform
|
|
94
|
-
context.insertOperation(
|
|
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/`):
|