@kubb/plugin-zod 5.1.0-canary.20260824T123003 → 5.1.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.
- package/dist/index.cjs +62 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +62 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
|
@@ -276,7 +276,9 @@ type ResolverZod = Resolver & {
|
|
|
276
276
|
*/
|
|
277
277
|
body(node: ast.OperationNode): string;
|
|
278
278
|
/**
|
|
279
|
-
* Resolves the name for the
|
|
279
|
+
* Resolves the name for the per-status record of all operation responses. Only meaningful when
|
|
280
|
+
* `inferred: true`, since the schema and type this name points to are generated only in that
|
|
281
|
+
* case. Generated clients key their `RequestResult` on the inferred type.
|
|
280
282
|
*
|
|
281
283
|
* @example Responses collection names
|
|
282
284
|
* `resolver.response.responses(node) // → 'listPetsResponsesSchema'`
|
package/dist/index.js
CHANGED
|
@@ -145,6 +145,62 @@ function buildOptionsSchema(node, resolver) {
|
|
|
145
145
|
})]
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* The schema a status occupies in the `<Name>Responses` record. A status that documents several
|
|
150
|
+
* content types becomes a `{ contentType; data }` union so the runtime can surface the negotiated type
|
|
151
|
+
* on `result.parsed`, while the standalone `<Name>StatusNNN` alias stays the plain body union that the
|
|
152
|
+
* query hooks and `result.data` use.
|
|
153
|
+
*/
|
|
154
|
+
function buildResponseRecordEntry(node, res, resolver) {
|
|
155
|
+
const statusName = resolver.response.status(node, res.statusCode);
|
|
156
|
+
const variants = (res.content ?? []).filter((entry) => entry.schema);
|
|
157
|
+
if (variants.length <= 1) return ast.factory.createSchema({
|
|
158
|
+
type: "ref",
|
|
159
|
+
name: statusName
|
|
160
|
+
});
|
|
161
|
+
return ast.factory.createSchema({
|
|
162
|
+
type: "union",
|
|
163
|
+
members: resolveContentTypeVariants(variants, statusName).map((variant) => ast.factory.createSchema({
|
|
164
|
+
type: "object",
|
|
165
|
+
primitive: "object",
|
|
166
|
+
properties: [ast.factory.createProperty({
|
|
167
|
+
name: "contentType",
|
|
168
|
+
required: true,
|
|
169
|
+
schema: ast.factory.createSchema({
|
|
170
|
+
type: "enum",
|
|
171
|
+
enumValues: [variant.contentType]
|
|
172
|
+
})
|
|
173
|
+
}), ast.factory.createProperty({
|
|
174
|
+
name: "data",
|
|
175
|
+
required: true,
|
|
176
|
+
schema: ast.factory.createSchema({
|
|
177
|
+
type: "ref",
|
|
178
|
+
name: variant.name
|
|
179
|
+
})
|
|
180
|
+
})]
|
|
181
|
+
}))
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Builds the per-status `<Name>Responses` record for an operation, referencing the already-resolved
|
|
186
|
+
* `<Name>StatusNNN` names. Shared by `@kubb/plugin-ts`'s `Responses` type and `@kubb/plugin-zod`'s
|
|
187
|
+
* inferred responses schema, so both emit the same shape from the same inputs.
|
|
188
|
+
*
|
|
189
|
+
* Always emits the keyed record, even when an operation declares no responses. An operation with no
|
|
190
|
+
* responses renders as an empty object, which keeps every consumer's import (for example the axios
|
|
191
|
+
* SDK's `RequestResult<XResponses>`) resolvable instead of pointing at a missing export.
|
|
192
|
+
*/
|
|
193
|
+
function buildResponses(node, resolver) {
|
|
194
|
+
return ast.factory.createSchema({
|
|
195
|
+
type: "object",
|
|
196
|
+
primitive: "object",
|
|
197
|
+
properties: node.responses.map((res) => ast.factory.createProperty({
|
|
198
|
+
name: String(res.statusCode),
|
|
199
|
+
required: true,
|
|
200
|
+
schema: buildResponseRecordEntry(node, res, resolver)
|
|
201
|
+
}))
|
|
202
|
+
});
|
|
203
|
+
}
|
|
148
204
|
function getStatusCodeNumber(statusCode) {
|
|
149
205
|
const code = Number(statusCode);
|
|
150
206
|
return Number.isNaN(code) ? null : code;
|
|
@@ -1869,6 +1925,10 @@ const zodGenerator = defineGenerator({
|
|
|
1869
1925
|
name: resolver.name(`${node.operationId} Options`),
|
|
1870
1926
|
direction: "input"
|
|
1871
1927
|
}) : null;
|
|
1928
|
+
const responsesSchema = inferred ? renderSchemaEntry({
|
|
1929
|
+
schema: buildResponses(node, resolver),
|
|
1930
|
+
name: resolver.response.responses(node)
|
|
1931
|
+
}) : null;
|
|
1872
1932
|
return /* @__PURE__ */ jsxs(File, {
|
|
1873
1933
|
baseName: meta.file.baseName,
|
|
1874
1934
|
path: meta.file.path,
|
|
@@ -1901,7 +1961,8 @@ const zodGenerator = defineGenerator({
|
|
|
1901
1961
|
errorUnionSchema,
|
|
1902
1962
|
requestSchema,
|
|
1903
1963
|
paramGroupSchemas,
|
|
1904
|
-
optionsSchema
|
|
1964
|
+
optionsSchema,
|
|
1965
|
+
responsesSchema
|
|
1905
1966
|
]
|
|
1906
1967
|
});
|
|
1907
1968
|
}
|