@kubb/plugin-swr 3.0.0-alpha.9 → 3.0.0-beta.2

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 (61) hide show
  1. package/README.md +14 -5
  2. package/dist/chunk-4EGGCGSE.js +217 -0
  3. package/dist/chunk-4EGGCGSE.js.map +1 -0
  4. package/dist/chunk-H23WKEYP.js +243 -0
  5. package/dist/chunk-H23WKEYP.js.map +1 -0
  6. package/dist/chunk-JWGWABV5.cjs +249 -0
  7. package/dist/chunk-JWGWABV5.cjs.map +1 -0
  8. package/dist/chunk-PTOQHHST.cjs +220 -0
  9. package/dist/chunk-PTOQHHST.cjs.map +1 -0
  10. package/dist/components.cjs +23 -6
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +88 -6
  13. package/dist/components.d.ts +88 -6
  14. package/dist/components.js +2 -10
  15. package/dist/components.js.map +1 -1
  16. package/dist/generators.cjs +17 -0
  17. package/dist/generators.cjs.map +1 -0
  18. package/dist/generators.d.cts +11 -0
  19. package/dist/generators.d.ts +11 -0
  20. package/dist/generators.js +4 -0
  21. package/dist/generators.js.map +1 -0
  22. package/dist/index.cjs +93 -123
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +2 -4
  25. package/dist/index.d.ts +2 -4
  26. package/dist/index.js +77 -114
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-DaH2Sc1M.d.cts +103 -0
  29. package/dist/types-DaH2Sc1M.d.ts +103 -0
  30. package/package.json +23 -17
  31. package/src/components/Mutation.tsx +93 -227
  32. package/src/components/MutationKey.tsx +48 -0
  33. package/src/components/Query.tsx +93 -259
  34. package/src/components/QueryKey.tsx +73 -0
  35. package/src/components/QueryOptions.tsx +54 -177
  36. package/src/components/index.ts +2 -0
  37. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +43 -0
  38. package/src/generators/__snapshots__/clientGetImportPath.ts +43 -0
  39. package/src/generators/__snapshots__/clientPostImportPath.ts +32 -0
  40. package/src/generators/__snapshots__/findByTags.ts +43 -0
  41. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +43 -0
  42. package/src/generators/__snapshots__/findByTagsWithZod.ts +43 -0
  43. package/src/generators/__snapshots__/getAsMutation.ts +34 -0
  44. package/src/generators/__snapshots__/postAsQuery.ts +41 -0
  45. package/src/generators/__snapshots__/updatePetById.ts +32 -0
  46. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +36 -0
  47. package/src/generators/index.ts +2 -0
  48. package/src/generators/mutationGenerator.tsx +109 -0
  49. package/src/generators/queryGenerator.tsx +114 -0
  50. package/src/plugin.ts +71 -52
  51. package/src/types.ts +62 -78
  52. package/dist/chunk-TGLXUPN4.cjs +0 -536
  53. package/dist/chunk-TGLXUPN4.cjs.map +0 -1
  54. package/dist/chunk-XWXMQJD6.js +0 -536
  55. package/dist/chunk-XWXMQJD6.js.map +0 -1
  56. package/dist/index-B3C-JOIU.d.cts +0 -299
  57. package/dist/index-B3C-JOIU.d.ts +0 -299
  58. package/src/OperationGenerator.tsx +0 -75
  59. package/src/components/SchemaType.tsx +0 -63
  60. package/src/components/__snapshots__/Mutation/Pets.ts +0 -46
  61. package/src/components/__snapshots__/Query/showPetById.ts +0 -59
@@ -1,284 +1,118 @@
1
- import { FunctionParams, URLPath } from '@kubb/core/utils'
2
- import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
3
- import { getASTParams, getComments } from '@kubb/plugin-oas/utils'
4
- import { pluginTsName } from '@kubb/plugin-ts'
5
- import { pluginZodName } from '@kubb/plugin-zod'
6
- import { File, Function, useApp } from '@kubb/react'
7
-
8
- import { QueryOptions } from './QueryOptions.tsx'
9
- import { SchemaType } from './SchemaType.tsx'
1
+ import { URLPath } from '@kubb/core/utils'
2
+ import { File, Function, FunctionParams } from '@kubb/react'
10
3
 
4
+ import { type Operation, isOptional } from '@kubb/oas'
5
+ import type { OperationSchemas } from '@kubb/plugin-oas'
6
+ import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
11
7
  import type { ReactNode } from 'react'
12
- import type { FileMeta, PluginSwr } from '../types.ts'
8
+ import type { PluginSwr } from '../types.ts'
9
+ import { QueryKey } from './QueryKey.tsx'
10
+ import { QueryOptions } from './QueryOptions.tsx'
13
11
 
