@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.cjs
CHANGED
|
@@ -155,6 +155,62 @@ function buildOptionsSchema(node, resolver) {
|
|
|
155
155
|
})]
|
|
156
156
|
});
|
|
157
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* The schema a status occupies in the `<Name>Responses` record. A status that documents several
|
|
160
|
+
* content types becomes a `{ contentType; data }` union so the runtime can surface the negotiated type
|
|
161
|
+
* on `result.parsed`, while the standalone `<Name>StatusNNN` alias stays the plain body union that the
|
|
162
|
+
* query hooks and `result.data` use.
|
|
163
|
+
*/
|
|
164
|
+
function buildResponseRecordEntry(node, res, resolver) {
|
|
165
|
+
const statusName = resolver.response.status(node, res.statusCode);
|
|
166
|
+
const variants = (res.content ?? []).filter((entry) => entry.schema);
|
|
167
|
+
if (variants.length <= 1) return kubb_kit.ast.factory.createSchema({
|
|
168
|
+
type: "ref",
|
|
169
|
+
name: statusName
|
|
170
|
+
});
|
|
171
|
+
return kubb_kit.ast.factory.createSchema({
|
|
172
|
+
type: "union",
|
|
173
|
+
members: resolveContentTypeVariants(variants, statusName).map((variant) => kubb_kit.ast.factory.createSchema({
|
|
174
|
+
type: "object",
|
|
175
|
+
primitive: "object",
|
|
176
|
+
properties: [kubb_kit.ast.factory.createProperty({
|
|
177
|
+
name: "contentType",
|
|
178
|
+
required: true,
|
|
179
|
+
schema: kubb_kit.ast.factory.createSchema({
|
|
180
|
+
type: "enum",
|
|
181
|
+
enumValues: [variant.contentType]
|
|
182
|
+
})
|
|
183
|
+
}), kubb_kit.ast.factory.createProperty({
|
|
184
|
+
name: "data",
|
|
185
|
+
required: true,
|
|
186
|
+
schema: kubb_kit.ast.factory.createSchema({
|
|
187
|
+
type: "ref",
|
|
188
|
+
name: variant.name
|
|
189
|
+
})
|
|
190
|
+
})]
|
|
191
|
+
}))
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Builds the per-status `<Name>Responses` record for an operation, referencing the already-resolved
|
|
196
|
+
* `<Name>StatusNNN` names. Shared by `@kubb/plugin-ts`'s `Responses` type and `@kubb/plugin-zod`'s
|
|
197
|
+
* inferred responses schema, so both emit the same shape from the same inputs.
|
|
198
|
+
*
|
|
199
|
+
* Always emits the keyed record, even when an operation declares no responses. An operation with no
|
|
200
|
+
* responses renders as an empty object, which keeps every consumer's import (for example the axios
|
|
201
|
+
* SDK's `RequestResult<XResponses>`) resolvable instead of pointing at a missing export.
|
|
202
|
+
*/
|
|
203
|
+
function buildResponses(node, resolver) {
|
|
204
|
+
return kubb_kit.ast.factory.createSchema({
|
|
205
|
+
type: "object",
|
|
206
|
+
primitive: "object",
|
|
207
|
+
properties: node.responses.map((res) => kubb_kit.ast.factory.createProperty({
|
|
208
|
+
name: String(res.statusCode),
|
|
209
|
+
required: true,
|
|
210
|
+
schema: buildResponseRecordEntry(node, res, resolver)
|
|
211
|
+
}))
|
|
212
|
+
});
|
|
213
|
+
}
|
|
158
214
|
function getStatusCodeNumber(statusCode) {
|
|
159
215
|
const code = Number(statusCode);
|
|
160
216
|
return Number.isNaN(code) ? null : code;
|
|
@@ -1879,6 +1935,10 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1879
1935
|
name: resolver.name(`${node.operationId} Options`),
|
|
1880
1936
|
direction: "input"
|
|
1881
1937
|
}) : null;
|
|
1938
|
+
const responsesSchema = inferred ? renderSchemaEntry({
|
|
1939
|
+
schema: buildResponses(node, resolver),
|
|
1940
|
+
name: resolver.response.responses(node)
|
|
1941
|
+
}) : null;
|
|
1882
1942
|
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File, {
|
|
1883
1943
|
baseName: meta.file.baseName,
|
|
1884
1944
|
path: meta.file.path,
|
|
@@ -1911,7 +1971,8 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1911
1971
|
errorUnionSchema,
|
|
1912
1972
|
requestSchema,
|
|
1913
1973
|
paramGroupSchemas,
|
|
1914
|
-
optionsSchema
|
|
1974
|
+
optionsSchema,
|
|
1975
|
+
responsesSchema
|
|
1915
1976
|
]
|
|
1916
1977
|
});
|
|
1917
1978
|
}
|