@kubb/plugin-client 5.0.0-alpha.27 → 5.0.0-alpha.29

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.
Files changed (41) hide show
  1. package/dist/index.cjs +1911 -62
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.ts +468 -2
  4. package/dist/index.js +1903 -65
  5. package/dist/index.js.map +1 -1
  6. package/package.json +5 -20
  7. package/src/components/ClassClient.tsx +42 -138
  8. package/src/components/Client.tsx +85 -125
  9. package/src/components/ClientLegacy.tsx +501 -0
  10. package/src/components/Operations.tsx +8 -8
  11. package/src/components/StaticClassClient.tsx +41 -135
  12. package/src/components/Url.tsx +37 -46
  13. package/src/generators/classClientGenerator.tsx +121 -131
  14. package/src/generators/clientGenerator.tsx +104 -80
  15. package/src/generators/groupedClientGenerator.tsx +28 -30
  16. package/src/generators/operationsGenerator.tsx +11 -17
  17. package/src/generators/staticClassClientGenerator.tsx +115 -121
  18. package/src/index.ts +11 -1
  19. package/src/plugin.ts +121 -92
  20. package/src/presets.ts +25 -0
  21. package/src/resolvers/resolverClient.ts +26 -0
  22. package/src/resolvers/resolverClientLegacy.ts +26 -0
  23. package/src/types.ts +93 -39
  24. package/src/utils.ts +148 -0
  25. package/dist/StaticClassClient-D6v3vhZL.js +0 -695
  26. package/dist/StaticClassClient-D6v3vhZL.js.map +0 -1
  27. package/dist/StaticClassClient-GyNiWMHA.cjs +0 -736
  28. package/dist/StaticClassClient-GyNiWMHA.cjs.map +0 -1
  29. package/dist/components.cjs +0 -7
  30. package/dist/components.d.ts +0 -216
  31. package/dist/components.js +0 -2
  32. package/dist/generators-C0t5dIvZ.js +0 -723
  33. package/dist/generators-C0t5dIvZ.js.map +0 -1
  34. package/dist/generators-D8A8QE4S.cjs +0 -753
  35. package/dist/generators-D8A8QE4S.cjs.map +0 -1
  36. package/dist/generators.cjs +0 -7
  37. package/dist/generators.d.ts +0 -21
  38. package/dist/generators.js +0 -2
  39. package/dist/types-jdcuAELq.d.ts +0 -169
  40. package/src/components/index.ts +0 -5
  41. package/src/generators/index.ts +0 -5