14
- type TemplateProps = {
12
+ type Props = {
15
13
  /**
16
14
  * Name of the function
17
15
  */
18
16
  name: string
19
- /**
20
- * Parameters/options/props that need to be used
21
- */
22
- params: string
23
- /**
24
- * Generics that needs to be added for TypeScript
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
- queryOptions: string
41
- }
42
- client: {
43
- path: URLPath
44
- withQueryParams: boolean
45
- }
46
- }
47
-
48
- function Template({ name, generics, returnType, params, JSDoc, hook, client }: TemplateProps): ReactNode {
49
- if (client.withQueryParams) {
50
- return (
51
- <File.Source name={name} isExportable isIndexable>
52
- <Function name={name} export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
53
- {`
54
- const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
55
-
56
- const url = ${client.path.template}
57
- const query = ${hook.name}<${hook.generics}>(
58
- shouldFetch ? [url, params]: null,
59
- {
60
- ...${hook.queryOptions},
61
- ...queryOptions
62
- }
63
- )
64
-
65
- return query
66
- `}
67
- </Function>
68
- </File.Source>
69
- )
70
- }
71
-
72
- return (
73
- <File.Source name={name} isExportable isIndexable>
74
- <Function name={name} export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
75
- {`
76
- const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
77
-
78
- const url = ${client.path.template}
79
- const query = ${hook.name}<${hook.generics}>(
80
- shouldFetch ? url : null,
81
- {
82
- ...${hook.queryOptions},
83
- ...queryOptions
84
- }
85
- )
86
-
87
- return query
88
- `}
89
- </Function>
90
- </File.Source>
91
- )
17
+ queryOptionsName: string
18
+ queryKeyName: string
19
+ queryKeyTypeName: string
20
+ typeSchemas: OperationSchemas
21
+ pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
22
+ dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
23
+ operation: Operation
92
24
  }
93
25
 
94
- const defaultTemplates = {
95
- default: Template,
96
- } as const
97
-
98
- type Props = {
99
- factory: {
100
- name: string
101
- }
102
- /**
103
- * This will make it possible to override the default behaviour.
104
- */
105
- Template?: React.ComponentType<TemplateProps>
106
- /**
107
- * This will make it possible to override the default behaviour.
108
- */
109
- QueryOptionsTemplate?: React.ComponentType<React.ComponentProps<typeof QueryOptions.templates.default>>
26
+ type GetParamsProps = {
27
+ pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
28
+ dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
29
+ queryKeyTypeName: string
30
+ typeSchemas: OperationSchemas
110
31
  }
111
32
 
112
- export function Query({ factory, Template = defaultTemplates.default, QueryOptionsTemplate = QueryOptions.templates.default }: Props): ReactNode {
113
- const {
114
- pluginManager,
115
- plugin: {
116
- key: pluginKey,
117
- options: { dataReturnType },
118
- },
119
- } = useApp<PluginSwr>()
120
-
121
- const operation = useOperation()
122
- const { getSchemas, getName } = useOperationManager()
123
-
124
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
125
- const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' })
126
-
127
- const name = getName(operation, { type: 'function' })
33
+ function getParams({ pathParamsType, dataReturnType, typeSchemas, queryKeyTypeName }: GetParamsProps) {
34
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
128
35
 
129
- const queryOptionsName = pluginManager.resolveName({
130
- name: `${factory.name}QueryOptions`,
131
- pluginKey,
132
- })
133
- const generics = new FunctionParams()
134
- const params = new FunctionParams()
135
- const queryParams = new FunctionParams()
136
- const client = {
137
- method: operation.method,
138
- path: new URLPath(operation.path),
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 = ['TData', `${factory.name}["error"]`]
146
-
147
- generics.add([{ type: 'TData', default: `${factory.name}["response"]` }])
148
-
149
- const queryOptionsGenerics = ['TData']
150
-
151
- params.add([
152
- ...getASTParams(schemas.pathParams, { typed: true }),
153
- {
154
- name: 'params',
155
- type: `${factory.name}['queryParams']`,
156
- enabled: client.withQueryParams,
157
- required: false,
36
+ return FunctionParams.factory({
37
+ pathParams: {
38
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
39
+ children: getPathParams(typeSchemas.pathParams, { typed: true }),
158
40
  },
159
- {
160
- name: 'headers',
161
- type: `${factory.name}['headerParams']`,
162
- enabled: client.withHeaders,
163
- required: false,
164
- },
165
- {
166
- name: 'options',
167
- required: false,
168
- type: `{
169
- query?: SWRConfiguration<${resultGenerics.join(', ')}>,
170
- client?: ${factory.name}['client']['parameters'],
171
- shouldFetch?: boolean,
172
- }`,
41
+ data: typeSchemas.request?.name
42
+ ? {
43
+ type: typeSchemas.request?.name,
44
+ optional: isOptional(typeSchemas.request?.schema),
45
+ }
46
+ : undefined,
47
+ params: typeSchemas.queryParams?.name
48
+ ? {
49
+ type: typeSchemas.queryParams?.name,
50
+ optional: isOptional(typeSchemas.queryParams?.schema),
51
+ }
52
+ : undefined,
53
+ headers: typeSchemas.headerParams?.name
54
+ ? {
55
+ type: typeSchemas.headerParams?.name,
56
+ optional: isOptional(typeSchemas.headerParams?.schema),
57
+ }
58
+ : undefined,
59
+ options: {
60
+ type: `
61
+ {
62
+ query?: Parameters<typeof useSWR<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', `${queryKeyTypeName} | null`].join(', ')}, any>>[2],
63
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'},
64
+ shouldFetch?: boolean,
65
+ }
66
+ `,
173
67
  default: '{}',
174
68
  },
175
- ])
176
-
177
- queryParams.add([
178
- ...getASTParams(schemas.pathParams, { typed: false }),
179
- {
180
- name: 'params',
181
- enabled: client.withQueryParams,
182
- required: false,
183
- },
184
- {
185
- name: 'headers',
186
- enabled: client.withHeaders,
187
- required: false,
188
- },
189
- {
190
- name: 'clientOptions',
191
- required: false,
192
- },
193
- ])
194
-
195
- const hook = {
196
- name: 'useSWR',
197
- generics: [...resultGenerics, client.withQueryParams ? '[typeof url, typeof params] | null' : 'typeof url | null'].join(', '),
198
- queryOptions: `${queryOptionsName}<${queryOptionsGenerics.join(', ')}>(${queryParams.toString()})`,
199
- }
200
-
201
- return (
202
- <>
203
- <QueryOptions factory={factory} Template={QueryOptionsTemplate} dataReturnType={dataReturnType} />
204
- <Template
205
- name={name}
206
- generics={generics.toString()}
207
- JSDoc={{ comments: getComments(operation) }}
208
- client={client}
209
- hook={hook}
210
- params={params.toString()}
211
- returnType={`SWRResponse<${resultGenerics.join(', ')}>`}
212
- />
213
- </>
214
- )
215
- }
216
-
217
- type FileProps = {
218
- /**
219
- * This will make it possible to override the default behaviour.
220
- */
221
- templates?: {
222
- query: typeof defaultTemplates
223
- queryOptions: typeof QueryOptions.templates
224
- }
69
+ })
225
70
  }
226
71
 
227
- Query.File = function ({ templates }: FileProps): ReactNode {
228
- const {
229
- pluginManager,
230
- plugin: {
231
- options: {
232
- extName,
233
- client: { importPath },
234
- parser,
235
- },
236
- },
237
- } = useApp<PluginSwr>()
238
- const { getSchemas, getFile, getName } = useOperationManager()
239
- const operation = useOperation()
72
+ export function Query({ name, typeSchemas, queryKeyName, queryKeyTypeName, queryOptionsName, operation, dataReturnType, pathParamsType }: Props): ReactNode {
73
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
74
+ const generics = [TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', `${queryKeyTypeName} | null`]
240
75
 
241
- const file = getFile(operation)
242
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
243
- const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' })
244
- const fileType = getFile(operation, { pluginKey: [pluginTsName] })
245
- const fileZodSchemas = getFile(operation, {
246
- pluginKey: [pluginZodName],
76
+ const queryKeyParams = QueryKey.getParams({
77
+ pathParamsType,
78
+ typeSchemas,
79
+ })
80
+ const params = getParams({
81
+ pathParamsType,
82
+ dataReturnType,
83
+ typeSchemas,
84
+ queryKeyTypeName,
247
85
  })
248
86
 
249
- const factoryName = getName(operation, { type: 'type' })
250
-
251
- const Template = templates?.query.default || defaultTemplates.default
252
- const QueryOptionsTemplate = templates?.queryOptions.default || QueryOptions.templates.default
253
- const factory = {
254
- name: factoryName,
255
- }
87
+ const queryOptionsParams = QueryOptions.getParams({
88
+ pathParamsType,
89
+ typeSchemas,
90
+ })
256
91
 
257
92
  return (
258
- <File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
259
- {parser === 'zod' && <File.Import name={[zodSchemas.response.name]} root={file.path} path={fileZodSchemas.path} />}
260
- <File.Import name="useSWR" path="swr" />
261
- <File.Import name={['SWRConfiguration', 'SWRResponse']} path="swr" isTypeOnly />
262
- <File.Import name={'client'} path={importPath} />
263
- <File.Import name={['ResponseConfig']} path={importPath} isTypeOnly />
264
- <File.Import
265
- name={[
266
- schemas.request?.name,
267
- schemas.response.name,
268
- schemas.pathParams?.name,
269
- schemas.queryParams?.name,
270
- schemas.headerParams?.name,
271
- ...(schemas.statusCodes?.map((item) => item.name) || []),
272
- ].filter(Boolean)}
273
- root={file.path}
274
- path={fileType.path}
275
- isTypeOnly
276
- />
93
+ <File.Source name={name} isExportable isIndexable>
94
+ <Function
95
+ name={name}
96
+ export
97
+ params={params.toConstructor()}
98
+ JSDoc={{
99
+ comments: getComments(operation),
100
+ }}
101
+ >
102
+ {`
103
+ const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
104
+
105
+ const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
277
106
 
278
- <SchemaType factory={factory} />
279
- <Query factory={factory} Template={Template} QueryOptionsTemplate={QueryOptionsTemplate} />
280
- </File>
107
+ return useSWR<${generics.join(', ')}>(
108
+ shouldFetch ? queryKey : null,
109
+ {
110
+ ...${queryOptionsName}(${queryOptionsParams.toCall()})
111
+ ...queryOptions
112
+ }
113
+ )
114
+ `}
115
+ </Function>
116
+ </File.Source>
281
117
  )
282
118
  }
283
-
284
- Query.templates = defaultTemplates
@@ -0,0 +1,73 @@
1
+ import { URLPath } from '@kubb/core/utils'
2
+ import { getPathParams } from '@kubb/plugin-oas/utils'
3
+ import { File, Function, FunctionParams, Type } from '@kubb/react'
4
+
5
+ import { type Operation, isOptional } from '@kubb/oas'
6
+ import type { OperationSchemas } from '@kubb/plugin-oas'
7
+ import type { ReactNode } from 'react'
8
+ import type { PluginSwr } from '../types'
9
+
10
+ type Props = {
11
+ name: string
12
+ typeName: string
13
+ typeSchemas: OperationSchemas
14
+ operation: Operation
15
+ pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
16
+ keysFn: ((keys: unknown[]) => unknown[]) | undefined
17
+ }
18
+
19
+ type GetParamsProps = {
20
+ pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
21
+ typeSchemas: OperationSchemas
22
+ }
23
+
24
+ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
25
+ return FunctionParams.factory({
26
+ pathParams: {
27
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
28
+ children: getPathParams(typeSchemas.pathParams, { typed: true }),
29
+ },
30
+ data: typeSchemas.request?.name
31
+ ? {
32
+ type: typeSchemas.request?.name,
33
+ optional: isOptional(typeSchemas.request?.schema),
34
+ }
35
+ : undefined,
36
+ params: typeSchemas.queryParams?.name
37
+ ? {
38
+ type: typeSchemas.queryParams?.name,
39
+ optional: isOptional(typeSchemas.queryParams?.schema),
40
+ }
41
+ : undefined,
42
+ })
43
+ }
44
+
45
+ export function QueryKey({ name, typeSchemas, pathParamsType, operation, typeName, keysFn = (name) => name }: Props): ReactNode {
46
+ const path = new URLPath(operation.path)
47
+ const params = getParams({ pathParamsType, typeSchemas })
48
+ const keys = [
49
+ path.toObject({
50
+ type: 'path',
51
+ stringify: true,
52
+ }),
53
+ typeSchemas.queryParams?.name ? '...(params ? [params] : [])' : undefined,
54
+ typeSchemas.request?.name ? '...(data ? [data] : [])' : undefined,
55
+ ].filter(Boolean)
56
+
57
+ return (
58
+ <>
59
+ <File.Source name={name} isExportable isIndexable>
60
+ <Function.Arrow name={name} export params={params.toConstructor()} singleLine>
61
+ {`[${keysFn(keys).join(', ')}] as const`}
62
+ </Function.Arrow>
63
+ </File.Source>
64
+ <File.Source name={typeName} isExportable isIndexable isTypeOnly>
65
+ <Type name={typeName} export>
66
+ {`ReturnType<typeof ${name}>`}
67
+ </Type>
68
+ </File.Source>
69
+ </>
70
+ )
71
+ }
72
+
73
+ QueryKey.getParams = getParams