@kubb/plugin-ts 5.0.0-beta.99 → 5.0.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/dist/index.cjs +244 -231
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +244 -231
- package/dist/index.js.map +1 -1
- package/package.json +17 -8
package/dist/index.cjs
CHANGED
|
@@ -32,7 +32,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
32
32
|
}
|
|
33
33
|
return to;
|
|
34
34
|
};
|
|
35
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
35
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
36
36
|
value: mod,
|
|
37
37
|
enumerable: true
|
|
38
38
|
}) : target, mod));
|
|
@@ -856,7 +856,7 @@ function buildTupleNode(node, print) {
|
|
|
856
856
|
*/
|
|
857
857
|
function buildPropertyType(schema, baseType, optionalType, optional) {
|
|
858
858
|
const addsUndefined = OPTIONAL_ADDS_UNDEFINED.has(optionalType);
|
|
859
|
-
const meta = kubb_kit.
|
|
859
|
+
const meta = (0, kubb_kit.syncSchemaRef)(schema);
|
|
860
860
|
let type = baseType;
|
|
861
861
|
if (meta.nullable) type = createUnionDeclaration({ nodes: [type, keywordTypeNodes.null] });
|
|
862
862
|
if ((optional || meta.nullish || meta.optional) && addsUndefined) type = createUnionDeclaration({ nodes: [type, keywordTypeNodes.undefined] });
|
|
@@ -950,6 +950,115 @@ function Enum({ node, enum: enumOptions, resolver }) {
|
|
|
950
950
|
})] });
|
|
951
951
|
}
|
|
952
952
|
//#endregion
|
|
953
|
+
//#region src/utils.ts
|
|
954
|
+
/**
|
|
955
|
+
* Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
|
|
956
|
+
* rather than a named enum reference or a runtime enum declaration.
|
|
957
|
+
*
|
|
958
|
+
* The parser folds `const` into a single-value enum node. The adapter decides which schemas are
|
|
959
|
+
* named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
|
|
960
|
+
* example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
|
|
961
|
+
* a named enum, which keeps the declaration and its references in sync across adapter versions.
|
|
962
|
+
*/
|
|
963
|
+
function isInlineConstEnum(node, enumSchemaNames) {
|
|
964
|
+
return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Collects JSDoc annotation strings for a schema node.
|
|
968
|
+
*
|
|
969
|
+
* Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
|
|
970
|
+
* Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
|
|
971
|
+
|
|
972
|
+
*/
|
|
973
|
+
function isSchemaOptional(schema) {
|
|
974
|
+
return Boolean("optional" in schema && schema.optional || "nullish" in schema && schema.nullish);
|
|
975
|
+
}
|
|
976
|
+
function formatExample(value) {
|
|
977
|
+
if (value === null || typeof value !== "object") return String(value);
|
|
978
|
+
return (JSON.stringify(value) ?? String(value)).replaceAll("*/", "*\\/");
|
|
979
|
+
}
|
|
980
|
+
function buildPropertyJSDocComments(schema, optional) {
|
|
981
|
+
const meta = (0, kubb_kit.syncSchemaRef)(schema);
|
|
982
|
+
const isArray = meta?.primitive === "array";
|
|
983
|
+
const hasDescription = meta && "description" in meta && meta.description;
|
|
984
|
+
const formatComment = meta && "format" in meta && meta.format ? hasDescription ? [" ", `Format: \`${meta.format}\``] : ["@description", `Format: \`${meta.format}\``] : [];
|
|
985
|
+
const exampleValues = meta?.examples ?? [];
|
|
986
|
+
const comments = [
|
|
987
|
+
hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
|
|
988
|
+
...formatComment,
|
|
989
|
+
meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
|
|
990
|
+
!isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
|
|
991
|
+
!isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
|
|
992
|
+
meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
|
|
993
|
+
meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" && typeof meta.default === "string" ? stringify(meta.default) : meta.default}` : null,
|
|
994
|
+
...exampleValues.map((example) => `@example ${formatExample(example)}`)
|
|
995
|
+
].filter(Boolean);
|
|
996
|
+
const typeTag = comments.length && meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null;
|
|
997
|
+
return [...comments, typeTag].filter(Boolean);
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Builds the object schema for a group of parameters sharing one `in` location (path, query, or
|
|
1001
|
+
* header), embedding each param's own schema (and JSDoc) directly rather than referencing a
|
|
1002
|
+
* separate per-param type — the group itself is the only type these params get exported as.
|
|
1003
|
+
*/
|
|
1004
|
+
function buildParams({ params }) {
|
|
1005
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1006
|
+
type: "object",
|
|
1007
|
+
properties: params.map((param) => kubb_kit.ast.factory.createProperty({
|
|
1008
|
+
name: param.name,
|
|
1009
|
+
required: param.required,
|
|
1010
|
+
schema: kubb_kit.ast.factory.createSchema({
|
|
1011
|
+
...param.schema,
|
|
1012
|
+
optional: !param.required
|
|
1013
|
+
})
|
|
1014
|
+
}))
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
function buildResponseUnion(node, { resolver }) {
|
|
1018
|
+
const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
|
|
1019
|
+
if (responsesWithSchema.length === 0) return null;
|
|
1020
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1021
|
+
type: "union",
|
|
1022
|
+
members: responsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
|
|
1023
|
+
type: "ref",
|
|
1024
|
+
name: resolver.response.status(node, res.statusCode)
|
|
1025
|
+
}))
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
//#endregion
|
|
1029
|
+
//#region src/components/Type.tsx
|
|
1030
|
+
function Type({ name, node, printer, enum: enumOptions, resolver }) {
|
|
1031
|
+
const enumSchemaNodes = kubb_kit.ast.collectSync(node, { schema(n) {
|
|
1032
|
+
const enumNode = kubb_kit.ast.narrowSchema(n, kubb_kit.ast.schemaTypes.enum);
|
|
1033
|
+
if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
|
|
1034
|
+
} });
|
|
1035
|
+
const output = printer.print(node);
|
|
1036
|
+
if (!output) return;
|
|
1037
|
+
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
1038
|
+
return {
|
|
1039
|
+
node,
|
|
1040
|
+
...getEnumNames({
|
|
1041
|
+
node,
|
|
1042
|
+
enum: enumOptions,
|
|
1043
|
+
resolver
|
|
1044
|
+
})
|
|
1045
|
+
};
|
|
1046
|
+
});
|
|
1047
|
+
const shouldExportEnums = enumOptions.type !== "inlineLiteral";
|
|
1048
|
+
const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
1049
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Enum, {
|
|
1050
|
+
node,
|
|
1051
|
+
enum: enumOptions,
|
|
1052
|
+
resolver
|
|
1053
|
+
}, node.name)), shouldExportType && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
1054
|
+
name,
|
|
1055
|
+
isTypeOnly: true,
|
|
1056
|
+
isExportable: true,
|
|
1057
|
+
isIndexable: true,
|
|
1058
|
+
children: output
|
|
1059
|
+
})] });
|
|
1060
|
+
}
|
|
1061
|
+
//#endregion
|
|
953
1062
|
//#region ../../internals/shared/src/params.ts
|
|
954
1063
|
/**
|
|
955
1064
|
* Drops parameters that share the same name, keeping the first.
|
|
@@ -1017,16 +1126,137 @@ function resolveContentTypeVariants(entries, baseName) {
|
|
|
1017
1126
|
};
|
|
1018
1127
|
});
|
|
1019
1128
|
}
|
|
1129
|
+
const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
1130
|
+
/**
|
|
1131
|
+
* Groups an operation's parameters by location (`path`/`query`/`header`/`cookie`), deduping each
|
|
1132
|
+
* group by name. Every plugin generator visiting the same `OperationNode` shares one AST instance
|
|
1133
|
+
* (see `KubbDriver`), so the result is cached per node to avoid re-filtering and re-deduping the
|
|
1134
|
+
* same parameters once per plugin.
|
|
1135
|
+
*/
|
|
1020
1136
|
function getOperationParameters(node) {
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
})),
|
|
1137
|
+
const cached = operationParameterGroupsByNode.get(node);
|
|
1138
|
+
if (cached) return cached;
|
|
1139
|
+
const groups = {
|
|
1140
|
+
path: dedupeParams(node.parameters.filter((param) => param.in === "path")),
|
|
1026
1141
|
query: dedupeParams(node.parameters.filter((param) => param.in === "query")),
|
|
1027
1142
|
header: dedupeParams(node.parameters.filter((param) => param.in === "header")),
|
|
1028
1143
|
cookie: dedupeParams(node.parameters.filter((param) => param.in === "cookie"))
|
|
1029
1144
|
};
|
|
1145
|
+
operationParameterGroupsByNode.set(node, groups);
|
|
1146
|
+
return groups;
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Builds the combined `{ body, path, query, headers }` options object schema for an operation,
|
|
1150
|
+
* referencing the already-resolved body and grouped param names. Shared by `@kubb/plugin-ts`'s
|
|
1151
|
+
* `Options` type and `@kubb/plugin-zod`'s inferred options schema, so both printers emit the same
|
|
1152
|
+
* shape from the same inputs. `primitive: 'object'` is a no-op for the TS printer and tells the Zod
|
|
1153
|
+
* printer to emit `z.object(…)` rather than a record.
|
|
1154
|
+
*/
|
|
1155
|
+
function buildOptionsSchema(node, resolver) {
|
|
1156
|
+
const { path, query, header } = getOperationParameters(node);
|
|
1157
|
+
const hasBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
1158
|
+
const createNever = () => kubb_kit.ast.factory.createSchema({
|
|
1159
|
+
type: "never",
|
|
1160
|
+
primitive: void 0,
|
|
1161
|
+
optional: true
|
|
1162
|
+
});
|
|
1163
|
+
const groups = [
|
|
1164
|
+
{
|
|
1165
|
+
name: "path",
|
|
1166
|
+
params: path,
|
|
1167
|
+
resolve: resolver.param.path
|
|
1168
|
+
},
|
|
1169
|
+
{
|
|
1170
|
+
name: "query",
|
|
1171
|
+
params: query,
|
|
1172
|
+
resolve: resolver.param.query
|
|
1173
|
+
},
|
|
1174
|
+
{
|
|
1175
|
+
name: "headers",
|
|
1176
|
+
params: header,
|
|
1177
|
+
resolve: resolver.param.headers
|
|
1178
|
+
}
|
|
1179
|
+
];
|
|
1180
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1181
|
+
type: "object",
|
|
1182
|
+
primitive: "object",
|
|
1183
|
+
deprecated: node.deprecated,
|
|
1184
|
+
properties: [kubb_kit.ast.factory.createProperty({
|
|
1185
|
+
name: "body",
|
|
1186
|
+
required: hasBody,
|
|
1187
|
+
schema: hasBody ? kubb_kit.ast.factory.createSchema({
|
|
1188
|
+
type: "ref",
|
|
1189
|
+
name: resolver.response.body(node)
|
|
1190
|
+
}) : createNever()
|
|
1191
|
+
}), ...groups.map(({ name, params, resolve }) => {
|
|
1192
|
+
const required = params.some((param) => param.required);
|
|
1193
|
+
return kubb_kit.ast.factory.createProperty({
|
|
1194
|
+
name,
|
|
1195
|
+
required,
|
|
1196
|
+
schema: params.length > 0 ? kubb_kit.ast.factory.createSchema({
|
|
1197
|
+
type: "ref",
|
|
1198
|
+
name: resolve.call(resolver.param, node, params[0]),
|
|
1199
|
+
optional: !required
|
|
1200
|
+
}) : createNever()
|
|
1201
|
+
});
|
|
1202
|
+
})]
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* The schema a status occupies in the `<Name>Responses` record. A status that documents several
|
|
1207
|
+
* content types becomes a `{ contentType; data }` union so the runtime can surface the negotiated type
|
|
1208
|
+
* on `result.parsed`, while the standalone `<Name>StatusNNN` alias stays the plain body union that the
|
|
1209
|
+
* query hooks and `result.data` use.
|
|
1210
|
+
*/
|
|
1211
|
+
function buildResponseRecordEntry(node, res, resolver) {
|
|
1212
|
+
const statusName = resolver.response.status(node, res.statusCode);
|
|
1213
|
+
const variants = (res.content ?? []).filter((entry) => entry.schema);
|
|
1214
|
+
if (variants.length <= 1) return kubb_kit.ast.factory.createSchema({
|
|
1215
|
+
type: "ref",
|
|
1216
|
+
name: statusName
|
|
1217
|
+
});
|
|
1218
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1219
|
+
type: "union",
|
|
1220
|
+
members: resolveContentTypeVariants(variants, statusName).map((variant) => kubb_kit.ast.factory.createSchema({
|
|
1221
|
+
type: "object",
|
|
1222
|
+
primitive: "object",
|
|
1223
|
+
properties: [kubb_kit.ast.factory.createProperty({
|
|
1224
|
+
name: "contentType",
|
|
1225
|
+
required: true,
|
|
1226
|
+
schema: kubb_kit.ast.factory.createSchema({
|
|
1227
|
+
type: "enum",
|
|
1228
|
+
enumValues: [variant.contentType]
|
|
1229
|
+
})
|
|
1230
|
+
}), kubb_kit.ast.factory.createProperty({
|
|
1231
|
+
name: "data",
|
|
1232
|
+
required: true,
|
|
1233
|
+
schema: kubb_kit.ast.factory.createSchema({
|
|
1234
|
+
type: "ref",
|
|
1235
|
+
name: variant.name
|
|
1236
|
+
})
|
|
1237
|
+
})]
|
|
1238
|
+
}))
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Builds the per-status `<Name>Responses` record for an operation, referencing the already-resolved
|
|
1243
|
+
* `<Name>StatusNNN` names. Shared by `@kubb/plugin-ts`'s `Responses` type and `@kubb/plugin-zod`'s
|
|
1244
|
+
* inferred responses schema, so both emit the same shape from the same inputs.
|
|
1245
|
+
*
|
|
1246
|
+
* Always emits the keyed record, even when an operation declares no responses. An operation with no
|
|
1247
|
+
* responses renders as an empty object, which keeps every consumer's import (for example the axios
|
|
1248
|
+
* SDK's `RequestResult<XResponses>`) resolvable instead of pointing at a missing export.
|
|
1249
|
+
*/
|
|
1250
|
+
function buildResponses(node, resolver) {
|
|
1251
|
+
return kubb_kit.ast.factory.createSchema({
|
|
1252
|
+
type: "object",
|
|
1253
|
+
primitive: "object",
|
|
1254
|
+
properties: node.responses.map((res) => kubb_kit.ast.factory.createProperty({
|
|
1255
|
+
name: String(res.statusCode),
|
|
1256
|
+
required: true,
|
|
1257
|
+
schema: buildResponseRecordEntry(node, res, resolver)
|
|
1258
|
+
}))
|
|
1259
|
+
});
|
|
1030
1260
|
}
|
|
1031
1261
|
//#endregion
|
|
1032
1262
|
//#region ../../internals/shared/src/resolver.ts
|
|
@@ -1111,7 +1341,7 @@ function createCasedFile(caseLast) {
|
|
|
1111
1341
|
* `resolver.imports` would resolve file paths that are then discarded.
|
|
1112
1342
|
*/
|
|
1113
1343
|
function collectRefNames(schema) {
|
|
1114
|
-
return kubb_kit.ast.
|
|
1344
|
+
return kubb_kit.ast.collectSync(schema, { schema: (node) => {
|
|
1115
1345
|
const refNode = kubb_kit.ast.narrowSchema(node, "ref");
|
|
1116
1346
|
if (!refNode?.ref) return null;
|
|
1117
1347
|
return kubb_kit.ast.resolveRefName(refNode);
|
|
@@ -1178,223 +1408,6 @@ function mapSchemaItems(node, transform) {
|
|
|
1178
1408
|
}));
|
|
1179
1409
|
}
|
|
1180
1410
|
//#endregion
|
|
1181
|
-
//#region src/utils.ts
|
|
1182
|
-
/**
|
|
1183
|
-
* Tells whether a `const` (single-value enum) should render as a bare literal type (`'active'`)
|
|
1184
|
-
* rather than a named enum reference or a runtime enum declaration.
|
|
1185
|
-
*
|
|
1186
|
-
* The parser folds `const` into a single-value enum node. The adapter decides which schemas are
|
|
1187
|
-
* named enums and lists them in `enumSchemaNames`, and references to those names get suffixed (for
|
|
1188
|
-
* example `StatusKey`). A const renders as a literal only when the adapter has not registered it as
|
|
1189
|
-
* a named enum, which keeps the declaration and its references in sync across adapter versions.
|
|
1190
|
-
*/
|
|
1191
|
-
function isInlineConstEnum(node, enumSchemaNames) {
|
|
1192
|
-
return (node.namedEnumValues ?? node.enumValues ?? []).length === 1 && !(node.name && enumSchemaNames?.has(node.name));
|
|
1193
|
-
}
|
|
1194
|
-
/**
|
|
1195
|
-
* Collects JSDoc annotation strings for a schema node.
|
|
1196
|
-
*
|
|
1197
|
-
* Only uses official JSDoc tags from https://jsdoc.app/: `@description`, `@deprecated`, `@default`, `@example`, `@type`.
|
|
1198
|
-
* Constraint metadata (min/max length, pattern, multipleOf, min/maxProperties) is emitted as plain-text lines.
|
|
1199
|
-
|
|
1200
|
-
*/
|
|
1201
|
-
function isSchemaOptional(schema) {
|
|
1202
|
-
return Boolean("optional" in schema && schema.optional || "nullish" in schema && schema.nullish);
|
|
1203
|
-
}
|
|
1204
|
-
function formatExample(value) {
|
|
1205
|
-
if (value === null || typeof value !== "object") return String(value);
|
|
1206
|
-
return (JSON.stringify(value) ?? String(value)).replaceAll("*/", "*\\/");
|
|
1207
|
-
}
|
|
1208
|
-
function buildPropertyJSDocComments(schema, optional) {
|
|
1209
|
-
const meta = kubb_kit.ast.syncSchemaRef(schema);
|
|
1210
|
-
const isArray = meta?.primitive === "array";
|
|
1211
|
-
const hasDescription = meta && "description" in meta && meta.description;
|
|
1212
|
-
const formatComment = meta && "format" in meta && meta.format ? hasDescription ? [" ", `Format: \`${meta.format}\``] : ["@description", `Format: \`${meta.format}\``] : [];
|
|
1213
|
-
const exampleValues = meta?.examples ?? [];
|
|
1214
|
-
return [
|
|
1215
|
-
hasDescription ? `@description ${jsStringEscape(meta.description)}` : null,
|
|
1216
|
-
...formatComment,
|
|
1217
|
-
meta && "deprecated" in meta && meta.deprecated ? "@deprecated" : null,
|
|
1218
|
-
!isArray && meta && "min" in meta && meta.min !== void 0 ? `@minLength ${meta.min}` : null,
|
|
1219
|
-
!isArray && meta && "max" in meta && meta.max !== void 0 ? `@maxLength ${meta.max}` : null,
|
|
1220
|
-
meta && "pattern" in meta && meta.pattern ? `@pattern ${meta.pattern}` : null,
|
|
1221
|
-
meta && "default" in meta && meta.default !== void 0 ? `@default ${"primitive" in meta && meta.primitive === "string" && typeof meta.default === "string" ? stringify(meta.default) : meta.default}` : null,
|
|
1222
|
-
...exampleValues.map((example) => `@example ${formatExample(example)}`),
|
|
1223
|
-
meta && "primitive" in meta && meta.primitive ? [`@type ${meta.primitive}`, optional ?? isSchemaOptional(schema) ? " | undefined" : null].filter(Boolean).join("") : null
|
|
1224
|
-
].filter(Boolean);
|
|
1225
|
-
}
|
|
1226
|
-
/**
|
|
1227
|
-
* Builds the object schema for a group of parameters sharing one `in` location (path, query, or
|
|
1228
|
-
* header), embedding each param's own schema (and JSDoc) directly rather than referencing a
|
|
1229
|
-
* separate per-param type — the group itself is the only type these params get exported as.
|
|
1230
|
-
*/
|
|
1231
|
-
function buildParams({ params }) {
|
|
1232
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1233
|
-
type: "object",
|
|
1234
|
-
properties: params.map((param) => kubb_kit.ast.factory.createProperty({
|
|
1235
|
-
name: param.name,
|
|
1236
|
-
required: param.required,
|
|
1237
|
-
schema: kubb_kit.ast.factory.createSchema({
|
|
1238
|
-
...param.schema,
|
|
1239
|
-
optional: !param.required
|
|
1240
|
-
})
|
|
1241
|
-
}))
|
|
1242
|
-
});
|
|
1243
|
-
}
|
|
1244
|
-
function buildOptions(node, { resolver }) {
|
|
1245
|
-
const { path: pathParams, query: queryParams, header: headerParams } = getOperationParameters(node);
|
|
1246
|
-
const hasBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
1247
|
-
const hasRequiredPath = pathParams.some((param) => param.required);
|
|
1248
|
-
const hasRequiredQuery = queryParams.some((param) => param.required);
|
|
1249
|
-
const hasRequiredHeader = headerParams.some((param) => param.required);
|
|
1250
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1251
|
-
type: "object",
|
|
1252
|
-
deprecated: node.deprecated,
|
|
1253
|
-
properties: [
|
|
1254
|
-
kubb_kit.ast.factory.createProperty({
|
|
1255
|
-
name: "body",
|
|
1256
|
-
required: hasBody,
|
|
1257
|
-
schema: hasBody ? kubb_kit.ast.factory.createSchema({
|
|
1258
|
-
type: "ref",
|
|
1259
|
-
name: resolver.response.body(node)
|
|
1260
|
-
}) : kubb_kit.ast.factory.createSchema({
|
|
1261
|
-
type: "never",
|
|
1262
|
-
primitive: void 0,
|
|
1263
|
-
optional: true
|
|
1264
|
-
})
|
|
1265
|
-
}),
|
|
1266
|
-
kubb_kit.ast.factory.createProperty({
|
|
1267
|
-
name: "path",
|
|
1268
|
-
required: hasRequiredPath,
|
|
1269
|
-
schema: pathParams.length > 0 ? kubb_kit.ast.factory.createSchema({
|
|
1270
|
-
type: "ref",
|
|
1271
|
-
name: resolver.param.path(node, pathParams[0]),
|
|
1272
|
-
optional: !hasRequiredPath
|
|
1273
|
-
}) : kubb_kit.ast.factory.createSchema({
|
|
1274
|
-
type: "never",
|
|
1275
|
-
primitive: void 0,
|
|
1276
|
-
optional: true
|
|
1277
|
-
})
|
|
1278
|
-
}),
|
|
1279
|
-
kubb_kit.ast.factory.createProperty({
|
|
1280
|
-
name: "query",
|
|
1281
|
-
required: hasRequiredQuery,
|
|
1282
|
-
schema: queryParams.length > 0 ? kubb_kit.ast.factory.createSchema({
|
|
1283
|
-
type: "ref",
|
|
1284
|
-
name: resolver.param.query(node, queryParams[0]),
|
|
1285
|
-
optional: !hasRequiredQuery
|
|
1286
|
-
}) : kubb_kit.ast.factory.createSchema({
|
|
1287
|
-
type: "never",
|
|
1288
|
-
primitive: void 0,
|
|
1289
|
-
optional: true
|
|
1290
|
-
})
|
|
1291
|
-
}),
|
|
1292
|
-
kubb_kit.ast.factory.createProperty({
|
|
1293
|
-
name: "headers",
|
|
1294
|
-
required: hasRequiredHeader,
|
|
1295
|
-
schema: headerParams.length > 0 ? kubb_kit.ast.factory.createSchema({
|
|
1296
|
-
type: "ref",
|
|
1297
|
-
name: resolver.param.headers(node, headerParams[0]),
|
|
1298
|
-
optional: !hasRequiredHeader
|
|
1299
|
-
}) : kubb_kit.ast.factory.createSchema({
|
|
1300
|
-
type: "never",
|
|
1301
|
-
primitive: void 0,
|
|
1302
|
-
optional: true
|
|
1303
|
-
})
|
|
1304
|
-
})
|
|
1305
|
-
]
|
|
1306
|
-
});
|
|
1307
|
-
}
|
|
1308
|
-
/**
|
|
1309
|
-
* The schema a status occupies in the `<Name>Responses` record. A status that documents several
|
|
1310
|
-
* content types becomes a `{ contentType; data }` union so the runtime can surface the negotiated type
|
|
1311
|
-
* on `result.parsed`, while the standalone `<Name>StatusNNN` alias stays the plain body union that the
|
|
1312
|
-
* query hooks and `result.data` use.
|
|
1313
|
-
*/
|
|
1314
|
-
function buildResponseRecordEntry(node, res, resolver) {
|
|
1315
|
-
const statusName = resolver.response.status(node, res.statusCode);
|
|
1316
|
-
const variants = (res.content ?? []).filter((entry) => entry.schema);
|
|
1317
|
-
if (variants.length <= 1) return kubb_kit.ast.factory.createSchema({
|
|
1318
|
-
type: "ref",
|
|
1319
|
-
name: statusName
|
|
1320
|
-
});
|
|
1321
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1322
|
-
type: "union",
|
|
1323
|
-
members: resolveContentTypeVariants(variants, statusName).map((variant) => kubb_kit.ast.factory.createSchema({
|
|
1324
|
-
type: "object",
|
|
1325
|
-
properties: [kubb_kit.ast.factory.createProperty({
|
|
1326
|
-
name: "contentType",
|
|
1327
|
-
required: true,
|
|
1328
|
-
schema: kubb_kit.ast.factory.createSchema({
|
|
1329
|
-
type: "enum",
|
|
1330
|
-
enumValues: [variant.contentType]
|
|
1331
|
-
})
|
|
1332
|
-
}), kubb_kit.ast.factory.createProperty({
|
|
1333
|
-
name: "data",
|
|
1334
|
-
required: true,
|
|
1335
|
-
schema: kubb_kit.ast.factory.createSchema({
|
|
1336
|
-
type: "ref",
|
|
1337
|
-
name: variant.name
|
|
1338
|
-
})
|
|
1339
|
-
})]
|
|
1340
|
-
}))
|
|
1341
|
-
});
|
|
1342
|
-
}
|
|
1343
|
-
function buildResponses(node, { resolver }) {
|
|
1344
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1345
|
-
type: "object",
|
|
1346
|
-
properties: node.responses.map((res) => kubb_kit.ast.factory.createProperty({
|
|
1347
|
-
name: String(res.statusCode),
|
|
1348
|
-
required: true,
|
|
1349
|
-
schema: buildResponseRecordEntry(node, res, resolver)
|
|
1350
|
-
}))
|
|
1351
|
-
});
|
|
1352
|
-
}
|
|
1353
|
-
function buildResponseUnion(node, { resolver }) {
|
|
1354
|
-
const responsesWithSchema = node.responses.filter((res) => res.content?.some((entry) => entry.schema));
|
|
1355
|
-
if (responsesWithSchema.length === 0) return null;
|
|
1356
|
-
return kubb_kit.ast.factory.createSchema({
|
|
1357
|
-
type: "union",
|
|
1358
|
-
members: responsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
|
|
1359
|
-
type: "ref",
|
|
1360
|
-
name: resolver.response.status(node, res.statusCode)
|
|
1361
|
-
}))
|
|
1362
|
-
});
|
|
1363
|
-
}
|
|
1364
|
-
//#endregion
|
|
1365
|
-
//#region src/components/Type.tsx
|
|
1366
|
-
function Type({ name, node, printer, enum: enumOptions, resolver }) {
|
|
1367
|
-
const enumSchemaNodes = kubb_kit.ast.collect(node, { schema(n) {
|
|
1368
|
-
const enumNode = kubb_kit.ast.narrowSchema(n, kubb_kit.ast.schemaTypes.enum);
|
|
1369
|
-
if (enumNode?.name && !isInlineConstEnum(enumNode, printer.options.enumSchemaNames)) return enumNode;
|
|
1370
|
-
} });
|
|
1371
|
-
const output = printer.print(node);
|
|
1372
|
-
if (!output) return;
|
|
1373
|
-
const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
|
|
1374
|
-
return {
|
|
1375
|
-
node,
|
|
1376
|
-
...getEnumNames({
|
|
1377
|
-
node,
|
|
1378
|
-
enum: enumOptions,
|
|
1379
|
-
resolver
|
|
1380
|
-
})
|
|
1381
|
-
};
|
|
1382
|
-
});
|
|
1383
|
-
const shouldExportEnums = enumOptions.type !== "inlineLiteral";
|
|
1384
|
-
const shouldExportType = enumOptions.type === "inlineLiteral" || enums.every((item) => item.typeName !== name);
|
|
1385
|
-
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Enum, {
|
|
1386
|
-
node,
|
|
1387
|
-
enum: enumOptions,
|
|
1388
|
-
resolver
|
|
1389
|
-
}, node.name)), shouldExportType && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
1390
|
-
name,
|
|
1391
|
-
isTypeOnly: true,
|
|
1392
|
-
isExportable: true,
|
|
1393
|
-
isIndexable: true,
|
|
1394
|
-
children: output
|
|
1395
|
-
})] });
|
|
1396
|
-
}
|
|
1397
|
-
//#endregion
|
|
1398
1411
|
//#region src/printers/printerTs.ts
|
|
1399
1412
|
const { isNonNullable } = factory_exports;
|
|
1400
1413
|
/**
|
|
@@ -1469,11 +1482,11 @@ const printerTs = kubb_kit.ast.createPrinter((options) => {
|
|
|
1469
1482
|
const hasStringLiteral = members.some((m) => {
|
|
1470
1483
|
return kubb_kit.ast.narrowSchema(m, kubb_kit.ast.schemaTypes.enum)?.primitive === "string";
|
|
1471
1484
|
});
|
|
1472
|
-
const hasPlainString = members.some((m) => kubb_kit.
|
|
1485
|
+
const hasPlainString = members.some((m) => (0, kubb_kit.isStringType)(m));
|
|
1473
1486
|
if (hasStringLiteral && hasPlainString) return createUnionDeclaration({
|
|
1474
1487
|
withParentheses: true,
|
|
1475
1488
|
nodes: members.map((m) => {
|
|
1476
|
-
if (kubb_kit.
|
|
1489
|
+
if ((0, kubb_kit.isStringType)(m)) return createIntersectionDeclaration({
|
|
1477
1490
|
nodes: [keywordTypeNodes.string, createTypeLiteralNode([])],
|
|
1478
1491
|
withParentheses: true
|
|
1479
1492
|
});
|
|
@@ -1507,7 +1520,7 @@ const printerTs = kubb_kit.ast.createPrinter((options) => {
|
|
|
1507
1520
|
const baseType = output ?? keywordTypeNodes.unknown;
|
|
1508
1521
|
const optional = !property.required || !!property.schema.optional || !!property.schema.nullish;
|
|
1509
1522
|
const type = buildPropertyType(property.schema, baseType, options.optionalType, optional);
|
|
1510
|
-
const propMeta = kubb_kit.
|
|
1523
|
+
const propMeta = (0, kubb_kit.syncSchemaRef)(property.schema);
|
|
1511
1524
|
return appendJSDocToNode({
|
|
1512
1525
|
node: createPropertySignature({
|
|
1513
1526
|
questionToken: optional ? addsQuestionToken : false,
|
|
@@ -1528,7 +1541,7 @@ const printerTs = kubb_kit.ast.createPrinter((options) => {
|
|
|
1528
1541
|
const { name, syntaxType = "type", description, keysToOmit } = this.options;
|
|
1529
1542
|
const transformed = this.transform(node);
|
|
1530
1543
|
if (!transformed) return null;
|
|
1531
|
-
const meta = kubb_kit.
|
|
1544
|
+
const meta = (0, kubb_kit.syncSchemaRef)(node);
|
|
1532
1545
|
if (!name) {
|
|
1533
1546
|
const withNullable = meta.nullable ? createUnionDeclaration({ nodes: [transformed, keywordTypeNodes.null] }) : transformed;
|
|
1534
1547
|
const result = (meta.nullish || meta.optional) && addsUndefined ? createUnionDeclaration({ nodes: [withNullable, keywordTypeNodes.undefined] }) : withNullable;
|
|
@@ -1784,11 +1797,11 @@ const typeGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1784
1797
|
});
|
|
1785
1798
|
});
|
|
1786
1799
|
const optionsType = renderSchemaType({
|
|
1787
|
-
schema:
|
|
1800
|
+
schema: buildOptionsSchema(node, resolver),
|
|
1788
1801
|
name: resolver.response.options(node)
|
|
1789
1802
|
});
|
|
1790
1803
|
const responsesType = renderSchemaType({
|
|
1791
|
-
schema: buildResponses(node,
|
|
1804
|
+
schema: buildResponses(node, resolver),
|
|
1792
1805
|
name: resolver.response.responses(node)
|
|
1793
1806
|
});
|
|
1794
1807
|
function buildResponseType() {
|