@kubb/plugin-svelte-query 0.0.0-canary-20241104172400

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/chunk-CDRGJAED.cjs +452 -0
  4. package/dist/chunk-CDRGJAED.cjs.map +1 -0
  5. package/dist/chunk-JGITTOE5.js +445 -0
  6. package/dist/chunk-JGITTOE5.js.map +1 -0
  7. package/dist/chunk-KRGCKSGR.cjs +527 -0
  8. package/dist/chunk-KRGCKSGR.cjs.map +1 -0
  9. package/dist/chunk-R6ZVBNG7.js +519 -0
  10. package/dist/chunk-R6ZVBNG7.js.map +1 -0
  11. package/dist/components.cjs +28 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +103 -0
  14. package/dist/components.d.ts +103 -0
  15. package/dist/components.js +3 -0
  16. package/dist/components.js.map +1 -0
  17. package/dist/generators.cjs +17 -0
  18. package/dist/generators.cjs.map +1 -0
  19. package/dist/generators.d.cts +10 -0
  20. package/dist/generators.d.ts +10 -0
  21. package/dist/generators.js +4 -0
  22. package/dist/generators.js.map +1 -0
  23. package/dist/index.cjs +127 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +9 -0
  26. package/dist/index.d.ts +9 -0
  27. package/dist/index.js +120 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-B9W9aYra.d.cts +365 -0
  30. package/dist/types-B9W9aYra.d.ts +365 -0
  31. package/package.json +102 -0
  32. package/src/components/Mutation.tsx +158 -0
  33. package/src/components/MutationKey.tsx +54 -0
  34. package/src/components/Query.tsx +176 -0
  35. package/src/components/QueryKey.tsx +83 -0
  36. package/src/components/QueryOptions.tsx +133 -0
  37. package/src/components/index.ts +5 -0
  38. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +51 -0
  39. package/src/generators/__snapshots__/clientGetImportPath.ts +51 -0
  40. package/src/generators/__snapshots__/clientPostImportPath.ts +44 -0
  41. package/src/generators/__snapshots__/findByTags.ts +51 -0
  42. package/src/generators/__snapshots__/findByTagsObject.ts +60 -0
  43. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +51 -0
  44. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +51 -0
  45. package/src/generators/__snapshots__/findByTagsWithZod.ts +51 -0
  46. package/src/generators/__snapshots__/postAsQuery.ts +50 -0
  47. package/src/generators/__snapshots__/updatePetById.ts +44 -0
  48. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +44 -0
  49. package/src/generators/index.ts +2 -0
  50. package/src/generators/mutationGenerator.tsx +117 -0
  51. package/src/generators/queryGenerator.tsx +129 -0
  52. package/src/index.ts +2 -0
  53. package/src/plugin.ts +141 -0
  54. package/src/types.ts +135 -0
