@kubb/plugin-swr 3.0.0-alpha.2 → 3.0.0-alpha.20
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 +13 -4
- package/dist/chunk-7WG6LPZJ.js +185 -0
- package/dist/chunk-7WG6LPZJ.js.map +1 -0
- package/dist/chunk-DGY6XSP2.js +187 -0
- package/dist/chunk-DGY6XSP2.js.map +1 -0
- package/dist/chunk-RMG5RYPG.cjs +189 -0
- package/dist/chunk-RMG5RYPG.cjs.map +1 -0
- package/dist/chunk-WI3XUYBA.cjs +194 -0
- package/dist/chunk-WI3XUYBA.cjs.map +1 -0
- package/dist/components.cjs +15 -6
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +49 -6
- package/dist/components.d.ts +49 -6
- package/dist/components.js +2 -10
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +17 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +10 -0
- package/dist/generators.d.ts +10 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +90 -127
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -4
- package/dist/index.d.ts +1 -4
- package/dist/index.js +70 -114
- package/dist/index.js.map +1 -1
- package/dist/types-BCd4p4VZ.d.cts +127 -0
- package/dist/types-BCd4p4VZ.d.ts +127 -0
- package/package.json +22 -16
- package/src/components/Mutation.tsx +82 -227
- package/src/components/Query.tsx +86 -264
- package/src/components/QueryOptions.tsx +58 -179
- package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +40 -0
- package/src/generators/__snapshots__/clientGetImportPath.ts +40 -0
- package/src/generators/__snapshots__/clientPostImportPath.ts +30 -0
- package/src/generators/__snapshots__/findByTags.ts +40 -0
- package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +40 -0
- package/src/generators/__snapshots__/findByTagsWithZod.ts +40 -0
- package/src/generators/__snapshots__/getAsMutation.ts +32 -0
- package/src/generators/__snapshots__/postAsQuery.ts +39 -0
- package/src/generators/__snapshots__/updatePetById.ts +30 -0
- package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +34 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/mutationGenerator.tsx +96 -0
- package/src/generators/queryGenerator.tsx +101 -0
- package/src/plugin.ts +61 -50
- package/src/types.ts +63 -55
- package/dist/chunk-ECJ346AA.js +0 -542
- package/dist/chunk-ECJ346AA.js.map +0 -1
- package/dist/chunk-H4LHXYRJ.cjs +0 -542
- package/dist/chunk-H4LHXYRJ.cjs.map +0 -1
- package/dist/index-B3C-JOIU.d.cts +0 -299
- package/dist/index-B3C-JOIU.d.ts +0 -299
- package/src/OperationGenerator.tsx +0 -75
- package/src/components/SchemaType.tsx +0 -59
- package/src/components/__snapshots__/Mutation/Pets.ts +0 -42
- package/src/components/__snapshots__/Query/showPetById.ts +0 -55
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { PluginFactoryOptions, Output, ResolveNameParams } from '@kubb/core';
|
|
2
|
+
import { HttpMethod } from '@kubb/oas';
|
|
3
|
+
import { ResolvePathOptions, Exclude, Include, Override } from '@kubb/plugin-oas';
|
|
4
|
+
|
|
5
|
+
type Options = {
|
|
6
|
+
/**
|
|
7
|
+
* @default 'hooks'
|
|
8
|
+
*/
|
|
9
|
+
output?: Output;
|
|
10
|
+
/**
|
|
11
|
+
* Group the SWR hooks based on the provided name.
|
|
12
|
+
*/
|
|
13
|
+
group?: {
|
|
14
|
+
/**
|
|
15
|
+
* Tag will group based on the operation tag inside the Swagger file
|
|
16
|
+
*/
|
|
17
|
+
type: 'tag';
|
|
18
|
+
/**
|
|
19
|
+
* Relative path to save the grouped SWR hooks.
|
|
20
|
+
*
|
|
21
|
+
* `{{tag}}` will be replaced by the current tagName.
|
|
22
|
+
* @example `${output}/{{tag}}Controller` => `hooks/PetController`
|
|
23
|
+
* @default `${output}/{{tag}}Controller`
|
|
24
|
+
*/
|
|
25
|
+
output?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Name to be used for the `export * as {{exportAs}} from './`
|
|
28
|
+
* @default `"{{tag}}SWRHooks"`
|
|
29
|
+
*/
|
|
30
|
+
exportAs?: string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
34
|
+
*/
|
|
35
|
+
exclude?: Array<Exclude>;
|
|
36
|
+
/**
|
|
37
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
38
|
+
*/
|
|
39
|
+
include?: Array<Include>;
|
|
40
|
+
/**
|
|
41
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
42
|
+
*/
|
|
43
|
+
override?: Array<Override<ResolvedOptions>>;
|
|
44
|
+
client?: {
|
|
45
|
+
/**
|
|
46
|
+
* Path to the client import path that will be used to do the API calls.
|
|
47
|
+
* It will be used as `import client from '${client.importPath}'`.
|
|
48
|
+
* It allows both relative and absolute path.
|
|
49
|
+
* the path will be applied as is, so relative path shoule be based on the file being generated.
|
|
50
|
+
* @default '@kubb/plugin-client/client'
|
|
51
|
+
*/
|
|
52
|
+
importPath?: string;
|
|
53
|
+
/**
|
|
54
|
+
* ReturnType that needs to be used when calling client().
|
|
55
|
+
*
|
|
56
|
+
* `Data` will return ResponseConfig[data].
|
|
57
|
+
*
|
|
58
|
+
* `Full` will return ResponseConfig.
|
|
59
|
+
* @default `'data'`
|
|
60
|
+
* @private
|
|
61
|
+
*/
|
|
62
|
+
dataReturnType?: 'data' | 'full';
|
|
63
|
+
};
|
|
64
|
+
query?: {
|
|
65
|
+
/**
|
|
66
|
+
* Define which HttpMethods can be used for queries
|
|
67
|
+
* @default ['get']
|
|
68
|
+
*/
|
|
69
|
+
methods?: Array<HttpMethod>;
|
|
70
|
+
/**
|
|
71
|
+
* Path to the useQuery that will be used to do the useQuery functionality.
|
|
72
|
+
* It will be used as `import { useQuery } from '${importPath}'`.
|
|
73
|
+
* It allows both relative and absolute path.
|
|
74
|
+
* the path will be applied as is, so relative path should be based on the file being generated.
|
|
75
|
+
* @default '@tanstack/react-query' if 'framework' is set to 'react'
|
|
76
|
+
*/
|
|
77
|
+
importPath?: string;
|
|
78
|
+
};
|
|
79
|
+
mutation?: {
|
|
80
|
+
/**
|
|
81
|
+
* Define which HttpMethods can be used for queries
|
|
82
|
+
* @default ['post', 'put', 'delete', 'patch']
|
|
83
|
+
*/
|
|
84
|
+
methods?: Array<HttpMethod>;
|
|
85
|
+
/**
|
|
86
|
+
* Path to the useQuery that will be used to do the useQuery functionality.
|
|
87
|
+
* It will be used as `import { useQuery } from '${importPath}'`.
|
|
88
|
+
* It allows both relative and absolute path.
|
|
89
|
+
* the path will be applied as is, so relative path should be based on the file being generated.
|
|
90
|
+
* @default '@tanstack/react-query' if 'framework' is set to 'react'
|
|
91
|
+
*/
|
|
92
|
+
importPath?: string;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* How to pass your pathParams.
|
|
96
|
+
*
|
|
97
|
+
* `object` will return the pathParams as an object.
|
|
98
|
+
*
|
|
99
|
+
* `inline` will return the pathParams as comma separated params.
|
|
100
|
+
* @default `'inline'`
|
|
101
|
+
* @private
|
|
102
|
+
*/
|
|
103
|
+
pathParamsType?: 'object' | 'inline';
|
|
104
|
+
/**
|
|
105
|
+
* Which parser can be used before returning the data to `swr`.
|
|
106
|
+
* `'zod'` will use `@kubb/plugin-zod` to parse the data.
|
|
107
|
+
*/
|
|
108
|
+
parser?: 'client' | 'zod';
|
|
109
|
+
transformers?: {
|
|
110
|
+
/**
|
|
111
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
112
|
+
*/
|
|
113
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
type ResolvedOptions = {
|
|
117
|
+
output: Output;
|
|
118
|
+
baseURL: string | undefined;
|
|
119
|
+
client: Required<NonNullable<Options['client']>>;
|
|
120
|
+
parser: Required<NonNullable<Options['parser']>>;
|
|
121
|
+
mutation: Required<NonNullable<Options['mutation']>>;
|
|
122
|
+
query: Required<NonNullable<Options['query']>>;
|
|
123
|
+
pathParamsType: NonNullable<Options['pathParamsType']>;
|
|
124
|
+
};
|
|
125
|
+
type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, never, ResolvePathOptions>;
|
|
126
|
+
|
|
127
|
+
export type { Options as O, PluginSwr as P };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { PluginFactoryOptions, Output, ResolveNameParams } from '@kubb/core';
|
|
2
|
+
import { HttpMethod } from '@kubb/oas';
|
|
3
|
+
import { ResolvePathOptions, Exclude, Include, Override } from '@kubb/plugin-oas';
|
|
4
|
+
|
|
5
|
+
type Options = {
|
|
6
|
+
/**
|
|
7
|
+
* @default 'hooks'
|
|
8
|
+
*/
|
|
9
|
+
output?: Output;
|
|
10
|
+
/**
|
|
11
|
+
* Group the SWR hooks based on the provided name.
|
|
12
|
+
*/
|
|
13
|
+
group?: {
|
|
14
|
+
/**
|
|
15
|
+
* Tag will group based on the operation tag inside the Swagger file
|
|
16
|
+
*/
|
|
17
|
+
type: 'tag';
|
|
18
|
+
/**
|
|
19
|
+
* Relative path to save the grouped SWR hooks.
|
|
20
|
+
*
|
|
21
|
+
* `{{tag}}` will be replaced by the current tagName.
|
|
22
|
+
* @example `${output}/{{tag}}Controller` => `hooks/PetController`
|
|
23
|
+
* @default `${output}/{{tag}}Controller`
|
|
24
|
+
*/
|
|
25
|
+
output?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Name to be used for the `export * as {{exportAs}} from './`
|
|
28
|
+
* @default `"{{tag}}SWRHooks"`
|
|
29
|
+
*/
|
|
30
|
+
exportAs?: string;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
34
|
+
*/
|
|
35
|
+
exclude?: Array<Exclude>;
|
|
36
|
+
/**
|
|
37
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
38
|
+
*/
|
|
39
|
+
include?: Array<Include>;
|
|
40
|
+
/**
|
|
41
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
42
|
+
*/
|
|
43
|
+
override?: Array<Override<ResolvedOptions>>;
|
|
44
|
+
client?: {
|
|
45
|
+
/**
|
|
46
|
+
* Path to the client import path that will be used to do the API calls.
|
|
47
|
+
* It will be used as `import client from '${client.importPath}'`.
|
|
48
|
+
* It allows both relative and absolute path.
|
|
49
|
+
* the path will be applied as is, so relative path shoule be based on the file being generated.
|
|
50
|
+
* @default '@kubb/plugin-client/client'
|
|
51
|
+
*/
|
|
52
|
+
importPath?: string;
|
|
53
|
+
/**
|
|
54
|
+
* ReturnType that needs to be used when calling client().
|
|
55
|
+
*
|
|
56
|
+
* `Data` will return ResponseConfig[data].
|
|
57
|
+
*
|
|
58
|
+
* `Full` will return ResponseConfig.
|
|
59
|
+
* @default `'data'`
|
|
60
|
+
* @private
|
|
61
|
+
*/
|
|
62
|
+
dataReturnType?: 'data' | 'full';
|
|
63
|
+
};
|
|
64
|
+
query?: {
|
|
65
|
+
/**
|
|
66
|
+
* Define which HttpMethods can be used for queries
|
|
67
|
+
* @default ['get']
|
|
68
|
+
*/
|
|
69
|
+
methods?: Array<HttpMethod>;
|
|
70
|
+
/**
|
|
71
|
+
* Path to the useQuery that will be used to do the useQuery functionality.
|
|
72
|
+
* It will be used as `import { useQuery } from '${importPath}'`.
|
|
73
|
+
* It allows both relative and absolute path.
|
|
74
|
+
* the path will be applied as is, so relative path should be based on the file being generated.
|
|
75
|
+
* @default '@tanstack/react-query' if 'framework' is set to 'react'
|
|
76
|
+
*/
|
|
77
|
+
importPath?: string;
|
|
78
|
+
};
|
|
79
|
+
mutation?: {
|
|
80
|
+
/**
|
|
81
|
+
* Define which HttpMethods can be used for queries
|
|
82
|
+
* @default ['post', 'put', 'delete', 'patch']
|
|
83
|
+
*/
|
|
84
|
+
methods?: Array<HttpMethod>;
|
|
85
|
+
/**
|
|
86
|
+
* Path to the useQuery that will be used to do the useQuery functionality.
|
|
87
|
+
* It will be used as `import { useQuery } from '${importPath}'`.
|
|
88
|
+
* It allows both relative and absolute path.
|
|
89
|
+
* the path will be applied as is, so relative path should be based on the file being generated.
|
|
90
|
+
* @default '@tanstack/react-query' if 'framework' is set to 'react'
|
|
91
|
+
*/
|
|
92
|
+
importPath?: string;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* How to pass your pathParams.
|
|
96
|
+
*
|
|
97
|
+
* `object` will return the pathParams as an object.
|
|
98
|
+
*
|
|
99
|
+
* `inline` will return the pathParams as comma separated params.
|
|
100
|
+
* @default `'inline'`
|
|
101
|
+
* @private
|
|
102
|
+
*/
|
|
103
|
+
pathParamsType?: 'object' | 'inline';
|
|
104
|
+
/**
|
|
105
|
+
* Which parser can be used before returning the data to `swr`.
|
|
106
|
+
* `'zod'` will use `@kubb/plugin-zod` to parse the data.
|
|
107
|
+
*/
|
|
108
|
+
parser?: 'client' | 'zod';
|
|
109
|
+
transformers?: {
|
|
110
|
+
/**
|
|
111
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
112
|
+
*/
|
|
113
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
type ResolvedOptions = {
|
|
117
|
+
output: Output;
|
|
118
|
+
baseURL: string | undefined;
|
|
119
|
+
client: Required<NonNullable<Options['client']>>;
|
|
120
|
+
parser: Required<NonNullable<Options['parser']>>;
|
|
121
|
+
mutation: Required<NonNullable<Options['mutation']>>;
|
|
122
|
+
query: Required<NonNullable<Options['query']>>;
|
|
123
|
+
pathParamsType: NonNullable<Options['pathParamsType']>;
|
|
124
|
+
};
|
|
125
|
+
type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, never, ResolvePathOptions>;
|
|
126
|
+
|
|
127
|
+
export type { Options as O, PluginSwr as P };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-swr",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.20",
|
|
4
4
|
"description": "Generator plugin-swr",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -30,6 +30,11 @@
|
|
|
30
30
|
"require": "./dist/index.cjs",
|
|
31
31
|
"default": "./dist/index.cjs"
|
|
32
32
|
},
|
|
33
|
+
"./generators": {
|
|
34
|
+
"import": "./dist/generators.js",
|
|
35
|
+
"require": "./dist/generators.cjs",
|
|
36
|
+
"default": "./dist/generators.cjs"
|
|
37
|
+
},
|
|
33
38
|
"./components": {
|
|
34
39
|
"import": "./dist/components.js",
|
|
35
40
|
"require": "./dist/components.cjs",
|
|
@@ -45,6 +50,9 @@
|
|
|
45
50
|
"*": {
|
|
46
51
|
"components": [
|
|
47
52
|
"./dist/components.d.ts"
|
|
53
|
+
],
|
|
54
|
+
"generators": [
|
|
55
|
+
"./dist/generators.d.ts"
|
|
48
56
|
]
|
|
49
57
|
}
|
|
50
58
|
},
|
|
@@ -55,27 +63,25 @@
|
|
|
55
63
|
"!/**/__tests__/**"
|
|
56
64
|
],
|
|
57
65
|
"dependencies": {
|
|
58
|
-
"@kubb/core": "3.0.0-alpha.
|
|
59
|
-
"@kubb/fs": "3.0.0-alpha.
|
|
60
|
-
"@kubb/oas": "3.0.0-alpha.
|
|
61
|
-
"@kubb/
|
|
62
|
-
"@kubb/plugin-
|
|
63
|
-
"@kubb/plugin-
|
|
64
|
-
"@kubb/plugin-
|
|
65
|
-
"@kubb/
|
|
66
|
-
"@kubb/react": "3.0.0-alpha.2"
|
|
66
|
+
"@kubb/core": "3.0.0-alpha.20",
|
|
67
|
+
"@kubb/fs": "3.0.0-alpha.20",
|
|
68
|
+
"@kubb/oas": "3.0.0-alpha.20",
|
|
69
|
+
"@kubb/plugin-client": "3.0.0-alpha.20",
|
|
70
|
+
"@kubb/plugin-oas": "3.0.0-alpha.20",
|
|
71
|
+
"@kubb/plugin-ts": "3.0.0-alpha.20",
|
|
72
|
+
"@kubb/plugin-zod": "3.0.0-alpha.20",
|
|
73
|
+
"@kubb/react": "3.0.0-alpha.20"
|
|
67
74
|
},
|
|
68
75
|
"devDependencies": {
|
|
69
|
-
"@types/react": "^18.3.
|
|
76
|
+
"@types/react": "^18.3.6",
|
|
70
77
|
"react": "^18.3.1",
|
|
71
78
|
"tsup": "^8.2.4",
|
|
72
|
-
"typescript": "^5.
|
|
73
|
-
"@kubb/config-
|
|
74
|
-
"@kubb/config-
|
|
75
|
-
"@kubb/config-tsup": "3.0.0-alpha.2"
|
|
79
|
+
"typescript": "^5.6.2",
|
|
80
|
+
"@kubb/config-ts": "3.0.0-alpha.20",
|
|
81
|
+
"@kubb/config-tsup": "3.0.0-alpha.20"
|
|
76
82
|
},
|
|
77
83
|
"peerDependencies": {
|
|
78
|
-
"@kubb/react": "3.0.0-alpha.
|
|
84
|
+
"@kubb/react": "3.0.0-alpha.20"
|
|
79
85
|
},
|
|
80
86
|
"engines": {
|
|
81
87
|
"node": ">=20"
|
|
@@ -1,252 +1,107 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { Parser, File, Function, useApp } from '@kubb/react'
|
|
4
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
5
|
-
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
6
|
-
import { getASTParams, getComments } from '@kubb/plugin-oas/utils'
|
|
1
|
+
import { URLPath } from '@kubb/core/utils'
|
|
2
|
+
import { File, Function, FunctionParams } from '@kubb/react'
|
|
7
3
|
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
import type {
|
|
4
|
+
import { type Operation, isOptional } from '@kubb/oas'
|
|
5
|
+
import { Client } from '@kubb/plugin-client/components'
|
|
6
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
7
|
+
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
11
8
|
import type { ReactNode } from 'react'
|
|
12
|
-
import type {
|
|
9
|
+
import type { PluginSwr } from '../types.ts'
|
|
13
10
|
|
|
14
|
-
type
|
|
11
|
+
type Props = {
|
|
15
12
|
/**
|
|
16
13
|
* Name of the function
|
|
17
14
|
*/
|
|
18
15
|
name: string
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*/
|
|
26
|
-
generics?: string
|
|
27
|
-
/**
|
|
28
|
-
* ReturnType(see async for adding Promise type)
|
|
29
|
-
*/
|
|
30
|
-
returnType?: string
|
|
31
|
-
/**
|
|
32
|
-
* Options for JSdocs
|
|
33
|
-
*/
|
|
34
|
-
JSDoc?: {
|
|
35
|
-
comments: string[]
|
|
36
|
-
}
|
|
37
|
-
hook: {
|
|
38
|
-
name: string
|
|
39
|
-
generics?: string
|
|
40
|
-
}
|
|
41
|
-
client: {
|
|
42
|
-
method: HttpMethod
|
|
43
|
-
generics: string
|
|
44
|
-
withQueryParams: boolean
|
|
45
|
-
withPathParams: boolean
|
|
46
|
-
withData: boolean
|
|
47
|
-
withHeaders: boolean
|
|
48
|
-
path: URLPath
|
|
49
|
-
}
|
|
50
|
-
dataReturnType: NonNullable<PluginSwr['options']['dataReturnType']>
|
|
16
|
+
typeName: string
|
|
17
|
+
clientName: string
|
|
18
|
+
typeSchemas: OperationSchemas
|
|
19
|
+
operation: Operation
|
|
20
|
+
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
21
|
+
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
51
22
|
}
|
|
52
23
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
client.withQueryParams ? 'params' : undefined,
|
|
58
|
-
client.withData ? 'data' : undefined,
|
|
59
|
-
client.withHeaders ? 'headers: { ...headers, ...clientOptions.headers }' : undefined,
|
|
60
|
-
'...clientOptions',
|
|
61
|
-
].filter(Boolean)
|
|
62
|
-
|
|
63
|
-
const resolvedClientOptions = `${transformers.createIndent(4)}${clientOptions.join(`,\n${transformers.createIndent(4)}`)}`
|
|
64
|
-
if (client.withQueryParams) {
|
|
65
|
-
return (
|
|
66
|
-
<Function export name={name} generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
|
|
67
|
-
{`
|
|
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
|
-
</Function>
|
|
84
|
-
)
|
|
85
|
-
}
|
|
86
|
-
return (
|
|
87
|
-
<Function export name={name} generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
|
|
88
|
-
{`
|
|
89
|
-
const { mutation: mutationOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
|
|
90
|
-
|
|
91
|
-
const url = ${client.path.template} as const
|
|
92
|
-
return ${hook.name}<${hook.generics}>(
|
|
93
|
-
shouldFetch ? url : null,
|
|
94
|
-
async (_url${client.withData ? ', { arg: data }' : ''}) => {
|
|
95
|
-
const res = await client<${client.generics}>({
|
|
96
|
-
${resolvedClientOptions}
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
return ${dataReturnType === 'data' ? 'res.data' : 'res'}
|
|
100
|
-
},
|
|
101
|
-
mutationOptions
|
|
102
|
-
)
|
|
103
|
-
`}
|
|
104
|
-
</Function>
|
|
105
|
-
)
|
|
24
|
+
type GetParamsProps = {
|
|
25
|
+
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
26
|
+
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
27
|
+
typeSchemas: OperationSchemas
|
|
106
28
|
}
|
|
107
29
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
} as const
|
|
30
|
+
function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
31
|
+
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
111
32
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* This will make it possible to override the default behaviour.
|
|
118
|
-
*/
|
|
119
|
-
Template?: React.ComponentType<TemplateProps>
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export function Mutation({ factory, Template = defaultTemplates.default }: Props): ReactNode {
|
|
123
|
-
const {
|
|
124
|
-
plugin: {
|
|
125
|
-
options: { dataReturnType },
|
|
126
|
-
},
|
|
127
|
-
} = useApp<PluginSwr>()
|
|
128
|
-
const { getSchemas, getName } = useOperationManager()
|
|
129
|
-
const operation = useOperation()
|
|
130
|
-
|
|
131
|
-
const name = getName(operation, { type: 'function' })
|
|
132
|
-
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
133
|
-
|
|
134
|
-
const params = new FunctionParams()
|
|
135
|
-
const client = {
|
|
136
|
-
method: operation.method,
|
|
137
|
-
path: new URLPath(operation.path),
|
|
138
|
-
generics: [`${factory.name}["data"]`, `${factory.name}["error"]`, schemas.request?.name ? `${factory.name}["request"]` : ''].filter(Boolean).join(', '),
|
|
139
|
-
withQueryParams: !!schemas.queryParams?.name,
|
|
140
|
-
withData: !!schemas.request?.name,
|
|
141
|
-
withPathParams: !!schemas.pathParams?.name,
|
|
142
|
-
withHeaders: !!schemas.headerParams?.name,
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const resultGenerics = [`${factory.name}["response"]`, `${factory.name}["error"]`]
|
|
146
|
-
|
|
147
|
-
params.add([
|
|
148
|
-
...getASTParams(schemas.pathParams, { typed: true }),
|
|
149
|
-
{
|
|
150
|
-
name: 'params',
|
|
151
|
-
type: `${factory.name}['queryParams']`,
|
|
152
|
-
enabled: client.withQueryParams,
|
|
153
|
-
required: false,
|
|
33
|
+
return FunctionParams.factory({
|
|
34
|
+
pathParams: {
|
|
35
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
36
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
154
37
|
},
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
38
|
+
params: typeSchemas.queryParams?.name
|
|
39
|
+
? {
|
|
40
|
+
type: typeSchemas.queryParams?.name,
|
|
41
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
42
|
+
}
|
|
43
|
+
: undefined,
|
|
44
|
+
headers: typeSchemas.headerParams?.name
|
|
45
|
+
? {
|
|
46
|
+
type: typeSchemas.headerParams?.name,
|
|
47
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
48
|
+
}
|
|
49
|
+
: undefined,
|
|
50
|
+
options: {
|
|
51
|
+
type: `
|
|
52
|
+
{
|
|
53
|
+
mutation?: SWRMutationConfiguration<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown'].join(', ')}>,
|
|
54
|
+
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'},
|
|
55
|
+
shouldFetch?: boolean,
|
|
56
|
+
}
|
|
57
|
+
`,
|
|
169
58
|
default: '{}',
|
|
170
59
|
},
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
const hook = {
|
|
174
|
-
name: 'useSWRMutation',
|
|
175
|
-
generics: [...resultGenerics, client.withQueryParams ? '[typeof url, typeof params] | null' : 'typeof url | null'].join(', '),
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return (
|
|
179
|
-
<Template
|
|
180
|
-
name={name}
|
|
181
|
-
JSDoc={{ comments: getComments(operation) }}
|
|
182
|
-
client={client}
|
|
183
|
-
hook={hook}
|
|
184
|
-
params={params.toString()}
|
|
185
|
-
returnType={`SWRMutationResponse<${resultGenerics.join(', ')}>`}
|
|
186
|
-
dataReturnType={dataReturnType}
|
|
187
|
-
/>
|
|
188
|
-
)
|
|
60
|
+
})
|
|
189
61
|
}
|
|
190
62
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
templates?: typeof defaultTemplates
|
|
196
|
-
}
|
|
63
|
+
export function Mutation({ name, clientName, pathParamsType, dataReturnType, typeSchemas, operation }: Props): ReactNode {
|
|
64
|
+
const path = new URLPath(operation.path)
|
|
65
|
+
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
66
|
+
const hookGenerics = [TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', 'Key']
|
|
197
67
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
client: { importPath },
|
|
204
|
-
},
|
|
205
|
-
},
|
|
206
|
-
} = useApp<PluginSwr>()
|
|
207
|
-
|
|
208
|
-
const { getSchemas, getFile, getName } = useOperationManager()
|
|
209
|
-
const operation = useOperation()
|
|
68
|
+
const params = getParams({
|
|
69
|
+
pathParamsType,
|
|
70
|
+
dataReturnType,
|
|
71
|
+
typeSchemas,
|
|
72
|
+
})
|
|
210
73
|
|
|
211
|
-
const
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
74
|
+
const clientParams = Client.getParams({
|
|
75
|
+
typeSchemas,
|
|
76
|
+
pathParamsType,
|
|
77
|
+
})
|
|
215
78
|
|
|
216
|
-
|
|
217
|
-
const
|
|
218
|
-
name: factoryName,
|
|
219
|
-
}
|
|
79
|
+
//fixed name, see Query.getParams and params
|
|
80
|
+
const swrKey = [path.template, typeSchemas.queryParams?.name ? 'params' : undefined].filter(Boolean)
|
|
220
81
|
|
|
221
82
|
return (
|
|
222
|
-
<
|
|
223
|
-
<
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
schemas.pathParams?.name,
|
|
234
|
-
schemas.queryParams?.name,
|
|
235
|
-
schemas.headerParams?.name,
|
|
236
|
-
...(schemas.errors?.map((error) => error.name) || []),
|
|
237
|
-
].filter(Boolean)}
|
|
238
|
-
root={file.path}
|
|
239
|
-
path={fileType.path}
|
|
240
|
-
isTypeOnly
|
|
241
|
-
/>
|
|
83
|
+
<File.Source name={name} isExportable isIndexable>
|
|
84
|
+
<Function
|
|
85
|
+
name={name}
|
|
86
|
+
export
|
|
87
|
+
params={params.toConstructor()}
|
|
88
|
+
JSDoc={{
|
|
89
|
+
comments: getComments(operation),
|
|
90
|
+
}}
|
|
91
|
+
>
|
|
92
|
+
{`
|
|
93
|
+
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
|
|
242
94
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
95
|
+
const swrKey = [${swrKey.join(', ')}] as const
|
|
96
|
+
return useSWRMutation<${hookGenerics}>(
|
|
97
|
+
shouldFetch ? swrKey : null,
|
|
98
|
+
async (_url${typeSchemas.request?.name ? ', { arg: data }' : ''}) => {
|
|
99
|
+
return ${clientName}(${clientParams.toCall()})
|
|
100
|
+
},
|
|
101
|
+
mutationOptions
|
|
102
|
+
)
|
|
103
|
+
`}
|
|
104
|
+
</Function>
|
|
105
|
+
</File.Source>
|
|
249
106
|
)
|
|
250
107
|
}
|
|
251
|
-
|
|
252
|
-
Mutation.templates = defaultTemplates
|