@@ -1,695 +0,0 @@
1
- import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { getComments, getParamsMapping, getPathParams } from "@kubb/plugin-oas/utils";
3
- import { Const, File, Function as Function$1, FunctionParams } from "@kubb/react-fabric";
4
- import { getDefaultValue, isOptional } from "@kubb/oas";
5
- import { Fragment, jsx, jsxs } from "@kubb/react-fabric/jsx-runtime";
6
- //#region ../../internals/utils/src/casing.ts
7
- /**
8
- * Shared implementation for camelCase and PascalCase conversion.
9
- * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
10
- * and capitalizes each word according to `pascal`.
11
- *
12
- * When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
13
- */
14
- function toCamelOrPascal(text, pascal) {
15
- return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
16
- if (word.length > 1 && word === word.toUpperCase()) return word;
17
- if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
18
- return word.charAt(0).toUpperCase() + word.slice(1);
19
- }).join("").replace(/[^a-zA-Z0-9]/g, "");
20
- }
21
- /**
22
- * Splits `text` on `.` and applies `transformPart` to each segment.
23
- * The last segment receives `isLast = true`, all earlier segments receive `false`.
24
- * Segments are joined with `/` to form a file path.
25
- *
26
- * Only splits on dots followed by a letter so that version numbers
27
- * embedded in operationIds (e.g. `v2025.0`) are kept intact.
28
- */
29
- function applyToFileParts(text, transformPart) {
30
- const parts = text.split(/\.(?=[a-zA-Z])/);
31
- return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
32
- }
33
- /**
34
- * Converts `text` to camelCase.
35
- * When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
36
- *
37
- * @example
38
- * camelCase('hello-world') // 'helloWorld'
39
- * camelCase('pet.petId', { isFile: true }) // 'pet/petId'
40
- */
41
- function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
42
- if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
43
- prefix,
44
- suffix
45
- } : {}));
46
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
47
- }
48
- /**
49
- * Converts `text` to PascalCase.
50
- * When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
51
- *
52
- * @example
53
- * pascalCase('hello-world') // 'HelloWorld'
54
- * pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
55
- */
56
- function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
57
- if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
58
- prefix,
59
- suffix
60
- }) : camelCase(part));
61
- return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
62
- }
63
- //#endregion
64
- //#region ../../internals/utils/src/jsdoc.ts
65
- /**
66
- * Builds a JSDoc comment block from an array of lines.
67
- * Returns `fallback` when `comments` is empty so callers always get a usable string.
68
- *
69
- * @example
70
- * ```ts
71
- * buildJSDoc(['@type string', '@example hello'])
72
- * // '/**\n * @type string\n * @example hello\n *\/\n '
73
- * ```
74
- */
75
- function buildJSDoc(comments, options = {}) {
76
- const { indent = " * ", suffix = "\n ", fallback = " " } = options;
77
- if (comments.length === 0) return fallback;
78
- return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
79
- }
80
- //#endregion
81
- //#region ../../internals/utils/src/reserved.ts
82
- /**
83
- * Returns `true` when `name` is a syntactically valid JavaScript variable name.
84
- *
85
- * @example
86
- * ```ts
87
- * isValidVarName('status') // true
88
- * isValidVarName('class') // false (reserved word)
89
- * isValidVarName('42foo') // false (starts with digit)
90
- * ```
91
- */
92
- function isValidVarName(name) {
93
- try {
94
- new Function(`var ${name}`);
95
- } catch {
96
- return false;
97
- }
98
- return true;
99
- }
100
- //#endregion
101
- //#region ../../internals/utils/src/urlPath.ts
102
- /**
103
- * Parses and transforms an OpenAPI/Swagger path string into various URL formats.
104
- *
105
- * @example
106
- * const p = new URLPath('/pet/{petId}')
107
- * p.URL // '/pet/:petId'
108
- * p.template // '`/pet/${petId}`'
109
- */
110
- var URLPath = class {
111
- /**
112
- * The raw OpenAPI/Swagger path string, e.g. `/pet/{petId}`.
113
- */
114
- path;
115
- #options;
116
- constructor(path, options = {}) {
117
- this.path = path;
118
- this.#options = options;
119
- }
120
- /** Converts the OpenAPI path to Express-style colon syntax, e.g. `/pet/{petId}` → `/pet/:petId`.
121
- *
122
- * @example
123
- * ```ts
124
- * new URLPath('/pet/{petId}').URL // '/pet/:petId'
125
- * ```
126
- */
127
- get URL() {
128
- return this.toURLPath();
129
- }
130
- /** Returns `true` when `path` is a fully-qualified URL (e.g. starts with `https://`).
131
- *
132
- * @example
133
- * ```ts
134
- * new URLPath('https://petstore.swagger.io/v2/pet').isURL // true
135
- * new URLPath('/pet/{petId}').isURL // false
136
- * ```
137
- */
138
- get isURL() {
139
- try {
140
- return !!new URL(this.path).href;
141
- } catch {
142
- return false;
143
- }
144
- }
145
- /**
146
- * Converts the OpenAPI path to a TypeScript template literal string.
147
- *
148
- * @example
149
- * new URLPath('/pet/{petId}').template // '`/pet/${petId}`'
150
- * new URLPath('/account/monetary-accountID').template // '`/account/${monetaryAccountId}`'
151
- */
152
- get template() {
153
- return this.toTemplateString();
154
- }
155
- /** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set.
156
- *
157
- * @example
158
- * ```ts
159
- * new URLPath('/pet/{petId}').object
160
- * // { url: '/pet/:petId', params: { petId: 'petId' } }
161
- * ```
162
- */
163
- get object() {
164
- return this.toObject();
165
- }
166
- /** Returns a map of path parameter names, or `undefined` when the path has no parameters.
167
- *
168
- * @example
169
- * ```ts
170
- * new URLPath('/pet/{petId}').params // { petId: 'petId' }
171
- * new URLPath('/pet').params // undefined
172
- * ```
173
- */
174
- get params() {
175
- return this.getParams();
176
- }
177
- #transformParam(raw) {
178
- const param = isValidVarName(raw) ? raw : camelCase(raw);
179
- return this.#options.casing === "camelcase" ? camelCase(param) : param;
180
- }
181
- /**
182
- * Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
183
- */
184
- #eachParam(fn) {
185
- for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
186
- const raw = match[1];
187
- fn(raw, this.#transformParam(raw));
188
- }
189
- }
190
- toObject({ type = "path", replacer, stringify } = {}) {
191
- const object = {
192
- url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
193
- params: this.getParams()
194
- };
195
- if (stringify) {
196
- if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
197
- if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
198
- return `{ url: '${object.url}' }`;
199
- }
200
- return object;
201
- }
202
- /**
203
- * Converts the OpenAPI path to a TypeScript template literal string.
204
- * An optional `replacer` can transform each extracted parameter name before interpolation.
205
- *
206
- * @example
207
- * new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
208
- */
209
- toTemplateString({ prefix = "", replacer } = {}) {
210
- return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
211
- if (i % 2 === 0) return part;
212
- const param = this.#transformParam(part);
213
- return `\${${replacer ? replacer(param) : param}}`;
214
- }).join("")}\``;
215
- }
216
- /**
217
- * Extracts all `{param}` segments from the path and returns them as a key-value map.
218
- * An optional `replacer` transforms each parameter name in both key and value positions.
219
- * Returns `undefined` when no path parameters are found.
220
- *
221
- * @example
222
- * ```ts
223
- * new URLPath('/pet/{petId}/tag/{tagId}').getParams()
224
- * // { petId: 'petId', tagId: 'tagId' }
225
- * ```
226
- */
227
- getParams(replacer) {
228
- const params = {};
229
- this.#eachParam((_raw, param) => {
230
- const key = replacer ? replacer(param) : param;
231
- params[key] = key;
232
- });
233
- return Object.keys(params).length > 0 ? params : void 0;
234
- }
235
- /** Converts the OpenAPI path to Express-style colon syntax.
236
- *
237
- * @example
238
- * ```ts
239
- * new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
240
- * ```
241
- */
242
- toURLPath() {
243
- return this.path.replace(/\{([^}]+)\}/g, ":$1");
244
- }
245
- };
246
- //#endregion
247
- //#region src/components/Url.tsx
248
- function getParams$1({ paramsType, paramsCasing, pathParamsType, typeSchemas }) {
249
- if (paramsType === "object") {
250
- const pathParams = getPathParams(typeSchemas.pathParams, {
251
- typed: true,
252
- casing: paramsCasing
253
- });
254
- return FunctionParams.factory({ data: {
255
- mode: "object",
256
- children: { ...pathParams }
257
- } });
258
- }
259
- return FunctionParams.factory({ pathParams: typeSchemas.pathParams?.name ? {
260
- mode: pathParamsType === "object" ? "object" : "inlineSpread",
261
- children: getPathParams(typeSchemas.pathParams, {
262
- typed: true,
263
- casing: paramsCasing
264
- }),
265
- default: getDefaultValue(typeSchemas.pathParams?.schema)
266
- } : void 0 });
267
- }
268
- __name(getParams$1, "getParams");
269
- function Url({ name, isExportable = true, isIndexable = true, typeSchemas, baseURL, paramsType, paramsCasing, pathParamsType, operation }) {
270
- const path = new URLPath(operation.path);
271
- const params = getParams$1({
272
- paramsType,
273
- paramsCasing,
274
- pathParamsType,
275
- typeSchemas
276
- });
277
- const pathParamsMapping = paramsCasing ? getParamsMapping(typeSchemas.pathParams, { casing: paramsCasing }) : void 0;
278
- return /* @__PURE__ */ jsx(File.Source, {
279
- name,
280
- isExportable,
281
- isIndexable,
282
- children: /* @__PURE__ */ jsxs(Function$1, {
283
- name,
284
- export: isExportable,
285
- params: params.toConstructor(),
286
- children: [
287
- pathParamsMapping && Object.entries(pathParamsMapping).filter(([originalName, camelCaseName]) => originalName !== camelCaseName && isValidVarName(originalName)).map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`).join("\n"),
288
- pathParamsMapping && /* @__PURE__ */ jsx("br", {}),
289
- /* @__PURE__ */ jsx(Const, {
290
- name: "res",
291
- children: `{ method: '${operation.method.toUpperCase()}', url: ${path.toTemplateString({ prefix: baseURL })} as const }`
292
- }),
293
- /* @__PURE__ */ jsx("br", {}),
294
- "return res"
295
- ]
296
- })
297
- });
298
- }
299
- Url.getParams = getParams$1;
300
- //#endregion
301
- //#region src/components/Client.tsx
302
- function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas, isConfigurable }) {
303
- if (paramsType === "object") {
304
- const children = {
305
- ...getPathParams(typeSchemas.pathParams, {
306
- typed: true,
307
- casing: paramsCasing
308
- }),
309
- data: typeSchemas.request?.name ? {
310
- type: typeSchemas.request?.name,
311
- optional: isOptional(typeSchemas.request?.schema)
312
- } : void 0,
313
- params: typeSchemas.queryParams?.name ? {
314
- type: typeSchemas.queryParams?.name,
315
- optional: isOptional(typeSchemas.queryParams?.schema)
316
- } : void 0,
317
- headers: typeSchemas.headerParams?.name ? {
318
- type: typeSchemas.headerParams?.name,
319
- optional: isOptional(typeSchemas.headerParams?.schema)
320
- } : void 0
321
- };
322
- const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional);
323
- return FunctionParams.factory({
324
- data: {
325
- mode: "object",
326
- children,
327
- default: allChildrenAreOptional ? "{}" : void 0
328
- },
329
- config: isConfigurable ? {
330
- type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : "Partial<RequestConfig> & { client?: Client }",
331
- default: "{}"
332
- } : void 0
333
- });
334
- }
335
- return FunctionParams.factory({
336
- pathParams: typeSchemas.pathParams?.name ? {
337
- mode: pathParamsType === "object" ? "object" : "inlineSpread",
338
- children: getPathParams(typeSchemas.pathParams, {
339
- typed: true,
340
- casing: paramsCasing
341
- }),
342
- default: getDefaultValue(typeSchemas.pathParams?.schema)
343
- } : void 0,
344
- data: typeSchemas.request?.name ? {
345
- type: typeSchemas.request?.name,
346
- optional: isOptional(typeSchemas.request?.schema)
347
- } : void 0,
348
- params: typeSchemas.queryParams?.name ? {
349
- type: typeSchemas.queryParams?.name,
350
- optional: isOptional(typeSchemas.queryParams?.schema)
351
- } : void 0,
352
- headers: typeSchemas.headerParams?.name ? {
353
- type: typeSchemas.headerParams?.name,
354
- optional: isOptional(typeSchemas.headerParams?.schema)
355
- } : void 0,
356
- config: isConfigurable ? {
357
- type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : "Partial<RequestConfig> & { client?: Client }",
358
- default: "{}"
359
- } : void 0
360
- });
361
- }
362
- function Client({ name, isExportable = true, isIndexable = true, returnType, typeSchemas, baseURL, dataReturnType, parser, zodSchemas, paramsType, paramsCasing, pathParamsType, operation, urlName, children, isConfigurable = true }) {
363
- const path = new URLPath(operation.path);
364
- const contentType = operation.getContentType();
365
- const isFormData = contentType === "multipart/form-data";
366
- const pathParamsMapping = paramsCasing && !urlName ? getParamsMapping(typeSchemas.pathParams, { casing: paramsCasing }) : void 0;
367
- const queryParamsMapping = paramsCasing ? getParamsMapping(typeSchemas.queryParams, { casing: paramsCasing }) : void 0;
368
- const headerParamsMapping = paramsCasing ? getParamsMapping(typeSchemas.headerParams, { casing: paramsCasing }) : void 0;
369
- const headers = [contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` : void 0, typeSchemas.headerParams?.name ? headerParamsMapping ? "...mappedHeaders" : "...headers" : void 0].filter(Boolean);
370
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
371
- const generics = [
372
- typeSchemas.response.name,
373
- TError,
374
- typeSchemas.request?.name || "unknown"
375
- ].filter(Boolean);
376
- const params = getParams({
377
- paramsType,
378
- paramsCasing,
379
- pathParamsType,
380
- typeSchemas,
381
- isConfigurable
382
- });
383
- const urlParams = Url.getParams({
384
- paramsType,
385
- paramsCasing,
386
- pathParamsType,
387
- typeSchemas
388
- });
389
- const clientParams = FunctionParams.factory({ config: {
390
- mode: "object",
391
- children: {
392
- method: { value: JSON.stringify(operation.method.toUpperCase()) },
393
- url: { value: urlName ? `${urlName}(${urlParams.toCall()}).url.toString()` : path.template },
394
- baseURL: baseURL && !urlName ? { value: `\`${baseURL}\`` } : void 0,
395
- params: typeSchemas.queryParams?.name ? queryParamsMapping ? { value: "mappedParams" } : {} : void 0,
396
- data: typeSchemas.request?.name ? { value: isFormData ? "formData as FormData" : "requestData" } : void 0,
397
- requestConfig: isConfigurable ? { mode: "inlineSpread" } : void 0,
398
- headers: headers.length ? { value: isConfigurable ? `{ ${headers.join(", ")}, ...requestConfig.headers }` : `{ ${headers.join(", ")} }` } : void 0
399
- }
400
- } });
401
- const childrenElement = children ? children : /* @__PURE__ */ jsxs(Fragment, { children: [
402
- dataReturnType === "full" && parser === "zod" && zodSchemas && `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}`,
403
- dataReturnType === "data" && parser === "zod" && zodSchemas && `return ${zodSchemas.response.name}.parse(res.data)`,
404
- dataReturnType === "full" && parser === "client" && "return res",
405
- dataReturnType === "data" && parser === "client" && "return res.data"
406
- ] });
407
- return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsx(File.Source, {
408
- name,
409
- isExportable,
410
- isIndexable,
411
- children: /* @__PURE__ */ jsxs(Function$1, {
412
- name,
413
- async: true,
414
- export: isExportable,
415
- params: params.toConstructor(),
416
- JSDoc: { comments: getComments(operation) },
417
- returnType,
418
- children: [
419
- isConfigurable ? "const { client: request = fetch, ...requestConfig } = config" : "",
420
- /* @__PURE__ */ jsx("br", {}),
421
- /* @__PURE__ */ jsx("br", {}),
422
- pathParamsMapping && Object.entries(pathParamsMapping).filter(([originalName, camelCaseName]) => originalName !== camelCaseName && isValidVarName(originalName)).map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`).join("\n"),
423
- pathParamsMapping && /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("br", {}), /* @__PURE__ */ jsx("br", {})] }),
424
- queryParamsMapping && typeSchemas.queryParams?.name && /* @__PURE__ */ jsxs(Fragment, { children: [
425
- `const mappedParams = params ? { ${Object.entries(queryParamsMapping).map(([originalName, camelCaseName]) => `"${originalName}": params.${camelCaseName}`).join(", ")} } : undefined`,
426
- /* @__PURE__ */ jsx("br", {}),
427
- /* @__PURE__ */ jsx("br", {})
428
- ] }),
429
- headerParamsMapping && typeSchemas.headerParams?.name && /* @__PURE__ */ jsxs(Fragment, { children: [
430
- `const mappedHeaders = headers ? { ${Object.entries(headerParamsMapping).map(([originalName, camelCaseName]) => `"${originalName}": headers.${camelCaseName}`).join(", ")} } : undefined`,
431
- /* @__PURE__ */ jsx("br", {}),
432
- /* @__PURE__ */ jsx("br", {})
433
- ] }),
434
- parser === "zod" && zodSchemas?.request?.name ? `const requestData = ${zodSchemas.request.name}.parse(data)` : typeSchemas?.request?.name && "const requestData = data",
435
- /* @__PURE__ */ jsx("br", {}),
436
- isFormData && typeSchemas?.request?.name && "const formData = buildFormData(requestData)",
437
- /* @__PURE__ */ jsx("br", {}),
438
- isConfigurable ? `const res = await request<${generics.join(", ")}>(${clientParams.toCall()})` : `const res = await fetch<${generics.join(", ")}>(${clientParams.toCall()})`,
439
- /* @__PURE__ */ jsx("br", {}),
440
- childrenElement
441
- ]
442
- })
443
- })] });
444
- }
445
- Client.getParams = getParams;
446
- //#endregion
447
- //#region src/components/ClassClient.tsx
448
- function buildHeaders$1(contentType, hasHeaderParams) {
449
- return [contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` : void 0, hasHeaderParams ? "...headers" : void 0].filter(Boolean);
450
- }
451
- __name(buildHeaders$1, "buildHeaders");
452
- function buildGenerics$1(typeSchemas) {
453
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
454
- return [
455
- typeSchemas.response.name,
456
- TError,
457
- typeSchemas.request?.name || "unknown"
458
- ].filter(Boolean);
459
- }
460
- __name(buildGenerics$1, "buildGenerics");
461
- function buildClientParams$1({ operation, path, baseURL, typeSchemas, isFormData, headers }) {
462
- return FunctionParams.factory({ config: {
463
- mode: "object",
464
- children: {
465
- requestConfig: { mode: "inlineSpread" },
466
- method: { value: JSON.stringify(operation.method.toUpperCase()) },
467
- url: { value: path.template },
468
- baseURL: baseURL ? { value: JSON.stringify(baseURL) } : void 0,
469
- params: typeSchemas.queryParams?.name ? {} : void 0,
470
- data: typeSchemas.request?.name ? { value: isFormData ? "formData as FormData" : "requestData" } : void 0,
471
- headers: headers.length ? { value: `{ ${headers.join(", ")}, ...requestConfig.headers }` } : void 0
472
- }
473
- } });
474
- }
475
- __name(buildClientParams$1, "buildClientParams");
476
- function buildRequestDataLine$1({ parser, zodSchemas, typeSchemas }) {
477
- if (parser === "zod" && zodSchemas?.request?.name) return `const requestData = ${zodSchemas.request.name}.parse(data)`;
478
- if (typeSchemas?.request?.name) return "const requestData = data";
479
- return "";
480
- }
481
- __name(buildRequestDataLine$1, "buildRequestDataLine");
482
- function buildFormDataLine$1(isFormData, hasRequest) {
483
- return isFormData && hasRequest ? "const formData = buildFormData(requestData)" : "";
484
- }
485
- __name(buildFormDataLine$1, "buildFormDataLine");
486
- function buildReturnStatement$1({ dataReturnType, parser, zodSchemas }) {
487
- if (dataReturnType === "full" && parser === "zod" && zodSchemas) return `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}`;
488
- if (dataReturnType === "data" && parser === "zod" && zodSchemas) return `return ${zodSchemas.response.name}.parse(res.data)`;
489
- if (dataReturnType === "full" && parser === "client") return "return res";
490
- return "return res.data";
491
- }
492
- __name(buildReturnStatement$1, "buildReturnStatement");
493
- function generateMethod$1({ operation, name, typeSchemas, zodSchemas, baseURL, dataReturnType, parser, paramsType, paramsCasing, pathParamsType }) {
494
- const path = new URLPath(operation.path, { casing: paramsCasing });
495
- const contentType = operation.getContentType();
496
- const isFormData = contentType === "multipart/form-data";
497
- const headers = buildHeaders$1(contentType, !!typeSchemas.headerParams?.name);
498
- const generics = buildGenerics$1(typeSchemas);
499
- const params = ClassClient.getParams({
500
- paramsType,
501
- paramsCasing,
502
- pathParamsType,
503
- typeSchemas,
504
- isConfigurable: true
505
- });
506
- const clientParams = buildClientParams$1({
507
- operation,
508
- path,
509
- baseURL,
510
- typeSchemas,
511
- isFormData,
512
- headers
513
- });
514
- const jsdoc = buildJSDoc(getComments(operation));
515
- const requestDataLine = buildRequestDataLine$1({
516
- parser,
517
- zodSchemas,
518
- typeSchemas
519
- });
520
- const formDataLine = buildFormDataLine$1(isFormData, !!typeSchemas?.request?.name);
521
- const returnStatement = buildReturnStatement$1({
522
- dataReturnType,
523
- parser,
524
- zodSchemas
525
- });
526
- const methodBody = [
527
- "const { client: request = fetch, ...requestConfig } = mergeConfig(this.#config, config)",
528
- "",
529
- requestDataLine,
530
- formDataLine,
531
- `const res = await request<${generics.join(", ")}>(${clientParams.toCall()})`,
532
- returnStatement
533
- ].filter(Boolean).map((line) => ` ${line}`).join("\n");
534
- return `${jsdoc}async ${name}(${params.toConstructor()}) {\n${methodBody}\n }`;
535
- }
536
- __name(generateMethod$1, "generateMethod");
537
- function ClassClient({ name, isExportable = true, isIndexable = true, operations, baseURL, dataReturnType, parser, paramsType, paramsCasing, pathParamsType, children }) {
538
- const classCode = `export class ${name} {
539
- #config: Partial<RequestConfig> & { client?: Client }
540
-
541
- constructor(config: Partial<RequestConfig> & { client?: Client } = {}) {
542
- this.#config = config
543
- }
544
-
545
- ${operations.map(({ operation, name: methodName, typeSchemas, zodSchemas }) => generateMethod$1({
546
- operation,
547
- name: methodName,
548
- typeSchemas,
549
- zodSchemas,
550
- baseURL,
551
- dataReturnType,
552
- parser,
553
- paramsType,
554
- paramsCasing,
555
- pathParamsType
556
- })).join("\n\n")}
557
- }`;
558
- return /* @__PURE__ */ jsxs(File.Source, {
559
- name,
560
- isExportable,
561
- isIndexable,
562
- children: [classCode, children]
563
- });
564
- }
565
- ClassClient.getParams = Client.getParams;
566
- //#endregion
567
- //#region src/components/Operations.tsx
568
- function Operations({ name, operations }) {
569
- const operationsObject = {};
570
- operations.forEach((operation) => {
571
- operationsObject[operation.getOperationId()] = {
572
- path: new URLPath(operation.path).URL,
573
- method: operation.method
574
- };
575
- });
576
- return /* @__PURE__ */ jsx(File.Source, {
577
- name,
578
- isExportable: true,
579
- isIndexable: true,
580
- children: /* @__PURE__ */ jsx(Const, {
581
- name,
582
- export: true,
583
- children: JSON.stringify(operationsObject, void 0, 2)
584
- })
585
- });
586
- }
587
- //#endregion
588
- //#region src/components/StaticClassClient.tsx
589
- function buildHeaders(contentType, hasHeaderParams) {
590
- return [contentType !== "application/json" && contentType !== "multipart/form-data" ? `'Content-Type': '${contentType}'` : void 0, hasHeaderParams ? "...headers" : void 0].filter(Boolean);
591
- }
592
- function buildGenerics(typeSchemas) {
593
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(" | ") || "Error"}>`;
594
- return [
595
- typeSchemas.response.name,
596
- TError,
597
- typeSchemas.request?.name || "unknown"
598
- ].filter(Boolean);
599
- }
600
- function buildClientParams({ operation, path, baseURL, typeSchemas, isFormData, headers }) {
601
- return FunctionParams.factory({ config: {
602
- mode: "object",
603
- children: {
604
- requestConfig: { mode: "inlineSpread" },
605
- method: { value: JSON.stringify(operation.method.toUpperCase()) },
606
- url: { value: path.template },
607
- baseURL: baseURL ? { value: JSON.stringify(baseURL) } : void 0,
608
- params: typeSchemas.queryParams?.name ? {} : void 0,
609
- data: typeSchemas.request?.name ? { value: isFormData ? "formData as FormData" : "requestData" } : void 0,
610
- headers: headers.length ? { value: `{ ${headers.join(", ")}, ...requestConfig.headers }` } : void 0
611
- }
612
- } });
613
- }
614
- function buildRequestDataLine({ parser, zodSchemas, typeSchemas }) {
615
- if (parser === "zod" && zodSchemas?.request?.name) return `const requestData = ${zodSchemas.request.name}.parse(data)`;
616
- if (typeSchemas?.request?.name) return "const requestData = data";
617
- return "";
618
- }
619
- function buildFormDataLine(isFormData, hasRequest) {
620
- return isFormData && hasRequest ? "const formData = buildFormData(requestData)" : "";
621
- }
622
- function buildReturnStatement({ dataReturnType, parser, zodSchemas }) {
623
- if (dataReturnType === "full" && parser === "zod" && zodSchemas) return `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}`;
624
- if (dataReturnType === "data" && parser === "zod" && zodSchemas) return `return ${zodSchemas.response.name}.parse(res.data)`;
625
- if (dataReturnType === "full" && parser === "client") return "return res";
626
- return "return res.data";
627
- }
628
- function generateMethod({ operation, name, typeSchemas, zodSchemas, baseURL, dataReturnType, parser, paramsType, paramsCasing, pathParamsType }) {
629
- const path = new URLPath(operation.path, { casing: paramsCasing });
630
- const contentType = operation.getContentType();
631
- const isFormData = contentType === "multipart/form-data";
632
- const headers = buildHeaders(contentType, !!typeSchemas.headerParams?.name);
633
- const generics = buildGenerics(typeSchemas);
634
- const params = Client.getParams({
635
- paramsType,
636
- paramsCasing,
637
- pathParamsType,
638
- typeSchemas,
639
- isConfigurable: true
640
- });
641
- const clientParams = buildClientParams({
642
- operation,
643
- path,
644
- baseURL,
645
- typeSchemas,
646
- isFormData,
647
- headers
648
- });
649
- const jsdoc = buildJSDoc(getComments(operation));
650
- const requestDataLine = buildRequestDataLine({
651
- parser,
652
- zodSchemas,
653
- typeSchemas
654
- });
655
- const formDataLine = buildFormDataLine(isFormData, !!typeSchemas?.request?.name);
656
- const returnStatement = buildReturnStatement({
657
- dataReturnType,
658
- parser,
659
- zodSchemas
660
- });
661
- const methodBody = [
662
- "const { client: request = fetch, ...requestConfig } = mergeConfig(this.#config, config)",
663
- "",
664
- requestDataLine,
665
- formDataLine,
666
- `const res = await request<${generics.join(", ")}>(${clientParams.toCall()})`,
667
- returnStatement
668
- ].filter(Boolean).map((line) => ` ${line}`).join("\n");
669
- return `${jsdoc} static async ${name}(${params.toConstructor()}) {\n${methodBody}\n }`;
670
- }
671
- function StaticClassClient({ name, isExportable = true, isIndexable = true, operations, baseURL, dataReturnType, parser, paramsType, paramsCasing, pathParamsType, children }) {
672
- const classCode = `export class ${name} {\n static #config: Partial<RequestConfig> & { client?: Client } = {}\n\n${operations.map(({ operation, name: methodName, typeSchemas, zodSchemas }) => generateMethod({
673
- operation,
674
- name: methodName,
675
- typeSchemas,
676
- zodSchemas,
677
- baseURL,
678
- dataReturnType,
679
- parser,
680
- paramsType,
681
- paramsCasing,
682
- pathParamsType
683
- })).join("\n\n")}\n}`;
684
- return /* @__PURE__ */ jsxs(File.Source, {
685
- name,
686
- isExportable,
687
- isIndexable,
688
- children: [classCode, children]
689
- });
690
- }
691
- StaticClassClient.getParams = Client.getParams;
692
- //#endregion
693
- export { Url as a, Client as i, Operations as n, camelCase as o, ClassClient as r, pascalCase as s, StaticClassClient as t };
694
-
695
- //# sourceMappingURL=StaticClassClient-D6v3vhZL.js.map