@@ -0,0 +1,365 @@
1
+ import { PluginFactoryOptions, Output, Group, ResolveNameParams } from '@kubb/core';
2
+ import { HttpMethod, Operation } from '@kubb/oas';
3
+ import { ResolvePathOptions, Exclude, Include, Override, Generator, OperationSchemas } from '@kubb/plugin-oas';
4
+
5
+ type Options$2 = {
6
+ /**
7
+ * Specify the export location for the files and define the behavior of the output
8
+ * @default { path: 'clients', barrelType: 'named' }
9
+ */
10
+ output?: Output;
11
+ /**
12
+ * Group the clients based on the provided name.
13
+ */
14
+ group?: Group;
15
+ /**
16
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
17
+ */
18
+ exclude?: Array<Exclude>;
19
+ /**
20
+ * Array containing include parameters to include tags/operations/methods/paths.
21
+ */
22
+ include?: Array<Include>;
23
+ /**
24
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
25
+ */
26
+ override?: Array<Override<ResolvedOptions$2>>;
27
+ /**
28
+ * Create `operations.ts` file with all operations grouped by methods.
29
+ * @default false
30
+ */
31
+ operations?: boolean;
32
+ /**
33
+ * Path to the client import path that will be used to do the API calls.
34
+ * It will be used as `import client from '${client.importPath}'`.
35
+ * It allows both relative and absolute path but be aware that we will not change the path.
36
+ * @default '@kubb/plugin-client/client'
37
+ */
38
+ importPath?: string;
39
+ /**
40
+ * Allows you to set a custom base url for all generated calls.
41
+ */
42
+ baseURL?: string;
43
+ /**
44
+ * ReturnType that will be used when calling the client.
45
+ * - 'data' will return ResponseConfig[data].
46
+ * - 'full' will return ResponseConfig.
47
+ * @default 'data'
48
+ */
49
+ dataReturnType?: 'data' | 'full';
50
+ /**
51
+ * How to pass your params
52
+ * - 'object' will return the params and pathParams as an object.
53
+ * - 'inline' will return the params as comma separated params.
54
+ * @default 'inline'
55
+ */
56
+ paramsType?: 'object' | 'inline';
57
+ /**
58
+ * How to pass your pathParams.
59
+ * - 'object' will return the pathParams as an object.
60
+ * - 'inline' will return the pathParams as comma separated params.
61
+ * @default 'inline'
62
+ */
63
+ pathParamsType?: 'object' | 'inline';
64
+ /**
65
+ * Which parser can be used before returning the data
66
+ * - 'zod' will use `@kubb/plugin-zod` to parse the data.
67
+ * @default 'client'
68
+ */
69
+ parser?: 'client' | 'zod';
70
+ transformers?: {
71
+ /**
72
+ * Customize the names based on the type that is provided by the plugin.
73
+ */
74
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
75
+ };
76
+ /**
77
+ * Define some generators next to the client generators
78
+ */
79
+ generators?: Array<Generator<PluginClient>>;
80
+ };
81
+ type ResolvedOptions$2 = {
82
+ output: Output;
83
+ group?: Options$2['group'];
84
+ baseURL: string | undefined;
85
+ parser: NonNullable<Options$2['parser']>;
86
+ importPath: NonNullable<Options$2['importPath']>;
87
+ dataReturnType: NonNullable<Options$2['dataReturnType']>;
88
+ pathParamsType: NonNullable<Options$2['pathParamsType']>;
89
+ paramsType: NonNullable<Options$2['paramsType']>;
90
+ };
91
+ type PluginClient = PluginFactoryOptions<'plugin-client', Options$2, ResolvedOptions$2, never, ResolvePathOptions>;
92
+
93
+ type TransformerProps$1 = {
94
+ operation: Operation;
95
+ schemas: OperationSchemas;
96
+ };
97
+ type Transformer$1 = (props: TransformerProps$1) => unknown[];
98
+ type Suspense = object;
99
+ /**
100
+ * Customize the queryKey
101
+ */
102
+ type QueryKey$1 = Transformer$1;
103
+ /**
104
+ * Customize the mutationKey
105
+ */
106
+ type MutationKey$1 = Transformer$1;
107
+ type Query$1 = {
108
+ /**
109
+ * Define which HttpMethods can be used for queries
110
+ * @default ['get']
111
+ */
112
+ methods: Array<HttpMethod>;
113
+ /**
114
+ * Path to the useQuery that will be used to do the useQuery functionality.
115
+ * It will be used as `import { useQuery } from '${importPath}'`.
116
+ * It allows both relative and absolute path.
117
+ * the path will be applied as is, so relative path should be based on the file being generated.
118
+ * @default '@tanstack/react-query'
119
+ */
120
+ importPath?: string;
121
+ };
122
+ type Mutation$1 = {
123
+ /**
124
+ * Define which HttpMethods can be used for mutations
125
+ * @default ['post', 'put', 'delete']
126
+ */
127
+ methods: Array<HttpMethod>;
128
+ /**
129
+ * Path to the useQuery that will be used to do the useQuery functionality.
130
+ * It will be used as `import { useQuery } from '${importPath}'`.
131
+ * It allows both relative and absolute path.
132
+ * the path will be applied as is, so relative path should be based on the file being generated.
133
+ * @default '@tanstack/react-query'
134
+ */
135
+ importPath?: string;
136
+ };
137
+ type Infinite = {
138
+ /**
139
+ * Specify the params key used for `pageParam`.
140
+ * @default 'id'
141
+ */
142
+ queryParam: string;
143
+ /**
144
+ * Which field of the data will be used, set it to undefined when no cursor is known.
145
+ */
146
+ cursorParam?: string | undefined;
147
+ /**
148
+ * The initial value, the value of the first page.
149
+ * @default 0
150
+ */
151
+ initialPageParam: unknown;
152
+ };
153
+ type Options$1 = {
154
+ /**
155
+ * Specify the export location for the files and define the behavior of the output
156
+ * @default { path: 'hooks', barrelType: 'named' }
157
+ */
158
+ output?: Output;
159
+ /**
160
+ * Group the @tanstack/query hooks based on the provided name.
161
+ */
162
+ group?: Group;
163
+ client?: Pick<PluginClient['options'], 'dataReturnType' | 'importPath' | 'baseURL'>;
164
+ /**
165
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
166
+ */
167
+ exclude?: Array<Exclude>;
168
+ /**
169
+ * Array containing include parameters to include tags/operations/methods/paths.
170
+ */
171
+ include?: Array<Include>;
172
+ /**
173
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
174
+ */
175
+ override?: Array<Override<ResolvedOptions$1>>;
176
+ /**
177
+ * How to pass your params
178
+ * - 'object' will return the params and pathParams as an object.
179
+ * - 'inline' will return the params as comma separated params.
180
+ * @default 'inline'
181
+ */
182
+ paramsType?: 'object' | 'inline';
183
+ /**
184
+ * How to pass your pathParams.
185
+ * - 'object' will return the pathParams as an object.
186
+ * - 'inline' will return the pathParams as comma separated params.
187
+ * @default 'inline'
188
+ */
189
+ pathParamsType?: PluginClient['options']['pathParamsType'];
190
+ /**
191
+ * When set, an infiniteQuery hooks will be added.
192
+ */
193
+ infinite?: Partial<Infinite> | false;
194
+ /**
195
+ * When set, a suspenseQuery hooks will be added.
196
+ */
197
+ suspense?: Partial<Suspense> | false;
198
+ queryKey?: QueryKey$1;
199
+ /**
200
+ * Override some useQuery behaviours.
201
+ */
202
+ query?: Partial<Query$1> | false;
203
+ mutationKey?: MutationKey$1;
204
+ /**
205
+ * Override some useMutation behaviours.
206
+ */
207
+ mutation?: Partial<Mutation$1> | false;
208
+ /**
209
+ * Which parser should be used before returning the data to `@tanstack/query`.
210
+ * `'zod'` will use `@kubb/plugin-zod` to parse the data.
211
+ */
212
+ parser?: PluginClient['options']['parser'];
213
+ transformers?: {
214
+ /**
215
+ * Customize the names based on the type that is provided by the plugin.
216
+ */
217
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
218
+ };
219
+ /**
220
+ * Define some generators next to the react-query generators
221
+ */
222
+ generators?: Array<Generator<PluginReactQuery>>;
223
+ };
224
+ type ResolvedOptions$1 = {
225
+ output: Output;
226
+ client: Required<Omit<NonNullable<PluginReactQuery['options']['client']>, 'baseURL'>> & {
227
+ baseURL?: string;
228
+ };
229
+ parser: Required<NonNullable<Options$1['parser']>>;
230
+ pathParamsType: NonNullable<Options$1['pathParamsType']>;
231
+ paramsType: NonNullable<Options$1['paramsType']>;
232
+ /**
233
+ * Only used of infinite
234
+ */
235
+ infinite: NonNullable<Infinite> | false;
236
+ suspense: Suspense | false;
237
+ queryKey: QueryKey$1 | undefined;
238
+ query: NonNullable<Required<Query$1>> | false;
239
+ mutationKey: MutationKey$1 | undefined;
240
+ mutation: NonNullable<Required<Mutation$1>> | false;
241
+ };
242
+ type PluginReactQuery = PluginFactoryOptions<'plugin-react-query', Options$1, ResolvedOptions$1, never, ResolvePathOptions>;
243
+
244
+ type TransformerProps = {
245
+ operation: Operation;
246
+ schemas: OperationSchemas;
247
+ };
248
+ type Transformer = (props: TransformerProps) => unknown[];
249
+ /**
250
+ * Customize the queryKey
251
+ */
252
+ type QueryKey = Transformer;
253
+ /**
254
+ * Customize the mutationKey
255
+ */
256
+ type MutationKey = Transformer;
257
+ type Query = {
258
+ /**
259
+ * Define which HttpMethods can be used for queries
260
+ * @default ['get']
261
+ */
262
+ methods: Array<HttpMethod>;
263
+ /**
264
+ * Path to the useQuery that will be used to do the useQuery functionality.
265
+ * It will be used as `import { useQuery } from '${importPath}'`.
266
+ * It allows both relative and absolute path.
267
+ * the path will be applied as is, so relative path should be based on the file being generated.
268
+ * @default '@tanstack/svelte-query'
269
+ */
270
+ importPath?: string;
271
+ };
272
+ type Mutation = {
273
+ /**
274
+ * Define which HttpMethods can be used for mutations
275
+ * @default ['post', 'put', 'delete']
276
+ */
277
+ methods: Array<HttpMethod>;
278
+ /**
279
+ * Path to the useQuery that will be used to do the useQuery functionality.
280
+ * It will be used as `import { useQuery } from '${importPath}'`.
281
+ * It allows both relative and absolute path.
282
+ * the path will be applied as is, so relative path should be based on the file being generated.
283
+ * @default '@tanstack/svelte-query'
284
+ */
285
+ importPath?: string;
286
+ };
287
+ type Options = {
288
+ /**
289
+ * Specify the export location for the files and define the behavior of the output
290
+ * @default { path: 'hooks', barrelType: 'named' }
291
+ */
292
+ output?: Output;
293
+ /**
294
+ * Group the @tanstack/query hooks based on the provided name.
295
+ */
296
+ group?: Group;
297
+ client?: Pick<PluginClient['options'], 'dataReturnType' | 'importPath' | 'baseURL'>;
298
+ /**
299
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
300
+ */
301
+ exclude?: Array<Exclude>;
302
+ /**
303
+ * Array containing include parameters to include tags/operations/methods/paths.
304
+ */
305
+ include?: Array<Include>;
306
+ /**
307
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
308
+ */
309
+ override?: Array<Override<ResolvedOptions>>;
310
+ /**
311
+ * How to pass your params
312
+ * - 'object' will return the params and pathParams as an object.
313
+ * - 'inline' will return the params as comma separated params.
314
+ * @default 'inline'
315
+ */
316
+ paramsType?: 'object' | 'inline';
317
+ /**
318
+ * How to pass your pathParams.
319
+ * - 'object' will return the pathParams as an object.
320
+ * - 'inline' will return the pathParams as comma separated params.
321
+ * @default 'inline'
322
+ */
323
+ pathParamsType?: PluginClient['options']['pathParamsType'];
324
+ queryKey?: QueryKey;
325
+ /**
326
+ * Override some useQuery behaviours.
327
+ */
328
+ query?: Partial<Query> | false;
329
+ mutationKey?: MutationKey;
330
+ /**
331
+ * Override some useMutation behaviours.
332
+ */
333
+ mutation?: Mutation | false;
334
+ /**
335
+ * Which parser should be used before returning the data to `@tanstack/query`.
336
+ * `'zod'` will use `@kubb/plugin-zod` to parse the data.
337
+ */
338
+ parser?: PluginClient['options']['parser'];
339
+ transformers?: {
340
+ /**
341
+ * Customize the names based on the type that is provided by the plugin.
342
+ */
343
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
344
+ };
345
+ /**
346
+ * Define some generators next to the svelte-query generators
347
+ */
348
+ generators?: Array<Generator<PluginSvelteQuery>>;
349
+ };
350
+ type ResolvedOptions = {
351
+ output: Output;
352
+ client: Required<Omit<NonNullable<PluginReactQuery['options']['client']>, 'baseURL'>> & {
353
+ baseURL?: string;
354
+ };
355
+ parser: Required<NonNullable<Options['parser']>>;
356
+ paramsType: NonNullable<Options['paramsType']>;
357
+ pathParamsType: NonNullable<Options['pathParamsType']>;
358
+ queryKey: QueryKey | undefined;
359
+ query: NonNullable<Required<Query>> | false;
360
+ mutationKey: MutationKey | undefined;
361
+ mutation: NonNullable<Required<Mutation>> | false;
362
+ };
363
+ type PluginSvelteQuery = PluginFactoryOptions<'plugin-svelte-query', Options, ResolvedOptions, never, ResolvePathOptions>;
364
+
365
+ export type { Options as O, PluginSvelteQuery as P, Transformer as T };
package/package.json ADDED
@@ -0,0 +1,102 @@
1
+ {
2
+ "name": "@kubb/plugin-svelte-query",
3
+ "version": "0.0.0-canary-20241104172400",
4
+ "description": "Generator svelte-query hooks",
5
+ "keywords": [
6
+ "faker",
7
+ "faker.js",
8
+ "msw",
9
+ "mock",
10
+ "mocking",
11
+ "plugins",
12
+ "kubb",
13
+ "codegen",
14
+ "swagger",
15
+ "openapi"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git://github.com/kubb-labs/kubb.git",
20
+ "directory": "packages/plugin-svelte-query"
21
+ },
22
+ "license": "MIT",
23
+ "author": "Stijn Van Hulle <stijn@stijnvanhulle.be",
24
+ "sideEffects": false,
25
+ "type": "module",
26
+ "exports": {
27
+ ".": {
28
+ "import": "./dist/index.js",
29
+ "require": "./dist/index.cjs",
30
+ "default": "./dist/index.cjs"
31
+ },
32
+ "./generators": {
33
+ "import": "./dist/generators.js",
34
+ "require": "./dist/generators.cjs",
35
+ "default": "./dist/generators.cjs"
36
+ },
37
+ "./components": {
38
+ "import": "./dist/components.js",
39
+ "require": "./dist/components.cjs",
40
+ "default": "./dist/components.cjs"
41
+ },
42
+ "./package.json": "./package.json",
43
+ "./*": "./*"
44
+ },
45
+ "main": "dist/index.cjs",
46
+ "module": "dist/index.js",
47
+ "types": "./dist/index.d.ts",
48
+ "typesVersions": {
49
+ "*": {
50
+ "components": [
51
+ "./dist/components.d.ts"
52
+ ],
53
+ "generators": [
54
+ "./dist/generators.d.ts"
55
+ ]
56
+ }
57
+ },
58
+ "files": [
59
+ "src",
60
+ "dist",
61
+ "!/**/**.test.**",
62
+ "!/**/__tests__/**"
63
+ ],
64
+ "dependencies": {
65
+ "@kubb/core": "0.0.0-canary-20241104172400",
66
+ "@kubb/fs": "0.0.0-canary-20241104172400",
67
+ "@kubb/oas": "0.0.0-canary-20241104172400",
68
+ "@kubb/plugin-oas": "0.0.0-canary-20241104172400",
69
+ "@kubb/plugin-ts": "0.0.0-canary-20241104172400",
70
+ "@kubb/plugin-zod": "0.0.0-canary-20241104172400",
71
+ "@kubb/react": "0.0.0-canary-20241104172400"
72
+ },
73
+ "devDependencies": {
74
+ "@types/react": "^18.3.12",
75
+ "react": "^18.3.1",
76
+ "tsup": "^8.3.5",
77
+ "typescript": "^5.6.3",
78
+ "@kubb/config-ts": "0.0.0-canary-20241104172400",
79
+ "@kubb/config-tsup": "0.0.0-canary-20241104172400"
80
+ },
81
+ "peerDependencies": {
82
+ "@kubb/react": "0.0.0-canary-20241104172400"
83
+ },
84
+ "engines": {
85
+ "node": ">=20"
86
+ },
87
+ "publishConfig": {
88
+ "access": "public",
89
+ "registry": "https://registry.npmjs.org/"
90
+ },
91
+ "scripts": {
92
+ "build": "tsup",
93
+ "clean": "npx rimraf ./dist",
94
+ "lint": "bun biome lint .",
95
+ "lint:fix": "bun biome lint --apply-unsafe .",
96
+ "release": "pnpm publish --no-git-check",
97
+ "release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
98
+ "start": "tsup --watch",
99
+ "test": "vitest --passWithNoTests",
100
+ "typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
101
+ }
102
+ }
@@ -0,0 +1,158 @@
1
+ import { File, Function, FunctionParams } from '@kubb/react'
2
+
3
+ import { type Operation, isOptional } from '@kubb/oas'
4
+ import { Client } from '@kubb/plugin-client/components'
5
+ import type { OperationSchemas } from '@kubb/plugin-oas'
6
+ import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
7
+ import type { Params } from '@kubb/react/types'
8
+ import type { ReactNode } from 'react'
9
+ import type { PluginSvelteQuery } from '../types.ts'
10
+ import { MutationKey } from './MutationKey.tsx'
11
+
12
+ type Props = {
13
+ /**
14
+ * Name of the function
15
+ */
16
+ name: string
17
+ typeName: string
18
+ clientName: string
19
+ mutationKeyName: string
20
+ typeSchemas: OperationSchemas
21
+ operation: Operation
22
+ paramsType: PluginSvelteQuery['resolvedOptions']['paramsType']
23
+ dataReturnType: PluginSvelteQuery['resolvedOptions']['client']['dataReturnType']
24
+ pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
25
+ }
26
+
27
+ type GetParamsProps = {
28
+ pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
29
+ dataReturnType: PluginSvelteQuery['resolvedOptions']['client']['dataReturnType']
30
+ typeSchemas: OperationSchemas
31
+ }
32
+
33
+ function getParams({ dataReturnType, typeSchemas }: GetParamsProps) {
34
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
35
+ const mutationParams = FunctionParams.factory({
36
+ ...getPathParams(typeSchemas.pathParams, { typed: true }),
37
+ data: typeSchemas.request?.name
38
+ ? {
39
+ type: typeSchemas.request?.name,
40
+ optional: isOptional(typeSchemas.request?.schema),
41
+ }
42
+ : undefined,
43
+ params: typeSchemas.queryParams?.name
44
+ ? {
45
+ type: typeSchemas.queryParams?.name,
46
+ optional: isOptional(typeSchemas.queryParams?.schema),
47
+ }
48
+ : undefined,
49
+ headers: typeSchemas.headerParams?.name
50
+ ? {
51
+ type: typeSchemas.headerParams?.name,
52
+ optional: isOptional(typeSchemas.headerParams?.schema),
53
+ }
54
+ : undefined,
55
+ })
56
+ const TRequest = mutationParams.toConstructor({ valueAsType: true })
57
+
58
+ return FunctionParams.factory({
59
+ options: {
60
+ type: `
61
+ {
62
+ mutation?: CreateMutationOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', TRequest ? `{${TRequest}}` : undefined].filter(Boolean).join(', ')}>,
63
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'},
64
+ }
65
+ `,
66
+ default: '{}',
67
+ },
68
+ })
69
+ }
70
+
71
+ export function Mutation({ name, clientName, paramsType, pathParamsType, dataReturnType, typeSchemas, operation, mutationKeyName }: Props): ReactNode {
72
+ const mutationKeyParams = MutationKey.getParams({
73
+ pathParamsType,
74
+ typeSchemas,
75
+ })
76
+
77
+ const params = getParams({
78
+ pathParamsType,
79
+ dataReturnType,
80
+ typeSchemas,
81
+ })
82
+
83
+ const clientParams = Client.getParams({
84
+ paramsType,
85
+ typeSchemas,
86
+ pathParamsType,
87
+ })
88
+ const mutationParams = FunctionParams.factory({
89
+ ...getPathParams(typeSchemas.pathParams, { typed: true }),
90
+ data: typeSchemas.request?.name
91
+ ? {
92
+ type: typeSchemas.request?.name,
93
+ optional: isOptional(typeSchemas.request?.schema),
94
+ }
95
+ : undefined,
96
+ params: typeSchemas.queryParams?.name
97
+ ? {
98
+ type: typeSchemas.queryParams?.name,
99
+ optional: isOptional(typeSchemas.queryParams?.schema),
100
+ }
101
+ : undefined,
102
+ headers: typeSchemas.headerParams?.name
103
+ ? {
104
+ type: typeSchemas.headerParams?.name,
105
+ optional: isOptional(typeSchemas.headerParams?.schema),
106
+ }
107
+ : undefined,
108
+ })
109
+ const dataParams = FunctionParams.factory({
110
+ data: {
111
+ // No use of pathParams because useMutation can only take one argument in object form,
112
+ // see https://tanstack.com/query/latest/docs/framework/react/reference/useMutation#usemutation
113
+ mode: 'object',
114
+ children: Object.entries(mutationParams.params).reduce((acc, [key, value]) => {
115
+ if (value) {
116
+ acc[key] = {
117
+ ...value,
118
+ type: undefined,
119
+ }
120
+ }
121
+
122
+ return acc
123
+ }, {} as Params),
124
+ },
125
+ })
126
+
127
+ const TRequest = mutationParams.toConstructor({ valueAsType: true })
128
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
129
+ const generics = [TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', TRequest ? `{${TRequest}}` : undefined]
130
+ .filter(Boolean)
131
+ .join(', ')
132
+
133
+ return (
134
+ <File.Source name={name} isExportable isIndexable>
135
+ <Function
136
+ name={name}
137
+ export
138
+ params={params.toConstructor()}
139
+ JSDoc={{
140
+ comments: getComments(operation),
141
+ }}
142
+ >
143
+ {`
144
+ const { mutation: mutationOptions, client: config = {} } = options ?? {}
145
+ const mutationKey = mutationOptions?.mutationKey ?? ${mutationKeyName}(${mutationKeyParams.toCall()})
146
+
147
+ return createMutation<${generics}>({
148
+ mutationFn: async(${dataParams.toConstructor()}) => {
149
+ return ${clientName}(${clientParams.toCall()})
150
+ },
151
+ mutationKey,
152
+ ...mutationOptions
153
+ })
154
+ `}
155
+ </Function>
156
+ </File.Source>
157
+ )
158
+ }
@@ -0,0 +1,54 @@
1
+ import { URLPath } from '@kubb/core/utils'
2
+ import { File, Function, FunctionParams, Type } from '@kubb/react'
3
+
4
+ import type { Operation } from '@kubb/oas'
5
+ import type { OperationSchemas } from '@kubb/plugin-oas'
6
+ import type { ReactNode } from 'react'
7
+ import type { PluginSvelteQuery, Transformer } from '../types'
8
+
9
+ type Props = {
10
+ name: string
11
+ typeName: string
12
+ typeSchemas: OperationSchemas
13
+ operation: Operation
14
+ pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
15
+ transformer: Transformer | undefined
16
+ }
17
+
18
+ type GetParamsProps = {
19
+ pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
20
+ typeSchemas: OperationSchemas
21
+ }
22
+
23
+ function getParams({}: GetParamsProps) {
24
+ return FunctionParams.factory({})
25
+ }
26
+
27
+ const getTransformer: Transformer = ({ operation }) => {
28
+ const path = new URLPath(operation.path)
29
+
30
+ return [JSON.stringify({ url: path.path })].filter(Boolean)
31
+ }
32
+
33
+ export function MutationKey({ name, typeSchemas, pathParamsType, operation, typeName, transformer = getTransformer }: Props): ReactNode {
34
+ const params = getParams({ pathParamsType, typeSchemas })
35
+ const keys = transformer({ operation, schemas: typeSchemas })
36
+
37
+ return (
38
+ <>
39
+ <File.Source name={name} isExportable isIndexable>
40
+ <Function.Arrow name={name} export params={params.toConstructor()} singleLine>
41
+ {`[${keys.join(', ')}] as const`}
42
+ </Function.Arrow>
43
+ </File.Source>
44
+ <File.Source name={typeName} isExportable isIndexable isTypeOnly>
45
+ <Type name={typeName} export>
46
+ {`ReturnType<typeof ${name}>`}
47
+ </Type>
48
+ </File.Source>
49
+ </>
50
+ )
51
+ }
52
+
53
+ MutationKey.getParams = getParams
54
+ MutationKey.getTransformer = getTransformer