@kubb/plugin-swr 5.0.0-beta.80 → 5.0.0-beta.84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +85 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +39 -167
- package/dist/index.js +41 -105
- package/dist/index.js.map +1 -1
- package/package.json +4 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,161 +1,5 @@
|
|
|
1
1
|
import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
-
import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "
|
|
3
|
-
//#region ../../internals/client/src/types.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Validator applied to request and response bodies using schemas from `@kubb/plugin-zod`.
|
|
6
|
-
* - `false`: no validation.
|
|
7
|
-
* - `'zod'`: validates the success (2xx) response body, and the error body when a non-2xx call does
|
|
8
|
-
* not throw (`throwOnError: false`).
|
|
9
|
-
* - `{ request?: 'zod'; response?: 'zod' }`: opt in per direction. `request` validates the request
|
|
10
|
-
* body and query parameters before the call; `response` validates the success response body and,
|
|
11
|
-
* on the non-throw path, the error body.
|
|
12
|
-
*/
|
|
13
|
-
type ValidatorOptions = false | 'zod' | {
|
|
14
|
-
request?: 'zod';
|
|
15
|
-
response?: 'zod';
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* How the class-based SDK groups operations.
|
|
19
|
-
* - `'tag'`: one class per tag, optionally composed into a root client.
|
|
20
|
-
* - `'flat'`: one class with every operation as a direct method.
|
|
21
|
-
*/
|
|
22
|
-
type Mode = 'tag' | 'flat';
|
|
23
|
-
/**
|
|
24
|
-
* The resolver shared by the client plugins. Functions and files use camelCase; URL helpers get
|
|
25
|
-
* a `get<Operation>Url` name.
|
|
26
|
-
*/
|
|
27
|
-
type ResolverClient = Resolver & {
|
|
28
|
-
/**
|
|
29
|
-
* Resolves the function name for a raw operation name.
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* `resolver.resolveName('show pet by id') // -> 'showPetById'`
|
|
33
|
-
*/
|
|
34
|
-
resolveName(this: ResolverClient, name: string): string;
|
|
35
|
-
/**
|
|
36
|
-
* Resolves the output file name for a generated client module.
|
|
37
|
-
*/
|
|
38
|
-
resolvePathName(this: ResolverClient, name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
|
|
39
|
-
/**
|
|
40
|
-
* Resolves the generated class name for class-based clients.
|
|
41
|
-
*/
|
|
42
|
-
resolveClassName(this: ResolverClient, name: string): string;
|
|
43
|
-
/**
|
|
44
|
-
* Resolves the generated class name for a tag-based client group. The default appends a
|
|
45
|
-
* `Client` suffix (tag `pet` becomes `PetClient`) so the class never collides with the schema
|
|
46
|
-
* model of the same name in the barrel.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* `resolver.resolveGroupName('pet') // -> 'PetClient'`
|
|
50
|
-
*/
|
|
51
|
-
resolveGroupName(this: ResolverClient, name: string): string;
|
|
52
|
-
/**
|
|
53
|
-
* Resolves the property name a tag client is exposed under on the composed root SDK
|
|
54
|
-
* (`new PetStore(config).pet`).
|
|
55
|
-
*/
|
|
56
|
-
resolveClientPropertyName(this: ResolverClient, name: string): string;
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* The shared options surface for the client plugins. Deliberately small: there is one
|
|
60
|
-
* response contract and one grouped options object. Each plugin extends this with its own
|
|
61
|
-
* `transport` field.
|
|
62
|
-
*/
|
|
63
|
-
type Options$1 = OutputOptions & {
|
|
64
|
-
/**
|
|
65
|
-
* Skip operations matching at least one entry in the list.
|
|
66
|
-
*/
|
|
67
|
-
exclude?: Array<Exclude>;
|
|
68
|
-
/**
|
|
69
|
-
* Restrict generation to operations matching at least one entry in the list.
|
|
70
|
-
*/
|
|
71
|
-
include?: Array<Include>;
|
|
72
|
-
/**
|
|
73
|
-
* Apply a different options object to operations matching a pattern.
|
|
74
|
-
*/
|
|
75
|
-
override?: Array<Override<ResolvedOptions$1>>;
|
|
76
|
-
/**
|
|
77
|
-
* Base URL prepended to every request. When omitted, falls back to the adapter's server URL.
|
|
78
|
-
*/
|
|
79
|
-
baseURL?: string;
|
|
80
|
-
/**
|
|
81
|
-
* Validate request and response bodies with schemas from `@kubb/plugin-zod`.
|
|
82
|
-
*
|
|
83
|
-
* @default false
|
|
84
|
-
*/
|
|
85
|
-
validator?: ValidatorOptions;
|
|
86
|
-
/**
|
|
87
|
-
* Generates a class-based SDK instead of the standalone functions. Each tag client is an instance
|
|
88
|
-
* class whose constructor takes a client config and builds its own client, so every environment is
|
|
89
|
-
* a separate instance. Leave `sdk` unset to keep the standalone per-operation functions (the
|
|
90
|
-
* default), which is also what query plugins consume.
|
|
91
|
-
*
|
|
92
|
-
* @example Instance class per tag
|
|
93
|
-
* ```ts
|
|
94
|
-
* pluginFetch({ sdk: {} })
|
|
95
|
-
* // const api = new PetClient({ baseURL: 'https://api.example.com' })
|
|
96
|
-
* // await api.getPetById({ path: { petId: 1 } })
|
|
97
|
-
* ```
|
|
98
|
-
* @example Composed root that instantiates every tag client from one config
|
|
99
|
-
* ```ts
|
|
100
|
-
* pluginFetch({ sdk: { name: 'petStore' } })
|
|
101
|
-
* // class PetStore {
|
|
102
|
-
* // readonly pet: PetClient
|
|
103
|
-
* // readonly store: StoreClient
|
|
104
|
-
* // constructor(config = {}) { ... }
|
|
105
|
-
* // }
|
|
106
|
-
* // const api = new PetStore({ baseURL })
|
|
107
|
-
* // await api.pet.getPetById({ path: { petId: 1 } })
|
|
108
|
-
* ```
|
|
109
|
-
* @example Flat class with every operation as a direct method
|
|
110
|
-
* ```ts
|
|
111
|
-
* pluginFetch({ sdk: { name: 'petStore', mode: 'flat' } })
|
|
112
|
-
* // const api = new PetStore({ baseURL })
|
|
113
|
-
* // await api.getPetById({ path: { petId: 1 } })
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
sdk?: {
|
|
117
|
-
/**
|
|
118
|
-
* How the SDK groups operations.
|
|
119
|
-
* - `'tag'`: one class per tag. With `name`, a composed root instantiates every tag client.
|
|
120
|
-
* - `'flat'`: one class named by `name`, with every operation as a direct method.
|
|
121
|
-
*
|
|
122
|
-
* @default 'tag'
|
|
123
|
-
*/
|
|
124
|
-
mode?: Mode;
|
|
125
|
-
/**
|
|
126
|
-
* Name of the generated entry point, also the file name. With `mode: 'tag'` it emits a
|
|
127
|
-
* composed root class that instantiates every tag client from one shared config. With
|
|
128
|
-
* `mode: 'flat'` it names the single class.
|
|
129
|
-
*/
|
|
130
|
-
name?: string;
|
|
131
|
-
};
|
|
132
|
-
/**
|
|
133
|
-
* Override how names and file paths are built. Methods you omit fall back to the default resolver.
|
|
134
|
-
*/
|
|
135
|
-
resolver?: Partial<ResolverClient> & ThisType<ResolverClient>;
|
|
136
|
-
/**
|
|
137
|
-
* Macros applied to each operation node before code is printed.
|
|
138
|
-
*/
|
|
139
|
-
macros?: Array<ast.Macro>;
|
|
140
|
-
};
|
|
141
|
-
/**
|
|
142
|
-
* The resolved options after defaults are applied.
|
|
143
|
-
*/
|
|
144
|
-
type ResolvedOptions$1 = {
|
|
145
|
-
output: Output;
|
|
146
|
-
exclude: Array<Exclude>;
|
|
147
|
-
include: Array<Include> | undefined;
|
|
148
|
-
override: Array<Override<ResolvedOptions$1>>;
|
|
149
|
-
group: Group | null;
|
|
150
|
-
baseURL: Options$1['baseURL'];
|
|
151
|
-
validator: NonNullable<Options$1['validator']>;
|
|
152
|
-
sdk: {
|
|
153
|
-
mode: Mode;
|
|
154
|
-
name: string | undefined;
|
|
155
|
-
} | undefined;
|
|
156
|
-
resolver: ResolverClient;
|
|
157
|
-
};
|
|
158
|
-
//#endregion
|
|
2
|
+
import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "kubb/kit";
|
|
159
3
|
//#region ../../internals/client/src/resolveClient.d.ts
|
|
160
4
|
/**
|
|
161
5
|
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
@@ -250,14 +94,14 @@ type Query = {
|
|
|
250
94
|
/**
|
|
251
95
|
* HTTP methods to use for queries.
|
|
252
96
|
*
|
|
253
|
-
* @default ['
|
|
97
|
+
* @default ['GET']
|
|
254
98
|
*/
|
|
255
99
|
methods?: Array<string>;
|
|
256
100
|
/**
|
|
257
101
|
* Path to the useSWR hook for useSWR functionality.
|
|
258
102
|
* Used as `import useSWR from '${importPath}'`.
|
|
259
103
|
* Accepts relative and absolute paths.
|
|
260
|
-
* Path is used as-is
|
|
104
|
+
* Path is used as-is. Relative paths are based on the generated file location.
|
|
261
105
|
* @default 'swr'
|
|
262
106
|
*/
|
|
263
107
|
importPath?: string;
|
|
@@ -266,14 +110,14 @@ type Mutation = {
|
|
|
266
110
|
/**
|
|
267
111
|
* HTTP methods to use for mutations.
|
|
268
112
|
*
|
|
269
|
-
* @default ['
|
|
113
|
+
* @default ['POST', 'PUT', 'PATCH', 'DELETE']
|
|
270
114
|
*/
|
|
271
115
|
methods?: Array<string>;
|
|
272
116
|
/**
|
|
273
117
|
* Path to the useSWRMutation hook for useSWRMutation functionality.
|
|
274
118
|
* Used as `import useSWRMutation from '${importPath}'`.
|
|
275
119
|
* Accepts relative and absolute paths.
|
|
276
|
-
* Path is used as-is
|
|
120
|
+
* Path is used as-is. Relative paths are based on the generated file location.
|
|
277
121
|
* @default 'swr/mutation'
|
|
278
122
|
*/
|
|
279
123
|
importPath?: string;
|
|
@@ -308,20 +152,24 @@ type Options = OutputOptions & {
|
|
|
308
152
|
* Override options for specific tags, operations, or paths.
|
|
309
153
|
*/
|
|
310
154
|
override?: Array<Override<ResolvedOptions>>;
|
|
155
|
+
/**
|
|
156
|
+
* Custom `queryKey` builder. Use to add a version namespace, swap to
|
|
157
|
+
* operation IDs, or shape keys to match an existing invalidation strategy.
|
|
158
|
+
*/
|
|
311
159
|
queryKey?: QueryKey;
|
|
312
160
|
/**
|
|
313
161
|
* Configure useSWR behavior.
|
|
314
162
|
*/
|
|
315
163
|
query?: Partial<Query> | false;
|
|
164
|
+
/**
|
|
165
|
+
* Custom `mutationKey` builder. Useful when you match keys from SWR's
|
|
166
|
+
* global `mutate` for cache updates.
|
|
167
|
+
*/
|
|
316
168
|
mutationKey?: MutationKey;
|
|
317
169
|
/**
|
|
318
170
|
* Configure useSWRMutation behavior.
|
|
319
171
|
*/
|
|
320
172
|
mutation?: Partial<Mutation> | false;
|
|
321
|
-
/**
|
|
322
|
-
* Parser to use for validating response data.
|
|
323
|
-
*/
|
|
324
|
-
validator?: Options$1['validator'];
|
|
325
173
|
/**
|
|
326
174
|
* Override naming conventions for function names and types.
|
|
327
175
|
*/
|
|
@@ -349,7 +197,6 @@ type ResolvedOptions = {
|
|
|
349
197
|
* The resolved contract client the generators import and call.
|
|
350
198
|
*/
|
|
351
199
|
client: ResolvedClient;
|
|
352
|
-
validator: NonNullable<Options['validator']>;
|
|
353
200
|
queryKey: QueryKey | undefined;
|
|
354
201
|
query: NonNullable<Required<Query>> | false;
|
|
355
202
|
mutationKey: MutationKey | undefined;
|
|
@@ -366,8 +213,33 @@ declare global {
|
|
|
366
213
|
}
|
|
367
214
|
//#endregion
|
|
368
215
|
//#region src/plugin.d.ts
|
|
216
|
+
/**
|
|
217
|
+
* Canonical plugin name for `@kubb/plugin-swr`. Used for driver lookups and cross-plugin
|
|
218
|
+
* dependency references.
|
|
219
|
+
*/
|
|
369
220
|
declare const pluginSwrName = "plugin-swr";
|
|
370
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Generates SWR hooks from OpenAPI operations. Read operations become `useFoo` hooks built on
|
|
223
|
+
* `useSWR` and write operations become `useFoo` hooks built on `useSWRMutation`, wrapping the
|
|
224
|
+
* client functions from `@kubb/plugin-axios` or `@kubb/plugin-fetch`.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* import { defineConfig } from 'kubb/config'
|
|
229
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
230
|
+
* import { pluginSwr } from '@kubb/plugin-swr'
|
|
231
|
+
*
|
|
232
|
+
* export default defineConfig({
|
|
233
|
+
* input: { path: './petStore.yaml' },
|
|
234
|
+
* output: { path: './src/gen' },
|
|
235
|
+
* plugins: [
|
|
236
|
+
* pluginTs(),
|
|
237
|
+
* pluginSwr({ output: { path: './hooks' } }),
|
|
238
|
+
* ],
|
|
239
|
+
* })
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
declare const pluginSwr: (options?: Options | undefined) => import("kubb/kit").Plugin<PluginSwr>;
|
|
371
243
|
//#endregion
|
|
372
244
|
export { type PluginSwr, type Transformer, pluginSwr as default, pluginSwr, pluginSwrName };
|
|
373
245
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { ast, defineGenerator, definePlugin, defineResolver } from "
|
|
3
|
+
import { ast, defineGenerator, definePlugin, defineResolver } from "kubb/kit";
|
|
4
4
|
import { createFunctionParameter, createFunctionParameters, createObjectBindingPattern, createTypeLiteral, functionPrinter, pluginTsName } from "@kubb/plugin-ts";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { Fragment, jsx, jsxs } from "@kubb/renderer-jsx/jsx-runtime";
|
|
5
|
+
import { File, Function, Type, jsxRenderer } from "kubb/jsx";
|
|
6
|
+
import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
|
|
8
7
|
//#region ../../internals/utils/src/casing.ts
|
|
9
8
|
/**
|
|
10
9
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -525,16 +524,6 @@ function createGroupConfig(group) {
|
|
|
525
524
|
};
|
|
526
525
|
}
|
|
527
526
|
//#endregion
|
|
528
|
-
//#region ../../internals/client/src/builders/validatorOptions.ts
|
|
529
|
-
/**
|
|
530
|
-
* Returns `true` when any direction of the validator uses zod (used for dependency checks).
|
|
531
|
-
*/
|
|
532
|
-
function isValidatorEnabled(validator) {
|
|
533
|
-
if (!validator) return false;
|
|
534
|
-
if (validator === "zod") return true;
|
|
535
|
-
return Boolean(validator.request || validator.response);
|
|
536
|
-
}
|
|
537
|
-
//#endregion
|
|
538
527
|
//#region ../../internals/client/src/resolveClient.ts
|
|
539
528
|
/**
|
|
540
529
|
* Resolves which client plugin a consumer (react-query, vue-query, swr, mcp) should call for an
|
|
@@ -753,51 +742,6 @@ function buildClientCall(node, options) {
|
|
|
753
742
|
].filter((part) => part !== null).join(", ")} })`;
|
|
754
743
|
}
|
|
755
744
|
/**
|
|
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
745
|
* Build QueryKey params as the grouped `{ path, query, body }` object (NO headers, NO config),
|
|
802
746
|
* typed from the operation's `RequestConfig` minus `url`. The query key transformer reads the
|
|
803
747
|
* grouped `path`/`query`/`body` bindings.
|
|
@@ -1062,7 +1006,7 @@ const mutationGenerator = defineGenerator({
|
|
|
1062
1006
|
operation(node, ctx) {
|
|
1063
1007
|
if (!ast.isHttpOperationNode(node)) return null;
|
|
1064
1008
|
const { config, driver, resolver, root } = ctx;
|
|
1065
|
-
const { output, query, mutation,
|
|
1009
|
+
const { output, query, mutation, client, group } = ctx.options;
|
|
1066
1010
|
const pluginTs = driver.getPlugin(pluginTsName);
|
|
1067
1011
|
if (!pluginTs) return null;
|
|
1068
1012
|
const tsResolver = driver.getResolver(pluginTsName);
|
|
@@ -1108,19 +1052,6 @@ const mutationGenerator = defineGenerator({
|
|
|
1108
1052
|
order: "body-response-first",
|
|
1109
1053
|
includeParams: false
|
|
1110
1054
|
})].filter((name) => Boolean(name));
|
|
1111
|
-
const pluginZod = isValidatorEnabled(validator) ? driver.getPlugin(pluginZodName) : void 0;
|
|
1112
|
-
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : void 0;
|
|
1113
|
-
const fileZod = zodResolver ? zodResolver.resolveFile({
|
|
1114
|
-
name: node.operationId,
|
|
1115
|
-
extname: ".ts",
|
|
1116
|
-
tag: node.tags[0] ?? "default",
|
|
1117
|
-
path: node.path
|
|
1118
|
-
}, {
|
|
1119
|
-
root,
|
|
1120
|
-
output: pluginZod?.options?.output ?? output,
|
|
1121
|
-
group: pluginZod?.options?.group
|
|
1122
|
-
}) : void 0;
|
|
1123
|
-
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, validator);
|
|
1124
1055
|
const calledClientName = contractOp.name;
|
|
1125
1056
|
return /* @__PURE__ */ jsxs(File, {
|
|
1126
1057
|
baseName: meta.file.baseName,
|
|
@@ -1143,11 +1074,6 @@ const mutationGenerator = defineGenerator({
|
|
|
1143
1074
|
}
|
|
1144
1075
|
}),
|
|
1145
1076
|
children: [
|
|
1146
|
-
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
|
|
1147
|
-
name: zodSchemaNames,
|
|
1148
|
-
root: meta.file.path,
|
|
1149
|
-
path: fileZod.path
|
|
1150
|
-
}),
|
|
1151
1077
|
/* @__PURE__ */ jsx(File.Import, {
|
|
1152
1078
|
name: [contractOp.name],
|
|
1153
1079
|
root: meta.file.path,
|
|
@@ -1197,13 +1123,17 @@ const mutationGenerator = defineGenerator({
|
|
|
1197
1123
|
});
|
|
1198
1124
|
//#endregion
|
|
1199
1125
|
//#region src/generators/queryGenerator.tsx
|
|
1126
|
+
/**
|
|
1127
|
+
* Built-in generator for `useSWR` hooks. Emits one `useFoo` hook per read operation plus the
|
|
1128
|
+
* matching `fooQueryKey` helper and `fooQueryOptions` factory.
|
|
1129
|
+
*/
|
|
1200
1130
|
const queryGenerator = defineGenerator({
|
|
1201
1131
|
name: "swr-query",
|
|
1202
1132
|
renderer: jsxRenderer,
|
|
1203
1133
|
operation(node, ctx) {
|
|
1204
1134
|
if (!ast.isHttpOperationNode(node)) return null;
|
|
1205
1135
|
const { config, driver, resolver, root } = ctx;
|
|
1206
|
-
const { output, query, mutation,
|
|
1136
|
+
const { output, query, mutation, client, group } = ctx.options;
|
|
1207
1137
|
const pluginTs = driver.getPlugin(pluginTsName);
|
|
1208
1138
|
if (!pluginTs) return null;
|
|
1209
1139
|
const tsResolver = driver.getResolver(pluginTsName);
|
|
@@ -1251,19 +1181,6 @@ const queryGenerator = defineGenerator({
|
|
|
1251
1181
|
order: "body-response-first",
|
|
1252
1182
|
includeParams: false
|
|
1253
1183
|
})].filter((name) => Boolean(name));
|
|
1254
|
-
const pluginZod = isValidatorEnabled(validator) ? driver.getPlugin(pluginZodName) : void 0;
|
|
1255
|
-
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : void 0;
|
|
1256
|
-
const fileZod = zodResolver ? zodResolver.resolveFile({
|
|
1257
|
-
name: node.operationId,
|
|
1258
|
-
extname: ".ts",
|
|
1259
|
-
tag: node.tags[0] ?? "default",
|
|
1260
|
-
path: node.path
|
|
1261
|
-
}, {
|
|
1262
|
-
root,
|
|
1263
|
-
output: pluginZod?.options?.output ?? output,
|
|
1264
|
-
group: pluginZod?.options?.group
|
|
1265
|
-
}) : void 0;
|
|
1266
|
-
const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, validator);
|
|
1267
1184
|
const calledClientName = contractOp.name;
|
|
1268
1185
|
return /* @__PURE__ */ jsxs(File, {
|
|
1269
1186
|
baseName: meta.file.baseName,
|
|
@@ -1286,11 +1203,6 @@ const queryGenerator = defineGenerator({
|
|
|
1286
1203
|
}
|
|
1287
1204
|
}),
|
|
1288
1205
|
children: [
|
|
1289
|
-
fileZod && zodSchemaNames.length > 0 && /* @__PURE__ */ jsx(File.Import, {
|
|
1290
|
-
name: zodSchemaNames,
|
|
1291
|
-
root: meta.file.path,
|
|
1292
|
-
path: fileZod.path
|
|
1293
|
-
}),
|
|
1294
1206
|
/* @__PURE__ */ jsx(File.Import, {
|
|
1295
1207
|
name: [contractOp.name],
|
|
1296
1208
|
root: meta.file.path,
|
|
@@ -1399,18 +1311,43 @@ const resolverSwr = defineResolver(() => ({
|
|
|
1399
1311
|
}));
|
|
1400
1312
|
//#endregion
|
|
1401
1313
|
//#region src/plugin.ts
|
|
1314
|
+
/**
|
|
1315
|
+
* Canonical plugin name for `@kubb/plugin-swr`. Used for driver lookups and cross-plugin
|
|
1316
|
+
* dependency references.
|
|
1317
|
+
*/
|
|
1402
1318
|
const pluginSwrName = "plugin-swr";
|
|
1319
|
+
/**
|
|
1320
|
+
* Generates SWR hooks from OpenAPI operations. Read operations become `useFoo` hooks built on
|
|
1321
|
+
* `useSWR` and write operations become `useFoo` hooks built on `useSWRMutation`, wrapping the
|
|
1322
|
+
* client functions from `@kubb/plugin-axios` or `@kubb/plugin-fetch`.
|
|
1323
|
+
*
|
|
1324
|
+
* @example
|
|
1325
|
+
* ```ts
|
|
1326
|
+
* import { defineConfig } from 'kubb/config'
|
|
1327
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1328
|
+
* import { pluginSwr } from '@kubb/plugin-swr'
|
|
1329
|
+
*
|
|
1330
|
+
* export default defineConfig({
|
|
1331
|
+
* input: { path: './petStore.yaml' },
|
|
1332
|
+
* output: { path: './src/gen' },
|
|
1333
|
+
* plugins: [
|
|
1334
|
+
* pluginTs(),
|
|
1335
|
+
* pluginSwr({ output: { path: './hooks' } }),
|
|
1336
|
+
* ],
|
|
1337
|
+
* })
|
|
1338
|
+
* ```
|
|
1339
|
+
*/
|
|
1403
1340
|
const pluginSwr = definePlugin((options) => {
|
|
1404
1341
|
const { output = {
|
|
1405
1342
|
path: "hooks",
|
|
1406
1343
|
barrel: { type: "named" }
|
|
1407
|
-
}, group, exclude = [], include, override = [],
|
|
1344
|
+
}, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, resolver: userResolver, macros: userMacros } = options;
|
|
1408
1345
|
const selectedGenerators = [queryGenerator, mutationGenerator].filter((generator) => Boolean(generator));
|
|
1409
1346
|
const groupConfig = createGroupConfig(group) ?? void 0;
|
|
1410
1347
|
return {
|
|
1411
1348
|
name: pluginSwrName,
|
|
1412
1349
|
options,
|
|
1413
|
-
dependencies: [pluginTsName
|
|
1350
|
+
dependencies: [pluginTsName],
|
|
1414
1351
|
hooks: { "kubb:plugin:setup"(ctx) {
|
|
1415
1352
|
const resolver = userResolver ? {
|
|
1416
1353
|
...resolverSwr,
|
|
@@ -1431,21 +1368,20 @@ const pluginSwr = definePlugin((options) => {
|
|
|
1431
1368
|
queryKey,
|
|
1432
1369
|
query: query === false ? false : {
|
|
1433
1370
|
importPath: "swr",
|
|
1434
|
-
methods: ["
|
|
1371
|
+
methods: ["GET"],
|
|
1435
1372
|
...query
|
|
1436
1373
|
},
|
|
1437
1374
|
mutationKey,
|
|
1438
1375
|
mutation: mutation === false ? false : {
|
|
1439
1376
|
importPath: "swr/mutation",
|
|
1440
1377
|
methods: [
|
|
1441
|
-
"
|
|
1442
|
-
"
|
|
1443
|
-
"
|
|
1444
|
-
"
|
|
1378
|
+
"POST",
|
|
1379
|
+
"PUT",
|
|
1380
|
+
"PATCH",
|
|
1381
|
+
"DELETE"
|
|
1445
1382
|
],
|
|
1446
1383
|
...mutation
|
|
1447
1384
|
},
|
|
1448
|
-
validator,
|
|
1449
1385
|
group: groupConfig,
|
|
1450
1386
|
exclude,
|
|
1451
1387
|
include,
|