@kubb/plugin-swr 3.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/chunk-ECJ346AA.js +542 -0
- package/dist/chunk-ECJ346AA.js.map +1 -0
- package/dist/chunk-H4LHXYRJ.cjs +542 -0
- package/dist/chunk-H4LHXYRJ.cjs.map +1 -0
- package/dist/components.cjs +11 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +7 -0
- package/dist/components.d.ts +7 -0
- package/dist/components.js +11 -0
- package/dist/components.js.map +1 -0
- package/dist/index-Ca8KyrCA.d.cts +304 -0
- package/dist/index-Ca8KyrCA.d.ts +304 -0
- package/dist/index.cjs +180 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +180 -0
- package/dist/index.js.map +1 -0
- package/package.json +98 -0
- package/src/OperationGenerator.tsx +75 -0
- package/src/components/Mutation.tsx +252 -0
- package/src/components/Query.tsx +290 -0
- package/src/components/QueryOptions.tsx +197 -0
- package/src/components/SchemaType.tsx +59 -0
- package/src/components/__snapshots__/Mutation/Pets.ts +42 -0
- package/src/components/__snapshots__/Query/showPetById.ts +55 -0
- package/src/components/index.ts +3 -0
- package/src/index.ts +15 -0
- package/src/plugin.ts +139 -0
- package/src/types.ts +125 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Stijn Van Hulle
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<!-- <img src="assets/logo.png" alt="logo" width="200" height="auto" /> -->
|
|
4
|
+
<h1>@kubb/plugin-swr</h1>
|
|
5
|
+
|
|
6
|
+
<p>
|
|
7
|
+
Swagger integration for SWR to generate all the different SWR hooks based on an OpenAPI specification.
|
|
8
|
+
</p>
|
|
9
|
+
<img src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/banner.png" alt="logo" height="auto" />
|
|
10
|
+
|
|
11
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
12
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
13
|
+
[![Coverage][coverage-src]][coverage-href]
|
|
14
|
+
[![License][license-src]][license-href]
|
|
15
|
+
|
|
16
|
+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
17
|
+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
<h4>
|
|
21
|
+
<a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/alpha/examples/typescript" target="_blank">View Demo</a>
|
|
22
|
+
<span> · </span>
|
|
23
|
+
<a href="https://kubb.dev/" target="_blank">Documentation</a>
|
|
24
|
+
<span> · </span>
|
|
25
|
+
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
|
|
26
|
+
<span> · </span>
|
|
27
|
+
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Request Feature</a>
|
|
28
|
+
</h4>
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
<!-- Badges -->
|
|
32
|
+
|
|
33
|
+
[npm-version-src]: https://img.shields.io/npm/v/@kubb/plugin-swr?flat&colorA=18181B&colorB=f58517
|
|
34
|
+
[npm-version-href]: https://npmjs.com/package/@kubb/plugin-swr
|
|
35
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@kubb/plugin-swr?flat&colorA=18181B&colorB=f58517
|
|
36
|
+
[npm-downloads-href]: https://npmjs.com/package/@kubb/plugin-swr
|
|
37
|
+
[license-src]: https://img.shields.io/github/license/kubb-labs/kubb.svg?flat&colorA=18181B&colorB=f58517
|
|
38
|
+
[license-href]: https://github.com/kubb-labs/kubb/blob/main/LICENSE
|
|
39
|
+
[build-src]: https://img.shields.io/github/actions/workflow/status/kubb-labs/kubb/ci.yaml?style=flat&colorA=18181B&colorB=f58517
|
|
40
|
+
[build-href]: https://www.npmjs.com/package/@kubb/plugin-swr
|
|
41
|
+
[minified-src]: https://img.shields.io/bundlephobia/min/@kubb/plugin-swr?style=flat&colorA=18181B&colorB=f58517
|
|
42
|
+
[minified-href]: https://www.npmjs.com/package/@kubb/plugin-swr
|
|
43
|
+
[coverage-src]: https://img.shields.io/codecov/c/github/kubb-labs/kubb?style=flat&colorA=18181B&colorB=f58517
|
|
44
|
+
[coverage-href]: https://www.npmjs.com/package/@kubb/plugin-swr
|
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
// src/components/Mutation.tsx
|
|
2
|
+
import transformers from "@kubb/core/transformers";
|
|
3
|
+
import { FunctionParams, URLPath } from "@kubb/core/utils";
|
|
4
|
+
import { Parser, File, Function, useApp as useApp2 } from "@kubb/react";
|
|
5
|
+
import { pluginTsName as pluginTsName2 } from "@kubb/plugin-ts";
|
|
6
|
+
import { useOperation as useOperation2, useOperationManager as useOperationManager2 } from "@kubb/plugin-oas/hooks";
|
|
7
|
+
import { getASTParams, getComments } from "@kubb/plugin-oas/utils";
|
|
8
|
+
|
|
9
|
+
// src/components/SchemaType.tsx
|
|
10
|
+
import { Type, useApp } from "@kubb/react";
|
|
11
|
+
import { useOperation, useOperationManager } from "@kubb/plugin-oas/hooks";
|
|
12
|
+
import { pluginTsName } from "@kubb/plugin-ts";
|
|
13
|
+
import { Fragment, jsx, jsxs } from "@kubb/react/jsx-runtime";
|
|
14
|
+
function SchemaType({ factory }) {
|
|
15
|
+
const {
|
|
16
|
+
plugin: {
|
|
17
|
+
options: { dataReturnType }
|
|
18
|
+
}
|
|
19
|
+
} = useApp();
|
|
20
|
+
const { getSchemas } = useOperationManager();
|
|
21
|
+
const operation = useOperation();
|
|
22
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: "type" });
|
|
23
|
+
const [TData, TError, TRequest, TPathParams, TQueryParams, THeaderParams, TResponse] = [
|
|
24
|
+
schemas.response.name,
|
|
25
|
+
schemas.errors?.map((item) => item.name).join(" | ") || "never",
|
|
26
|
+
schemas.request?.name || "never",
|
|
27
|
+
schemas.pathParams?.name || "never",
|
|
28
|
+
schemas.queryParams?.name || "never",
|
|
29
|
+
schemas.headerParams?.name || "never",
|
|
30
|
+
schemas.response.name
|
|
31
|
+
];
|
|
32
|
+
const clientType = `${factory.name}Client`;
|
|
33
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
34
|
+
/* @__PURE__ */ jsx(Type, { name: clientType, children: `typeof client<${TResponse}, ${TError}, ${TRequest}>` }),
|
|
35
|
+
/* @__PURE__ */ jsx(Type, { name: factory.name, children: `
|
|
36
|
+
{
|
|
37
|
+
data: ${TData}
|
|
38
|
+
error: ${TError}
|
|
39
|
+
request: ${TRequest}
|
|
40
|
+
pathParams: ${TPathParams}
|
|
41
|
+
queryParams: ${TQueryParams}
|
|
42
|
+
headerParams: ${THeaderParams}
|
|
43
|
+
response: ${dataReturnType === "data" ? TData : `Awaited<ReturnType<${clientType}>>`}
|
|
44
|
+
client: {
|
|
45
|
+
parameters: Partial<Parameters<${clientType}>[0]>
|
|
46
|
+
return: Awaited<ReturnType<${clientType}>>
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
` })
|
|
50
|
+
] });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/components/Mutation.tsx
|
|
54
|
+
import { jsx as jsx2, jsxs as jsxs2 } from "@kubb/react/jsx-runtime";
|
|
55
|
+
function Template({ name, generics, returnType, params, JSDoc, client, hook, dataReturnType }) {
|
|
56
|
+
const clientOptions = [
|
|
57
|
+
`method: "${client.method}"`,
|
|
58
|
+
"url",
|
|
59
|
+
client.withQueryParams ? "params" : void 0,
|
|
60
|
+
client.withData ? "data" : void 0,
|
|
61
|
+
client.withHeaders ? "headers: { ...headers, ...clientOptions.headers }" : void 0,
|
|
62
|
+
"...clientOptions"
|
|
63
|
+
].filter(Boolean);
|
|
64
|
+
const resolvedClientOptions = `${transformers.createIndent(4)}${clientOptions.join(`,
|
|
65
|
+
${transformers.createIndent(4)}`)}`;
|
|
66
|
+
if (client.withQueryParams) {
|
|
67
|
+
return /* @__PURE__ */ jsx2(Function, { export: true, name, generics, returnType, params, JSDoc, children: `
|
|
68
|
+
const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
|
|
69
|
+
|
|
70
|
+
const url = ${client.path.template} as const
|
|
71
|
+
return ${hook.name}<${hook.generics}>(
|
|
72
|
+
shouldFetch ? [url, params]: null,
|
|
73
|
+
async (_url${client.withData ? ", { arg: data }" : ""}) => {
|
|
74
|
+
const res = await client<${client.generics}>({
|
|
75
|
+
${resolvedClientOptions}
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return ${dataReturnType === "data" ? "res.data" : "res"}
|
|
79
|
+
},
|
|
80
|
+
mutationOptions
|
|
81
|
+
)
|
|
82
|
+
` });
|
|
83
|
+
}
|
|
84
|
+
return /* @__PURE__ */ jsx2(Function, { export: true, name, generics, returnType, params, JSDoc, children: `
|
|
85
|
+
const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
|
|
86
|
+
|
|
87
|
+
const url = ${client.path.template} as const
|
|
88
|
+
return ${hook.name}<${hook.generics}>(
|
|
89
|
+
shouldFetch ? url : null,
|
|
90
|
+
async (_url${client.withData ? ", { arg: data }" : ""}) => {
|
|
91
|
+
const res = await client<${client.generics}>({
|
|
92
|
+
${resolvedClientOptions}
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
return ${dataReturnType === "data" ? "res.data" : "res"}
|
|
96
|
+
},
|
|
97
|
+
mutationOptions
|
|
98
|
+
)
|
|
99
|
+
` });
|
|
100
|
+
}
|
|
101
|
+
var defaultTemplates = {
|
|
102
|
+
default: Template
|
|
103
|
+
};
|
|
104
|
+
function Mutation({ factory, Template: Template4 = defaultTemplates.default }) {
|
|
105
|
+
const {
|
|
106
|
+
plugin: {
|
|
107
|
+
options: { dataReturnType }
|
|
108
|
+
}
|
|
109
|
+
} = useApp2();
|
|
110
|
+
const { getSchemas, getName } = useOperationManager2();
|
|
111
|
+
const operation = useOperation2();
|
|
112
|
+
const name = getName(operation, { type: "function" });
|
|
113
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName2], type: "type" });
|
|
114
|
+
const params = new FunctionParams();
|
|
115
|
+
const client = {
|
|
116
|
+
method: operation.method,
|
|
117
|
+
path: new URLPath(operation.path),
|
|
118
|
+
generics: [`${factory.name}["data"]`, `${factory.name}["error"]`, schemas.request?.name ? `${factory.name}["request"]` : ""].filter(Boolean).join(", "),
|
|
119
|
+
withQueryParams: !!schemas.queryParams?.name,
|
|
120
|
+
withData: !!schemas.request?.name,
|
|
121
|
+
withPathParams: !!schemas.pathParams?.name,
|
|
122
|
+
withHeaders: !!schemas.headerParams?.name
|
|
123
|
+
};
|
|
124
|
+
const resultGenerics = [`${factory.name}["response"]`, `${factory.name}["error"]`];
|
|
125
|
+
params.add([
|
|
126
|
+
...getASTParams(schemas.pathParams, { typed: true }),
|
|
127
|
+
{
|
|
128
|
+
name: "params",
|
|
129
|
+
type: `${factory.name}['queryParams']`,
|
|
130
|
+
enabled: client.withQueryParams,
|
|
131
|
+
required: false
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: "headers",
|
|
135
|
+
type: `${factory.name}['headerParams']`,
|
|
136
|
+
enabled: client.withHeaders,
|
|
137
|
+
required: false
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: "options",
|
|
141
|
+
required: false,
|
|
142
|
+
type: `{
|
|
143
|
+
mutation?: SWRMutationConfiguration<${resultGenerics.join(", ")}>,
|
|
144
|
+
client?: ${factory.name}['client']['parameters'],
|
|
145
|
+
shouldFetch?: boolean,
|
|
146
|
+
}`,
|
|
147
|
+
default: "{}"
|
|
148
|
+
}
|
|
149
|
+
]);
|
|
150
|
+
const hook = {
|
|
151
|
+
name: "useSWRMutation",
|
|
152
|
+
generics: [...resultGenerics, client.withQueryParams ? "[typeof url, typeof params] | null" : "typeof url | null"].join(", ")
|
|
153
|
+
};
|
|
154
|
+
return /* @__PURE__ */ jsx2(
|
|
155
|
+
Template4,
|
|
156
|
+
{
|
|
157
|
+
name,
|
|
158
|
+
JSDoc: { comments: getComments(operation) },
|
|
159
|
+
client,
|
|
160
|
+
hook,
|
|
161
|
+
params: params.toString(),
|
|
162
|
+
returnType: `SWRMutationResponse<${resultGenerics.join(", ")}>`,
|
|
163
|
+
dataReturnType
|
|
164
|
+
}
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
Mutation.File = function({ templates = defaultTemplates }) {
|
|
168
|
+
const {
|
|
169
|
+
plugin: {
|
|
170
|
+
options: {
|
|
171
|
+
extName,
|
|
172
|
+
client: { importPath }
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
} = useApp2();
|
|
176
|
+
const { getSchemas, getFile, getName } = useOperationManager2();
|
|
177
|
+
const operation = useOperation2();
|
|
178
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName2], type: "type" });
|
|
179
|
+
const file = getFile(operation);
|
|
180
|
+
const fileType = getFile(operation, { pluginKey: [pluginTsName2] });
|
|
181
|
+
const factoryName = getName(operation, { type: "type" });
|
|
182
|
+
const Template4 = templates.default;
|
|
183
|
+
const factory = {
|
|
184
|
+
name: factoryName
|
|
185
|
+
};
|
|
186
|
+
return /* @__PURE__ */ jsx2(Parser, { language: "typescript", children: /* @__PURE__ */ jsxs2(File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
|
187
|
+
/* @__PURE__ */ jsx2(File.Import, { name: "useSWRMutation", path: "swr/mutation" }),
|
|
188
|
+
/* @__PURE__ */ jsx2(File.Import, { name: ["SWRMutationConfiguration", "SWRMutationResponse"], path: "swr/mutation", isTypeOnly: true }),
|
|
189
|
+
/* @__PURE__ */ jsx2(File.Import, { name: "client", path: importPath }),
|
|
190
|
+
/* @__PURE__ */ jsx2(File.Import, { name: ["ResponseConfig"], path: importPath, isTypeOnly: true }),
|
|
191
|
+
/* @__PURE__ */ jsx2(
|
|
192
|
+
File.Import,
|
|
193
|
+
{
|
|
194
|
+
extName,
|
|
195
|
+
name: [
|
|
196
|
+
schemas.request?.name,
|
|
197
|
+
schemas.response.name,
|
|
198
|
+
schemas.pathParams?.name,
|
|
199
|
+
schemas.queryParams?.name,
|
|
200
|
+
schemas.headerParams?.name,
|
|
201
|
+
...schemas.errors?.map((error) => error.name) || []
|
|
202
|
+
].filter(Boolean),
|
|
203
|
+
root: file.path,
|
|
204
|
+
path: fileType.path,
|
|
205
|
+
isTypeOnly: true
|
|
206
|
+
}
|
|
207
|
+
),
|
|
208
|
+
/* @__PURE__ */ jsxs2(File.Source, { children: [
|
|
209
|
+
/* @__PURE__ */ jsx2(SchemaType, { factory }),
|
|
210
|
+
/* @__PURE__ */ jsx2(Mutation, { Template: Template4, factory })
|
|
211
|
+
] })
|
|
212
|
+
] }) });
|
|
213
|
+
};
|
|
214
|
+
Mutation.templates = defaultTemplates;
|
|
215
|
+
|
|
216
|
+
// src/components/Query.tsx
|
|
217
|
+
import { FunctionParams as FunctionParams3, URLPath as URLPath3 } from "@kubb/core/utils";
|
|
218
|
+
import { Parser as Parser2, File as File2, Function as Function3, useApp as useApp4 } from "@kubb/react";
|
|
219
|
+
import { pluginTsName as pluginTsName4 } from "@kubb/plugin-ts";
|
|
220
|
+
import { useOperation as useOperation4, useOperationManager as useOperationManager4 } from "@kubb/plugin-oas/hooks";
|
|
221
|
+
import { getASTParams as getASTParams3, getComments as getComments2 } from "@kubb/plugin-oas/utils";
|
|
222
|
+
import { pluginZodName as pluginZodName2 } from "@kubb/plugin-zod";
|
|
223
|
+
|
|
224
|
+
// src/components/QueryOptions.tsx
|
|
225
|
+
import transformers2 from "@kubb/core/transformers";
|
|
226
|
+
import { FunctionParams as FunctionParams2, URLPath as URLPath2 } from "@kubb/core/utils";
|
|
227
|
+
import { useOperation as useOperation3, useOperationManager as useOperationManager3 } from "@kubb/plugin-oas/hooks";
|
|
228
|
+
import { getASTParams as getASTParams2 } from "@kubb/plugin-oas/utils";
|
|
229
|
+
import { Function as Function2, useApp as useApp3 } from "@kubb/react";
|
|
230
|
+
import { pluginZodName } from "@kubb/plugin-zod";
|
|
231
|
+
import { pluginTsName as pluginTsName3 } from "@kubb/plugin-ts";
|
|
232
|
+
import { jsx as jsx3 } from "@kubb/react/jsx-runtime";
|
|
233
|
+
function Template2({ name, params, generics, returnType, JSDoc, client, dataReturnType, parser }) {
|
|
234
|
+
const isFormData = client.contentType === "multipart/form-data";
|
|
235
|
+
const headers = [
|
|
236
|
+
client.contentType !== "application/json" ? `'Content-Type': '${client.contentType}'` : void 0,
|
|
237
|
+
client.withHeaders ? "...headers" : void 0
|
|
238
|
+
].filter(Boolean).join(", ");
|
|
239
|
+
const clientOptions = [
|
|
240
|
+
`method: "${client.method}"`,
|
|
241
|
+
`url: ${client.path.template}`,
|
|
242
|
+
client.withQueryParams ? "params" : void 0,
|
|
243
|
+
client.withData && !isFormData ? "data" : void 0,
|
|
244
|
+
client.withData && isFormData ? "data: formData" : void 0,
|
|
245
|
+
headers.length ? `headers: { ${headers}, ...options.headers }` : void 0,
|
|
246
|
+
"...options"
|
|
247
|
+
].filter(Boolean);
|
|
248
|
+
const resolvedClientOptions = `${transformers2.createIndent(4)}${clientOptions.join(`,
|
|
249
|
+
${transformers2.createIndent(4)}`)}`;
|
|
250
|
+
let returnRes = parser ? `return ${parser}(res.data)` : "return res.data";
|
|
251
|
+
if (dataReturnType === "full") {
|
|
252
|
+
returnRes = parser ? `return {...res, data: ${parser}(res.data)}` : "return res";
|
|
253
|
+
}
|
|
254
|
+
const formData = isFormData ? `
|
|
255
|
+
const formData = new FormData()
|
|
256
|
+
if(data) {
|
|
257
|
+
Object.keys(data).forEach((key) => {
|
|
258
|
+
const value = data[key];
|
|
259
|
+
if (typeof key === "string" && (typeof value === "string" || value instanceof Blob)) {
|
|
260
|
+
formData.append(key, value);
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
}
|
|
264
|
+
` : void 0;
|
|
265
|
+
return /* @__PURE__ */ jsx3(Function2, { name, export: true, generics, returnType, params, JSDoc, children: `
|
|
266
|
+
return {
|
|
267
|
+
fetcher: async () => {
|
|
268
|
+
${formData || ""}
|
|
269
|
+
const res = await client<${client.generics}>({
|
|
270
|
+
${resolvedClientOptions}
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
${returnRes}
|
|
274
|
+
},
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
` });
|
|
278
|
+
}
|
|
279
|
+
var defaultTemplates2 = {
|
|
280
|
+
default: Template2
|
|
281
|
+
};
|
|
282
|
+
function QueryOptions({ factory, dataReturnType, Template: Template4 = defaultTemplates2.default }) {
|
|
283
|
+
const {
|
|
284
|
+
pluginManager,
|
|
285
|
+
plugin: {
|
|
286
|
+
key: pluginKey,
|
|
287
|
+
options: { parser }
|
|
288
|
+
}
|
|
289
|
+
} = useApp3();
|
|
290
|
+
const { getSchemas } = useOperationManager3();
|
|
291
|
+
const operation = useOperation3();
|
|
292
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName3], type: "type" });
|
|
293
|
+
const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName], type: "function" });
|
|
294
|
+
const name = pluginManager.resolveName({
|
|
295
|
+
name: `${factory.name}QueryOptions`,
|
|
296
|
+
pluginKey
|
|
297
|
+
});
|
|
298
|
+
const contentType = operation.getContentType();
|
|
299
|
+
const generics = new FunctionParams2();
|
|
300
|
+
const params = new FunctionParams2();
|
|
301
|
+
const clientGenerics = ["TData", `${factory.name}['error']`];
|
|
302
|
+
const resultGenerics = ["TData", `${factory.name}['error']`];
|
|
303
|
+
generics.add([{ type: "TData", default: `${factory.name}['response']` }]);
|
|
304
|
+
params.add([
|
|
305
|
+
...getASTParams2(schemas.pathParams, { typed: true }),
|
|
306
|
+
{
|
|
307
|
+
name: "params",
|
|
308
|
+
type: `${factory.name}['queryParams']`,
|
|
309
|
+
enabled: !!schemas.queryParams?.name,
|
|
310
|
+
required: false
|
|
311
|
+
},
|
|
312
|
+
{
|
|
313
|
+
name: "headers",
|
|
314
|
+
type: `${factory.name}['headerParams']`,
|
|
315
|
+
enabled: !!schemas.headerParams?.name,
|
|
316
|
+
required: false
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
name: "options",
|
|
320
|
+
type: `${factory.name}['client']['parameters']`,
|
|
321
|
+
default: "{}"
|
|
322
|
+
}
|
|
323
|
+
]);
|
|
324
|
+
const client = {
|
|
325
|
+
withQueryParams: !!schemas.queryParams?.name,
|
|
326
|
+
withData: !!schemas.request?.name,
|
|
327
|
+
withPathParams: !!schemas.pathParams?.name,
|
|
328
|
+
withHeaders: !!schemas.headerParams?.name,
|
|
329
|
+
method: operation.method,
|
|
330
|
+
path: new URLPath2(operation.path),
|
|
331
|
+
generics: clientGenerics.join(", "),
|
|
332
|
+
contentType
|
|
333
|
+
};
|
|
334
|
+
return /* @__PURE__ */ jsx3(
|
|
335
|
+
Template4,
|
|
336
|
+
{
|
|
337
|
+
name,
|
|
338
|
+
params: params.toString(),
|
|
339
|
+
generics: generics.toString(),
|
|
340
|
+
returnType: `SWRConfiguration<${resultGenerics.join(", ")}>`,
|
|
341
|
+
client,
|
|
342
|
+
dataReturnType,
|
|
343
|
+
parser: parser === "zod" ? `${zodSchemas.response.name}.parse` : void 0
|
|
344
|
+
}
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
QueryOptions.templates = defaultTemplates2;
|
|
348
|
+
|
|
349
|
+
// src/components/Query.tsx
|
|
350
|
+
import { Fragment as Fragment2, jsx as jsx4, jsxs as jsxs3 } from "@kubb/react/jsx-runtime";
|
|
351
|
+
function Template3({ name, generics, returnType, params, JSDoc, hook, client }) {
|
|
352
|
+
if (client.withQueryParams) {
|
|
353
|
+
return /* @__PURE__ */ jsx4(Fragment2, { children: /* @__PURE__ */ jsx4(Function3, { name, export: true, generics, returnType, params, JSDoc, children: `
|
|
354
|
+
const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
|
|
355
|
+
|
|
356
|
+
const url = ${client.path.template}
|
|
357
|
+
const query = ${hook.name}<${hook.generics}>(
|
|
358
|
+
shouldFetch ? [url, params]: null,
|
|
359
|
+
{
|
|
360
|
+
...${hook.queryOptions},
|
|
361
|
+
...queryOptions
|
|
362
|
+
}
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
return query
|
|
366
|
+
` }) });
|
|
367
|
+
}
|
|
368
|
+
return /* @__PURE__ */ jsx4(Fragment2, { children: /* @__PURE__ */ jsx4(Function3, { name, export: true, generics, returnType, params, JSDoc, children: `
|
|
369
|
+
const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
|
|
370
|
+
|
|
371
|
+
const url = ${client.path.template}
|
|
372
|
+
const query = ${hook.name}<${hook.generics}>(
|
|
373
|
+
shouldFetch ? url : null,
|
|
374
|
+
{
|
|
375
|
+
...${hook.queryOptions},
|
|
376
|
+
...queryOptions
|
|
377
|
+
}
|
|
378
|
+
)
|
|
379
|
+
|
|
380
|
+
return query
|
|
381
|
+
` }) });
|
|
382
|
+
}
|
|
383
|
+
var defaultTemplates3 = {
|
|
384
|
+
default: Template3
|
|
385
|
+
};
|
|
386
|
+
function Query({ factory, Template: Template4 = defaultTemplates3.default, QueryOptionsTemplate = QueryOptions.templates.default }) {
|
|
387
|
+
const {
|
|
388
|
+
pluginManager,
|
|
389
|
+
plugin: {
|
|
390
|
+
key: pluginKey,
|
|
391
|
+
options: { dataReturnType }
|
|
392
|
+
}
|
|
393
|
+
} = useApp4();
|
|
394
|
+
const operation = useOperation4();
|
|
395
|
+
const { getSchemas, getName } = useOperationManager4();
|
|
396
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName4], type: "type" });
|
|
397
|
+
const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName2], type: "function" });
|
|
398
|
+
const name = getName(operation, { type: "function" });
|
|
399
|
+
const queryOptionsName = pluginManager.resolveName({
|
|
400
|
+
name: `${factory.name}QueryOptions`,
|
|
401
|
+
pluginKey
|
|
402
|
+
});
|
|
403
|
+
const generics = new FunctionParams3();
|
|
404
|
+
const params = new FunctionParams3();
|
|
405
|
+
const queryParams = new FunctionParams3();
|
|
406
|
+
const client = {
|
|
407
|
+
method: operation.method,
|
|
408
|
+
path: new URLPath3(operation.path),
|
|
409
|
+
withQueryParams: !!schemas.queryParams?.name,
|
|
410
|
+
withData: !!schemas.request?.name,
|
|
411
|
+
withPathParams: !!schemas.pathParams?.name,
|
|
412
|
+
withHeaders: !!schemas.headerParams?.name
|
|
413
|
+
};
|
|
414
|
+
const resultGenerics = ["TData", `${factory.name}["error"]`];
|
|
415
|
+
generics.add([{ type: "TData", default: `${factory.name}["response"]` }]);
|
|
416
|
+
const queryOptionsGenerics = ["TData"];
|
|
417
|
+
params.add([
|
|
418
|
+
...getASTParams3(schemas.pathParams, { typed: true }),
|
|
419
|
+
{
|
|
420
|
+
name: "params",
|
|
421
|
+
type: `${factory.name}['queryParams']`,
|
|
422
|
+
enabled: client.withQueryParams,
|
|
423
|
+
required: false
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
name: "headers",
|
|
427
|
+
type: `${factory.name}['headerParams']`,
|
|
428
|
+
enabled: client.withHeaders,
|
|
429
|
+
required: false
|
|
430
|
+
},
|
|
431
|
+
{
|
|
432
|
+
name: "options",
|
|
433
|
+
required: false,
|
|
434
|
+
type: `{
|
|
435
|
+
query?: SWRConfiguration<${resultGenerics.join(", ")}>,
|
|
436
|
+
client?: ${factory.name}['client']['parameters'],
|
|
437
|
+
shouldFetch?: boolean,
|
|
438
|
+
}`,
|
|
439
|
+
default: "{}"
|
|
440
|
+
}
|
|
441
|
+
]);
|
|
442
|
+
queryParams.add([
|
|
443
|
+
...getASTParams3(schemas.pathParams, { typed: false }),
|
|
444
|
+
{
|
|
445
|
+
name: "params",
|
|
446
|
+
enabled: client.withQueryParams,
|
|
447
|
+
required: false
|
|
448
|
+
},
|
|
449
|
+
{
|
|
450
|
+
name: "headers",
|
|
451
|
+
enabled: client.withHeaders,
|
|
452
|
+
required: false
|
|
453
|
+
},
|
|
454
|
+
{
|
|
455
|
+
name: "clientOptions",
|
|
456
|
+
required: false
|
|
457
|
+
}
|
|
458
|
+
]);
|
|
459
|
+
const hook = {
|
|
460
|
+
name: "useSWR",
|
|
461
|
+
generics: [...resultGenerics, client.withQueryParams ? "[typeof url, typeof params] | null" : "typeof url | null"].join(", "),
|
|
462
|
+
queryOptions: `${queryOptionsName}<${queryOptionsGenerics.join(", ")}>(${queryParams.toString()})`
|
|
463
|
+
};
|
|
464
|
+
return /* @__PURE__ */ jsxs3(Fragment2, { children: [
|
|
465
|
+
/* @__PURE__ */ jsx4(QueryOptions, { factory, Template: QueryOptionsTemplate, dataReturnType }),
|
|
466
|
+
/* @__PURE__ */ jsx4(
|
|
467
|
+
Template4,
|
|
468
|
+
{
|
|
469
|
+
name,
|
|
470
|
+
generics: generics.toString(),
|
|
471
|
+
JSDoc: { comments: getComments2(operation) },
|
|
472
|
+
client,
|
|
473
|
+
hook,
|
|
474
|
+
params: params.toString(),
|
|
475
|
+
returnType: `SWRResponse<${resultGenerics.join(", ")}>`
|
|
476
|
+
}
|
|
477
|
+
)
|
|
478
|
+
] });
|
|
479
|
+
}
|
|
480
|
+
Query.File = function({ templates }) {
|
|
481
|
+
const {
|
|
482
|
+
pluginManager,
|
|
483
|
+
plugin: {
|
|
484
|
+
options: {
|
|
485
|
+
extName,
|
|
486
|
+
client: { importPath },
|
|
487
|
+
parser
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
} = useApp4();
|
|
491
|
+
const { getSchemas, getFile, getName } = useOperationManager4();
|
|
492
|
+
const operation = useOperation4();
|
|
493
|
+
const file = getFile(operation);
|
|
494
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName4], type: "type" });
|
|
495
|
+
const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName2], type: "function" });
|
|
496
|
+
const fileType = getFile(operation, { pluginKey: [pluginTsName4] });
|
|
497
|
+
const fileZodSchemas = getFile(operation, {
|
|
498
|
+
pluginKey: [pluginZodName2]
|
|
499
|
+
});
|
|
500
|
+
const factoryName = getName(operation, { type: "type" });
|
|
501
|
+
const Template4 = templates?.query.default || defaultTemplates3.default;
|
|
502
|
+
const QueryOptionsTemplate = templates?.queryOptions.default || QueryOptions.templates.default;
|
|
503
|
+
const factory = {
|
|
504
|
+
name: factoryName
|
|
505
|
+
};
|
|
506
|
+
return /* @__PURE__ */ jsx4(Parser2, { language: "typescript", children: /* @__PURE__ */ jsxs3(File2, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
|
507
|
+
parser === "zod" && /* @__PURE__ */ jsx4(File2.Import, { extName, name: [zodSchemas.response.name], root: file.path, path: fileZodSchemas.path }),
|
|
508
|
+
/* @__PURE__ */ jsx4(File2.Import, { name: "useSWR", path: "swr" }),
|
|
509
|
+
/* @__PURE__ */ jsx4(File2.Import, { name: ["SWRConfiguration", "SWRResponse"], path: "swr", isTypeOnly: true }),
|
|
510
|
+
/* @__PURE__ */ jsx4(File2.Import, { name: "client", path: importPath }),
|
|
511
|
+
/* @__PURE__ */ jsx4(File2.Import, { name: ["ResponseConfig"], path: importPath, isTypeOnly: true }),
|
|
512
|
+
/* @__PURE__ */ jsx4(
|
|
513
|
+
File2.Import,
|
|
514
|
+
{
|
|
515
|
+
extName,
|
|
516
|
+
name: [
|
|
517
|
+
schemas.request?.name,
|
|
518
|
+
schemas.response.name,
|
|
519
|
+
schemas.pathParams?.name,
|
|
520
|
+
schemas.queryParams?.name,
|
|
521
|
+
schemas.headerParams?.name,
|
|
522
|
+
...schemas.statusCodes?.map((item) => item.name) || []
|
|
523
|
+
].filter(Boolean),
|
|
524
|
+
root: file.path,
|
|
525
|
+
path: fileType.path,
|
|
526
|
+
isTypeOnly: true
|
|
527
|
+
}
|
|
528
|
+
),
|
|
529
|
+
/* @__PURE__ */ jsxs3(File2.Source, { children: [
|
|
530
|
+
/* @__PURE__ */ jsx4(SchemaType, { factory }),
|
|
531
|
+
/* @__PURE__ */ jsx4(Query, { factory, Template: Template4, QueryOptionsTemplate })
|
|
532
|
+
] })
|
|
533
|
+
] }) });
|
|
534
|
+
};
|
|
535
|
+
Query.templates = defaultTemplates3;
|
|
536
|
+
|
|
537
|
+
export {
|
|
538
|
+
Mutation,
|
|
539
|
+
QueryOptions,
|
|
540
|
+
Query
|
|
541
|
+
};
|
|
542
|
+
//# sourceMappingURL=chunk-ECJ346AA.js.map
|