@kubb/plugin-react-query 5.0.0-beta.64 → 5.0.0-beta.73
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/README.md +2 -2
- package/dist/index.cjs +2520 -58
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +521 -3
- package/dist/index.js +2487 -48
- package/dist/index.js.map +1 -1
- package/package.json +10 -19
- package/dist/components-B0j2b6y8.cjs +0 -1583
- package/dist/components-B0j2b6y8.cjs.map +0 -1
- package/dist/components-Ds-e_nnK.js +0 -1433
- package/dist/components-Ds-e_nnK.js.map +0 -1
- package/dist/components.cjs +0 -13
- package/dist/components.d.ts +0 -270
- package/dist/components.js +0 -2
- package/dist/generators-CU6zfet7.cjs +0 -1349
- package/dist/generators-CU6zfet7.cjs.map +0 -1
- package/dist/generators-nnZhtlrk.js +0 -1307
- package/dist/generators-nnZhtlrk.js.map +0 -1
- package/dist/generators.cjs +0 -9
- package/dist/generators.d.ts +0 -66
- package/dist/generators.js +0 -2
- package/dist/types-ANQXv7Gr.d.ts +0 -396
- /package/dist/{chunk-C0LytTxp.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -2,17 +2,65 @@ Object.defineProperties(exports, {
|
|
|
2
2
|
__esModule: { value: true },
|
|
3
3
|
[Symbol.toStringTag]: { value: "Module" }
|
|
4
4
|
});
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
6
|
+
var __create = Object.create;
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
14
|
+
key = keys[i];
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
+
get: ((k) => from[k]).bind(null, key),
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
//#endregion
|
|
7
27
|
let node_path = require("node:path");
|
|
8
|
-
node_path =
|
|
28
|
+
node_path = __toESM(node_path, 1);
|
|
9
29
|
let _kubb_core = require("@kubb/core");
|
|
10
|
-
let _kubb_plugin_client = require("@kubb/plugin-client");
|
|
11
|
-
let _kubb_plugin_client_templates_clients_axios_source = require("@kubb/plugin-client/templates/clients/axios.source");
|
|
12
|
-
let _kubb_plugin_client_templates_clients_fetch_source = require("@kubb/plugin-client/templates/clients/fetch.source");
|
|
13
|
-
let _kubb_plugin_client_templates_config_source = require("@kubb/plugin-client/templates/config.source");
|
|
14
30
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
15
31
|
let _kubb_plugin_zod = require("@kubb/plugin-zod");
|
|
32
|
+
let _kubb_renderer_jsx = require("@kubb/renderer-jsx");
|
|
33
|
+
let _kubb_renderer_jsx_jsx_runtime = require("@kubb/renderer-jsx/jsx-runtime");
|
|
34
|
+
let node_fs = require("node:fs");
|
|
35
|
+
node_fs = __toESM(node_fs, 1);
|
|
36
|
+
let _kubb_ast_utils = require("@kubb/ast/utils");
|
|
37
|
+
//#region ../../internals/utils/src/casing.ts
|
|
38
|
+
/**
|
|
39
|
+
* Shared implementation for camelCase and PascalCase conversion.
|
|
40
|
+
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
41
|
+
* and capitalizes each word according to `pascal`.
|
|
42
|
+
*
|
|
43
|
+
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
44
|
+
*/
|
|
45
|
+
function toCamelOrPascal(text, pascal) {
|
|
46
|
+
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) => {
|
|
47
|
+
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
48
|
+
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
49
|
+
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Converts `text` to camelCase.
|
|
53
|
+
*
|
|
54
|
+
* @example Word boundaries
|
|
55
|
+
* `camelCase('hello-world') // 'helloWorld'`
|
|
56
|
+
*
|
|
57
|
+
* @example With a prefix
|
|
58
|
+
* `camelCase('tag', { prefix: 'create' }) // 'createTag'`
|
|
59
|
+
*/
|
|
60
|
+
function camelCase(text, { prefix = "", suffix = "" } = {}) {
|
|
61
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
16
64
|
//#region ../../internals/utils/src/fs.ts
|
|
17
65
|
/**
|
|
18
66
|
* Builds a nested file path from a dotted name. Splits on dots that precede a letter
|
|
@@ -33,9 +81,396 @@ let _kubb_plugin_zod = require("@kubb/plugin-zod");
|
|
|
33
81
|
* @example Suffix applied to the final segment only
|
|
34
82
|
* `toFilePath('tag.tag', (part) => camelCase(part, { suffix: 'schema' })) // 'tag/tagSchema'`
|
|
35
83
|
*/
|
|
36
|
-
function toFilePath(name, caseLast =
|
|
84
|
+
function toFilePath(name, caseLast = camelCase) {
|
|
37
85
|
const parts = name.split(/\.(?=[a-zA-Z])/);
|
|
38
|
-
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) :
|
|
86
|
+
return parts.map((part, i) => i === parts.length - 1 ? caseLast(part) : camelCase(part)).filter(Boolean).join("/");
|
|
87
|
+
}
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region ../../internals/utils/src/reserved.ts
|
|
90
|
+
/**
|
|
91
|
+
* JavaScript and Java reserved words.
|
|
92
|
+
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
93
|
+
*/
|
|
94
|
+
const reservedWords = new Set([
|
|
95
|
+
"abstract",
|
|
96
|
+
"arguments",
|
|
97
|
+
"boolean",
|
|
98
|
+
"break",
|
|
99
|
+
"byte",
|
|
100
|
+
"case",
|
|
101
|
+
"catch",
|
|
102
|
+
"char",
|
|
103
|
+
"class",
|
|
104
|
+
"const",
|
|
105
|
+
"continue",
|
|
106
|
+
"debugger",
|
|
107
|
+
"default",
|
|
108
|
+
"delete",
|
|
109
|
+
"do",
|
|
110
|
+
"double",
|
|
111
|
+
"else",
|
|
112
|
+
"enum",
|
|
113
|
+
"eval",
|
|
114
|
+
"export",
|
|
115
|
+
"extends",
|
|
116
|
+
"false",
|
|
117
|
+
"final",
|
|
118
|
+
"finally",
|
|
119
|
+
"float",
|
|
120
|
+
"for",
|
|
121
|
+
"function",
|
|
122
|
+
"goto",
|
|
123
|
+
"if",
|
|
124
|
+
"implements",
|
|
125
|
+
"import",
|
|
126
|
+
"in",
|
|
127
|
+
"instanceof",
|
|
128
|
+
"int",
|
|
129
|
+
"interface",
|
|
130
|
+
"let",
|
|
131
|
+
"long",
|
|
132
|
+
"native",
|
|
133
|
+
"new",
|
|
134
|
+
"null",
|
|
135
|
+
"package",
|
|
136
|
+
"private",
|
|
137
|
+
"protected",
|
|
138
|
+
"public",
|
|
139
|
+
"return",
|
|
140
|
+
"short",
|
|
141
|
+
"static",
|
|
142
|
+
"super",
|
|
143
|
+
"switch",
|
|
144
|
+
"synchronized",
|
|
145
|
+
"this",
|
|
146
|
+
"throw",
|
|
147
|
+
"throws",
|
|
148
|
+
"transient",
|
|
149
|
+
"true",
|
|
150
|
+
"try",
|
|
151
|
+
"typeof",
|
|
152
|
+
"var",
|
|
153
|
+
"void",
|
|
154
|
+
"volatile",
|
|
155
|
+
"while",
|
|
156
|
+
"with",
|
|
157
|
+
"yield",
|
|
158
|
+
"Array",
|
|
159
|
+
"Date",
|
|
160
|
+
"hasOwnProperty",
|
|
161
|
+
"Infinity",
|
|
162
|
+
"isFinite",
|
|
163
|
+
"isNaN",
|
|
164
|
+
"isPrototypeOf",
|
|
165
|
+
"length",
|
|
166
|
+
"Math",
|
|
167
|
+
"name",
|
|
168
|
+
"NaN",
|
|
169
|
+
"Number",
|
|
170
|
+
"Object",
|
|
171
|
+
"prototype",
|
|
172
|
+
"String",
|
|
173
|
+
"toString",
|
|
174
|
+
"undefined",
|
|
175
|
+
"valueOf"
|
|
176
|
+
]);
|
|
177
|
+
/**
|
|
178
|
+
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* isValidVarName('status') // true
|
|
183
|
+
* isValidVarName('class') // false (reserved word)
|
|
184
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
function isValidVarName(name) {
|
|
188
|
+
if (!name || reservedWords.has(name)) return false;
|
|
189
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region ../../internals/utils/src/url.ts
|
|
193
|
+
function transformParam(raw, casing) {
|
|
194
|
+
const param = isValidVarName(raw) ? raw : camelCase(raw);
|
|
195
|
+
return casing === "camelcase" ? camelCase(param) : param;
|
|
196
|
+
}
|
|
197
|
+
function toParamsObject(path, { replacer, casing } = {}) {
|
|
198
|
+
const params = {};
|
|
199
|
+
for (const match of path.matchAll(/\{([^}]+)\}/g)) {
|
|
200
|
+
const param = transformParam(match[1], casing);
|
|
201
|
+
const key = replacer ? replacer(param) : param;
|
|
202
|
+
params[key] = key;
|
|
203
|
+
}
|
|
204
|
+
return Object.keys(params).length > 0 ? params : null;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Helpers for OpenAPI/Swagger paths, plus a thin wrapper over the native `URL`.
|
|
208
|
+
*/
|
|
209
|
+
var Url = class Url {
|
|
210
|
+
/**
|
|
211
|
+
* Reports whether `url` is a parseable absolute URL. Delegates to the native `URL.canParse`.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* Url.canParse('https://petstore.swagger.io/v2') // true
|
|
215
|
+
* Url.canParse('/pet/{petId}') // false
|
|
216
|
+
*/
|
|
217
|
+
static canParse(url, base) {
|
|
218
|
+
return URL.canParse(url, base);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Converts an OpenAPI/Swagger path to Express-style colon syntax.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* Url.toPath('/pet/{petId}') // '/pet/:petId'
|
|
225
|
+
*/
|
|
226
|
+
static toPath(path) {
|
|
227
|
+
return path.replace(/\{([^}]+)\}/g, ":$1");
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Converts an OpenAPI/Swagger path to a TypeScript template literal string.
|
|
231
|
+
* `prefix` is prepended inside the literal, `replacer` transforms each parameter name,
|
|
232
|
+
* and `casing` controls parameter identifier casing.
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* Url.toTemplateString('/pet/{petId}') // '`/pet/${petId}`'
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* Url.toTemplateString('/pet/{petId}', { prefix: 'https://api' }) // '`https://api/pet/${petId}`'
|
|
239
|
+
*/
|
|
240
|
+
static toTemplateString(path, { prefix, replacer, casing } = {}) {
|
|
241
|
+
const result = path.split(/\{([^}]+)\}/).map((part, i) => {
|
|
242
|
+
if (i % 2 === 0) return part;
|
|
243
|
+
const param = transformParam(part, casing);
|
|
244
|
+
return `\${${replacer ? replacer(param) : param}}`;
|
|
245
|
+
}).join("");
|
|
246
|
+
return `\`${prefix ?? ""}${result}\``;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Converts an OpenAPI/Swagger path to a template literal that reads each parameter off the
|
|
250
|
+
* grouped `path` request option, e.g. `/pet/{petId}` becomes `` `/pet/${path.petId}` ``. Parameter
|
|
251
|
+
* names are camelCased to match the generated `path` type, and `prefix` is prepended inside the
|
|
252
|
+
* literal. Shared by the client and cypress generators that pass a grouped `path` object.
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* Url.toGroupedTemplateString('/pet/{petId}') // '`/pet/${path.petId}`'
|
|
256
|
+
*/
|
|
257
|
+
static toGroupedTemplateString(path, { prefix } = {}) {
|
|
258
|
+
return Url.toTemplateString(path, {
|
|
259
|
+
prefix,
|
|
260
|
+
casing: "camelcase",
|
|
261
|
+
replacer: (name) => `path.${name}`
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Returns the path and its extracted params as a structured `URLObject`, or as a stringified
|
|
266
|
+
* expression when `stringify` is set.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* Url.toObject('/pet/{petId}')
|
|
270
|
+
* // { url: '/pet/:petId', params: { petId: 'petId' } }
|
|
271
|
+
*/
|
|
272
|
+
static toObject(path, { type = "path", replacer, stringify, casing } = {}) {
|
|
273
|
+
const object = {
|
|
274
|
+
url: type === "path" ? Url.toPath(path) : Url.toTemplateString(path, {
|
|
275
|
+
replacer,
|
|
276
|
+
casing
|
|
277
|
+
}),
|
|
278
|
+
params: toParamsObject(path, {
|
|
279
|
+
replacer,
|
|
280
|
+
casing
|
|
281
|
+
})
|
|
282
|
+
};
|
|
283
|
+
if (stringify) {
|
|
284
|
+
if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
|
|
285
|
+
if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
|
|
286
|
+
return `{ url: '${object.url}' }`;
|
|
287
|
+
}
|
|
288
|
+
return object;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region ../../internals/shared/src/params.ts
|
|
293
|
+
const caseParamsCache = /* @__PURE__ */ new WeakMap();
|
|
294
|
+
/**
|
|
295
|
+
* Applies camelCase to parameter names and returns a new array without mutating the input.
|
|
296
|
+
*
|
|
297
|
+
* Run it before handing parameters to schema builders so output property keys get the right casing
|
|
298
|
+
* while `OperationNode.parameters` stays intact for other consumers. When `casing` is unset, the
|
|
299
|
+
* original array is returned unchanged. Results are cached per input array.
|
|
300
|
+
*/
|
|
301
|
+
function caseParams(params, casing) {
|
|
302
|
+
if (!casing) return params;
|
|
303
|
+
const cached = caseParamsCache.get(params);
|
|
304
|
+
if (cached) return cached;
|
|
305
|
+
const result = params.map((param) => ({
|
|
306
|
+
...param,
|
|
307
|
+
name: camelCase(param.name)
|
|
308
|
+
}));
|
|
309
|
+
caseParamsCache.set(params, result);
|
|
310
|
+
return result;
|
|
311
|
+
}
|
|
312
|
+
//#endregion
|
|
313
|
+
//#region ../../internals/shared/src/operation.ts
|
|
314
|
+
/**
|
|
315
|
+
* Builds the `ResolverFileParams` every operation generator passes to
|
|
316
|
+
* `resolver.resolveFile`: a file named `name`, tagged by the operation's first
|
|
317
|
+
* tag (or `'default'`), at the operation's path. Centralizes the entry object
|
|
318
|
+
* that was repeated at dozens of call sites across the client and query plugins.
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* resolver.resolveFile(operationFileEntry(node, node.operationId), { root, output, group })
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
function operationFileEntry(node, name, extname = ".ts") {
|
|
326
|
+
return {
|
|
327
|
+
name,
|
|
328
|
+
extname,
|
|
329
|
+
tag: node.tags[0] ?? "default",
|
|
330
|
+
path: node.path
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
function getOperationLink(node, link) {
|
|
334
|
+
if (!link) return null;
|
|
335
|
+
if (typeof link === "function") return link(node) ?? null;
|
|
336
|
+
if (link === "urlPath") return node.path ? `{@link ${Url.toPath(node.path)}}` : null;
|
|
337
|
+
return node.path ? `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}` : null;
|
|
338
|
+
}
|
|
339
|
+
function getContentTypeInfo(node) {
|
|
340
|
+
const contentTypes = node.requestBody?.content?.map((e) => e.contentType) ?? [];
|
|
341
|
+
const isMultipleContentTypes = contentTypes.length > 1;
|
|
342
|
+
return {
|
|
343
|
+
contentTypes,
|
|
344
|
+
isMultipleContentTypes,
|
|
345
|
+
contentTypeUnion: isMultipleContentTypes ? contentTypes.map((ct) => JSON.stringify(ct)).join(" | ") : "",
|
|
346
|
+
defaultContentType: contentTypes[0] ?? "application/json",
|
|
347
|
+
hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function buildRequestConfigType(node) {
|
|
351
|
+
const { isMultipleContentTypes, contentTypeUnion } = getContentTypeInfo(node);
|
|
352
|
+
const configType = `Partial<Omit<RequestConfig, 'path' | 'query' | 'body' | 'headers' | 'url'>>`;
|
|
353
|
+
const contentTypeProp = isMultipleContentTypes ? `contentType?: ${contentTypeUnion}` : null;
|
|
354
|
+
return contentTypeProp ? `${configType} & { ${contentTypeProp} }` : configType;
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Builds the `client?:` option type shared by the generated query hooks (`useQuery`,
|
|
358
|
+
* `useInfiniteQuery`, `useSWR`, ...). Unlike {@link buildRequestConfigType}, it never adds a
|
|
359
|
+
* `contentType?:` member: query hooks wrap GET operations, which carry no request body to select a
|
|
360
|
+
* content type for.
|
|
361
|
+
*/
|
|
362
|
+
function buildClientOptionType() {
|
|
363
|
+
return `Partial<Omit<RequestConfig, 'path' | 'query' | 'body' | 'headers' | 'url'>>`;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Which of the grouped request options an operation carries.
|
|
367
|
+
*/
|
|
368
|
+
function getRequestGroups(node) {
|
|
369
|
+
const { path, query, header } = getOperationParameters(node);
|
|
370
|
+
return {
|
|
371
|
+
path: path.length > 0,
|
|
372
|
+
query: query.length > 0,
|
|
373
|
+
body: Boolean(node.requestBody?.content?.[0]?.schema),
|
|
374
|
+
headers: header.length > 0
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Resolves which grouped request options an operation carries together with whether each group
|
|
379
|
+
* holds a required member. The grouped parameter stays optional only when nothing inside it is
|
|
380
|
+
* required, matching the generated `RequestConfig` type.
|
|
381
|
+
*/
|
|
382
|
+
function getRequestGroupOptionality(node) {
|
|
383
|
+
const groups = getRequestGroups(node);
|
|
384
|
+
const { path, query, header } = getOperationParameters(node);
|
|
385
|
+
const hasRequiredPath = path.some((param) => param.required);
|
|
386
|
+
const hasRequiredQuery = query.some((param) => param.required);
|
|
387
|
+
const hasRequiredHeader = header.some((param) => param.required);
|
|
388
|
+
return {
|
|
389
|
+
groups,
|
|
390
|
+
hasRequiredPath,
|
|
391
|
+
hasRequiredQuery,
|
|
392
|
+
hasRequiredHeader,
|
|
393
|
+
isOptional: !hasRequiredPath && !hasRequiredQuery && !hasRequiredHeader && !groups.body
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
function buildOperationComments(node, options = {}) {
|
|
397
|
+
const { link = "pathTemplate", linkPosition = "afterDeprecated", splitLines = false } = options;
|
|
398
|
+
const linkComment = getOperationLink(node, link);
|
|
399
|
+
const filteredComments = (linkPosition === "beforeDeprecated" ? [
|
|
400
|
+
node.description && `@description ${node.description}`,
|
|
401
|
+
node.summary && `@summary ${node.summary}`,
|
|
402
|
+
linkComment,
|
|
403
|
+
node.deprecated && "@deprecated"
|
|
404
|
+
] : [
|
|
405
|
+
node.description && `@description ${node.description}`,
|
|
406
|
+
node.summary && `@summary ${node.summary}`,
|
|
407
|
+
node.deprecated && "@deprecated",
|
|
408
|
+
linkComment
|
|
409
|
+
]).filter((comment) => Boolean(comment));
|
|
410
|
+
if (!splitLines) return filteredComments;
|
|
411
|
+
return filteredComments.flatMap((text) => text.split(/\r?\n/).map((line) => line.trim())).filter((comment) => Boolean(comment));
|
|
412
|
+
}
|
|
413
|
+
function getOperationParameters(node, options = {}) {
|
|
414
|
+
const params = caseParams(node.parameters, options.paramsCasing === "original" ? void 0 : "camelcase");
|
|
415
|
+
return {
|
|
416
|
+
path: params.filter((param) => param.in === "path"),
|
|
417
|
+
query: params.filter((param) => param.in === "query"),
|
|
418
|
+
header: params.filter((param) => param.in === "header"),
|
|
419
|
+
cookie: params.filter((param) => param.in === "cookie")
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
function getStatusCodeNumber(statusCode) {
|
|
423
|
+
const code = Number(statusCode);
|
|
424
|
+
return Number.isNaN(code) ? null : code;
|
|
425
|
+
}
|
|
426
|
+
function isSuccessStatusCode(statusCode) {
|
|
427
|
+
const code = getStatusCodeNumber(statusCode);
|
|
428
|
+
return code !== null && code >= 200 && code < 300;
|
|
429
|
+
}
|
|
430
|
+
function isErrorStatusCode(statusCode) {
|
|
431
|
+
const code = getStatusCodeNumber(statusCode);
|
|
432
|
+
return code !== null && code >= 400;
|
|
433
|
+
}
|
|
434
|
+
function resolveErrorNames(node, resolver) {
|
|
435
|
+
return node.responses.filter((response) => isErrorStatusCode(response.statusCode)).map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
436
|
+
}
|
|
437
|
+
function resolveSuccessNames(node, resolver) {
|
|
438
|
+
return node.responses.filter((response) => isSuccessStatusCode(response.statusCode)).map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
439
|
+
}
|
|
440
|
+
function resolveStatusCodeNames(node, resolver) {
|
|
441
|
+
return node.responses.map((response) => resolver.resolveResponseStatusName(node, response.statusCode));
|
|
442
|
+
}
|
|
443
|
+
const typeNamesByResolver = /* @__PURE__ */ new WeakMap();
|
|
444
|
+
function resolveOperationTypeNames(node, resolver, options = {}) {
|
|
445
|
+
const cacheKey = `${node.operationId}\0${options.paramsCasing ?? ""}\0${options.order ?? ""}\0${options.responseStatusNames ?? ""}\0${options.includeParams === false ? "noparams" : ""}\0${(options.exclude ?? []).join(",")}`;
|
|
446
|
+
let byResolver = typeNamesByResolver.get(resolver);
|
|
447
|
+
if (byResolver) {
|
|
448
|
+
const cached = byResolver.get(cacheKey);
|
|
449
|
+
if (cached) return cached;
|
|
450
|
+
} else {
|
|
451
|
+
byResolver = /* @__PURE__ */ new Map();
|
|
452
|
+
typeNamesByResolver.set(resolver, byResolver);
|
|
453
|
+
}
|
|
454
|
+
const { path, query, header } = getOperationParameters(node, { paramsCasing: options.paramsCasing });
|
|
455
|
+
const responseStatusNames = options.responseStatusNames === "error" ? resolveErrorNames(node, resolver) : options.responseStatusNames === false ? [] : resolveStatusCodeNames(node, resolver);
|
|
456
|
+
const exclude = new Set(options.exclude ?? []);
|
|
457
|
+
const paramNames = options.includeParams === false ? [] : [
|
|
458
|
+
...path.map((param) => resolver.resolvePathParamsName(node, param)),
|
|
459
|
+
...query.map((param) => resolver.resolveQueryParamsName(node, param)),
|
|
460
|
+
...header.map((param) => resolver.resolveHeaderParamsName(node, param))
|
|
461
|
+
];
|
|
462
|
+
const bodyAndResponseNames = [node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null, resolver.resolveResponseName(node)];
|
|
463
|
+
const result = (options.order === "body-response-first" ? [
|
|
464
|
+
...bodyAndResponseNames,
|
|
465
|
+
...paramNames,
|
|
466
|
+
...responseStatusNames
|
|
467
|
+
] : [
|
|
468
|
+
...paramNames,
|
|
469
|
+
...bodyAndResponseNames,
|
|
470
|
+
...responseStatusNames
|
|
471
|
+
]).filter((name) => Boolean(name) && !exclude.has(name));
|
|
472
|
+
byResolver.set(cacheKey, result);
|
|
473
|
+
return result;
|
|
39
474
|
}
|
|
40
475
|
//#endregion
|
|
41
476
|
//#region ../../internals/shared/src/group.ts
|
|
@@ -61,7 +496,7 @@ function createGroupConfig(group) {
|
|
|
61
496
|
if (!group) return null;
|
|
62
497
|
const defaultName = (ctx) => {
|
|
63
498
|
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
64
|
-
return
|
|
499
|
+
return camelCase(ctx.group);
|
|
65
500
|
};
|
|
66
501
|
return {
|
|
67
502
|
...group,
|
|
@@ -69,6 +504,2060 @@ function createGroupConfig(group) {
|
|
|
69
504
|
};
|
|
70
505
|
}
|
|
71
506
|
//#endregion
|
|
507
|
+
//#region ../../internals/client/src/builders/parser.ts
|
|
508
|
+
/**
|
|
509
|
+
* Returns `true` when any direction of the parser uses zod (used for dependency checks).
|
|
510
|
+
*/
|
|
511
|
+
function isParserEnabled(parser) {
|
|
512
|
+
if (!parser) return false;
|
|
513
|
+
if (parser === "zod") return true;
|
|
514
|
+
return Boolean(parser.request || parser.response);
|
|
515
|
+
}
|
|
516
|
+
//#endregion
|
|
517
|
+
//#region ../../internals/client/src/resolveClient.ts
|
|
518
|
+
/**
|
|
519
|
+
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
520
|
+
* operation, shared so the selection rules and diagnostics stay in one place.
|
|
521
|
+
*
|
|
522
|
+
* Every client runtime lives in a dedicated client plugin, so a consumer always calls a registered
|
|
523
|
+
* contract client plugin (plugin-fetch or plugin-axios) and never emits its own inline client:
|
|
524
|
+
*
|
|
525
|
+
* - `contract` — a registered contract client plugin owns the `<op>` functions and the consumer
|
|
526
|
+
* imports and calls them.
|
|
527
|
+
* - `error` — no client plugin is registered, several are registered without a selector, or the
|
|
528
|
+
* requested one is missing.
|
|
529
|
+
*
|
|
530
|
+
* The `client` string selects explicitly (`'fetch'` / `'axios'`); when it is unset a lone registered
|
|
531
|
+
* contract client plugin is picked up automatically.
|
|
532
|
+
*/
|
|
533
|
+
const pluginFetchName = "plugin-fetch";
|
|
534
|
+
const pluginAxiosName = "plugin-axios";
|
|
535
|
+
const selectorToPlugin = {
|
|
536
|
+
fetch: pluginFetchName,
|
|
537
|
+
axios: pluginAxiosName
|
|
538
|
+
};
|
|
539
|
+
const contractPlugins = [pluginFetchName, pluginAxiosName];
|
|
540
|
+
/**
|
|
541
|
+
* Applies the `client` resolution rules. See the module comment for the strategy shape.
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* ```ts
|
|
545
|
+
* resolveClient({ client: 'fetch', pluginNames: ['plugin-ts', 'plugin-fetch'] })
|
|
546
|
+
* // { kind: 'contract', pluginName: 'plugin-fetch' }
|
|
547
|
+
* ```
|
|
548
|
+
*
|
|
549
|
+
* @example
|
|
550
|
+
* ```ts
|
|
551
|
+
* resolveClient({ client: undefined, pluginNames: ['plugin-ts'] })
|
|
552
|
+
* // { kind: 'error', message: 'No client plugin is registered…' }
|
|
553
|
+
* ```
|
|
554
|
+
*/
|
|
555
|
+
function resolveClient(options) {
|
|
556
|
+
const { client, pluginNames } = options;
|
|
557
|
+
const has = (name) => pluginNames.includes(name);
|
|
558
|
+
if (client === "fetch" || client === "axios") {
|
|
559
|
+
const pluginName = selectorToPlugin[client];
|
|
560
|
+
if (!has(pluginName)) return {
|
|
561
|
+
kind: "error",
|
|
562
|
+
message: `\`client: '${client}'\` is set but \`@kubb/plugin-${client}\` is not registered in \`plugins\`. Add it, or drop \`client\` to use a different client plugin.`
|
|
563
|
+
};
|
|
564
|
+
return {
|
|
565
|
+
kind: "contract",
|
|
566
|
+
pluginName
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
const registered = contractPlugins.filter(has);
|
|
570
|
+
if (registered.length === 1) return {
|
|
571
|
+
kind: "contract",
|
|
572
|
+
pluginName: registered[0]
|
|
573
|
+
};
|
|
574
|
+
if (registered.length > 1) return {
|
|
575
|
+
kind: "error",
|
|
576
|
+
message: `Multiple client plugins are registered (${registered.map((name) => `\`@kubb/${name}\``).join(", ")}). Set \`client: 'fetch' | 'axios'\` to choose which client the hooks call, or register a single client plugin.`
|
|
577
|
+
};
|
|
578
|
+
return {
|
|
579
|
+
kind: "error",
|
|
580
|
+
message: "No client plugin is registered. Add `@kubb/plugin-axios` or `@kubb/plugin-fetch` to `plugins` so the generated code has an HTTP client to call."
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
//#endregion
|
|
584
|
+
//#region ../../internals/client/src/resolveClientOperation.ts
|
|
585
|
+
/**
|
|
586
|
+
* Resolves the contract client `<op>` a consumer (query hook, MCP handler) imports, by looking up
|
|
587
|
+
* the registered contract client plugin's resolver and output. Works for any contract client plugin
|
|
588
|
+
* (plugin-fetch or plugin-axios). Returns `null` when no contract plugin is in play (the inline
|
|
589
|
+
* path). The plugin injects `.kubb/client.ts` at the global output root, the same path consumers
|
|
590
|
+
* read `RequestConfig` / `ResponseErrorConfig` from.
|
|
591
|
+
*/
|
|
592
|
+
function resolveClientOperation(options) {
|
|
593
|
+
const { clientPlugin, driver, node, root, output } = options;
|
|
594
|
+
if (!clientPlugin) return null;
|
|
595
|
+
const resolver = driver.getResolver(clientPlugin.pluginName);
|
|
596
|
+
if (!resolver) return null;
|
|
597
|
+
const plugin = driver.getPlugin(clientPlugin.pluginName);
|
|
598
|
+
const file = resolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
599
|
+
root,
|
|
600
|
+
output: plugin?.options?.output ?? output,
|
|
601
|
+
group: plugin?.options?.group ?? void 0
|
|
602
|
+
});
|
|
603
|
+
return {
|
|
604
|
+
name: resolver.resolveName(node.operationId),
|
|
605
|
+
path: file.path,
|
|
606
|
+
clientPath: node_path.default.resolve(root, ".kubb/client.ts")
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
//#endregion
|
|
610
|
+
//#region ../../internals/tanstack-query/src/components/MutationKey.tsx
|
|
611
|
+
const declarationPrinter$10 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
612
|
+
const mutationKeyTransformer = ({ node }) => {
|
|
613
|
+
if (!node.path) return [];
|
|
614
|
+
return [`{ url: '${Url.toPath(node.path)}' }`];
|
|
615
|
+
};
|
|
616
|
+
function MutationKey({ name, node, transformer }) {
|
|
617
|
+
const paramsNode = (0, _kubb_plugin_ts.createFunctionParameters)({ params: [] });
|
|
618
|
+
const paramsSignature = declarationPrinter$10.print(paramsNode) ?? "";
|
|
619
|
+
const keys = (transformer ?? mutationKeyTransformer)({
|
|
620
|
+
node,
|
|
621
|
+
casing: "camelcase"
|
|
622
|
+
});
|
|
623
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
624
|
+
name,
|
|
625
|
+
isExportable: true,
|
|
626
|
+
isIndexable: true,
|
|
627
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function.Arrow, {
|
|
628
|
+
name,
|
|
629
|
+
export: true,
|
|
630
|
+
params: paramsSignature,
|
|
631
|
+
singleLine: true,
|
|
632
|
+
children: `[${keys.join(", ")}] as const`
|
|
633
|
+
})
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
//#endregion
|
|
637
|
+
//#region ../../internals/tanstack-query/src/utils.ts
|
|
638
|
+
/**
|
|
639
|
+
* The grouped request options, ordered for both the destructured signature and the
|
|
640
|
+
* call passed to the underlying client.
|
|
641
|
+
*/
|
|
642
|
+
const requestGroupOrder = [
|
|
643
|
+
"path",
|
|
644
|
+
"query",
|
|
645
|
+
"body",
|
|
646
|
+
"headers"
|
|
647
|
+
];
|
|
648
|
+
/**
|
|
649
|
+
* Builds the grouped `{ path, query, body, headers }` parameter that mirrors the client
|
|
650
|
+
* function signature. Only the groups the operation carries are emitted, typed from the
|
|
651
|
+
* operation's `RequestConfig`. `keys` narrows the emitted groups, used by the query key which
|
|
652
|
+
* never carries `headers`.
|
|
653
|
+
*
|
|
654
|
+
* By default the whole group is typed as the single `RequestConfig` reference. When
|
|
655
|
+
* `memberTypeWrapper` is set, each group is emitted as its own member typed from the matching
|
|
656
|
+
* `RequestConfig['<group>']` slice and wrapped, used by vue-query to apply
|
|
657
|
+
* `MaybeRefOrGetter` per group.
|
|
658
|
+
*/
|
|
659
|
+
function buildGroupedRequestParam(node, options) {
|
|
660
|
+
const { resolver, keys = requestGroupOrder, memberTypeWrapper } = options;
|
|
661
|
+
const { groups, hasRequiredPath, hasRequiredQuery, hasRequiredHeader } = getRequestGroupOptionality(node);
|
|
662
|
+
const names = keys.filter((key) => groups[key]);
|
|
663
|
+
if (names.length === 0) return null;
|
|
664
|
+
const requiredByGroup = {
|
|
665
|
+
path: hasRequiredPath,
|
|
666
|
+
query: hasRequiredQuery,
|
|
667
|
+
body: groups.body,
|
|
668
|
+
headers: hasRequiredHeader
|
|
669
|
+
};
|
|
670
|
+
const isOptional = names.every((name) => !requiredByGroup[name]);
|
|
671
|
+
const requestConfigName = resolver.resolveRequestConfigName(node);
|
|
672
|
+
const omittedKeys = requestGroupOrder.filter((key) => !keys.includes(key));
|
|
673
|
+
const requestConfigType = omittedKeys.length > 0 ? `Omit<${requestConfigName}, ${omittedKeys.map((key) => `'${key}'`).join(" | ")}>` : requestConfigName;
|
|
674
|
+
if (memberTypeWrapper) {
|
|
675
|
+
const members = names.map((name) => ({
|
|
676
|
+
name,
|
|
677
|
+
type: memberTypeWrapper(`${requestConfigType}['${name}']`),
|
|
678
|
+
optional: !requiredByGroup[name]
|
|
679
|
+
}));
|
|
680
|
+
return (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
681
|
+
name: (0, _kubb_plugin_ts.createObjectBindingPattern)({ elements: names.map((name) => ({ name })) }),
|
|
682
|
+
type: (0, _kubb_plugin_ts.createTypeLiteral)({ members }),
|
|
683
|
+
optional: false,
|
|
684
|
+
...isOptional ? { default: "{}" } : {}
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
return (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
688
|
+
name: (0, _kubb_plugin_ts.createObjectBindingPattern)({ elements: names.map((name) => ({ name })) }),
|
|
689
|
+
type: requestConfigType,
|
|
690
|
+
optional: false,
|
|
691
|
+
...isOptional ? { default: "{}" } : {}
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Builds the shared `({ path, query, body, headers }, config = {})` parameter list for a
|
|
696
|
+
* TanStack query-options function. The leading parameter mirrors the client signature, and the
|
|
697
|
+
* trailing `config` is a partial `RequestConfig` minus the grouped data-shape keys, which are passed
|
|
698
|
+
* explicitly. Framework plugins wrap the result when needed, for example vue-query applies `MaybeRefOrGetter`.
|
|
699
|
+
*/
|
|
700
|
+
function buildQueryOptionsParams(node, options) {
|
|
701
|
+
const { resolver, memberTypeWrapper } = options;
|
|
702
|
+
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [buildGroupedRequestParam(node, {
|
|
703
|
+
resolver,
|
|
704
|
+
memberTypeWrapper
|
|
705
|
+
}), (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
706
|
+
name: "config",
|
|
707
|
+
type: `Partial<Omit<RequestConfig, 'path' | 'query' | 'body' | 'headers' | 'url'>>`,
|
|
708
|
+
default: "{}"
|
|
709
|
+
})].filter((param) => param !== null) });
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Builds the call to a client `<op>` function inside a query/mutation hook body. The function takes
|
|
713
|
+
* a single grouped options object, so the operation's request groups are passed as
|
|
714
|
+
* shorthand alongside the spread `config`, with `throwOnError: true` pinned last so a caller's config
|
|
715
|
+
* can't flip the query's error semantics. Queries thread the TanStack `signal`; mutations omit it.
|
|
716
|
+
*
|
|
717
|
+
* @example
|
|
718
|
+
* ```ts
|
|
719
|
+
* buildClientCall(node, { clientName: 'getPetById', signal: true })
|
|
720
|
+
* // getPetById({ path, ...config, signal: config.signal ?? signal, throwOnError: true })
|
|
721
|
+
* ```
|
|
722
|
+
*/
|
|
723
|
+
function buildClientCall(node, options) {
|
|
724
|
+
const { clientName, signal = false } = options;
|
|
725
|
+
const { groups } = getRequestGroupOptionality(node);
|
|
726
|
+
return `${clientName}({ ${[
|
|
727
|
+
"...config",
|
|
728
|
+
...requestGroupOrder.filter((key) => groups[key]),
|
|
729
|
+
signal ? "signal: config.signal ?? signal" : null,
|
|
730
|
+
"throwOnError: true"
|
|
731
|
+
].filter((part) => part !== null).join(", ")} })`;
|
|
732
|
+
}
|
|
733
|
+
function matchesPattern(node, ov) {
|
|
734
|
+
const { type, pattern } = ov;
|
|
735
|
+
const matches = (value) => typeof pattern === "string" ? value === pattern : pattern.test(value);
|
|
736
|
+
if (type === "operationId") return matches(node.operationId);
|
|
737
|
+
if (type === "tag") return node.tags.some((t) => matches(t));
|
|
738
|
+
if (type === "path") return node.path !== void 0 && matches(node.path);
|
|
739
|
+
if (type === "method") return node.method !== void 0 && matches(node.method);
|
|
740
|
+
return false;
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Resolves per-operation overrides (first matching override wins).
|
|
744
|
+
*
|
|
745
|
+
* @example
|
|
746
|
+
* ```ts
|
|
747
|
+
* const opts = resolveOperationOverrides(node, override)
|
|
748
|
+
* const queryOpts = 'query' in opts ? opts.query : defaultQuery
|
|
749
|
+
* ```
|
|
750
|
+
*/
|
|
751
|
+
function resolveOperationOverrides(node, override) {
|
|
752
|
+
if (!override) return {};
|
|
753
|
+
return override.find((ov) => matchesPattern(node, ov))?.options ?? {};
|
|
754
|
+
}
|
|
755
|
+
/**
|
|
756
|
+
* Returns `'zod'` when response-direction parsing is enabled.
|
|
757
|
+
* The string shorthand `'zod'` also enables response parsing.
|
|
758
|
+
*/
|
|
759
|
+
function resolveResponseParser(parser) {
|
|
760
|
+
if (!parser) return null;
|
|
761
|
+
if (parser === "zod") return "zod";
|
|
762
|
+
return parser.response ?? null;
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Returns `'zod'` when request body parsing is enabled.
|
|
766
|
+
* The string shorthand `'zod'` also enables request body parsing (existing behavior).
|
|
767
|
+
*/
|
|
768
|
+
function resolveRequestParser(parser) {
|
|
769
|
+
if (!parser) return null;
|
|
770
|
+
if (parser === "zod") return "zod";
|
|
771
|
+
return parser.request ?? null;
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Returns `'zod'` when query-params parsing is enabled.
|
|
775
|
+
* Only the object form `{ request: 'zod' }` enables this. `parser: 'zod'` does not.
|
|
776
|
+
*/
|
|
777
|
+
function resolveQueryParamsParser(parser) {
|
|
778
|
+
if (!parser || parser === "zod") return null;
|
|
779
|
+
return parser.request ?? null;
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Collects the Zod schema import names for an operation based on the active parser directions.
|
|
783
|
+
*
|
|
784
|
+
* - `parser: 'zod'`: response and request body names (backward-compatible behavior).
|
|
785
|
+
* - `parser: { request: 'zod' }`: request body and query params names.
|
|
786
|
+
* - `parser: { response: 'zod' }`: response name only.
|
|
787
|
+
* - `parser: { request: 'zod', response: 'zod' }`: all three.
|
|
788
|
+
*
|
|
789
|
+
* Returns an empty array when no resolver is provided or `parser` is falsy.
|
|
790
|
+
*/
|
|
791
|
+
function resolveZodSchemaNames(node, zodResolver, parser) {
|
|
792
|
+
if (!zodResolver || !parser) return [];
|
|
793
|
+
const { query: queryParams } = getOperationParameters(node, { paramsCasing: "original" });
|
|
794
|
+
return [
|
|
795
|
+
resolveResponseParser(parser) === "zod" ? zodResolver.resolveResponseName?.(node) : null,
|
|
796
|
+
resolveRequestParser(parser) === "zod" && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null,
|
|
797
|
+
resolveQueryParamsParser(parser) === "zod" && queryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, queryParams[0]) : null
|
|
798
|
+
].filter((n) => Boolean(n));
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
|
|
802
|
+
* typed from the operation's `RequestConfig` minus `url`. The query key transformer reads the
|
|
803
|
+
* grouped `path`/`query`/`body` bindings.
|
|
804
|
+
*/
|
|
805
|
+
function buildQueryKeyParams(node, options) {
|
|
806
|
+
const { resolver } = options;
|
|
807
|
+
const groupedParam = buildGroupedRequestParam(node, {
|
|
808
|
+
resolver,
|
|
809
|
+
keys: [
|
|
810
|
+
"path",
|
|
811
|
+
"query",
|
|
812
|
+
"body"
|
|
813
|
+
]
|
|
814
|
+
});
|
|
815
|
+
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: groupedParam ? [groupedParam] : [] });
|
|
816
|
+
}
|
|
817
|
+
//#endregion
|
|
818
|
+
//#region ../../internals/tanstack-query/src/components/QueryKey.tsx
|
|
819
|
+
const declarationPrinter$9 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
820
|
+
const queryKeyTransformer = ({ node }) => {
|
|
821
|
+
if (!node.path) return [];
|
|
822
|
+
const hasPathParams = getOperationParameters(node).path.length > 0;
|
|
823
|
+
const hasQueryParams = getOperationParameters(node).query.length > 0;
|
|
824
|
+
const hasRequestBody = !!node.requestBody?.content?.[0]?.schema;
|
|
825
|
+
return [
|
|
826
|
+
hasPathParams ? `{ url: '${Url.toPath(node.path)}', params: path }` : `{ url: '${Url.toPath(node.path)}' }`,
|
|
827
|
+
hasQueryParams ? "...(query ? [query] : [])" : null,
|
|
828
|
+
hasRequestBody ? "...(body ? [body] : [])" : null
|
|
829
|
+
].filter(Boolean);
|
|
830
|
+
};
|
|
831
|
+
function QueryKey({ name, node, tsResolver, typeName, transformer }) {
|
|
832
|
+
const paramsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
833
|
+
const paramsSignature = declarationPrinter$9.print(paramsNode) ?? "";
|
|
834
|
+
const keys = (transformer ?? queryKeyTransformer)({
|
|
835
|
+
node,
|
|
836
|
+
casing: "camelcase"
|
|
837
|
+
});
|
|
838
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
839
|
+
name,
|
|
840
|
+
isExportable: true,
|
|
841
|
+
isIndexable: true,
|
|
842
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function.Arrow, {
|
|
843
|
+
name,
|
|
844
|
+
export: true,
|
|
845
|
+
params: paramsSignature,
|
|
846
|
+
singleLine: true,
|
|
847
|
+
children: `[${keys.join(", ")}] as const`
|
|
848
|
+
})
|
|
849
|
+
}), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
850
|
+
name: typeName,
|
|
851
|
+
isTypeOnly: true,
|
|
852
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Type, {
|
|
853
|
+
name: typeName,
|
|
854
|
+
children: `ReturnType<typeof ${name}>`
|
|
855
|
+
})
|
|
856
|
+
})] });
|
|
857
|
+
}
|
|
858
|
+
//#endregion
|
|
859
|
+
//#region src/generators/customHookOptionsFileGenerator.tsx
|
|
860
|
+
/**
|
|
861
|
+
* Scaffolds the user-editable `useCustomHookOptions` file when
|
|
862
|
+
* `pluginReactQuery({ customOptions: { ... } })` is configured. The file is
|
|
863
|
+
* only created when it does not already exist, so user edits persist across
|
|
864
|
+
* regeneration.
|
|
865
|
+
*/
|
|
866
|
+
const customHookOptionsFileGenerator = (0, _kubb_core.defineGenerator)({
|
|
867
|
+
name: "react-query-custom-hook-options-file",
|
|
868
|
+
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
869
|
+
operations(nodes, ctx) {
|
|
870
|
+
const { resolver, root } = ctx;
|
|
871
|
+
const { output, customOptions, query, group } = ctx.options;
|
|
872
|
+
if (!customOptions) return null;
|
|
873
|
+
const { importPath, name } = customOptions;
|
|
874
|
+
const hookOptionsName = resolver.resolveHookOptionsName();
|
|
875
|
+
const customHookOptionsName = resolver.resolveCustomHookOptionsName();
|
|
876
|
+
const reactQueryImportPath = query ? query.importPath : "@tanstack/react-query";
|
|
877
|
+
let hookFilePath;
|
|
878
|
+
const firstNode = nodes[0];
|
|
879
|
+
if (firstNode) {
|
|
880
|
+
const hookName = resolver.resolveQueryName(firstNode);
|
|
881
|
+
hookFilePath = resolver.resolveFile({
|
|
882
|
+
name: hookName,
|
|
883
|
+
extname: ".ts",
|
|
884
|
+
tag: firstNode.tags[0] ?? "default",
|
|
885
|
+
path: firstNode.path
|
|
886
|
+
}, {
|
|
887
|
+
root,
|
|
888
|
+
output,
|
|
889
|
+
group: group ?? void 0
|
|
890
|
+
}).path;
|
|
891
|
+
} else hookFilePath = node_path.default.resolve(root, "index.ts");
|
|
892
|
+
const ensureExtension = (filePath, extname) => {
|
|
893
|
+
if (node_path.default.extname(filePath) === "") return filePath + extname;
|
|
894
|
+
return filePath;
|
|
895
|
+
};
|
|
896
|
+
const basePath = node_path.default.dirname(hookFilePath);
|
|
897
|
+
const actualFilePath = ensureExtension(importPath, ".ts");
|
|
898
|
+
const file = {
|
|
899
|
+
baseName: node_path.default.basename(actualFilePath),
|
|
900
|
+
name: node_path.default.basename(actualFilePath, node_path.default.extname(actualFilePath)),
|
|
901
|
+
path: node_path.default.resolve(basePath, actualFilePath)
|
|
902
|
+
};
|
|
903
|
+
if (node_fs.default.existsSync(file.path)) return null;
|
|
904
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
905
|
+
baseName: file.baseName,
|
|
906
|
+
path: file.path,
|
|
907
|
+
children: [
|
|
908
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
909
|
+
name: ["QueryClient"],
|
|
910
|
+
path: reactQueryImportPath,
|
|
911
|
+
isTypeOnly: true
|
|
912
|
+
}),
|
|
913
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
914
|
+
name: ["useQueryClient"],
|
|
915
|
+
path: reactQueryImportPath
|
|
916
|
+
}),
|
|
917
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
918
|
+
name: [hookOptionsName],
|
|
919
|
+
root: file.path,
|
|
920
|
+
path: node_path.default.resolve(root, "./index.ts")
|
|
921
|
+
}),
|
|
922
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File.Source, {
|
|
923
|
+
name: file.name,
|
|
924
|
+
isExportable: true,
|
|
925
|
+
isIndexable: true,
|
|
926
|
+
children: [/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
927
|
+
name: customHookOptionsName,
|
|
928
|
+
params: "{ queryClient }: { queryClient: QueryClient }",
|
|
929
|
+
returnType: `Partial<${hookOptionsName}>`,
|
|
930
|
+
children: `return {
|
|
931
|
+
// TODO: Define custom hook options here
|
|
932
|
+
// Example:
|
|
933
|
+
// useUpdatePetHook: {
|
|
934
|
+
// onSuccess: () => {
|
|
935
|
+
// void queryClient.invalidateQueries({ queryKey: ['pet'] })
|
|
936
|
+
// }
|
|
937
|
+
// }
|
|
938
|
+
}`
|
|
939
|
+
}), /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
940
|
+
name,
|
|
941
|
+
generics: `T extends keyof ${hookOptionsName}`,
|
|
942
|
+
params: "{ hookName, operationId }: { hookName: T, operationId: string }",
|
|
943
|
+
returnType: `${hookOptionsName}[T]`,
|
|
944
|
+
export: true,
|
|
945
|
+
children: `const queryClient = useQueryClient()
|
|
946
|
+
const customOptions = ${customHookOptionsName}({ queryClient })
|
|
947
|
+
return customOptions[hookName] ?? {}`
|
|
948
|
+
})]
|
|
949
|
+
})
|
|
950
|
+
]
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
});
|
|
954
|
+
//#endregion
|
|
955
|
+
//#region src/generators/hookOptionsGenerator.tsx
|
|
956
|
+
/**
|
|
957
|
+
* Emits the `HookOptions` type used by `customOptions`. Enabled when
|
|
958
|
+
* `pluginReactQuery({ customOptions: { ... } })`. The generated type lists
|
|
959
|
+
* every hook keyed by name so user-supplied options stay in sync with the
|
|
960
|
+
* generated hooks at compile time.
|
|
961
|
+
*/
|
|
962
|
+
const hookOptionsGenerator = (0, _kubb_core.defineGenerator)({
|
|
963
|
+
name: "react-query-hook-options",
|
|
964
|
+
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
965
|
+
operations(nodes, ctx) {
|
|
966
|
+
const { resolver, config, root } = ctx;
|
|
967
|
+
const { output, customOptions, query, mutation, suspense, infinite, group, override } = ctx.options;
|
|
968
|
+
if (!customOptions) return null;
|
|
969
|
+
const name = resolver.resolveHookOptionsName();
|
|
970
|
+
const resolvedFile = resolver.resolveFile({
|
|
971
|
+
name,
|
|
972
|
+
extname: ".ts"
|
|
973
|
+
}, {
|
|
974
|
+
root,
|
|
975
|
+
output,
|
|
976
|
+
group: group ?? void 0
|
|
977
|
+
});
|
|
978
|
+
const hookOptionsFile = {
|
|
979
|
+
...resolvedFile,
|
|
980
|
+
baseName: `${name}.ts`,
|
|
981
|
+
path: resolvedFile.path.replace(/[^/\\]+\.ts$/, `${name}.ts`)
|
|
982
|
+
};
|
|
983
|
+
const imports = [];
|
|
984
|
+
const hookOptions = {};
|
|
985
|
+
for (const node of nodes) {
|
|
986
|
+
if (!_kubb_core.ast.isHttpOperationNode(node)) continue;
|
|
987
|
+
const opOverrides = resolveOperationOverrides(node, override);
|
|
988
|
+
const nodeQuery = "query" in opOverrides ? opOverrides.query : query;
|
|
989
|
+
const nodeMutation = "mutation" in opOverrides ? opOverrides.mutation : mutation;
|
|
990
|
+
const nodeInfinite = "infinite" in opOverrides ? opOverrides.infinite : infinite;
|
|
991
|
+
const nodeInfiniteOptions = nodeInfinite && typeof nodeInfinite === "object" ? nodeInfinite : null;
|
|
992
|
+
const isQueryOp = nodeQuery === false ? !!query && query.methods.some((m) => node.method.toLowerCase() === m.toLowerCase()) : !!nodeQuery && nodeQuery.methods.some((m) => node.method.toLowerCase() === m.toLowerCase());
|
|
993
|
+
const nodeQueryMethods = new Set(nodeQuery ? nodeQuery.methods : []);
|
|
994
|
+
const isMutationOp = nodeMutation !== false && !isQueryOp && (nodeMutation ? nodeMutation.methods : []).some((m) => !nodeQueryMethods.has(m) && node.method.toLowerCase() === m.toLowerCase());
|
|
995
|
+
const isSuspenseOp = !!suspense;
|
|
996
|
+
const isInfiniteOp = !!nodeInfiniteOptions;
|
|
997
|
+
if (isQueryOp) {
|
|
998
|
+
const queryOptionsName = resolver.resolveQueryOptionsName(node);
|
|
999
|
+
const queryHookName = resolver.resolveQueryName(node);
|
|
1000
|
+
const queryHookFile = resolver.resolveFile(operationFileEntry(node, queryHookName), {
|
|
1001
|
+
root,
|
|
1002
|
+
output,
|
|
1003
|
+
group: group ?? void 0
|
|
1004
|
+
});
|
|
1005
|
+
imports.push(/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1006
|
+
name: [queryOptionsName],
|
|
1007
|
+
root: hookOptionsFile.path,
|
|
1008
|
+
path: queryHookFile.path
|
|
1009
|
+
}));
|
|
1010
|
+
hookOptions[queryHookName] = `Partial<ReturnType<typeof ${queryOptionsName}>>`;
|
|
1011
|
+
if (isSuspenseOp) {
|
|
1012
|
+
const suspenseOptionsName = resolver.resolveSuspenseQueryOptionsName(node);
|
|
1013
|
+
const suspenseHookName = resolver.resolveSuspenseQueryName(node);
|
|
1014
|
+
const suspenseHookFile = resolver.resolveFile(operationFileEntry(node, suspenseHookName), {
|
|
1015
|
+
root,
|
|
1016
|
+
output,
|
|
1017
|
+
group: group ?? void 0
|
|
1018
|
+
});
|
|
1019
|
+
imports.push(/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1020
|
+
name: [suspenseOptionsName],
|
|
1021
|
+
root: hookOptionsFile.path,
|
|
1022
|
+
path: suspenseHookFile.path
|
|
1023
|
+
}));
|
|
1024
|
+
hookOptions[suspenseHookName] = `Partial<ReturnType<typeof ${suspenseOptionsName}>>`;
|
|
1025
|
+
}
|
|
1026
|
+
if (isInfiniteOp) {
|
|
1027
|
+
const normalizeKey = (key) => key.replace(/\?$/, "");
|
|
1028
|
+
const queryParamKeys = getOperationParameters(node, { paramsCasing: "original" }).query.map((p) => p.name);
|
|
1029
|
+
if (nodeInfiniteOptions.queryParam ? queryParamKeys.some((k) => normalizeKey(k) === nodeInfiniteOptions.queryParam) : false) {
|
|
1030
|
+
const infiniteOptionsName = resolver.resolveInfiniteQueryOptionsName(node);
|
|
1031
|
+
const infiniteHookName = resolver.resolveInfiniteQueryName(node);
|
|
1032
|
+
const infiniteHookFile = resolver.resolveFile(operationFileEntry(node, infiniteHookName), {
|
|
1033
|
+
root,
|
|
1034
|
+
output,
|
|
1035
|
+
group: group ?? void 0
|
|
1036
|
+
});
|
|
1037
|
+
imports.push(/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1038
|
+
name: [infiniteOptionsName],
|
|
1039
|
+
root: hookOptionsFile.path,
|
|
1040
|
+
path: infiniteHookFile.path
|
|
1041
|
+
}));
|
|
1042
|
+
hookOptions[infiniteHookName] = `Partial<ReturnType<typeof ${infiniteOptionsName}>>`;
|
|
1043
|
+
if (isSuspenseOp) {
|
|
1044
|
+
const suspenseInfiniteOptionsName = resolver.resolveSuspenseInfiniteQueryOptionsName(node);
|
|
1045
|
+
const suspenseInfiniteHookName = resolver.resolveSuspenseInfiniteQueryName(node);
|
|
1046
|
+
const suspenseInfiniteHookFile = resolver.resolveFile(operationFileEntry(node, suspenseInfiniteHookName), {
|
|
1047
|
+
root,
|
|
1048
|
+
output,
|
|
1049
|
+
group: group ?? void 0
|
|
1050
|
+
});
|
|
1051
|
+
imports.push(/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1052
|
+
name: [suspenseInfiniteOptionsName],
|
|
1053
|
+
root: hookOptionsFile.path,
|
|
1054
|
+
path: suspenseInfiniteHookFile.path
|
|
1055
|
+
}));
|
|
1056
|
+
hookOptions[suspenseInfiniteHookName] = `Partial<ReturnType<typeof ${suspenseInfiniteOptionsName}>>`;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
if (isMutationOp) {
|
|
1062
|
+
const mutationOptionsName = resolver.resolveMutationOptionsName(node);
|
|
1063
|
+
const mutationHookName = resolver.resolveMutationName(node);
|
|
1064
|
+
const mutationHookFile = resolver.resolveFile(operationFileEntry(node, mutationHookName), {
|
|
1065
|
+
root,
|
|
1066
|
+
output,
|
|
1067
|
+
group: group ?? void 0
|
|
1068
|
+
});
|
|
1069
|
+
imports.push(/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1070
|
+
name: [mutationOptionsName],
|
|
1071
|
+
root: hookOptionsFile.path,
|
|
1072
|
+
path: mutationHookFile.path
|
|
1073
|
+
}));
|
|
1074
|
+
hookOptions[mutationHookName] = `Partial<ReturnType<typeof ${mutationOptionsName}>>`;
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1078
|
+
baseName: hookOptionsFile.baseName,
|
|
1079
|
+
path: hookOptionsFile.path,
|
|
1080
|
+
meta: hookOptionsFile.meta,
|
|
1081
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
1082
|
+
output,
|
|
1083
|
+
config,
|
|
1084
|
+
file: {
|
|
1085
|
+
path: hookOptionsFile.path,
|
|
1086
|
+
baseName: hookOptionsFile.baseName
|
|
1087
|
+
}
|
|
1088
|
+
}),
|
|
1089
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
1090
|
+
output,
|
|
1091
|
+
config,
|
|
1092
|
+
file: {
|
|
1093
|
+
path: hookOptionsFile.path,
|
|
1094
|
+
baseName: hookOptionsFile.baseName
|
|
1095
|
+
}
|
|
1096
|
+
}),
|
|
1097
|
+
children: [imports, /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1098
|
+
name,
|
|
1099
|
+
isExportable: true,
|
|
1100
|
+
isIndexable: true,
|
|
1101
|
+
isTypeOnly: true,
|
|
1102
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Type, {
|
|
1103
|
+
export: true,
|
|
1104
|
+
name,
|
|
1105
|
+
children: `{ ${Object.keys(hookOptions).map((key) => `${JSON.stringify(key)}: ${hookOptions[key]}`).join(", ")} }`
|
|
1106
|
+
})
|
|
1107
|
+
})]
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
//#endregion
|
|
1112
|
+
//#region src/components/QueryOptions.tsx
|
|
1113
|
+
const declarationPrinter$8 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1114
|
+
const callPrinter$8 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1115
|
+
function getQueryOptionsParams(node, options) {
|
|
1116
|
+
return buildQueryOptionsParams(node, options);
|
|
1117
|
+
}
|
|
1118
|
+
function QueryOptions({ name, clientName, node, tsResolver, queryKeyName }) {
|
|
1119
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1120
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1121
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1122
|
+
const TData = responseName;
|
|
1123
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
1124
|
+
const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
1125
|
+
const queryKeyParamsCall = callPrinter$8.print(queryKeyParamsNode) ?? "";
|
|
1126
|
+
const paramsNode = getQueryOptionsParams(node, { resolver: tsResolver });
|
|
1127
|
+
const paramsSignature = declarationPrinter$8.print(paramsNode) ?? "";
|
|
1128
|
+
const queryFnBody = `const { data } = await ${buildClientCall(node, {
|
|
1129
|
+
clientName,
|
|
1130
|
+
signal: true
|
|
1131
|
+
})}
|
|
1132
|
+
return data`;
|
|
1133
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1134
|
+
name,
|
|
1135
|
+
isExportable: true,
|
|
1136
|
+
isIndexable: true,
|
|
1137
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1138
|
+
name,
|
|
1139
|
+
export: true,
|
|
1140
|
+
params: paramsSignature,
|
|
1141
|
+
children: `
|
|
1142
|
+
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
1143
|
+
return queryOptions<${TData}, ${TError}, ${TData}, typeof queryKey>({
|
|
1144
|
+
queryKey,
|
|
1145
|
+
queryFn: async ({ signal }) => {
|
|
1146
|
+
${queryFnBody}
|
|
1147
|
+
},
|
|
1148
|
+
})
|
|
1149
|
+
`
|
|
1150
|
+
})
|
|
1151
|
+
});
|
|
1152
|
+
}
|
|
1153
|
+
//#endregion
|
|
1154
|
+
//#region src/components/InfiniteQuery.tsx
|
|
1155
|
+
const declarationPrinter$7 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1156
|
+
const callPrinter$7 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1157
|
+
function buildInfiniteQueryParamsNode(node, options) {
|
|
1158
|
+
const { resolver, pageParamGeneric } = options;
|
|
1159
|
+
const optionsParam = (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
1160
|
+
name: "options",
|
|
1161
|
+
type: `{
|
|
1162
|
+
query?: Partial<InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient },
|
|
1163
|
+
client?: ${buildClientOptionType()}
|
|
1164
|
+
}`,
|
|
1165
|
+
default: "{}"
|
|
1166
|
+
});
|
|
1167
|
+
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [buildGroupedRequestParam(node, { resolver }), optionsParam].filter((param) => param !== null) });
|
|
1168
|
+
}
|
|
1169
|
+
function InfiniteQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, initialPageParam, queryParam, customOptions }) {
|
|
1170
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1171
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1172
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1173
|
+
const responseType = responseName;
|
|
1174
|
+
const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
1175
|
+
const isInitialPageParamDefined = initialPageParam !== void 0 && initialPageParam !== null;
|
|
1176
|
+
const fallbackPageParamType = typeof initialPageParam === "number" ? "number" : typeof initialPageParam === "string" ? initialPageParam.includes(" as ") ? (() => {
|
|
1177
|
+
const parts = initialPageParam.split(" as ");
|
|
1178
|
+
return parts[parts.length - 1] ?? "unknown";
|
|
1179
|
+
})() : "string" : typeof initialPageParam === "boolean" ? "boolean" : "unknown";
|
|
1180
|
+
const rawQueryParams = getOperationParameters(node, { paramsCasing: "original" }).query;
|
|
1181
|
+
const queryParamsTypeName = rawQueryParams.length > 0 ? (() => {
|
|
1182
|
+
const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]);
|
|
1183
|
+
return groupName !== tsResolver.resolveParamName(node, rawQueryParams[0]) ? groupName : null;
|
|
1184
|
+
})() : null;
|
|
1185
|
+
const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null;
|
|
1186
|
+
const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType;
|
|
1187
|
+
const returnType = "UseInfiniteQueryResult<TData, TError> & { queryKey: TQueryKey }";
|
|
1188
|
+
const generics = [
|
|
1189
|
+
`TQueryFnData = ${responseType}`,
|
|
1190
|
+
`TError = ${errorType}`,
|
|
1191
|
+
"TData = InfiniteData<TQueryFnData>",
|
|
1192
|
+
`TQueryKey extends QueryKey = ${queryKeyTypeName}`,
|
|
1193
|
+
`TPageParam = ${pageParamType}`
|
|
1194
|
+
];
|
|
1195
|
+
const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
1196
|
+
const queryKeyParamsCall = callPrinter$7.print(queryKeyParamsNode) ?? "";
|
|
1197
|
+
const queryOptionsParamsNode = getQueryOptionsParams(node, { resolver: tsResolver });
|
|
1198
|
+
const queryOptionsParamsCall = callPrinter$7.print(queryOptionsParamsNode) ?? "";
|
|
1199
|
+
const paramsNode = buildInfiniteQueryParamsNode(node, {
|
|
1200
|
+
resolver: tsResolver,
|
|
1201
|
+
pageParamGeneric: "TPageParam"
|
|
1202
|
+
});
|
|
1203
|
+
const paramsSignature = declarationPrinter$7.print(paramsNode) ?? "";
|
|
1204
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1205
|
+
name,
|
|
1206
|
+
isExportable: true,
|
|
1207
|
+
isIndexable: true,
|
|
1208
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1209
|
+
name,
|
|
1210
|
+
export: true,
|
|
1211
|
+
generics: generics.join(", "),
|
|
1212
|
+
params: paramsSignature,
|
|
1213
|
+
returnType: void 0,
|
|
1214
|
+
JSDoc: { comments: buildOperationComments(node) },
|
|
1215
|
+
children: `
|
|
1216
|
+
const { query: queryConfig = {}, client: config = {} } = options ?? {}
|
|
1217
|
+
const { client: queryClient, ...resolvedOptions } = queryConfig
|
|
1218
|
+
const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ""}
|
|
1219
|
+
|
|
1220
|
+
const queryResult = useInfiniteQuery({
|
|
1221
|
+
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? "\n ...customOptions," : ""}
|
|
1222
|
+
...resolvedOptions,
|
|
1223
|
+
queryKey,
|
|
1224
|
+
} as unknown as InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient) as ${returnType}
|
|
1225
|
+
|
|
1226
|
+
queryResult.queryKey = queryKey as TQueryKey
|
|
1227
|
+
|
|
1228
|
+
return queryResult
|
|
1229
|
+
`
|
|
1230
|
+
})
|
|
1231
|
+
});
|
|
1232
|
+
}
|
|
1233
|
+
//#endregion
|
|
1234
|
+
//#region src/components/InfiniteQueryOptions.tsx
|
|
1235
|
+
const declarationPrinter$6 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1236
|
+
const callPrinter$6 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1237
|
+
function InfiniteQueryOptions({ name, clientName, initialPageParam, cursorParam, nextParam, previousParam, node, tsResolver, queryParam, queryKeyName }) {
|
|
1238
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1239
|
+
const queryFnDataType = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1240
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1241
|
+
const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
1242
|
+
const isInitialPageParamDefined = initialPageParam !== void 0 && initialPageParam !== null;
|
|
1243
|
+
const fallbackPageParamType = typeof initialPageParam === "number" ? "number" : typeof initialPageParam === "string" ? initialPageParam.includes(" as ") ? (() => {
|
|
1244
|
+
const parts = initialPageParam.split(" as ");
|
|
1245
|
+
return parts[parts.length - 1] ?? "unknown";
|
|
1246
|
+
})() : "string" : typeof initialPageParam === "boolean" ? "boolean" : "unknown";
|
|
1247
|
+
const rawQueryParams = getOperationParameters(node, { paramsCasing: "original" }).query;
|
|
1248
|
+
const queryParamsTypeName = rawQueryParams.length > 0 ? (() => {
|
|
1249
|
+
const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]);
|
|
1250
|
+
return groupName !== tsResolver.resolveParamName(node, rawQueryParams[0]) ? groupName : null;
|
|
1251
|
+
})() : null;
|
|
1252
|
+
const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null;
|
|
1253
|
+
const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType;
|
|
1254
|
+
const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
1255
|
+
const queryKeyParamsCall = callPrinter$6.print(queryKeyParamsNode) ?? "";
|
|
1256
|
+
const paramsNode = getQueryOptionsParams(node, { resolver: tsResolver });
|
|
1257
|
+
const paramsSignature = declarationPrinter$6.print(paramsNode) ?? "";
|
|
1258
|
+
const queryFnBody = `const { data } = await ${buildClientCall(node, {
|
|
1259
|
+
clientName,
|
|
1260
|
+
signal: true
|
|
1261
|
+
})}
|
|
1262
|
+
return data`;
|
|
1263
|
+
const hasNewParams = nextParam != null || previousParam != null;
|
|
1264
|
+
const [getNextPageParamExpr, getPreviousPageParamExpr] = (() => {
|
|
1265
|
+
if (hasNewParams) {
|
|
1266
|
+
const nextAccessor = nextParam ? (0, _kubb_ast_utils.getNestedAccessor)(nextParam, "lastPage") : null;
|
|
1267
|
+
const prevAccessor = previousParam ? (0, _kubb_ast_utils.getNestedAccessor)(previousParam, "firstPage") : null;
|
|
1268
|
+
return [nextAccessor ? `getNextPageParam: (lastPage) => ${nextAccessor}` : null, prevAccessor ? `getPreviousPageParam: (firstPage) => ${prevAccessor}` : null];
|
|
1269
|
+
}
|
|
1270
|
+
if (cursorParam) return [`getNextPageParam: (lastPage) => lastPage['${cursorParam}']`, `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`];
|
|
1271
|
+
return ["getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1", "getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1"];
|
|
1272
|
+
})();
|
|
1273
|
+
const queryOptionsArr = [
|
|
1274
|
+
`initialPageParam: ${typeof initialPageParam === "string" ? JSON.stringify(initialPageParam) : initialPageParam}`,
|
|
1275
|
+
getNextPageParamExpr,
|
|
1276
|
+
getPreviousPageParamExpr
|
|
1277
|
+
].filter(Boolean);
|
|
1278
|
+
const infiniteOverrideParams = queryParam && queryParamsTypeName ? `query = {
|
|
1279
|
+
...(query ?? {}),
|
|
1280
|
+
['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
|
|
1281
|
+
} as ${queryParamsTypeName}` : "";
|
|
1282
|
+
if (infiniteOverrideParams) return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1283
|
+
name,
|
|
1284
|
+
isExportable: true,
|
|
1285
|
+
isIndexable: true,
|
|
1286
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1287
|
+
name,
|
|
1288
|
+
export: true,
|
|
1289
|
+
params: paramsSignature,
|
|
1290
|
+
children: `
|
|
1291
|
+
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
1292
|
+
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
1293
|
+
queryKey,
|
|
1294
|
+
queryFn: async ({ signal, pageParam }) => {
|
|
1295
|
+
${infiniteOverrideParams}
|
|
1296
|
+
${queryFnBody}
|
|
1297
|
+
},
|
|
1298
|
+
${queryOptionsArr.join(",\n ")}
|
|
1299
|
+
})
|
|
1300
|
+
`
|
|
1301
|
+
})
|
|
1302
|
+
});
|
|
1303
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1304
|
+
name,
|
|
1305
|
+
isExportable: true,
|
|
1306
|
+
isIndexable: true,
|
|
1307
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1308
|
+
name,
|
|
1309
|
+
export: true,
|
|
1310
|
+
params: paramsSignature,
|
|
1311
|
+
children: `
|
|
1312
|
+
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
1313
|
+
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
1314
|
+
queryKey,
|
|
1315
|
+
queryFn: async ({ signal }) => {
|
|
1316
|
+
${queryFnBody}
|
|
1317
|
+
},
|
|
1318
|
+
${queryOptionsArr.join(",\n ")}
|
|
1319
|
+
})
|
|
1320
|
+
`
|
|
1321
|
+
})
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
//#endregion
|
|
1325
|
+
//#region src/components/MutationOptions.tsx
|
|
1326
|
+
const declarationPrinter$5 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1327
|
+
const callPrinter$5 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1328
|
+
function buildMutationConfigParamsNode(node) {
|
|
1329
|
+
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [(0, _kubb_plugin_ts.createFunctionParameter)({
|
|
1330
|
+
name: "config",
|
|
1331
|
+
type: buildRequestConfigType(node),
|
|
1332
|
+
default: "{}"
|
|
1333
|
+
})] });
|
|
1334
|
+
}
|
|
1335
|
+
function MutationOptions({ name, clientName, node, tsResolver, mutationKeyName }) {
|
|
1336
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1337
|
+
const TData = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1338
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1339
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
1340
|
+
const configParamsNode = buildMutationConfigParamsNode(node);
|
|
1341
|
+
const paramsSignature = declarationPrinter$5.print(configParamsNode) ?? "";
|
|
1342
|
+
const groupedParam = buildGroupedRequestParam(node, { resolver: tsResolver });
|
|
1343
|
+
const hasMutationParams = groupedParam !== null;
|
|
1344
|
+
const groupedParamsNode = (0, _kubb_plugin_ts.createFunctionParameters)({ params: groupedParam ? [groupedParam] : [] });
|
|
1345
|
+
const TRequest = hasMutationParams ? tsResolver.resolveRequestConfigName(node) : "undefined";
|
|
1346
|
+
const argBindingStr = hasMutationParams ? callPrinter$5.print(groupedParamsNode) ?? "" : "_";
|
|
1347
|
+
const mutationFnBody = `const { data } = await ${buildClientCall(node, {
|
|
1348
|
+
clientName,
|
|
1349
|
+
signal: false
|
|
1350
|
+
})}
|
|
1351
|
+
return data`;
|
|
1352
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1353
|
+
name,
|
|
1354
|
+
isExportable: true,
|
|
1355
|
+
isIndexable: true,
|
|
1356
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1357
|
+
name,
|
|
1358
|
+
export: true,
|
|
1359
|
+
params: paramsSignature,
|
|
1360
|
+
generics: ["TContext = unknown"],
|
|
1361
|
+
children: `
|
|
1362
|
+
const mutationKey = ${mutationKeyName}()
|
|
1363
|
+
return mutationOptions<${TData}, ${TError}, ${TRequest}, TContext>({
|
|
1364
|
+
mutationKey,
|
|
1365
|
+
mutationFn: async(${argBindingStr}) => {
|
|
1366
|
+
${mutationFnBody}
|
|
1367
|
+
},
|
|
1368
|
+
})
|
|
1369
|
+
`
|
|
1370
|
+
})
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
//#endregion
|
|
1374
|
+
//#region src/components/Mutation.tsx
|
|
1375
|
+
const declarationPrinter$4 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1376
|
+
const callPrinter$4 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1377
|
+
function resolveMutationRequestType(node, resolver) {
|
|
1378
|
+
return buildGroupedRequestParam(node, { resolver }) ? resolver.resolveRequestConfigName(node) : "undefined";
|
|
1379
|
+
}
|
|
1380
|
+
function buildMutationParamsNode(node, options) {
|
|
1381
|
+
const { resolver } = options;
|
|
1382
|
+
const successNames = resolveSuccessNames(node, resolver);
|
|
1383
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : resolver.resolveResponseName(node);
|
|
1384
|
+
const errorNames = resolveErrorNames(node, resolver);
|
|
1385
|
+
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [(0, _kubb_plugin_ts.createFunctionParameter)({
|
|
1386
|
+
name: "options",
|
|
1387
|
+
type: `{
|
|
1388
|
+
mutation?: UseMutationOptions<${[
|
|
1389
|
+
responseName,
|
|
1390
|
+
`ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`,
|
|
1391
|
+
resolveMutationRequestType(node, resolver),
|
|
1392
|
+
"TContext"
|
|
1393
|
+
].join(", ")}> & { client?: QueryClient },
|
|
1394
|
+
client?: ${buildRequestConfigType(node)},
|
|
1395
|
+
}`,
|
|
1396
|
+
default: "{}"
|
|
1397
|
+
})] });
|
|
1398
|
+
}
|
|
1399
|
+
function Mutation({ name, mutationOptionsName, node, tsResolver, mutationKeyName, customOptions }) {
|
|
1400
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1401
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1402
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1403
|
+
const generics = [
|
|
1404
|
+
responseName,
|
|
1405
|
+
`ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`,
|
|
1406
|
+
resolveMutationRequestType(node, tsResolver),
|
|
1407
|
+
"TContext"
|
|
1408
|
+
].join(", ");
|
|
1409
|
+
const returnType = `UseMutationResult<${generics}>`;
|
|
1410
|
+
const mutationOptionsConfigNode = buildMutationConfigParamsNode(node);
|
|
1411
|
+
const mutationOptionsParamsCall = callPrinter$4.print(mutationOptionsConfigNode) ?? "";
|
|
1412
|
+
const paramsNode = buildMutationParamsNode(node, { resolver: tsResolver });
|
|
1413
|
+
const paramsSignature = declarationPrinter$4.print(paramsNode) ?? "";
|
|
1414
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1415
|
+
name,
|
|
1416
|
+
isExportable: true,
|
|
1417
|
+
isIndexable: true,
|
|
1418
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1419
|
+
name,
|
|
1420
|
+
export: true,
|
|
1421
|
+
params: paramsSignature,
|
|
1422
|
+
JSDoc: { comments: buildOperationComments(node) },
|
|
1423
|
+
generics: ["TContext"],
|
|
1424
|
+
children: `
|
|
1425
|
+
const { mutation = {}, client: config = {} } = options ?? {}
|
|
1426
|
+
const { client: queryClient, ...mutationOptions } = mutation;
|
|
1427
|
+
const mutationKey = mutationOptions.mutationKey ?? ${mutationKeyName}()
|
|
1428
|
+
|
|
1429
|
+
const baseOptions = ${mutationOptionsName}(${mutationOptionsParamsCall}) as UseMutationOptions<${generics}>${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' }) as UseMutationOptions<${generics}>` : ""}
|
|
1430
|
+
|
|
1431
|
+
return useMutation<${generics}>({
|
|
1432
|
+
...baseOptions,${customOptions ? "\n ...customOptions," : ""}
|
|
1433
|
+
mutationKey,
|
|
1434
|
+
...mutationOptions,
|
|
1435
|
+
}, queryClient) as ${returnType}
|
|
1436
|
+
`
|
|
1437
|
+
})
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
//#endregion
|
|
1441
|
+
//#region src/components/Query.tsx
|
|
1442
|
+
const declarationPrinter$3 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1443
|
+
const callPrinter$3 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1444
|
+
function buildQueryParamsNode(node, options) {
|
|
1445
|
+
const { resolver } = options;
|
|
1446
|
+
const successNames = resolveSuccessNames(node, resolver);
|
|
1447
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : resolver.resolveResponseName(node);
|
|
1448
|
+
const errorNames = resolveErrorNames(node, resolver);
|
|
1449
|
+
const optionsParam = (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
1450
|
+
name: "options",
|
|
1451
|
+
type: `{
|
|
1452
|
+
query?: Partial<QueryObserverOptions<${[
|
|
1453
|
+
responseName,
|
|
1454
|
+
`ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`,
|
|
1455
|
+
"TData",
|
|
1456
|
+
"TQueryData",
|
|
1457
|
+
"TQueryKey"
|
|
1458
|
+
].join(", ")}>> & { client?: QueryClient },
|
|
1459
|
+
client?: ${buildClientOptionType()}
|
|
1460
|
+
}`,
|
|
1461
|
+
default: "{}"
|
|
1462
|
+
});
|
|
1463
|
+
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [buildGroupedRequestParam(node, { resolver }), optionsParam].filter((param) => param !== null) });
|
|
1464
|
+
}
|
|
1465
|
+
function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, customOptions }) {
|
|
1466
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1467
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1468
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1469
|
+
const TData = responseName;
|
|
1470
|
+
const returnType = `UseQueryResult<TData, ${`ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`}> & { queryKey: TQueryKey }`;
|
|
1471
|
+
const generics = [
|
|
1472
|
+
`TData = ${TData}`,
|
|
1473
|
+
`TQueryData = ${TData}`,
|
|
1474
|
+
`TQueryKey extends QueryKey = ${queryKeyTypeName}`
|
|
1475
|
+
];
|
|
1476
|
+
const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
1477
|
+
const queryKeyParamsCall = callPrinter$3.print(queryKeyParamsNode) ?? "";
|
|
1478
|
+
const queryOptionsParamsNode = getQueryOptionsParams(node, { resolver: tsResolver });
|
|
1479
|
+
const queryOptionsParamsCall = callPrinter$3.print(queryOptionsParamsNode) ?? "";
|
|
1480
|
+
const paramsNode = buildQueryParamsNode(node, { resolver: tsResolver });
|
|
1481
|
+
const paramsSignature = declarationPrinter$3.print(paramsNode) ?? "";
|
|
1482
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1483
|
+
name,
|
|
1484
|
+
isExportable: true,
|
|
1485
|
+
isIndexable: true,
|
|
1486
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1487
|
+
name,
|
|
1488
|
+
export: true,
|
|
1489
|
+
generics: generics.join(", "),
|
|
1490
|
+
params: paramsSignature,
|
|
1491
|
+
returnType: void 0,
|
|
1492
|
+
JSDoc: { comments: buildOperationComments(node) },
|
|
1493
|
+
children: `
|
|
1494
|
+
const { query: queryConfig = {}, client: config = {} } = options ?? {}
|
|
1495
|
+
const { client: queryClient, ...resolvedOptions } = queryConfig
|
|
1496
|
+
const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ""}
|
|
1497
|
+
|
|
1498
|
+
const queryResult = useQuery({
|
|
1499
|
+
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? "\n ...customOptions," : ""}
|
|
1500
|
+
...resolvedOptions,
|
|
1501
|
+
queryKey,
|
|
1502
|
+
} as unknown as QueryObserverOptions, queryClient) as ${returnType}
|
|
1503
|
+
|
|
1504
|
+
queryResult.queryKey = queryKey as TQueryKey
|
|
1505
|
+
|
|
1506
|
+
return queryResult
|
|
1507
|
+
`
|
|
1508
|
+
})
|
|
1509
|
+
});
|
|
1510
|
+
}
|
|
1511
|
+
//#endregion
|
|
1512
|
+
//#region src/components/SuspenseInfiniteQuery.tsx
|
|
1513
|
+
const declarationPrinter$2 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1514
|
+
const callPrinter$2 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1515
|
+
function buildSuspenseInfiniteQueryParamsNode(node, options) {
|
|
1516
|
+
const { resolver, pageParamGeneric } = options;
|
|
1517
|
+
const optionsParam = (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
1518
|
+
name: "options",
|
|
1519
|
+
type: `{
|
|
1520
|
+
query?: Partial<UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient },
|
|
1521
|
+
client?: ${buildClientOptionType()}
|
|
1522
|
+
}`,
|
|
1523
|
+
default: "{}"
|
|
1524
|
+
});
|
|
1525
|
+
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [buildGroupedRequestParam(node, { resolver }), optionsParam].filter((param) => param !== null) });
|
|
1526
|
+
}
|
|
1527
|
+
function SuspenseInfiniteQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, customOptions, initialPageParam, queryParam }) {
|
|
1528
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1529
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1530
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1531
|
+
const responseType = responseName;
|
|
1532
|
+
const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
1533
|
+
const isInitialPageParamDefined = initialPageParam !== void 0 && initialPageParam !== null;
|
|
1534
|
+
const fallbackPageParamType = typeof initialPageParam === "number" ? "number" : typeof initialPageParam === "string" ? initialPageParam.includes(" as ") ? (() => {
|
|
1535
|
+
const parts = initialPageParam.split(" as ");
|
|
1536
|
+
return parts[parts.length - 1] ?? "unknown";
|
|
1537
|
+
})() : "string" : typeof initialPageParam === "boolean" ? "boolean" : "unknown";
|
|
1538
|
+
const rawQueryParams = getOperationParameters(node, { paramsCasing: "original" }).query;
|
|
1539
|
+
const queryParamsTypeName = rawQueryParams.length > 0 ? (() => {
|
|
1540
|
+
const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]);
|
|
1541
|
+
return groupName !== tsResolver.resolveParamName(node, rawQueryParams[0]) ? groupName : null;
|
|
1542
|
+
})() : null;
|
|
1543
|
+
const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null;
|
|
1544
|
+
const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType;
|
|
1545
|
+
const returnType = "UseSuspenseInfiniteQueryResult<TData, TError> & { queryKey: TQueryKey }";
|
|
1546
|
+
const generics = [
|
|
1547
|
+
`TQueryFnData = ${responseType}`,
|
|
1548
|
+
`TError = ${errorType}`,
|
|
1549
|
+
"TData = InfiniteData<TQueryFnData>",
|
|
1550
|
+
`TQueryKey extends QueryKey = ${queryKeyTypeName}`,
|
|
1551
|
+
`TPageParam = ${pageParamType}`
|
|
1552
|
+
];
|
|
1553
|
+
const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
1554
|
+
const queryKeyParamsCall = callPrinter$2.print(queryKeyParamsNode) ?? "";
|
|
1555
|
+
const queryOptionsParamsNode = getQueryOptionsParams(node, { resolver: tsResolver });
|
|
1556
|
+
const queryOptionsParamsCall = callPrinter$2.print(queryOptionsParamsNode) ?? "";
|
|
1557
|
+
const paramsNode = buildSuspenseInfiniteQueryParamsNode(node, {
|
|
1558
|
+
resolver: tsResolver,
|
|
1559
|
+
pageParamGeneric: "TPageParam"
|
|
1560
|
+
});
|
|
1561
|
+
const paramsSignature = declarationPrinter$2.print(paramsNode) ?? "";
|
|
1562
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1563
|
+
name,
|
|
1564
|
+
isExportable: true,
|
|
1565
|
+
isIndexable: true,
|
|
1566
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1567
|
+
name,
|
|
1568
|
+
export: true,
|
|
1569
|
+
generics: generics.join(", "),
|
|
1570
|
+
params: paramsSignature,
|
|
1571
|
+
returnType: void 0,
|
|
1572
|
+
JSDoc: { comments: buildOperationComments(node) },
|
|
1573
|
+
children: `
|
|
1574
|
+
const { query: queryConfig = {}, client: config = {} } = options ?? {}
|
|
1575
|
+
const { client: queryClient, ...resolvedOptions } = queryConfig
|
|
1576
|
+
const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ""}
|
|
1577
|
+
|
|
1578
|
+
const queryResult = useSuspenseInfiniteQuery({
|
|
1579
|
+
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? "\n ...customOptions," : ""}
|
|
1580
|
+
...resolvedOptions,
|
|
1581
|
+
queryKey,
|
|
1582
|
+
} as unknown as UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient) as ${returnType}
|
|
1583
|
+
|
|
1584
|
+
queryResult.queryKey = queryKey as TQueryKey
|
|
1585
|
+
|
|
1586
|
+
return queryResult
|
|
1587
|
+
`
|
|
1588
|
+
})
|
|
1589
|
+
});
|
|
1590
|
+
}
|
|
1591
|
+
//#endregion
|
|
1592
|
+
//#region src/components/SuspenseInfiniteQueryOptions.tsx
|
|
1593
|
+
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1594
|
+
const callPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1595
|
+
function SuspenseInfiniteQueryOptions({ name, clientName, initialPageParam, cursorParam, nextParam, previousParam, node, tsResolver, queryParam, queryKeyName }) {
|
|
1596
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1597
|
+
const queryFnDataType = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1598
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1599
|
+
const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`;
|
|
1600
|
+
const isInitialPageParamDefined = initialPageParam !== void 0 && initialPageParam !== null;
|
|
1601
|
+
const fallbackPageParamType = typeof initialPageParam === "number" ? "number" : typeof initialPageParam === "string" ? initialPageParam.includes(" as ") ? (() => {
|
|
1602
|
+
const parts = initialPageParam.split(" as ");
|
|
1603
|
+
return parts[parts.length - 1] ?? "unknown";
|
|
1604
|
+
})() : "string" : typeof initialPageParam === "boolean" ? "boolean" : "unknown";
|
|
1605
|
+
const rawQueryParams = getOperationParameters(node, { paramsCasing: "original" }).query;
|
|
1606
|
+
const queryParamsTypeName = rawQueryParams.length > 0 ? (() => {
|
|
1607
|
+
const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]);
|
|
1608
|
+
return groupName !== tsResolver.resolveParamName(node, rawQueryParams[0]) ? groupName : null;
|
|
1609
|
+
})() : null;
|
|
1610
|
+
const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null;
|
|
1611
|
+
const pageParamType = queryParamType ? isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType : fallbackPageParamType;
|
|
1612
|
+
const paramsNode = getQueryOptionsParams(node, { resolver: tsResolver });
|
|
1613
|
+
const paramsSignature = declarationPrinter$1.print(paramsNode) ?? "";
|
|
1614
|
+
const queryFnBody = `const { data } = await ${buildClientCall(node, {
|
|
1615
|
+
clientName,
|
|
1616
|
+
signal: true
|
|
1617
|
+
})}
|
|
1618
|
+
return data`;
|
|
1619
|
+
const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
1620
|
+
const queryKeyParamsCall = callPrinter$1.print(queryKeyParamsNode) ?? "";
|
|
1621
|
+
const hasNewParams = nextParam != null || previousParam != null;
|
|
1622
|
+
const [getNextPageParamExpr, getPreviousPageParamExpr] = (() => {
|
|
1623
|
+
if (hasNewParams) {
|
|
1624
|
+
const nextAccessor = nextParam ? (0, _kubb_ast_utils.getNestedAccessor)(nextParam, "lastPage") : null;
|
|
1625
|
+
const prevAccessor = previousParam ? (0, _kubb_ast_utils.getNestedAccessor)(previousParam, "firstPage") : null;
|
|
1626
|
+
return [nextAccessor ? `getNextPageParam: (lastPage) => ${nextAccessor}` : null, prevAccessor ? `getPreviousPageParam: (firstPage) => ${prevAccessor}` : null];
|
|
1627
|
+
}
|
|
1628
|
+
if (cursorParam) return [`getNextPageParam: (lastPage) => lastPage['${cursorParam}']`, `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`];
|
|
1629
|
+
return ["getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1", "getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1"];
|
|
1630
|
+
})();
|
|
1631
|
+
const queryOptionsArr = [
|
|
1632
|
+
`initialPageParam: ${typeof initialPageParam === "string" ? JSON.stringify(initialPageParam) : initialPageParam}`,
|
|
1633
|
+
getNextPageParamExpr,
|
|
1634
|
+
getPreviousPageParamExpr
|
|
1635
|
+
].filter(Boolean);
|
|
1636
|
+
const infiniteOverrideParams = queryParam && queryParamsTypeName ? `query = {
|
|
1637
|
+
...(query ?? {}),
|
|
1638
|
+
['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
|
|
1639
|
+
} as ${queryParamsTypeName}` : "";
|
|
1640
|
+
if (infiniteOverrideParams) return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1641
|
+
name,
|
|
1642
|
+
isExportable: true,
|
|
1643
|
+
isIndexable: true,
|
|
1644
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1645
|
+
name,
|
|
1646
|
+
export: true,
|
|
1647
|
+
params: paramsSignature,
|
|
1648
|
+
children: `
|
|
1649
|
+
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
1650
|
+
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
1651
|
+
queryKey,
|
|
1652
|
+
queryFn: async ({ signal, pageParam }) => {
|
|
1653
|
+
${infiniteOverrideParams}
|
|
1654
|
+
${queryFnBody}
|
|
1655
|
+
},
|
|
1656
|
+
${queryOptionsArr.join(",\n ")}
|
|
1657
|
+
})
|
|
1658
|
+
`
|
|
1659
|
+
})
|
|
1660
|
+
});
|
|
1661
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1662
|
+
name,
|
|
1663
|
+
isExportable: true,
|
|
1664
|
+
isIndexable: true,
|
|
1665
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1666
|
+
name,
|
|
1667
|
+
export: true,
|
|
1668
|
+
params: paramsSignature,
|
|
1669
|
+
children: `
|
|
1670
|
+
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
1671
|
+
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
1672
|
+
queryKey,
|
|
1673
|
+
queryFn: async ({ signal }) => {
|
|
1674
|
+
${queryFnBody}
|
|
1675
|
+
},
|
|
1676
|
+
${queryOptionsArr.join(",\n ")}
|
|
1677
|
+
})
|
|
1678
|
+
`
|
|
1679
|
+
})
|
|
1680
|
+
});
|
|
1681
|
+
}
|
|
1682
|
+
//#endregion
|
|
1683
|
+
//#region src/components/SuspenseQuery.tsx
|
|
1684
|
+
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
1685
|
+
const callPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
|
|
1686
|
+
function buildSuspenseQueryParamsNode(node, options) {
|
|
1687
|
+
const { resolver } = options;
|
|
1688
|
+
const successNames = resolveSuccessNames(node, resolver);
|
|
1689
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : resolver.resolveResponseName(node);
|
|
1690
|
+
const errorNames = resolveErrorNames(node, resolver);
|
|
1691
|
+
const optionsParam = (0, _kubb_plugin_ts.createFunctionParameter)({
|
|
1692
|
+
name: "options",
|
|
1693
|
+
type: `{
|
|
1694
|
+
query?: Partial<UseSuspenseQueryOptions<${[
|
|
1695
|
+
responseName,
|
|
1696
|
+
`ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`,
|
|
1697
|
+
"TData",
|
|
1698
|
+
"TQueryKey"
|
|
1699
|
+
].join(", ")}>> & { client?: QueryClient },
|
|
1700
|
+
client?: ${buildClientOptionType()}
|
|
1701
|
+
}`,
|
|
1702
|
+
default: "{}"
|
|
1703
|
+
});
|
|
1704
|
+
return (0, _kubb_plugin_ts.createFunctionParameters)({ params: [buildGroupedRequestParam(node, { resolver }), optionsParam].filter((param) => param !== null) });
|
|
1705
|
+
}
|
|
1706
|
+
function SuspenseQuery({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, tsResolver, customOptions }) {
|
|
1707
|
+
const successNames = resolveSuccessNames(node, tsResolver);
|
|
1708
|
+
const responseName = successNames.length > 0 ? successNames.join(" | ") : tsResolver.resolveResponseName(node);
|
|
1709
|
+
const errorNames = resolveErrorNames(node, tsResolver);
|
|
1710
|
+
const TData = responseName;
|
|
1711
|
+
const returnType = `UseSuspenseQueryResult<TData, ${`ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(" | ") : "Error"}>`}> & { queryKey: TQueryKey }`;
|
|
1712
|
+
const generics = [`TData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`];
|
|
1713
|
+
const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
|
|
1714
|
+
const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? "";
|
|
1715
|
+
const queryOptionsParamsNode = getQueryOptionsParams(node, { resolver: tsResolver });
|
|
1716
|
+
const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? "";
|
|
1717
|
+
const paramsNode = buildSuspenseQueryParamsNode(node, { resolver: tsResolver });
|
|
1718
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
|
|
1719
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
1720
|
+
name,
|
|
1721
|
+
isExportable: true,
|
|
1722
|
+
isIndexable: true,
|
|
1723
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
1724
|
+
name,
|
|
1725
|
+
export: true,
|
|
1726
|
+
generics: generics.join(", "),
|
|
1727
|
+
params: paramsSignature,
|
|
1728
|
+
returnType: void 0,
|
|
1729
|
+
JSDoc: { comments: buildOperationComments(node) },
|
|
1730
|
+
children: `
|
|
1731
|
+
const { query: queryConfig = {}, client: config = {} } = options ?? {}
|
|
1732
|
+
const { client: queryClient, ...resolvedOptions } = queryConfig
|
|
1733
|
+
const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ""}
|
|
1734
|
+
|
|
1735
|
+
const queryResult = useSuspenseQuery({
|
|
1736
|
+
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? "\n ...customOptions," : ""}
|
|
1737
|
+
...resolvedOptions,
|
|
1738
|
+
queryKey,
|
|
1739
|
+
} as unknown as UseSuspenseQueryOptions, queryClient) as ${returnType}
|
|
1740
|
+
|
|
1741
|
+
queryResult.queryKey = queryKey as TQueryKey
|
|
1742
|
+
|
|
1743
|
+
return queryResult
|
|
1744
|
+
`
|
|
1745
|
+
})
|
|
1746
|
+
});
|
|
1747
|
+
}
|
|
1748
|
+
//#endregion
|
|
1749
|
+
//#region src/generators/infiniteQueryGenerator.tsx
|
|
1750
|
+
/**
|
|
1751
|
+
* Built-in generator for `useInfiniteQuery` hooks. Enabled when
|
|
1752
|
+
* `pluginReactQuery({ infinite: { ... } })`. Emits one `useFooInfiniteQuery`
|
|
1753
|
+
* hook per query operation, wiring the configured `nextParam` /
|
|
1754
|
+
* `previousParam` paths into TanStack Query's cursor-based pagination.
|
|
1755
|
+
*/
|
|
1756
|
+
const infiniteQueryGenerator = (0, _kubb_core.defineGenerator)({
|
|
1757
|
+
name: "react-infinite-query",
|
|
1758
|
+
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
1759
|
+
operation(node, ctx) {
|
|
1760
|
+
if (!_kubb_core.ast.isHttpOperationNode(node)) return null;
|
|
1761
|
+
const { config, driver, resolver, root } = ctx;
|
|
1762
|
+
const { output, query, mutation, infinite, parser, client, group, customOptions } = ctx.options;
|
|
1763
|
+
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1764
|
+
if (!pluginTs) return null;
|
|
1765
|
+
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
1766
|
+
const isQuery = query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
|
|
1767
|
+
const queryMethods = new Set(query ? query.methods : []);
|
|
1768
|
+
const isMutation = mutation !== false && !isQuery && (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase());
|
|
1769
|
+
const infiniteOptions = infinite && typeof infinite === "object" ? infinite : null;
|
|
1770
|
+
if (!isQuery || isMutation || !infiniteOptions) return null;
|
|
1771
|
+
const normalizeKey = (key) => key.replace(/\?$/, "");
|
|
1772
|
+
const queryParamKeys = getOperationParameters(node, { paramsCasing: "original" }).query.map((p) => p.name);
|
|
1773
|
+
const hasQueryParam = infiniteOptions.queryParam ? queryParamKeys.some((k) => normalizeKey(k) === infiniteOptions.queryParam) : false;
|
|
1774
|
+
const hasCursorParam = !infiniteOptions.cursorParam || true;
|
|
1775
|
+
if (!hasQueryParam || !hasCursorParam) return null;
|
|
1776
|
+
const importPath = query ? query.importPath : "@tanstack/react-query";
|
|
1777
|
+
const contractOp = resolveClientOperation({
|
|
1778
|
+
clientPlugin: { pluginName: client.pluginName },
|
|
1779
|
+
driver,
|
|
1780
|
+
node,
|
|
1781
|
+
root,
|
|
1782
|
+
output
|
|
1783
|
+
});
|
|
1784
|
+
if (!contractOp) return null;
|
|
1785
|
+
const queryName = resolver.resolveInfiniteQueryName(node);
|
|
1786
|
+
const queryOptionsName = resolver.resolveInfiniteQueryOptionsName(node);
|
|
1787
|
+
const queryKeyName = resolver.resolveInfiniteQueryKeyName(node);
|
|
1788
|
+
const queryKeyTypeName = resolver.resolveInfiniteQueryKeyTypeName(node);
|
|
1789
|
+
const meta = {
|
|
1790
|
+
file: resolver.resolveFile(operationFileEntry(node, queryName), {
|
|
1791
|
+
root,
|
|
1792
|
+
output,
|
|
1793
|
+
group: group ?? void 0
|
|
1794
|
+
}),
|
|
1795
|
+
fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1796
|
+
root,
|
|
1797
|
+
output: pluginTs.options?.output ?? output,
|
|
1798
|
+
group: pluginTs.options?.group ?? void 0
|
|
1799
|
+
})
|
|
1800
|
+
};
|
|
1801
|
+
const rawQueryParams = getOperationParameters(node, { paramsCasing: "original" }).query;
|
|
1802
|
+
const queryParamsTypeName = rawQueryParams.length > 0 && tsResolver.resolveQueryParamsName(node, rawQueryParams[0]) !== tsResolver.resolveParamName(node, rawQueryParams[0]) ? tsResolver.resolveQueryParamsName(node, rawQueryParams[0]) : null;
|
|
1803
|
+
const importedTypeNames = [
|
|
1804
|
+
tsResolver.resolveRequestConfigName(node),
|
|
1805
|
+
queryParamsTypeName,
|
|
1806
|
+
...resolveOperationTypeNames(node, tsResolver, {
|
|
1807
|
+
exclude: [queryKeyTypeName],
|
|
1808
|
+
order: "body-response-first",
|
|
1809
|
+
includeParams: false
|
|
1810
|
+
})
|
|
1811
|
+
].filter((name) => Boolean(name));
|
|
1812
|
+
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
1813
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
1814
|
+
const fileZod = zodResolver ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1815
|
+
root,
|
|
1816
|
+
output: pluginZod?.options?.output ?? output,
|
|
1817
|
+
group: pluginZod?.options?.group ?? void 0
|
|
1818
|
+
}) : null;
|
|
1819
|
+
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser);
|
|
1820
|
+
const calledClientName = contractOp.name;
|
|
1821
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1822
|
+
baseName: meta.file.baseName,
|
|
1823
|
+
path: meta.file.path,
|
|
1824
|
+
meta: meta.file.meta,
|
|
1825
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
1826
|
+
output,
|
|
1827
|
+
config,
|
|
1828
|
+
file: {
|
|
1829
|
+
path: meta.file.path,
|
|
1830
|
+
baseName: meta.file.baseName
|
|
1831
|
+
}
|
|
1832
|
+
}),
|
|
1833
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
1834
|
+
output,
|
|
1835
|
+
config,
|
|
1836
|
+
file: {
|
|
1837
|
+
path: meta.file.path,
|
|
1838
|
+
baseName: meta.file.baseName
|
|
1839
|
+
}
|
|
1840
|
+
}),
|
|
1841
|
+
children: [
|
|
1842
|
+
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1843
|
+
name: zodSchemaNames,
|
|
1844
|
+
root: meta.file.path,
|
|
1845
|
+
path: fileZod.path
|
|
1846
|
+
}),
|
|
1847
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1848
|
+
name: [contractOp.name],
|
|
1849
|
+
root: meta.file.path,
|
|
1850
|
+
path: contractOp.path
|
|
1851
|
+
}),
|
|
1852
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1853
|
+
name: ["RequestConfig", "ResponseErrorConfig"],
|
|
1854
|
+
root: meta.file.path,
|
|
1855
|
+
path: contractOp.clientPath,
|
|
1856
|
+
isTypeOnly: true
|
|
1857
|
+
}),
|
|
1858
|
+
customOptions && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1859
|
+
name: [customOptions.name],
|
|
1860
|
+
path: customOptions.importPath
|
|
1861
|
+
}),
|
|
1862
|
+
meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1863
|
+
name: Array.from(new Set(importedTypeNames)),
|
|
1864
|
+
root: meta.file.path,
|
|
1865
|
+
path: meta.fileTs.path,
|
|
1866
|
+
isTypeOnly: true
|
|
1867
|
+
}),
|
|
1868
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(QueryKey, {
|
|
1869
|
+
name: queryKeyName,
|
|
1870
|
+
typeName: queryKeyTypeName,
|
|
1871
|
+
node,
|
|
1872
|
+
tsResolver,
|
|
1873
|
+
transformer: ctx.options.queryKey
|
|
1874
|
+
}),
|
|
1875
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1876
|
+
name: ["InfiniteData"],
|
|
1877
|
+
isTypeOnly: true,
|
|
1878
|
+
path: importPath
|
|
1879
|
+
}),
|
|
1880
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1881
|
+
name: ["infiniteQueryOptions"],
|
|
1882
|
+
path: importPath
|
|
1883
|
+
}),
|
|
1884
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(InfiniteQueryOptions, {
|
|
1885
|
+
name: queryOptionsName,
|
|
1886
|
+
clientName: calledClientName,
|
|
1887
|
+
queryKeyName,
|
|
1888
|
+
node,
|
|
1889
|
+
tsResolver,
|
|
1890
|
+
cursorParam: infiniteOptions.cursorParam,
|
|
1891
|
+
nextParam: infiniteOptions.nextParam,
|
|
1892
|
+
previousParam: infiniteOptions.previousParam,
|
|
1893
|
+
initialPageParam: infiniteOptions.initialPageParam,
|
|
1894
|
+
queryParam: infiniteOptions.queryParam
|
|
1895
|
+
}),
|
|
1896
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1897
|
+
name: ["useInfiniteQuery"],
|
|
1898
|
+
path: importPath
|
|
1899
|
+
}),
|
|
1900
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1901
|
+
name: [
|
|
1902
|
+
"QueryKey",
|
|
1903
|
+
"QueryClient",
|
|
1904
|
+
"InfiniteQueryObserverOptions",
|
|
1905
|
+
"UseInfiniteQueryResult"
|
|
1906
|
+
],
|
|
1907
|
+
path: importPath,
|
|
1908
|
+
isTypeOnly: true
|
|
1909
|
+
}),
|
|
1910
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(InfiniteQuery, {
|
|
1911
|
+
name: queryName,
|
|
1912
|
+
queryOptionsName,
|
|
1913
|
+
queryKeyName,
|
|
1914
|
+
queryKeyTypeName,
|
|
1915
|
+
node,
|
|
1916
|
+
tsResolver,
|
|
1917
|
+
initialPageParam: infiniteOptions.initialPageParam,
|
|
1918
|
+
queryParam: infiniteOptions.queryParam,
|
|
1919
|
+
customOptions
|
|
1920
|
+
})
|
|
1921
|
+
]
|
|
1922
|
+
});
|
|
1923
|
+
}
|
|
1924
|
+
});
|
|
1925
|
+
//#endregion
|
|
1926
|
+
//#region src/generators/mutationGenerator.tsx
|
|
1927
|
+
/**
|
|
1928
|
+
* Built-in generator for `useMutation` hooks. Emits one `useFooMutation` hook
|
|
1929
|
+
* per POST/PUT/DELETE operation (configurable via `mutation.methods`) plus
|
|
1930
|
+
* the matching `fooMutationKey` / `fooMutationOptions` helpers.
|
|
1931
|
+
*/
|
|
1932
|
+
const mutationGenerator = (0, _kubb_core.defineGenerator)({
|
|
1933
|
+
name: "react-query-mutation",
|
|
1934
|
+
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
1935
|
+
operation(node, ctx) {
|
|
1936
|
+
if (!_kubb_core.ast.isHttpOperationNode(node)) return null;
|
|
1937
|
+
const { config, driver, resolver, root } = ctx;
|
|
1938
|
+
const { output, query, mutation, parser, client, group, customOptions } = ctx.options;
|
|
1939
|
+
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
1940
|
+
if (!pluginTs) return null;
|
|
1941
|
+
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
1942
|
+
const isQuery = query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
|
|
1943
|
+
const queryMethods = new Set(query ? query.methods : []);
|
|
1944
|
+
if (!(mutation !== false && !isQuery && (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase()))) return null;
|
|
1945
|
+
const importPath = mutation ? mutation.importPath : "@tanstack/react-query";
|
|
1946
|
+
const contractOp = resolveClientOperation({
|
|
1947
|
+
clientPlugin: { pluginName: client.pluginName },
|
|
1948
|
+
driver,
|
|
1949
|
+
node,
|
|
1950
|
+
root,
|
|
1951
|
+
output
|
|
1952
|
+
});
|
|
1953
|
+
if (!contractOp) return null;
|
|
1954
|
+
const mutationHookName = resolver.resolveMutationName(node);
|
|
1955
|
+
const mutationTypeName = resolver.resolveMutationTypeName(node);
|
|
1956
|
+
const mutationOptionsName = resolver.resolveMutationOptionsName(node);
|
|
1957
|
+
const mutationKeyName = resolver.resolveMutationKeyName(node);
|
|
1958
|
+
const meta = {
|
|
1959
|
+
file: resolver.resolveFile(operationFileEntry(node, mutationHookName), {
|
|
1960
|
+
root,
|
|
1961
|
+
output,
|
|
1962
|
+
group: group ?? void 0
|
|
1963
|
+
}),
|
|
1964
|
+
fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1965
|
+
root,
|
|
1966
|
+
output: pluginTs.options?.output ?? output,
|
|
1967
|
+
group: pluginTs.options?.group ?? void 0
|
|
1968
|
+
})
|
|
1969
|
+
};
|
|
1970
|
+
const importedTypeNames = [tsResolver.resolveRequestConfigName(node), ...resolveOperationTypeNames(node, tsResolver, {
|
|
1971
|
+
order: "body-response-first",
|
|
1972
|
+
includeParams: false
|
|
1973
|
+
})].filter((name) => Boolean(name));
|
|
1974
|
+
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
1975
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
1976
|
+
const fileZod = zodResolver ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
1977
|
+
root,
|
|
1978
|
+
output: pluginZod?.options?.output ?? output,
|
|
1979
|
+
group: pluginZod?.options?.group ?? void 0
|
|
1980
|
+
}) : null;
|
|
1981
|
+
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser);
|
|
1982
|
+
const calledClientName = contractOp.name;
|
|
1983
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
1984
|
+
baseName: meta.file.baseName,
|
|
1985
|
+
path: meta.file.path,
|
|
1986
|
+
meta: meta.file.meta,
|
|
1987
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
1988
|
+
output,
|
|
1989
|
+
config,
|
|
1990
|
+
file: {
|
|
1991
|
+
path: meta.file.path,
|
|
1992
|
+
baseName: meta.file.baseName
|
|
1993
|
+
}
|
|
1994
|
+
}),
|
|
1995
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
1996
|
+
output,
|
|
1997
|
+
config,
|
|
1998
|
+
file: {
|
|
1999
|
+
path: meta.file.path,
|
|
2000
|
+
baseName: meta.file.baseName
|
|
2001
|
+
}
|
|
2002
|
+
}),
|
|
2003
|
+
children: [
|
|
2004
|
+
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2005
|
+
name: zodSchemaNames,
|
|
2006
|
+
root: meta.file.path,
|
|
2007
|
+
path: fileZod.path
|
|
2008
|
+
}),
|
|
2009
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2010
|
+
name: [contractOp.name],
|
|
2011
|
+
root: meta.file.path,
|
|
2012
|
+
path: contractOp.path
|
|
2013
|
+
}),
|
|
2014
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2015
|
+
name: ["RequestConfig", "ResponseErrorConfig"],
|
|
2016
|
+
root: meta.file.path,
|
|
2017
|
+
path: contractOp.clientPath,
|
|
2018
|
+
isTypeOnly: true
|
|
2019
|
+
}),
|
|
2020
|
+
customOptions && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2021
|
+
name: [customOptions.name],
|
|
2022
|
+
path: customOptions.importPath
|
|
2023
|
+
}),
|
|
2024
|
+
meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2025
|
+
name: Array.from(new Set(importedTypeNames)),
|
|
2026
|
+
root: meta.file.path,
|
|
2027
|
+
path: meta.fileTs.path,
|
|
2028
|
+
isTypeOnly: true
|
|
2029
|
+
}),
|
|
2030
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(MutationKey, {
|
|
2031
|
+
name: mutationKeyName,
|
|
2032
|
+
node,
|
|
2033
|
+
transformer: ctx.options.mutationKey
|
|
2034
|
+
}),
|
|
2035
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2036
|
+
name: ["mutationOptions"],
|
|
2037
|
+
path: importPath
|
|
2038
|
+
}),
|
|
2039
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(MutationOptions, {
|
|
2040
|
+
name: mutationOptionsName,
|
|
2041
|
+
clientName: calledClientName,
|
|
2042
|
+
mutationKeyName,
|
|
2043
|
+
node,
|
|
2044
|
+
tsResolver
|
|
2045
|
+
}),
|
|
2046
|
+
mutation && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
2047
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2048
|
+
name: ["useMutation"],
|
|
2049
|
+
path: importPath
|
|
2050
|
+
}),
|
|
2051
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2052
|
+
name: [
|
|
2053
|
+
"UseMutationOptions",
|
|
2054
|
+
"UseMutationResult",
|
|
2055
|
+
"QueryClient"
|
|
2056
|
+
],
|
|
2057
|
+
path: importPath,
|
|
2058
|
+
isTypeOnly: true
|
|
2059
|
+
}),
|
|
2060
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(Mutation, {
|
|
2061
|
+
name: mutationHookName,
|
|
2062
|
+
typeName: mutationTypeName,
|
|
2063
|
+
mutationOptionsName,
|
|
2064
|
+
mutationKeyName,
|
|
2065
|
+
node,
|
|
2066
|
+
tsResolver,
|
|
2067
|
+
customOptions
|
|
2068
|
+
})
|
|
2069
|
+
] })
|
|
2070
|
+
]
|
|
2071
|
+
});
|
|
2072
|
+
}
|
|
2073
|
+
});
|
|
2074
|
+
//#endregion
|
|
2075
|
+
//#region src/generators/queryGenerator.tsx
|
|
2076
|
+
/**
|
|
2077
|
+
* Built-in generator for `useQuery` hooks. Emits one `useFooQuery` hook per
|
|
2078
|
+
* GET operation (configurable via `query.methods`) plus the matching
|
|
2079
|
+
* `fooQueryKey` / `fooQueryOptions` helpers.
|
|
2080
|
+
*/
|
|
2081
|
+
const queryGenerator = (0, _kubb_core.defineGenerator)({
|
|
2082
|
+
name: "react-query",
|
|
2083
|
+
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
2084
|
+
operation(node, ctx) {
|
|
2085
|
+
if (!_kubb_core.ast.isHttpOperationNode(node)) return null;
|
|
2086
|
+
const { config, driver, resolver, root } = ctx;
|
|
2087
|
+
const { output, query, mutation, parser, client, group, customOptions } = ctx.options;
|
|
2088
|
+
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
2089
|
+
if (!pluginTs) return null;
|
|
2090
|
+
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
2091
|
+
const isQuery = query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
|
|
2092
|
+
const queryMethods = new Set(query ? query.methods : []);
|
|
2093
|
+
const isMutation = mutation !== false && !isQuery && (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase());
|
|
2094
|
+
if (!isQuery || isMutation) return null;
|
|
2095
|
+
const importPath = query ? query.importPath : "@tanstack/react-query";
|
|
2096
|
+
const contractOp = resolveClientOperation({
|
|
2097
|
+
clientPlugin: { pluginName: client.pluginName },
|
|
2098
|
+
driver,
|
|
2099
|
+
node,
|
|
2100
|
+
root,
|
|
2101
|
+
output
|
|
2102
|
+
});
|
|
2103
|
+
if (!contractOp) return null;
|
|
2104
|
+
const queryName = resolver.resolveQueryName(node);
|
|
2105
|
+
const queryOptionsName = resolver.resolveQueryOptionsName(node);
|
|
2106
|
+
const queryKeyName = resolver.resolveQueryKeyName(node);
|
|
2107
|
+
const queryKeyTypeName = resolver.resolveQueryKeyTypeName(node);
|
|
2108
|
+
const meta = {
|
|
2109
|
+
file: resolver.resolveFile(operationFileEntry(node, queryName), {
|
|
2110
|
+
root,
|
|
2111
|
+
output,
|
|
2112
|
+
group: group ?? void 0
|
|
2113
|
+
}),
|
|
2114
|
+
fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
2115
|
+
root,
|
|
2116
|
+
output: pluginTs.options?.output ?? output,
|
|
2117
|
+
group: pluginTs.options?.group ?? void 0
|
|
2118
|
+
})
|
|
2119
|
+
};
|
|
2120
|
+
const importedTypeNames = [tsResolver.resolveRequestConfigName(node), ...resolveOperationTypeNames(node, tsResolver, {
|
|
2121
|
+
exclude: [queryKeyTypeName],
|
|
2122
|
+
order: "body-response-first",
|
|
2123
|
+
includeParams: false
|
|
2124
|
+
})].filter((name) => Boolean(name));
|
|
2125
|
+
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
2126
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
2127
|
+
const fileZod = zodResolver ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
2128
|
+
root,
|
|
2129
|
+
output: pluginZod?.options?.output ?? output,
|
|
2130
|
+
group: pluginZod?.options?.group ?? void 0
|
|
2131
|
+
}) : null;
|
|
2132
|
+
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser);
|
|
2133
|
+
const calledClientName = contractOp.name;
|
|
2134
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
2135
|
+
baseName: meta.file.baseName,
|
|
2136
|
+
path: meta.file.path,
|
|
2137
|
+
meta: meta.file.meta,
|
|
2138
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
2139
|
+
output,
|
|
2140
|
+
config,
|
|
2141
|
+
file: {
|
|
2142
|
+
path: meta.file.path,
|
|
2143
|
+
baseName: meta.file.baseName
|
|
2144
|
+
}
|
|
2145
|
+
}),
|
|
2146
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
2147
|
+
output,
|
|
2148
|
+
config,
|
|
2149
|
+
file: {
|
|
2150
|
+
path: meta.file.path,
|
|
2151
|
+
baseName: meta.file.baseName
|
|
2152
|
+
}
|
|
2153
|
+
}),
|
|
2154
|
+
children: [
|
|
2155
|
+
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2156
|
+
name: zodSchemaNames,
|
|
2157
|
+
root: meta.file.path,
|
|
2158
|
+
path: fileZod.path
|
|
2159
|
+
}),
|
|
2160
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2161
|
+
name: [contractOp.name],
|
|
2162
|
+
root: meta.file.path,
|
|
2163
|
+
path: contractOp.path
|
|
2164
|
+
}),
|
|
2165
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2166
|
+
name: ["RequestConfig", "ResponseErrorConfig"],
|
|
2167
|
+
root: meta.file.path,
|
|
2168
|
+
path: contractOp.clientPath,
|
|
2169
|
+
isTypeOnly: true
|
|
2170
|
+
}),
|
|
2171
|
+
customOptions && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2172
|
+
name: [customOptions.name],
|
|
2173
|
+
path: customOptions.importPath
|
|
2174
|
+
}),
|
|
2175
|
+
meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2176
|
+
name: Array.from(new Set(importedTypeNames)),
|
|
2177
|
+
root: meta.file.path,
|
|
2178
|
+
path: meta.fileTs.path,
|
|
2179
|
+
isTypeOnly: true
|
|
2180
|
+
}),
|
|
2181
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(QueryKey, {
|
|
2182
|
+
name: queryKeyName,
|
|
2183
|
+
typeName: queryKeyTypeName,
|
|
2184
|
+
node,
|
|
2185
|
+
tsResolver,
|
|
2186
|
+
transformer: ctx.options.queryKey
|
|
2187
|
+
}),
|
|
2188
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2189
|
+
name: ["queryOptions"],
|
|
2190
|
+
path: importPath
|
|
2191
|
+
}),
|
|
2192
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(QueryOptions, {
|
|
2193
|
+
name: queryOptionsName,
|
|
2194
|
+
clientName: calledClientName,
|
|
2195
|
+
queryKeyName,
|
|
2196
|
+
node,
|
|
2197
|
+
tsResolver
|
|
2198
|
+
}),
|
|
2199
|
+
query && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
2200
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2201
|
+
name: ["useQuery"],
|
|
2202
|
+
path: importPath
|
|
2203
|
+
}),
|
|
2204
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2205
|
+
name: [
|
|
2206
|
+
"QueryKey",
|
|
2207
|
+
"QueryClient",
|
|
2208
|
+
"QueryObserverOptions",
|
|
2209
|
+
"UseQueryResult"
|
|
2210
|
+
],
|
|
2211
|
+
path: importPath,
|
|
2212
|
+
isTypeOnly: true
|
|
2213
|
+
}),
|
|
2214
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(Query, {
|
|
2215
|
+
name: queryName,
|
|
2216
|
+
queryOptionsName,
|
|
2217
|
+
queryKeyName,
|
|
2218
|
+
queryKeyTypeName,
|
|
2219
|
+
node,
|
|
2220
|
+
tsResolver,
|
|
2221
|
+
customOptions
|
|
2222
|
+
})
|
|
2223
|
+
] })
|
|
2224
|
+
]
|
|
2225
|
+
});
|
|
2226
|
+
}
|
|
2227
|
+
});
|
|
2228
|
+
//#endregion
|
|
2229
|
+
//#region src/generators/suspenseInfiniteQueryGenerator.tsx
|
|
2230
|
+
/**
|
|
2231
|
+
* Built-in generator for `useSuspenseInfiniteQuery` hooks. Enabled when both
|
|
2232
|
+
* `suspense` and `infinite` are configured. Combines suspense semantics with
|
|
2233
|
+
* cursor-based pagination — handlers throw promises while loading and pull
|
|
2234
|
+
* additional pages on demand.
|
|
2235
|
+
*/
|
|
2236
|
+
const suspenseInfiniteQueryGenerator = (0, _kubb_core.defineGenerator)({
|
|
2237
|
+
name: "react-suspense-infinite-query",
|
|
2238
|
+
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
2239
|
+
operation(node, ctx) {
|
|
2240
|
+
if (!_kubb_core.ast.isHttpOperationNode(node)) return null;
|
|
2241
|
+
const { config, driver, resolver, root } = ctx;
|
|
2242
|
+
const { output, query, mutation, infinite, suspense, parser, client, group, customOptions } = ctx.options;
|
|
2243
|
+
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
2244
|
+
if (!pluginTs) return null;
|
|
2245
|
+
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
2246
|
+
const isQuery = query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
|
|
2247
|
+
const queryMethods = new Set(query ? query.methods : []);
|
|
2248
|
+
const isMutation = mutation !== false && !isQuery && (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase());
|
|
2249
|
+
const isSuspense = !!suspense;
|
|
2250
|
+
const infiniteOptions = infinite && typeof infinite === "object" ? infinite : null;
|
|
2251
|
+
if (!isQuery || isMutation || !isSuspense || !infiniteOptions) return null;
|
|
2252
|
+
const normalizeKey = (key) => key.replace(/\?$/, "");
|
|
2253
|
+
const queryParamKeys = getOperationParameters(node, { paramsCasing: "original" }).query.map((p) => p.name);
|
|
2254
|
+
const hasQueryParam = infiniteOptions.queryParam ? queryParamKeys.some((k) => normalizeKey(k) === infiniteOptions.queryParam) : false;
|
|
2255
|
+
const hasCursorParam = !infiniteOptions.cursorParam || true;
|
|
2256
|
+
if (!hasQueryParam || !hasCursorParam) return null;
|
|
2257
|
+
const importPath = query ? query.importPath : "@tanstack/react-query";
|
|
2258
|
+
const contractOp = resolveClientOperation({
|
|
2259
|
+
clientPlugin: { pluginName: client.pluginName },
|
|
2260
|
+
driver,
|
|
2261
|
+
node,
|
|
2262
|
+
root,
|
|
2263
|
+
output
|
|
2264
|
+
});
|
|
2265
|
+
if (!contractOp) return null;
|
|
2266
|
+
const queryName = resolver.resolveSuspenseInfiniteQueryName(node);
|
|
2267
|
+
const queryOptionsName = resolver.resolveSuspenseInfiniteQueryOptionsName(node);
|
|
2268
|
+
const queryKeyName = resolver.resolveSuspenseInfiniteQueryKeyName(node);
|
|
2269
|
+
const queryKeyTypeName = resolver.resolveSuspenseInfiniteQueryKeyTypeName(node);
|
|
2270
|
+
const meta = {
|
|
2271
|
+
file: resolver.resolveFile(operationFileEntry(node, queryName), {
|
|
2272
|
+
root,
|
|
2273
|
+
output,
|
|
2274
|
+
group: group ?? void 0
|
|
2275
|
+
}),
|
|
2276
|
+
fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
2277
|
+
root,
|
|
2278
|
+
output: pluginTs.options?.output ?? output,
|
|
2279
|
+
group: pluginTs.options?.group ?? void 0
|
|
2280
|
+
})
|
|
2281
|
+
};
|
|
2282
|
+
const rawQueryParams = getOperationParameters(node, { paramsCasing: "original" }).query;
|
|
2283
|
+
const queryParamsTypeName = rawQueryParams.length > 0 && tsResolver.resolveQueryParamsName(node, rawQueryParams[0]) !== tsResolver.resolveParamName(node, rawQueryParams[0]) ? tsResolver.resolveQueryParamsName(node, rawQueryParams[0]) : null;
|
|
2284
|
+
const importedTypeNames = [
|
|
2285
|
+
tsResolver.resolveRequestConfigName(node),
|
|
2286
|
+
queryParamsTypeName,
|
|
2287
|
+
...resolveOperationTypeNames(node, tsResolver, {
|
|
2288
|
+
order: "body-response-first",
|
|
2289
|
+
includeParams: false
|
|
2290
|
+
})
|
|
2291
|
+
].filter((name) => Boolean(name));
|
|
2292
|
+
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
2293
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
2294
|
+
const fileZod = zodResolver ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
2295
|
+
root,
|
|
2296
|
+
output: pluginZod?.options?.output ?? output,
|
|
2297
|
+
group: pluginZod?.options?.group ?? void 0
|
|
2298
|
+
}) : null;
|
|
2299
|
+
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser);
|
|
2300
|
+
const calledClientName = contractOp.name;
|
|
2301
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
2302
|
+
baseName: meta.file.baseName,
|
|
2303
|
+
path: meta.file.path,
|
|
2304
|
+
meta: meta.file.meta,
|
|
2305
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
2306
|
+
output,
|
|
2307
|
+
config,
|
|
2308
|
+
file: {
|
|
2309
|
+
path: meta.file.path,
|
|
2310
|
+
baseName: meta.file.baseName
|
|
2311
|
+
}
|
|
2312
|
+
}),
|
|
2313
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
2314
|
+
output,
|
|
2315
|
+
config,
|
|
2316
|
+
file: {
|
|
2317
|
+
path: meta.file.path,
|
|
2318
|
+
baseName: meta.file.baseName
|
|
2319
|
+
}
|
|
2320
|
+
}),
|
|
2321
|
+
children: [
|
|
2322
|
+
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2323
|
+
name: zodSchemaNames,
|
|
2324
|
+
root: meta.file.path,
|
|
2325
|
+
path: fileZod.path
|
|
2326
|
+
}),
|
|
2327
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2328
|
+
name: [contractOp.name],
|
|
2329
|
+
root: meta.file.path,
|
|
2330
|
+
path: contractOp.path
|
|
2331
|
+
}),
|
|
2332
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2333
|
+
name: ["RequestConfig", "ResponseErrorConfig"],
|
|
2334
|
+
root: meta.file.path,
|
|
2335
|
+
path: contractOp.clientPath,
|
|
2336
|
+
isTypeOnly: true
|
|
2337
|
+
}),
|
|
2338
|
+
customOptions && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2339
|
+
name: [customOptions.name],
|
|
2340
|
+
path: customOptions.importPath
|
|
2341
|
+
}),
|
|
2342
|
+
meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2343
|
+
name: Array.from(new Set(importedTypeNames)),
|
|
2344
|
+
root: meta.file.path,
|
|
2345
|
+
path: meta.fileTs.path,
|
|
2346
|
+
isTypeOnly: true
|
|
2347
|
+
}),
|
|
2348
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(QueryKey, {
|
|
2349
|
+
name: queryKeyName,
|
|
2350
|
+
typeName: queryKeyTypeName,
|
|
2351
|
+
node,
|
|
2352
|
+
tsResolver,
|
|
2353
|
+
transformer: ctx.options.queryKey
|
|
2354
|
+
}),
|
|
2355
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2356
|
+
name: ["InfiniteData"],
|
|
2357
|
+
isTypeOnly: true,
|
|
2358
|
+
path: importPath
|
|
2359
|
+
}),
|
|
2360
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2361
|
+
name: ["infiniteQueryOptions"],
|
|
2362
|
+
path: importPath
|
|
2363
|
+
}),
|
|
2364
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(SuspenseInfiniteQueryOptions, {
|
|
2365
|
+
name: queryOptionsName,
|
|
2366
|
+
clientName: calledClientName,
|
|
2367
|
+
queryKeyName,
|
|
2368
|
+
node,
|
|
2369
|
+
tsResolver,
|
|
2370
|
+
cursorParam: infiniteOptions.cursorParam,
|
|
2371
|
+
nextParam: infiniteOptions.nextParam,
|
|
2372
|
+
previousParam: infiniteOptions.previousParam,
|
|
2373
|
+
initialPageParam: infiniteOptions.initialPageParam,
|
|
2374
|
+
queryParam: infiniteOptions.queryParam
|
|
2375
|
+
}),
|
|
2376
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2377
|
+
name: ["useSuspenseInfiniteQuery"],
|
|
2378
|
+
path: importPath
|
|
2379
|
+
}),
|
|
2380
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2381
|
+
name: [
|
|
2382
|
+
"QueryKey",
|
|
2383
|
+
"QueryClient",
|
|
2384
|
+
"UseSuspenseInfiniteQueryOptions",
|
|
2385
|
+
"UseSuspenseInfiniteQueryResult"
|
|
2386
|
+
],
|
|
2387
|
+
path: importPath,
|
|
2388
|
+
isTypeOnly: true
|
|
2389
|
+
}),
|
|
2390
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(SuspenseInfiniteQuery, {
|
|
2391
|
+
name: queryName,
|
|
2392
|
+
queryOptionsName,
|
|
2393
|
+
queryKeyName,
|
|
2394
|
+
queryKeyTypeName,
|
|
2395
|
+
node,
|
|
2396
|
+
tsResolver,
|
|
2397
|
+
initialPageParam: infiniteOptions.initialPageParam,
|
|
2398
|
+
queryParam: infiniteOptions.queryParam,
|
|
2399
|
+
customOptions
|
|
2400
|
+
})
|
|
2401
|
+
]
|
|
2402
|
+
});
|
|
2403
|
+
}
|
|
2404
|
+
});
|
|
2405
|
+
//#endregion
|
|
2406
|
+
//#region src/generators/suspenseQueryGenerator.tsx
|
|
2407
|
+
/**
|
|
2408
|
+
* Built-in generator for `useSuspenseQuery` hooks. Enabled when
|
|
2409
|
+
* `pluginReactQuery({ suspense: {} })`. Emits one `useFooSuspenseQuery` hook
|
|
2410
|
+
* per query operation. Suspense queries throw promises while loading and
|
|
2411
|
+
* require a `<Suspense>` boundary in the React tree. TanStack Query v5+ only.
|
|
2412
|
+
*/
|
|
2413
|
+
const suspenseQueryGenerator = (0, _kubb_core.defineGenerator)({
|
|
2414
|
+
name: "react-suspense-query",
|
|
2415
|
+
renderer: _kubb_renderer_jsx.jsxRenderer,
|
|
2416
|
+
operation(node, ctx) {
|
|
2417
|
+
if (!_kubb_core.ast.isHttpOperationNode(node)) return null;
|
|
2418
|
+
const { config, driver, resolver, root } = ctx;
|
|
2419
|
+
const { output, query, mutation, suspense, parser, client, group, customOptions } = ctx.options;
|
|
2420
|
+
const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
|
|
2421
|
+
if (!pluginTs) return null;
|
|
2422
|
+
const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
|
|
2423
|
+
const isQuery = query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
|
|
2424
|
+
const queryMethods = new Set(query ? query.methods : []);
|
|
2425
|
+
const isMutation = mutation !== false && !isQuery && (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase());
|
|
2426
|
+
if (!isQuery || isMutation || !!!suspense) return null;
|
|
2427
|
+
const importPath = query ? query.importPath : "@tanstack/react-query";
|
|
2428
|
+
const contractOp = resolveClientOperation({
|
|
2429
|
+
clientPlugin: { pluginName: client.pluginName },
|
|
2430
|
+
driver,
|
|
2431
|
+
node,
|
|
2432
|
+
root,
|
|
2433
|
+
output
|
|
2434
|
+
});
|
|
2435
|
+
if (!contractOp) return null;
|
|
2436
|
+
const queryName = resolver.resolveSuspenseQueryName(node);
|
|
2437
|
+
const queryOptionsName = resolver.resolveSuspenseQueryOptionsName(node);
|
|
2438
|
+
const queryKeyName = resolver.resolveSuspenseQueryKeyName(node);
|
|
2439
|
+
const queryKeyTypeName = resolver.resolveSuspenseQueryKeyTypeName(node);
|
|
2440
|
+
const meta = {
|
|
2441
|
+
file: resolver.resolveFile(operationFileEntry(node, queryName), {
|
|
2442
|
+
root,
|
|
2443
|
+
output,
|
|
2444
|
+
group: group ?? void 0
|
|
2445
|
+
}),
|
|
2446
|
+
fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
2447
|
+
root,
|
|
2448
|
+
output: pluginTs.options?.output ?? output,
|
|
2449
|
+
group: pluginTs.options?.group ?? void 0
|
|
2450
|
+
})
|
|
2451
|
+
};
|
|
2452
|
+
const importedTypeNames = [tsResolver.resolveRequestConfigName(node), ...resolveOperationTypeNames(node, tsResolver, {
|
|
2453
|
+
exclude: [queryKeyTypeName],
|
|
2454
|
+
order: "body-response-first",
|
|
2455
|
+
includeParams: false
|
|
2456
|
+
})].filter((name) => Boolean(name));
|
|
2457
|
+
const pluginZod = isParserEnabled(parser) ? driver.getPlugin(_kubb_plugin_zod.pluginZodName) : null;
|
|
2458
|
+
const zodResolver = pluginZod ? driver.getResolver(_kubb_plugin_zod.pluginZodName) : null;
|
|
2459
|
+
const fileZod = zodResolver ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
|
|
2460
|
+
root,
|
|
2461
|
+
output: pluginZod?.options?.output ?? output,
|
|
2462
|
+
group: pluginZod?.options?.group ?? void 0
|
|
2463
|
+
}) : null;
|
|
2464
|
+
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser);
|
|
2465
|
+
const calledClientName = contractOp.name;
|
|
2466
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx.File, {
|
|
2467
|
+
baseName: meta.file.baseName,
|
|
2468
|
+
path: meta.file.path,
|
|
2469
|
+
meta: meta.file.meta,
|
|
2470
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
2471
|
+
output,
|
|
2472
|
+
config,
|
|
2473
|
+
file: {
|
|
2474
|
+
path: meta.file.path,
|
|
2475
|
+
baseName: meta.file.baseName
|
|
2476
|
+
}
|
|
2477
|
+
}),
|
|
2478
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
2479
|
+
output,
|
|
2480
|
+
config,
|
|
2481
|
+
file: {
|
|
2482
|
+
path: meta.file.path,
|
|
2483
|
+
baseName: meta.file.baseName
|
|
2484
|
+
}
|
|
2485
|
+
}),
|
|
2486
|
+
children: [
|
|
2487
|
+
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2488
|
+
name: zodSchemaNames,
|
|
2489
|
+
root: meta.file.path,
|
|
2490
|
+
path: fileZod.path
|
|
2491
|
+
}),
|
|
2492
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2493
|
+
name: [contractOp.name],
|
|
2494
|
+
root: meta.file.path,
|
|
2495
|
+
path: contractOp.path
|
|
2496
|
+
}),
|
|
2497
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2498
|
+
name: ["RequestConfig", "ResponseErrorConfig"],
|
|
2499
|
+
root: meta.file.path,
|
|
2500
|
+
path: contractOp.clientPath,
|
|
2501
|
+
isTypeOnly: true
|
|
2502
|
+
}),
|
|
2503
|
+
customOptions && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2504
|
+
name: [customOptions.name],
|
|
2505
|
+
path: customOptions.importPath
|
|
2506
|
+
}),
|
|
2507
|
+
meta.fileTs && importedTypeNames.length > 0 && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2508
|
+
name: Array.from(new Set(importedTypeNames)),
|
|
2509
|
+
root: meta.file.path,
|
|
2510
|
+
path: meta.fileTs.path,
|
|
2511
|
+
isTypeOnly: true
|
|
2512
|
+
}),
|
|
2513
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(QueryKey, {
|
|
2514
|
+
name: queryKeyName,
|
|
2515
|
+
typeName: queryKeyTypeName,
|
|
2516
|
+
node,
|
|
2517
|
+
tsResolver,
|
|
2518
|
+
transformer: ctx.options.queryKey
|
|
2519
|
+
}),
|
|
2520
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2521
|
+
name: ["queryOptions"],
|
|
2522
|
+
path: importPath
|
|
2523
|
+
}),
|
|
2524
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(QueryOptions, {
|
|
2525
|
+
name: queryOptionsName,
|
|
2526
|
+
clientName: calledClientName,
|
|
2527
|
+
queryKeyName,
|
|
2528
|
+
node,
|
|
2529
|
+
tsResolver
|
|
2530
|
+
}),
|
|
2531
|
+
suspense && /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsxs)(_kubb_renderer_jsx_jsx_runtime.Fragment, { children: [
|
|
2532
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2533
|
+
name: ["useSuspenseQuery"],
|
|
2534
|
+
path: importPath
|
|
2535
|
+
}),
|
|
2536
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
2537
|
+
name: [
|
|
2538
|
+
"QueryKey",
|
|
2539
|
+
"QueryClient",
|
|
2540
|
+
"UseSuspenseQueryOptions",
|
|
2541
|
+
"UseSuspenseQueryResult"
|
|
2542
|
+
],
|
|
2543
|
+
path: importPath,
|
|
2544
|
+
isTypeOnly: true
|
|
2545
|
+
}),
|
|
2546
|
+
/* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(SuspenseQuery, {
|
|
2547
|
+
name: queryName,
|
|
2548
|
+
queryOptionsName,
|
|
2549
|
+
queryKeyName,
|
|
2550
|
+
queryKeyTypeName,
|
|
2551
|
+
node,
|
|
2552
|
+
tsResolver,
|
|
2553
|
+
customOptions
|
|
2554
|
+
})
|
|
2555
|
+
] })
|
|
2556
|
+
]
|
|
2557
|
+
});
|
|
2558
|
+
}
|
|
2559
|
+
});
|
|
2560
|
+
//#endregion
|
|
72
2561
|
//#region src/resolvers/resolverReactQuery.ts
|
|
73
2562
|
function capitalize(name) {
|
|
74
2563
|
return `${name.charAt(0).toUpperCase()}${name.slice(1)}`;
|
|
@@ -96,7 +2585,7 @@ const resolverReactQuery = (0, _kubb_core.defineResolver)(() => ({
|
|
|
96
2585
|
name: "default",
|
|
97
2586
|
pluginName: "plugin-react-query",
|
|
98
2587
|
default(name, type) {
|
|
99
|
-
return type === "file" ? toFilePath(name) :
|
|
2588
|
+
return type === "file" ? toFilePath(name) : camelCase(name);
|
|
100
2589
|
},
|
|
101
2590
|
resolveName(name) {
|
|
102
2591
|
return this.default(name, "function");
|
|
@@ -219,39 +2708,38 @@ const pluginReactQuery = (0, _kubb_core.definePlugin)((options) => {
|
|
|
219
2708
|
const { output = {
|
|
220
2709
|
path: "hooks",
|
|
221
2710
|
barrel: { type: "named" }
|
|
222
|
-
}, group, exclude = [], include, override = [], parser = false, suspense = {}, infinite = false,
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
require_generators.hookOptionsGenerator,
|
|
232
|
-
require_generators.customHookOptionsFileGenerator
|
|
2711
|
+
}, group, exclude = [], include, override = [], parser = false, suspense = {}, infinite = false, mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, customOptions, client, resolver: userResolver, macros: userMacros } = options;
|
|
2712
|
+
const selectedGenerators = [
|
|
2713
|
+
queryGenerator,
|
|
2714
|
+
suspenseQueryGenerator,
|
|
2715
|
+
infiniteQueryGenerator,
|
|
2716
|
+
suspenseInfiniteQueryGenerator,
|
|
2717
|
+
mutationGenerator,
|
|
2718
|
+
hookOptionsGenerator,
|
|
2719
|
+
customHookOptionsFileGenerator
|
|
233
2720
|
].filter((generator) => Boolean(generator));
|
|
234
2721
|
const groupConfig = createGroupConfig(group);
|
|
235
2722
|
return {
|
|
236
2723
|
name: pluginReactQueryName,
|
|
237
2724
|
options,
|
|
238
|
-
dependencies: [_kubb_plugin_ts.pluginTsName,
|
|
2725
|
+
dependencies: [_kubb_plugin_ts.pluginTsName, isParserEnabled(parser) ? _kubb_plugin_zod.pluginZodName : void 0].filter((dependency) => Boolean(dependency)),
|
|
239
2726
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
240
2727
|
const resolver = userResolver ? {
|
|
241
2728
|
...resolverReactQuery,
|
|
242
2729
|
...userResolver
|
|
243
2730
|
} : resolverReactQuery;
|
|
2731
|
+
const resolvedClient = resolveClient({
|
|
2732
|
+
client,
|
|
2733
|
+
pluginNames: (ctx.config.plugins ?? []).map((p) => p.name).filter((name) => Boolean(name))
|
|
2734
|
+
});
|
|
2735
|
+
if (resolvedClient.kind === "error") throw new Error(resolvedClient.message);
|
|
2736
|
+
const resolvedClientDescriptor = {
|
|
2737
|
+
kind: "contract",
|
|
2738
|
+
pluginName: resolvedClient.pluginName
|
|
2739
|
+
};
|
|
244
2740
|
ctx.setOptions({
|
|
245
2741
|
output,
|
|
246
|
-
client:
|
|
247
|
-
bundle: client?.bundle,
|
|
248
|
-
baseURL: client?.baseURL,
|
|
249
|
-
client: clientName,
|
|
250
|
-
clientType: client?.clientType ?? "function",
|
|
251
|
-
importPath: clientImportPath,
|
|
252
|
-
dataReturnType: client?.dataReturnType ?? "data",
|
|
253
|
-
paramsCasing
|
|
254
|
-
},
|
|
2742
|
+
client: resolvedClientDescriptor,
|
|
255
2743
|
queryKey,
|
|
256
2744
|
query: query === false ? false : {
|
|
257
2745
|
importPath: "@tanstack/react-query",
|
|
@@ -283,9 +2771,6 @@ const pluginReactQuery = (0, _kubb_core.definePlugin)((options) => {
|
|
|
283
2771
|
...customOptions
|
|
284
2772
|
} : null,
|
|
285
2773
|
parser,
|
|
286
|
-
paramsType,
|
|
287
|
-
pathParamsType,
|
|
288
|
-
paramsCasing,
|
|
289
2774
|
group: groupConfig,
|
|
290
2775
|
exclude,
|
|
291
2776
|
include,
|
|
@@ -295,29 +2780,6 @@ const pluginReactQuery = (0, _kubb_core.definePlugin)((options) => {
|
|
|
295
2780
|
ctx.setResolver(resolver);
|
|
296
2781
|
if (userMacros?.length) ctx.setMacros(userMacros);
|
|
297
2782
|
for (const gen of selectedGenerators) ctx.addGenerator(gen);
|
|
298
|
-
for (const gen of userGenerators) ctx.addGenerator(gen);
|
|
299
|
-
const root = node_path.default.resolve(ctx.config.root, ctx.config.output.path);
|
|
300
|
-
const hasClientPlugin = !!ctx.config.plugins?.some((p) => p.name === _kubb_plugin_client.pluginClientName);
|
|
301
|
-
if (client?.bundle && !hasClientPlugin && !clientImportPath) ctx.injectFile({
|
|
302
|
-
baseName: "client.ts",
|
|
303
|
-
path: node_path.default.resolve(root, ".kubb/client.ts"),
|
|
304
|
-
sources: [_kubb_core.ast.factory.createSource({
|
|
305
|
-
name: "client",
|
|
306
|
-
nodes: [_kubb_core.ast.factory.createText(clientName === "fetch" ? _kubb_plugin_client_templates_clients_fetch_source.source : _kubb_plugin_client_templates_clients_axios_source.source)],
|
|
307
|
-
isExportable: true,
|
|
308
|
-
isIndexable: true
|
|
309
|
-
})]
|
|
310
|
-
});
|
|
311
|
-
if (!hasClientPlugin) ctx.injectFile({
|
|
312
|
-
baseName: "config.ts",
|
|
313
|
-
path: node_path.default.resolve(root, ".kubb/config.ts"),
|
|
314
|
-
sources: [_kubb_core.ast.factory.createSource({
|
|
315
|
-
name: "config",
|
|
316
|
-
nodes: [_kubb_core.ast.factory.createText(_kubb_plugin_client_templates_config_source.source)],
|
|
317
|
-
isExportable: false,
|
|
318
|
-
isIndexable: false
|
|
319
|
-
})]
|
|
320
|
-
});
|
|
321
2783
|
} }
|
|
322
2784
|
};
|
|
323
2785
|
});
|