@kubb/plugin-client 5.0.0-beta.22 → 5.0.0-beta.25
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/clients/fetch.cjs.map +1 -1
- package/dist/clients/fetch.d.ts +1 -1
- package/dist/clients/fetch.js.map +1 -1
- package/dist/index.cjs +135 -88
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +125 -65
- package/dist/index.js +135 -88
- package/dist/index.js.map +1 -1
- package/dist/templates/clients/fetch.source.cjs +1 -1
- package/dist/templates/clients/fetch.source.js +1 -1
- package/extension.yaml +774 -260
- package/package.json +6 -6
- package/src/clients/fetch.ts +1 -1
- package/src/components/ClassClient.tsx +3 -3
- package/src/components/Client.tsx +17 -17
- package/src/components/StaticClassClient.tsx +3 -3
- package/src/components/Url.tsx +1 -1
- package/src/functionParams.ts +8 -8
- package/src/generators/classClientGenerator.tsx +16 -11
- package/src/generators/clientGenerator.tsx +16 -8
- package/src/generators/groupedClientGenerator.tsx +9 -3
- package/src/generators/operationsGenerator.tsx +7 -1
- package/src/generators/staticClassClientGenerator.tsx +16 -10
- package/src/plugin.ts +24 -11
- package/src/resolvers/resolverClient.ts +10 -4
- package/src/types.ts +66 -53
- package/src/utils.ts +14 -14
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.cjs","names":[],"sources":["../../src/clients/fetch.ts"],"sourcesContent":["/**\n * RequestCredentials\n */\nexport type RequestCredentials = 'omit' | 'same-origin' | 'include'\n\n/**\n * Subset of FetchRequestConfig\n */\nexport type RequestConfig<TData = unknown> = {\n baseURL?: string\n url?: string\n method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'\n params?: unknown\n data?: TData | FormData\n responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'\n signal?: AbortSignal\n headers?: [string, string]
|
|
1
|
+
{"version":3,"file":"fetch.cjs","names":[],"sources":["../../src/clients/fetch.ts"],"sourcesContent":["/**\n * RequestCredentials\n */\nexport type RequestCredentials = 'omit' | 'same-origin' | 'include'\n\n/**\n * Subset of FetchRequestConfig\n */\nexport type RequestConfig<TData = unknown> = {\n baseURL?: string\n url?: string\n method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'\n params?: unknown\n data?: TData | FormData\n responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'\n signal?: AbortSignal\n headers?: Array<[string, string]> | Record<string, string>\n credentials?: RequestCredentials\n contentType?: string\n}\n\n/**\n * Subset of FetchResponse\n */\nexport type ResponseConfig<TData = unknown> = {\n data: TData\n status: number\n statusText: string\n headers: Headers\n}\n\nlet _config: Partial<RequestConfig> = {}\n\nexport const getConfig = () => _config\n\nexport const setConfig = (config: Partial<RequestConfig>) => {\n _config = config\n return getConfig()\n}\n\nexport const mergeConfig = <T extends RequestConfig>(...configs: Array<Partial<T>>): Partial<T> => {\n return configs.reduce<Partial<T>>((merged, config) => {\n return {\n ...merged,\n ...config,\n headers: {\n ...(Array.isArray(merged.headers) ? Object.fromEntries(merged.headers) : merged.headers),\n ...(Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers),\n },\n }\n }, {})\n}\n\nexport type ResponseErrorConfig<TError = unknown> = TError\n\nexport type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(\n config: RequestConfig<TRequestData>,\n request?: unknown,\n) => Promise<ResponseConfig<TResponseData>>\n\nexport const client = async <TResponseData, _TError = unknown, RequestData = unknown>(\n paramsConfig: RequestConfig<RequestData>,\n _request?: unknown,\n): Promise<ResponseConfig<TResponseData>> => {\n const normalizedParams = new URLSearchParams()\n\n const config = mergeConfig(getConfig(), paramsConfig)\n\n Object.entries(config.params || {}).forEach(([key, value]) => {\n if (value !== undefined) {\n normalizedParams.append(key, value === null ? 'null' : value.toString())\n }\n })\n\n let targetUrl = [config.baseURL, config.url].filter(Boolean).join('')\n\n if (config.params) {\n targetUrl += `?${normalizedParams}`\n }\n\n const response = await fetch(targetUrl, {\n credentials: config.credentials || 'same-origin',\n method: config.method?.toUpperCase(),\n body: config.data instanceof FormData ? config.data : JSON.stringify(config.data),\n signal: config.signal,\n headers: {\n ...(config.contentType && config.contentType !== 'multipart/form-data' ? { 'Content-Type': config.contentType } : {}),\n ...(Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers),\n },\n })\n\n const data = [204, 205, 304].includes(response.status) || !response.body ? {} : await response.json()\n\n return {\n data: data as TResponseData,\n status: response.status,\n statusText: response.statusText,\n headers: response.headers as Headers,\n }\n}\n\nclient.getConfig = getConfig\nclient.setConfig = setConfig\n\nexport default client\n"],"mappings":";;;;;;AA+BA,IAAI,UAAkC,EAAE;AAExC,MAAa,kBAAkB;AAE/B,MAAa,aAAa,WAAmC;CAC3D,UAAU;CACV,OAAO,WAAW;;AAGpB,MAAa,eAAwC,GAAG,YAA2C;CACjG,OAAO,QAAQ,QAAoB,QAAQ,WAAW;EACpD,OAAO;GACL,GAAG;GACH,GAAG;GACH,SAAS;IACP,GAAI,MAAM,QAAQ,OAAO,QAAQ,GAAG,OAAO,YAAY,OAAO,QAAQ,GAAG,OAAO;IAChF,GAAI,MAAM,QAAQ,OAAO,QAAQ,GAAG,OAAO,YAAY,OAAO,QAAQ,GAAG,OAAO;IACjF;GACF;IACA,EAAE,CAAC;;AAUR,MAAa,SAAS,OACpB,cACA,aAC2C;CAC3C,MAAM,mBAAmB,IAAI,iBAAiB;CAE9C,MAAM,SAAS,YAAY,WAAW,EAAE,aAAa;CAErD,OAAO,QAAQ,OAAO,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC,KAAK,WAAW;EAC5D,IAAI,UAAU,KAAA,GACZ,iBAAiB,OAAO,KAAK,UAAU,OAAO,SAAS,MAAM,UAAU,CAAC;GAE1E;CAEF,IAAI,YAAY,CAAC,OAAO,SAAS,OAAO,IAAI,CAAC,OAAO,QAAQ,CAAC,KAAK,GAAG;CAErE,IAAI,OAAO,QACT,aAAa,IAAI;CAGnB,MAAM,WAAW,MAAM,MAAM,WAAW;EACtC,aAAa,OAAO,eAAe;EACnC,QAAQ,OAAO,QAAQ,aAAa;EACpC,MAAM,OAAO,gBAAgB,WAAW,OAAO,OAAO,KAAK,UAAU,OAAO,KAAK;EACjF,QAAQ,OAAO;EACf,SAAS;GACP,GAAI,OAAO,eAAe,OAAO,gBAAgB,wBAAwB,EAAE,gBAAgB,OAAO,aAAa,GAAG,EAAE;GACpH,GAAI,MAAM,QAAQ,OAAO,QAAQ,GAAG,OAAO,YAAY,OAAO,QAAQ,GAAG,OAAO;GACjF;EACF,CAAC;CAIF,OAAO;EACL,MAHW;GAAC;GAAK;GAAK;GAAI,CAAC,SAAS,SAAS,OAAO,IAAI,CAAC,SAAS,OAAO,EAAE,GAAG,MAAM,SAAS,MAAM;EAInG,QAAQ,SAAS;EACjB,YAAY,SAAS;EACrB,SAAS,SAAS;EACnB;;AAGH,OAAO,YAAY;AACnB,OAAO,YAAY"}
|
package/dist/clients/fetch.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ type RequestConfig<TData = unknown> = {
|
|
|
16
16
|
data?: TData | FormData;
|
|
17
17
|
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream';
|
|
18
18
|
signal?: AbortSignal;
|
|
19
|
-
headers?: [string, string]
|
|
19
|
+
headers?: Array<[string, string]> | Record<string, string>;
|
|
20
20
|
credentials?: RequestCredentials;
|
|
21
21
|
contentType?: string;
|
|
22
22
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.js","names":[],"sources":["../../src/clients/fetch.ts"],"sourcesContent":["/**\n * RequestCredentials\n */\nexport type RequestCredentials = 'omit' | 'same-origin' | 'include'\n\n/**\n * Subset of FetchRequestConfig\n */\nexport type RequestConfig<TData = unknown> = {\n baseURL?: string\n url?: string\n method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'\n params?: unknown\n data?: TData | FormData\n responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'\n signal?: AbortSignal\n headers?: [string, string]
|
|
1
|
+
{"version":3,"file":"fetch.js","names":[],"sources":["../../src/clients/fetch.ts"],"sourcesContent":["/**\n * RequestCredentials\n */\nexport type RequestCredentials = 'omit' | 'same-origin' | 'include'\n\n/**\n * Subset of FetchRequestConfig\n */\nexport type RequestConfig<TData = unknown> = {\n baseURL?: string\n url?: string\n method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'\n params?: unknown\n data?: TData | FormData\n responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'\n signal?: AbortSignal\n headers?: Array<[string, string]> | Record<string, string>\n credentials?: RequestCredentials\n contentType?: string\n}\n\n/**\n * Subset of FetchResponse\n */\nexport type ResponseConfig<TData = unknown> = {\n data: TData\n status: number\n statusText: string\n headers: Headers\n}\n\nlet _config: Partial<RequestConfig> = {}\n\nexport const getConfig = () => _config\n\nexport const setConfig = (config: Partial<RequestConfig>) => {\n _config = config\n return getConfig()\n}\n\nexport const mergeConfig = <T extends RequestConfig>(...configs: Array<Partial<T>>): Partial<T> => {\n return configs.reduce<Partial<T>>((merged, config) => {\n return {\n ...merged,\n ...config,\n headers: {\n ...(Array.isArray(merged.headers) ? Object.fromEntries(merged.headers) : merged.headers),\n ...(Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers),\n },\n }\n }, {})\n}\n\nexport type ResponseErrorConfig<TError = unknown> = TError\n\nexport type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(\n config: RequestConfig<TRequestData>,\n request?: unknown,\n) => Promise<ResponseConfig<TResponseData>>\n\nexport const client = async <TResponseData, _TError = unknown, RequestData = unknown>(\n paramsConfig: RequestConfig<RequestData>,\n _request?: unknown,\n): Promise<ResponseConfig<TResponseData>> => {\n const normalizedParams = new URLSearchParams()\n\n const config = mergeConfig(getConfig(), paramsConfig)\n\n Object.entries(config.params || {}).forEach(([key, value]) => {\n if (value !== undefined) {\n normalizedParams.append(key, value === null ? 'null' : value.toString())\n }\n })\n\n let targetUrl = [config.baseURL, config.url].filter(Boolean).join('')\n\n if (config.params) {\n targetUrl += `?${normalizedParams}`\n }\n\n const response = await fetch(targetUrl, {\n credentials: config.credentials || 'same-origin',\n method: config.method?.toUpperCase(),\n body: config.data instanceof FormData ? config.data : JSON.stringify(config.data),\n signal: config.signal,\n headers: {\n ...(config.contentType && config.contentType !== 'multipart/form-data' ? { 'Content-Type': config.contentType } : {}),\n ...(Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers),\n },\n })\n\n const data = [204, 205, 304].includes(response.status) || !response.body ? {} : await response.json()\n\n return {\n data: data as TResponseData,\n status: response.status,\n statusText: response.statusText,\n headers: response.headers as Headers,\n }\n}\n\nclient.getConfig = getConfig\nclient.setConfig = setConfig\n\nexport default client\n"],"mappings":";;AA+BA,IAAI,UAAkC,EAAE;AAExC,MAAa,kBAAkB;AAE/B,MAAa,aAAa,WAAmC;CAC3D,UAAU;CACV,OAAO,WAAW;;AAGpB,MAAa,eAAwC,GAAG,YAA2C;CACjG,OAAO,QAAQ,QAAoB,QAAQ,WAAW;EACpD,OAAO;GACL,GAAG;GACH,GAAG;GACH,SAAS;IACP,GAAI,MAAM,QAAQ,OAAO,QAAQ,GAAG,OAAO,YAAY,OAAO,QAAQ,GAAG,OAAO;IAChF,GAAI,MAAM,QAAQ,OAAO,QAAQ,GAAG,OAAO,YAAY,OAAO,QAAQ,GAAG,OAAO;IACjF;GACF;IACA,EAAE,CAAC;;AAUR,MAAa,SAAS,OACpB,cACA,aAC2C;CAC3C,MAAM,mBAAmB,IAAI,iBAAiB;CAE9C,MAAM,SAAS,YAAY,WAAW,EAAE,aAAa;CAErD,OAAO,QAAQ,OAAO,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC,KAAK,WAAW;EAC5D,IAAI,UAAU,KAAA,GACZ,iBAAiB,OAAO,KAAK,UAAU,OAAO,SAAS,MAAM,UAAU,CAAC;GAE1E;CAEF,IAAI,YAAY,CAAC,OAAO,SAAS,OAAO,IAAI,CAAC,OAAO,QAAQ,CAAC,KAAK,GAAG;CAErE,IAAI,OAAO,QACT,aAAa,IAAI;CAGnB,MAAM,WAAW,MAAM,MAAM,WAAW;EACtC,aAAa,OAAO,eAAe;EACnC,QAAQ,OAAO,QAAQ,aAAa;EACpC,MAAM,OAAO,gBAAgB,WAAW,OAAO,OAAO,KAAK,UAAU,OAAO,KAAK;EACjF,QAAQ,OAAO;EACf,SAAS;GACP,GAAI,OAAO,eAAe,OAAO,gBAAgB,wBAAwB,EAAE,gBAAgB,OAAO,aAAa,GAAG,EAAE;GACpH,GAAI,MAAM,QAAQ,OAAO,QAAQ,GAAG,OAAO,YAAY,OAAO,QAAQ,GAAG,OAAO;GACjF;EACF,CAAC;CAIF,OAAO;EACL,MAHW;GAAC;GAAK;GAAK;GAAI,CAAC,SAAS,SAAS,OAAO,IAAI,CAAC,SAAS,OAAO,EAAE,GAAG,MAAM,SAAS,MAAM;EAInG,QAAQ,SAAS;EACjB,YAAY,SAAS;EACrB,SAAS,SAAS;EACnB;;AAGH,OAAO,YAAY;AACnB,OAAO,YAAY"}
|
package/dist/index.cjs
CHANGED
|
@@ -257,12 +257,12 @@ var URLPath = class {
|
|
|
257
257
|
get object() {
|
|
258
258
|
return this.toObject();
|
|
259
259
|
}
|
|
260
|
-
/** Returns a map of path parameter names, or `
|
|
260
|
+
/** Returns a map of path parameter names, or `null` when the path has no parameters.
|
|
261
261
|
*
|
|
262
262
|
* @example
|
|
263
263
|
* ```ts
|
|
264
264
|
* new URLPath('/pet/{petId}').params // { petId: 'petId' }
|
|
265
|
-
* new URLPath('/pet').params //
|
|
265
|
+
* new URLPath('/pet').params // null
|
|
266
266
|
* ```
|
|
267
267
|
*/
|
|
268
268
|
get params() {
|
|
@@ -325,7 +325,7 @@ var URLPath = class {
|
|
|
325
325
|
const key = replacer ? replacer(param) : param;
|
|
326
326
|
params[key] = key;
|
|
327
327
|
});
|
|
328
|
-
return Object.keys(params).length > 0 ? params :
|
|
328
|
+
return Object.keys(params).length > 0 ? params : null;
|
|
329
329
|
}
|
|
330
330
|
/** Converts the OpenAPI path to Express-style colon syntax.
|
|
331
331
|
*
|
|
@@ -341,9 +341,9 @@ var URLPath = class {
|
|
|
341
341
|
//#endregion
|
|
342
342
|
//#region ../../internals/shared/src/operation.ts
|
|
343
343
|
function getOperationLink(node, link) {
|
|
344
|
-
if (!link) return;
|
|
345
|
-
if (typeof link === "function") return link(node);
|
|
346
|
-
if (link === "urlPath") return node.path ? `{@link ${new URLPath(node.path).URL}}` :
|
|
344
|
+
if (!link) return null;
|
|
345
|
+
if (typeof link === "function") return link(node) ?? null;
|
|
346
|
+
if (link === "urlPath") return node.path ? `{@link ${new URLPath(node.path).URL}}` : null;
|
|
347
347
|
return `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}`;
|
|
348
348
|
}
|
|
349
349
|
function getContentTypeInfo(node) {
|
|
@@ -358,9 +358,9 @@ function getContentTypeInfo(node) {
|
|
|
358
358
|
};
|
|
359
359
|
}
|
|
360
360
|
function buildRequestConfigType(node, resolver) {
|
|
361
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) :
|
|
361
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null;
|
|
362
362
|
const { isMultipleContentTypes, contentTypeUnion } = getContentTypeInfo(node);
|
|
363
|
-
return `${requestName ? `Partial<RequestConfig<${requestName}>>` : "Partial<RequestConfig>"} & { ${["client?: Client", isMultipleContentTypes ? `contentType?: ${contentTypeUnion}` :
|
|
363
|
+
return `${requestName ? `Partial<RequestConfig<${requestName}>>` : "Partial<RequestConfig>"} & { ${["client?: Client", isMultipleContentTypes ? `contentType?: ${contentTypeUnion}` : null].filter(Boolean).join("; ")} }`;
|
|
364
364
|
}
|
|
365
365
|
function buildOperationComments(node, options = {}) {
|
|
366
366
|
const { link = "pathTemplate", linkPosition = "afterDeprecated", splitLines = false } = options;
|
|
@@ -390,11 +390,11 @@ function getOperationParameters(node, options = {}) {
|
|
|
390
390
|
}
|
|
391
391
|
function getStatusCodeNumber(statusCode) {
|
|
392
392
|
const code = Number(statusCode);
|
|
393
|
-
return Number.isNaN(code) ?
|
|
393
|
+
return Number.isNaN(code) ? null : code;
|
|
394
394
|
}
|
|
395
395
|
function isErrorStatusCode(statusCode) {
|
|
396
396
|
const code = getStatusCodeNumber(statusCode);
|
|
397
|
-
return code !==
|
|
397
|
+
return code !== null && code >= 400;
|
|
398
398
|
}
|
|
399
399
|
function resolveErrorNames(node, resolver) {
|
|
400
400
|
return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
@@ -421,7 +421,7 @@ function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
|
421
421
|
...query.map((param) => resolver.resolveQueryParamsName(node, param)),
|
|
422
422
|
...header.map((param) => resolver.resolveHeaderParamsName(node, param))
|
|
423
423
|
];
|
|
424
|
-
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) :
|
|
424
|
+
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null, resolver.resolveResponseName(node)];
|
|
425
425
|
const result = (options.order === "body-response-first" ? [
|
|
426
426
|
...bodyAndResponseNames,
|
|
427
427
|
...paramNames,
|
|
@@ -444,7 +444,7 @@ function buildParamsMapping(originalParams, mappedParams) {
|
|
|
444
444
|
mapping[param.name] = mappedName;
|
|
445
445
|
if (param.name !== mappedName) hasChanged = true;
|
|
446
446
|
});
|
|
447
|
-
return hasChanged ? mapping :
|
|
447
|
+
return hasChanged ? mapping : null;
|
|
448
448
|
}
|
|
449
449
|
//#endregion
|
|
450
450
|
//#region src/functionParams.ts
|
|
@@ -457,18 +457,18 @@ function createType(type) {
|
|
|
457
457
|
return type ? _kubb_core.ast.createParamsType({
|
|
458
458
|
variant: "reference",
|
|
459
459
|
name: type
|
|
460
|
-
}) :
|
|
460
|
+
}) : null;
|
|
461
461
|
}
|
|
462
462
|
function createDeclarationLeaf(name, spec) {
|
|
463
463
|
if (spec.default !== void 0) return _kubb_core.ast.createFunctionParameter({
|
|
464
464
|
name,
|
|
465
|
-
type: createType(spec.type),
|
|
465
|
+
type: createType(spec.type) ?? void 0,
|
|
466
466
|
default: spec.default,
|
|
467
467
|
rest: spec.mode === "inlineSpread"
|
|
468
468
|
});
|
|
469
469
|
return _kubb_core.ast.createFunctionParameter({
|
|
470
470
|
name,
|
|
471
|
-
type: createType(spec.type),
|
|
471
|
+
type: createType(spec.type) ?? void 0,
|
|
472
472
|
optional: !!spec.optional,
|
|
473
473
|
rest: spec.mode === "inlineSpread"
|
|
474
474
|
});
|
|
@@ -477,14 +477,14 @@ function createDeclarationParam(name, spec) {
|
|
|
477
477
|
if (isGroup(spec)) return _kubb_core.ast.createParameterGroup({
|
|
478
478
|
inline: spec.mode === "inlineSpread",
|
|
479
479
|
default: spec.default,
|
|
480
|
-
properties: Object.entries(spec.children).filter(([, child]) => child
|
|
480
|
+
properties: Object.entries(spec.children).filter(([, child]) => child != null).map(([childName, child]) => createDeclarationLeaf(childName, child))
|
|
481
481
|
});
|
|
482
482
|
return createDeclarationLeaf(name, spec);
|
|
483
483
|
}
|
|
484
484
|
function createCallParam(name, spec) {
|
|
485
485
|
if (isGroup(spec)) return _kubb_core.ast.createParameterGroup({
|
|
486
486
|
inline: spec.mode === "inlineSpread",
|
|
487
|
-
properties: Object.entries(spec.children).filter(([, child]) => child
|
|
487
|
+
properties: Object.entries(spec.children).filter(([, child]) => child != null).map(([childName, child]) => _kubb_core.ast.createFunctionParameter({
|
|
488
488
|
name: child?.mode === "inlineSpread" ? spec.mode === "inlineSpread" ? child.value ?? childName : `...${child.value ?? childName}` : child?.value ? `${childName}: ${child.value}` : childName,
|
|
489
489
|
rest: spec.mode === "inlineSpread" && child?.mode === "inlineSpread"
|
|
490
490
|
}))
|
|
@@ -499,7 +499,7 @@ function createCallParam(name, spec) {
|
|
|
499
499
|
* Returns utilities to output constructor signatures (`toConstructor()`) or call expressions (`toCall()`).
|
|
500
500
|
*/
|
|
501
501
|
function createFunctionParams(params) {
|
|
502
|
-
const entries = Object.entries(params).filter(([, spec]) => spec
|
|
502
|
+
const entries = Object.entries(params).filter(([, spec]) => spec != null);
|
|
503
503
|
return {
|
|
504
504
|
toConstructor() {
|
|
505
505
|
return declarationPrinter$4.print(_kubb_core.ast.createFunctionParameters({ params: entries.map(([name, spec]) => createDeclarationParam(name, spec)) })) ?? "";
|
|
@@ -537,7 +537,7 @@ function Url({ name, isExportable = true, isIndexable = true, baseURL, paramsTyp
|
|
|
537
537
|
const paramsSignature = declarationPrinter$3.print(paramsNode) ?? "";
|
|
538
538
|
const { path: originalPathParams } = getOperationParameters(node);
|
|
539
539
|
const { path: casedPathParams } = getOperationParameters(node, { paramsCasing });
|
|
540
|
-
const pathParamsMapping = paramsCasing ? buildParamsMapping(originalPathParams, casedPathParams) :
|
|
540
|
+
const pathParamsMapping = paramsCasing ? buildParamsMapping(originalPathParams, casedPathParams) : null;
|
|
541
541
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
542
542
|
name,
|
|
543
543
|
isExportable,
|
|
@@ -584,19 +584,19 @@ function Client({ name, isExportable = true, isIndexable = true, returnType, bas
|
|
|
584
584
|
const isFormData = !isMultipleContentTypes && contentType === "multipart/form-data";
|
|
585
585
|
const { path: originalPathParams, query: originalQueryParams, header: originalHeaderParams } = getOperationParameters(node);
|
|
586
586
|
const { path: casedPathParams, query: casedQueryParams, header: casedHeaderParams } = getOperationParameters(node, { paramsCasing });
|
|
587
|
-
const pathParamsMapping = paramsCasing && !urlName ? buildParamsMapping(originalPathParams, casedPathParams) :
|
|
588
|
-
const queryParamsMapping = paramsCasing ? buildParamsMapping(originalQueryParams, casedQueryParams) :
|
|
589
|
-
const headerParamsMapping = paramsCasing ? buildParamsMapping(originalHeaderParams, casedHeaderParams) :
|
|
590
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) :
|
|
587
|
+
const pathParamsMapping = paramsCasing && !urlName ? buildParamsMapping(originalPathParams, casedPathParams) : null;
|
|
588
|
+
const queryParamsMapping = paramsCasing ? buildParamsMapping(originalQueryParams, casedQueryParams) : null;
|
|
589
|
+
const headerParamsMapping = paramsCasing ? buildParamsMapping(originalHeaderParams, casedHeaderParams) : null;
|
|
590
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null;
|
|
591
591
|
const responseName = tsResolver.resolveResponseName(node);
|
|
592
|
-
const queryParamsName = originalQueryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, originalQueryParams[0]) :
|
|
593
|
-
const headerParamsName = originalHeaderParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, originalHeaderParams[0]) :
|
|
594
|
-
const zodResponseName = zodResolver && parser === "zod" ? zodResolver.resolveResponseName?.(node) :
|
|
595
|
-
const zodRequestName = zodResolver && parser === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) :
|
|
592
|
+
const queryParamsName = originalQueryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, originalQueryParams[0]) : null;
|
|
593
|
+
const headerParamsName = originalHeaderParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, originalHeaderParams[0]) : null;
|
|
594
|
+
const zodResponseName = zodResolver && parser === "zod" ? zodResolver.resolveResponseName?.(node) : null;
|
|
595
|
+
const zodRequestName = zodResolver && parser === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null;
|
|
596
596
|
const errorNames = node.responses.filter((r) => {
|
|
597
597
|
return Number.parseInt(r.statusCode, 10) >= 400;
|
|
598
598
|
}).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode));
|
|
599
|
-
const headers = [!isMultipleContentTypes && contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` :
|
|
599
|
+
const headers = [!isMultipleContentTypes && contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` : null, headerParamsName ? headerParamsMapping ? "...mappedHeaders" : "...headers" : null].filter(Boolean);
|
|
600
600
|
const generics = [
|
|
601
601
|
responseName,
|
|
602
602
|
`ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`,
|
|
@@ -624,12 +624,12 @@ function Client({ name, isExportable = true, isIndexable = true, returnType, bas
|
|
|
624
624
|
children: {
|
|
625
625
|
method: { value: JSON.stringify(node.method.toUpperCase()) },
|
|
626
626
|
url: { value: urlName ? `${urlName}(${urlParamsCall}).url.toString()` : path.template },
|
|
627
|
-
baseURL: baseURL && !urlName ? { value: `\`${baseURL}\`` } :
|
|
628
|
-
params: queryParamsName ? queryParamsMapping ? { value: "mappedParams" } : {} :
|
|
629
|
-
data: requestName ? { value: isMultipleContentTypes && hasFormData ? "contentType === 'multipart/form-data' ? formData as FormData : requestData" : isFormData ? "formData as FormData" : "requestData" } :
|
|
630
|
-
contentType: isConfigurable && isMultipleContentTypes ? {} :
|
|
631
|
-
requestConfig: isConfigurable ? { mode: "inlineSpread" } :
|
|
632
|
-
headers: headers.length ? { value: isConfigurable ? `{ ${headers.join(", ")}, ...requestConfig.headers }` : `{ ${headers.join(", ")} }` } :
|
|
627
|
+
baseURL: baseURL && !urlName ? { value: `\`${baseURL}\`` } : null,
|
|
628
|
+
params: queryParamsName ? queryParamsMapping ? { value: "mappedParams" } : {} : null,
|
|
629
|
+
data: requestName ? { value: isMultipleContentTypes && hasFormData ? "contentType === 'multipart/form-data' ? formData as FormData : requestData" : isFormData ? "formData as FormData" : "requestData" } : null,
|
|
630
|
+
contentType: isConfigurable && isMultipleContentTypes ? {} : null,
|
|
631
|
+
requestConfig: isConfigurable ? { mode: "inlineSpread" } : null,
|
|
632
|
+
headers: headers.length ? { value: isConfigurable ? `{ ${headers.join(", ")}, ...requestConfig.headers }` : `{ ${headers.join(", ")} }` } : null
|
|
633
633
|
}
|
|
634
634
|
} });
|
|
635
635
|
const childrenElement = children ? children : /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
@@ -687,7 +687,7 @@ function Client({ name, isExportable = true, isIndexable = true, returnType, bas
|
|
|
687
687
|
* Includes Content-Type (if not default) and spreads header parameters if present.
|
|
688
688
|
*/
|
|
689
689
|
function buildHeaders(contentType, hasHeaderParams) {
|
|
690
|
-
return [contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` :
|
|
690
|
+
return [contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` : null, hasHeaderParams ? "...headers" : null].filter(Boolean);
|
|
691
691
|
}
|
|
692
692
|
/**
|
|
693
693
|
* Builds TypeScript generic parameters for a client method.
|
|
@@ -695,7 +695,7 @@ function buildHeaders(contentType, hasHeaderParams) {
|
|
|
695
695
|
*/
|
|
696
696
|
function buildGenerics(node, tsResolver) {
|
|
697
697
|
const responseName = tsResolver.resolveResponseName(node);
|
|
698
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) :
|
|
698
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null;
|
|
699
699
|
const errorNames = node.responses.filter((r) => Number.parseInt(r.statusCode, 10) >= 400).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode));
|
|
700
700
|
return [
|
|
701
701
|
responseName,
|
|
@@ -709,19 +709,19 @@ function buildGenerics(node, tsResolver) {
|
|
|
709
709
|
*/
|
|
710
710
|
function buildClassClientParams({ node, path, baseURL, tsResolver, isFormData, isMultipleContentTypes, hasFormData, headers }) {
|
|
711
711
|
const { query: queryParams } = getOperationParameters(node);
|
|
712
|
-
const queryParamsName = queryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, queryParams[0]) :
|
|
713
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) :
|
|
712
|
+
const queryParamsName = queryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, queryParams[0]) : null;
|
|
713
|
+
const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null;
|
|
714
714
|
return createFunctionParams({ config: {
|
|
715
715
|
mode: "object",
|
|
716
716
|
children: {
|
|
717
717
|
requestConfig: { mode: "inlineSpread" },
|
|
718
718
|
method: { value: JSON.stringify(node.method.toUpperCase()) },
|
|
719
719
|
url: { value: path.template },
|
|
720
|
-
baseURL: baseURL ? { value: JSON.stringify(baseURL) } :
|
|
721
|
-
params: queryParamsName ? {} :
|
|
722
|
-
data: requestName ? { value: isMultipleContentTypes && hasFormData ? "contentType === 'multipart/form-data' ? formData as FormData : requestData" : isFormData ? "formData as FormData" : "requestData" } :
|
|
723
|
-
contentType: isMultipleContentTypes ? {} :
|
|
724
|
-
headers: headers.length ? { value: `{ ${headers.join(", ")}, ...requestConfig.headers }` } :
|
|
720
|
+
baseURL: baseURL ? { value: JSON.stringify(baseURL) } : null,
|
|
721
|
+
params: queryParamsName ? {} : null,
|
|
722
|
+
data: requestName ? { value: isMultipleContentTypes && hasFormData ? "contentType === 'multipart/form-data' ? formData as FormData : requestData" : isFormData ? "formData as FormData" : "requestData" } : null,
|
|
723
|
+
contentType: isMultipleContentTypes ? {} : null,
|
|
724
|
+
headers: headers.length ? { value: `{ ${headers.join(", ")}, ...requestConfig.headers }` } : null
|
|
725
725
|
}
|
|
726
726
|
} });
|
|
727
727
|
}
|
|
@@ -730,7 +730,7 @@ function buildClassClientParams({ node, path, baseURL, tsResolver, isFormData, i
|
|
|
730
730
|
* Applies Zod validation if configured, otherwise uses data directly.
|
|
731
731
|
*/
|
|
732
732
|
function buildRequestDataLine({ parser, node, zodResolver }) {
|
|
733
|
-
const zodRequestName = zodResolver && parser === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) :
|
|
733
|
+
const zodRequestName = zodResolver && parser === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null;
|
|
734
734
|
if (parser === "zod" && zodRequestName) return `const requestData = ${zodRequestName}.parse(data)`;
|
|
735
735
|
if (node.requestBody?.content?.[0]?.schema) return "const requestData = data";
|
|
736
736
|
return "";
|
|
@@ -747,7 +747,7 @@ function buildFormDataLine(isFormData, hasRequest) {
|
|
|
747
747
|
* Applies Zod validation to response data if configured, otherwise returns raw response.
|
|
748
748
|
*/
|
|
749
749
|
function buildReturnStatement({ dataReturnType, parser, node, zodResolver }) {
|
|
750
|
-
const zodResponseName = zodResolver && parser === "zod" ? zodResolver.resolveResponseName?.(node) :
|
|
750
|
+
const zodResponseName = zodResolver && parser === "zod" ? zodResolver.resolveResponseName?.(node) : null;
|
|
751
751
|
if (dataReturnType === "full" && parser === "zod" && zodResponseName) return `return {...res, data: ${zodResponseName}.parse(res.data)}`;
|
|
752
752
|
if (dataReturnType === "data" && parser === "zod" && zodResponseName) return `return ${zodResponseName}.parse(res.data)`;
|
|
753
753
|
if (dataReturnType === "full" && parser === "client") return "return res";
|
|
@@ -761,7 +761,7 @@ function generateMethod$1({ node, name, tsResolver, zodResolver, baseURL, dataRe
|
|
|
761
761
|
const { defaultContentType: contentType, isMultipleContentTypes, hasFormData } = getContentTypeInfo(node);
|
|
762
762
|
const isFormData = !isMultipleContentTypes && contentType === "multipart/form-data";
|
|
763
763
|
const { header: headerParams } = getOperationParameters(node);
|
|
764
|
-
const headerParamsName = headerParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, headerParams[0]) :
|
|
764
|
+
const headerParamsName = headerParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, headerParams[0]) : null;
|
|
765
765
|
const headers = isMultipleContentTypes ? headerParamsName ? ["...headers"] : [] : buildHeaders(contentType, !!headerParamsName);
|
|
766
766
|
const generics = buildGenerics(node, tsResolver);
|
|
767
767
|
const paramsNode = buildClientParamsNode({
|
|
@@ -862,9 +862,14 @@ function resolveTypeImportNames$1(node, tsResolver) {
|
|
|
862
862
|
}
|
|
863
863
|
require_chunk.__name(resolveTypeImportNames$1, "resolveTypeImportNames");
|
|
864
864
|
function resolveZodImportNames$1(node, zodResolver) {
|
|
865
|
-
return [zodResolver.resolveResponseName?.(node), node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) :
|
|
865
|
+
return [zodResolver.resolveResponseName?.(node), node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null].filter((n) => Boolean(n));
|
|
866
866
|
}
|
|
867
867
|
require_chunk.__name(resolveZodImportNames$1, "resolveZodImportNames");
|
|
868
|
+
/**
|
|
869
|
+
* Built-in `operations` generator for `@kubb/plugin-client` when
|
|
870
|
+
* `clientType: 'class'`. Emits one class per tag, with one instance method
|
|
871
|
+
* per operation and a shared constructor for request configuration.
|
|
872
|
+
*/
|
|
868
873
|
const classClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
869
874
|
name: "classClient",
|
|
870
875
|
renderer: _kubb_renderer_jsx.jsxRendererSync,
|
|
@@ -876,8 +881,8 @@ const classClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
876
881
|
if (!pluginTs) return null;
|
|
877
882
|
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
878
883
|
const tsPluginOptions = pluginTs.options;
|
|
879
|
-
const pluginZod = parser === "zod" ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) :
|
|
880
|
-
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) :
|
|
884
|
+
const pluginZod = parser === "zod" ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
885
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
881
886
|
function buildOperationData(node) {
|
|
882
887
|
const typeFile = tsResolver.resolveFile({
|
|
883
888
|
name: node.operationId,
|
|
@@ -897,8 +902,8 @@ const classClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
897
902
|
}, {
|
|
898
903
|
root,
|
|
899
904
|
output: pluginZod.options?.output ?? output,
|
|
900
|
-
group: pluginZod.options?.group
|
|
901
|
-
}) :
|
|
905
|
+
group: pluginZod.options?.group ?? void 0
|
|
906
|
+
}) : null;
|
|
902
907
|
return {
|
|
903
908
|
node,
|
|
904
909
|
name: resolver.resolveName(node.operationId),
|
|
@@ -919,7 +924,7 @@ const classClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
919
924
|
}, {
|
|
920
925
|
root,
|
|
921
926
|
output,
|
|
922
|
-
group
|
|
927
|
+
group: group ?? void 0
|
|
923
928
|
});
|
|
924
929
|
const operationData = buildOperationData(operationNode);
|
|
925
930
|
const previous = acc.find((item) => item.file.path === file.path);
|
|
@@ -941,7 +946,7 @@ const classClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
941
946
|
}, {
|
|
942
947
|
root,
|
|
943
948
|
output,
|
|
944
|
-
group
|
|
949
|
+
group: group ?? void 0
|
|
945
950
|
});
|
|
946
951
|
const operationData = buildOperationData(operationNode);
|
|
947
952
|
const previous = acc.find((item) => item.file.path === file.path);
|
|
@@ -1098,7 +1103,7 @@ const classClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1098
1103
|
}, {
|
|
1099
1104
|
root,
|
|
1100
1105
|
output,
|
|
1101
|
-
group
|
|
1106
|
+
group: group ?? void 0
|
|
1102
1107
|
});
|
|
1103
1108
|
files.push(/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1104
1109
|
baseName: sdkFile.baseName,
|
|
@@ -1143,6 +1148,11 @@ const classClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1143
1148
|
});
|
|
1144
1149
|
//#endregion
|
|
1145
1150
|
//#region src/generators/clientGenerator.tsx
|
|
1151
|
+
/**
|
|
1152
|
+
* Built-in operation generator for `@kubb/plugin-client`. Emits one async
|
|
1153
|
+
* function per OpenAPI operation, plus the matching URL helper. Used when
|
|
1154
|
+
* `clientType: 'function'` (the default).
|
|
1155
|
+
*/
|
|
1146
1156
|
const clientGenerator = (0, _kubb_core.defineGenerator)({
|
|
1147
1157
|
name: "client",
|
|
1148
1158
|
renderer: _kubb_renderer_jsx.jsxRendererSync,
|
|
@@ -1153,10 +1163,10 @@ const clientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1153
1163
|
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1154
1164
|
if (!pluginTs) return null;
|
|
1155
1165
|
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
1156
|
-
const pluginZod = parser === "zod" ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) :
|
|
1157
|
-
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) :
|
|
1166
|
+
const pluginZod = parser === "zod" ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
1167
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
1158
1168
|
const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing });
|
|
1159
|
-
const importedZodNames = zodResolver && parser === "zod" ? [zodResolver.resolveResponseName?.(node), node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) :
|
|
1169
|
+
const importedZodNames = zodResolver && parser === "zod" ? [zodResolver.resolveResponseName?.(node), node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null].filter((name) => Boolean(name)) : [];
|
|
1160
1170
|
const meta = {
|
|
1161
1171
|
name: resolver.resolveName(node.operationId),
|
|
1162
1172
|
urlName: resolver.resolveUrlName(node),
|
|
@@ -1168,7 +1178,7 @@ const clientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1168
1178
|
}, {
|
|
1169
1179
|
root,
|
|
1170
1180
|
output,
|
|
1171
|
-
group
|
|
1181
|
+
group: group ?? void 0
|
|
1172
1182
|
}),
|
|
1173
1183
|
fileTs: tsResolver.resolveFile({
|
|
1174
1184
|
name: node.operationId,
|
|
@@ -1178,7 +1188,7 @@ const clientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1178
1188
|
}, {
|
|
1179
1189
|
root,
|
|
1180
1190
|
output: pluginTs.options?.output ?? output,
|
|
1181
|
-
group: pluginTs.options?.group
|
|
1191
|
+
group: pluginTs.options?.group ?? void 0
|
|
1182
1192
|
}),
|
|
1183
1193
|
fileZod: zodResolver && pluginZod?.options ? zodResolver.resolveFile({
|
|
1184
1194
|
name: node.operationId,
|
|
@@ -1188,8 +1198,8 @@ const clientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1188
1198
|
}, {
|
|
1189
1199
|
root,
|
|
1190
1200
|
output: pluginZod.options.output ?? output,
|
|
1191
|
-
group: pluginZod.options?.group
|
|
1192
|
-
}) :
|
|
1201
|
+
group: pluginZod.options?.group ?? void 0
|
|
1202
|
+
}) : null
|
|
1193
1203
|
};
|
|
1194
1204
|
const hasFormData = node.requestBody?.content?.some((e) => e.contentType === "multipart/form-data") ?? false;
|
|
1195
1205
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
@@ -1276,6 +1286,12 @@ const clientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1276
1286
|
});
|
|
1277
1287
|
//#endregion
|
|
1278
1288
|
//#region src/generators/groupedClientGenerator.tsx
|
|
1289
|
+
/**
|
|
1290
|
+
* Emits one aggregate file per tag/group when `group` is configured. Each
|
|
1291
|
+
* file re-exports every client function for that group, so callers can
|
|
1292
|
+
* `import { petController } from './gen/clients'` instead of importing
|
|
1293
|
+
* each operation individually.
|
|
1294
|
+
*/
|
|
1279
1295
|
const groupedClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
1280
1296
|
name: "groupedClient",
|
|
1281
1297
|
renderer: _kubb_renderer_jsx.jsxRendererSync,
|
|
@@ -1285,7 +1301,7 @@ const groupedClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1285
1301
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: nodes.reduce((acc, operationNode) => {
|
|
1286
1302
|
if (group?.type === "tag") {
|
|
1287
1303
|
const tag = operationNode.tags[0];
|
|
1288
|
-
const name = tag ? group?.name?.({ group: camelCase(tag) }) :
|
|
1304
|
+
const name = tag ? group?.name?.({ group: camelCase(tag) }) : null;
|
|
1289
1305
|
if (!tag || !name) return acc;
|
|
1290
1306
|
const file = resolver.resolveFile({
|
|
1291
1307
|
name,
|
|
@@ -1294,7 +1310,7 @@ const groupedClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1294
1310
|
}, {
|
|
1295
1311
|
root,
|
|
1296
1312
|
output,
|
|
1297
|
-
group
|
|
1313
|
+
group: group ?? void 0
|
|
1298
1314
|
});
|
|
1299
1315
|
const clientFile = resolver.resolveFile({
|
|
1300
1316
|
name: operationNode.operationId,
|
|
@@ -1304,7 +1320,7 @@ const groupedClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1304
1320
|
}, {
|
|
1305
1321
|
root,
|
|
1306
1322
|
output,
|
|
1307
|
-
group
|
|
1323
|
+
group: group ?? void 0
|
|
1308
1324
|
});
|
|
1309
1325
|
const client = {
|
|
1310
1326
|
name: resolver.resolveName(operationNode.operationId),
|
|
@@ -1373,6 +1389,12 @@ function Operations({ name, nodes }) {
|
|
|
1373
1389
|
}
|
|
1374
1390
|
//#endregion
|
|
1375
1391
|
//#region src/generators/operationsGenerator.tsx
|
|
1392
|
+
/**
|
|
1393
|
+
* Generates an `operations.ts` file that re-exports every operation grouped
|
|
1394
|
+
* by HTTP method. Enabled when `pluginClient({ operations: true })`. Useful
|
|
1395
|
+
* for building meta-tooling on top of the generated client (route
|
|
1396
|
+
* registries, API explorers).
|
|
1397
|
+
*/
|
|
1376
1398
|
const operationsGenerator = (0, _kubb_core.defineGenerator)({
|
|
1377
1399
|
name: "client",
|
|
1378
1400
|
renderer: _kubb_renderer_jsx.jsxRendererSync,
|
|
@@ -1386,7 +1408,7 @@ const operationsGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1386
1408
|
}, {
|
|
1387
1409
|
root,
|
|
1388
1410
|
output,
|
|
1389
|
-
group
|
|
1411
|
+
group: group ?? void 0
|
|
1390
1412
|
});
|
|
1391
1413
|
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File, {
|
|
1392
1414
|
baseName: file.baseName,
|
|
@@ -1415,7 +1437,7 @@ function generateMethod({ node, name, tsResolver, zodResolver, baseURL, dataRetu
|
|
|
1415
1437
|
const { defaultContentType: contentType, isMultipleContentTypes, hasFormData } = getContentTypeInfo(node);
|
|
1416
1438
|
const isFormData = !isMultipleContentTypes && contentType === "multipart/form-data";
|
|
1417
1439
|
const { header: headerParams } = getOperationParameters(node);
|
|
1418
|
-
const headerParamsName = headerParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, headerParams[0]) :
|
|
1440
|
+
const headerParamsName = headerParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, headerParams[0]) : null;
|
|
1419
1441
|
const headers = isMultipleContentTypes ? headerParamsName ? ["...headers"] : [] : buildHeaders(contentType, !!headerParamsName);
|
|
1420
1442
|
const generics = buildGenerics(node, tsResolver);
|
|
1421
1443
|
const paramsNode = buildClientParamsNode({
|
|
@@ -1489,8 +1511,14 @@ function resolveTypeImportNames(node, tsResolver) {
|
|
|
1489
1511
|
return resolveOperationTypeNames(node, tsResolver, { order: "body-response-first" });
|
|
1490
1512
|
}
|
|
1491
1513
|
function resolveZodImportNames(node, zodResolver) {
|
|
1492
|
-
return [zodResolver.resolveResponseName?.(node), node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) :
|
|
1514
|
+
return [zodResolver.resolveResponseName?.(node), node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null].filter((n) => Boolean(n));
|
|
1493
1515
|
}
|
|
1516
|
+
/**
|
|
1517
|
+
* Built-in `operations` generator for `@kubb/plugin-client` when
|
|
1518
|
+
* `clientType: 'staticClass'`. Emits one class per tag, with a static method
|
|
1519
|
+
* per operation so callers can use `Pet.getPetById(...)` without
|
|
1520
|
+
* instantiating the class.
|
|
1521
|
+
*/
|
|
1494
1522
|
const staticClassClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
1495
1523
|
name: "staticClassClient",
|
|
1496
1524
|
renderer: _kubb_renderer_jsx.jsxRendererSync,
|
|
@@ -1502,8 +1530,8 @@ const staticClassClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1502
1530
|
if (!pluginTs) return null;
|
|
1503
1531
|
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
1504
1532
|
const tsPluginOptions = pluginTs.options;
|
|
1505
|
-
const pluginZod = parser === "zod" ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) :
|
|
1506
|
-
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) :
|
|
1533
|
+
const pluginZod = parser === "zod" ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
1534
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
1507
1535
|
function buildOperationData(node) {
|
|
1508
1536
|
const typeFile = tsResolver.resolveFile({
|
|
1509
1537
|
name: node.operationId,
|
|
@@ -1523,8 +1551,8 @@ const staticClassClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1523
1551
|
}, {
|
|
1524
1552
|
root,
|
|
1525
1553
|
output: pluginZod.options?.output ?? output,
|
|
1526
|
-
group: pluginZod.options?.group
|
|
1527
|
-
}) :
|
|
1554
|
+
group: pluginZod.options?.group ?? void 0
|
|
1555
|
+
}) : null;
|
|
1528
1556
|
return {
|
|
1529
1557
|
node,
|
|
1530
1558
|
name: resolver.resolveName(node.operationId),
|
|
@@ -1545,7 +1573,7 @@ const staticClassClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1545
1573
|
}, {
|
|
1546
1574
|
root,
|
|
1547
1575
|
output,
|
|
1548
|
-
group
|
|
1576
|
+
group: group ?? void 0
|
|
1549
1577
|
});
|
|
1550
1578
|
const operationData = buildOperationData(operationNode);
|
|
1551
1579
|
const previous = acc.find((item) => item.file.path === file.path);
|
|
@@ -1566,7 +1594,7 @@ const staticClassClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1566
1594
|
}, {
|
|
1567
1595
|
root,
|
|
1568
1596
|
output,
|
|
1569
|
-
group
|
|
1597
|
+
group: group ?? void 0
|
|
1570
1598
|
});
|
|
1571
1599
|
const operationData = buildOperationData(operationNode);
|
|
1572
1600
|
const previous = acc.find((item) => item.file.path === file.path);
|
|
@@ -1720,12 +1748,18 @@ const staticClassClientGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1720
1748
|
//#endregion
|
|
1721
1749
|
//#region src/resolvers/resolverClient.ts
|
|
1722
1750
|
/**
|
|
1723
|
-
*
|
|
1751
|
+
* Default resolver used by `@kubb/plugin-client`. Decides the names and file
|
|
1752
|
+
* paths for every generated client function or class. Functions and files use
|
|
1753
|
+
* camelCase; classes and tag groups use PascalCase.
|
|
1724
1754
|
*
|
|
1725
|
-
*
|
|
1755
|
+
* @example Resolve client function and class names
|
|
1756
|
+
* ```ts
|
|
1757
|
+
* import { resolverClient } from '@kubb/plugin-client'
|
|
1726
1758
|
*
|
|
1727
|
-
*
|
|
1728
|
-
*
|
|
1759
|
+
* resolverClient.default('list pets', 'function') // 'listPets'
|
|
1760
|
+
* resolverClient.resolveClassName('pet') // 'Pet'
|
|
1761
|
+
* resolverClient.resolveUrlName(operationNode) // 'getShowPetByIdUrl'
|
|
1762
|
+
* ```
|
|
1729
1763
|
*/
|
|
1730
1764
|
const resolverClient = (0, _kubb_core.defineResolver)(() => ({
|
|
1731
1765
|
name: "default",
|
|
@@ -1756,19 +1790,32 @@ const resolverClient = (0, _kubb_core.defineResolver)(() => ({
|
|
|
1756
1790
|
//#endregion
|
|
1757
1791
|
//#region src/plugin.ts
|
|
1758
1792
|
/**
|
|
1759
|
-
* Canonical plugin name for `@kubb/plugin-client
|
|
1793
|
+
* Canonical plugin name for `@kubb/plugin-client`. Used for driver lookups and
|
|
1794
|
+
* cross-plugin dependency references.
|
|
1760
1795
|
*/
|
|
1761
1796
|
const pluginClientName = "plugin-client";
|
|
1762
1797
|
/**
|
|
1763
|
-
* Generates
|
|
1764
|
-
*
|
|
1765
|
-
*
|
|
1798
|
+
* Generates one HTTP client function per OpenAPI operation. Each function has
|
|
1799
|
+
* typed path params, query params, body, and response, so callers use the API
|
|
1800
|
+
* like any other typed function. Ships with `axios` and `fetch` runtimes; bring
|
|
1801
|
+
* your own by setting `importPath`.
|
|
1766
1802
|
*
|
|
1767
|
-
* @example
|
|
1803
|
+
* @example
|
|
1768
1804
|
* ```ts
|
|
1769
|
-
* import
|
|
1805
|
+
* import { defineConfig } from 'kubb'
|
|
1806
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1807
|
+
* import { pluginClient } from '@kubb/plugin-client'
|
|
1808
|
+
*
|
|
1770
1809
|
* export default defineConfig({
|
|
1771
|
-
*
|
|
1810
|
+
* input: { path: './petStore.yaml' },
|
|
1811
|
+
* output: { path: './src/gen' },
|
|
1812
|
+
* plugins: [
|
|
1813
|
+
* pluginTs(),
|
|
1814
|
+
* pluginClient({
|
|
1815
|
+
* output: { path: './clients' },
|
|
1816
|
+
* client: 'fetch',
|
|
1817
|
+
* }),
|
|
1818
|
+
* ],
|
|
1772
1819
|
* })
|
|
1773
1820
|
* ```
|
|
1774
1821
|
*/
|
|
@@ -1780,8 +1827,8 @@ const pluginClient = (0, _kubb_core.definePlugin)((options) => {
|
|
|
1780
1827
|
const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : void 0);
|
|
1781
1828
|
const selectedGenerators = options.generators ?? [
|
|
1782
1829
|
clientType === "staticClass" ? staticClassClientGenerator : clientType === "class" ? classClientGenerator : clientGenerator,
|
|
1783
|
-
group && clientType === "function" ? groupedClientGenerator :
|
|
1784
|
-
operations ? operationsGenerator :
|
|
1830
|
+
group && clientType === "function" ? groupedClientGenerator : null,
|
|
1831
|
+
operations ? operationsGenerator : null
|
|
1785
1832
|
].filter((x) => Boolean(x));
|
|
1786
1833
|
const groupConfig = group ? {
|
|
1787
1834
|
...group,
|
|
@@ -1789,11 +1836,11 @@ const pluginClient = (0, _kubb_core.definePlugin)((options) => {
|
|
|
1789
1836
|
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
1790
1837
|
return `${camelCase(ctx.group)}Controller`;
|
|
1791
1838
|
}
|
|
1792
|
-
} :
|
|
1839
|
+
} : null;
|
|
1793
1840
|
return {
|
|
1794
1841
|
name: pluginClientName,
|
|
1795
1842
|
options,
|
|
1796
|
-
dependencies: [_kubb_plugin_ts.pluginTsName, parser === "zod" ? _kubb_plugin_zod.pluginZodName :
|
|
1843
|
+
dependencies: [_kubb_plugin_ts.pluginTsName, parser === "zod" ? _kubb_plugin_zod.pluginZodName : null].filter((dependency) => Boolean(dependency)),
|
|
1797
1844
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
1798
1845
|
const resolver = userResolver ? {
|
|
1799
1846
|
...resolverClient,
|