@kubb/plugin-swr 5.0.0-alpha.9 → 5.0.0-beta.103

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/src/plugin.ts DELETED
@@ -1,207 +0,0 @@
1
- import path from 'node:path'
2
- import { camelCase, pascalCase } from '@internals/utils'
3
- import { createPlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'
4
- import { pluginClientName } from '@kubb/plugin-client'
5
- import { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'
6
- import { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'
7
- import { source as configSource } from '@kubb/plugin-client/templates/config.source'
8
- import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
9
- import { pluginTsName } from '@kubb/plugin-ts'
10
- import { pluginZodName } from '@kubb/plugin-zod'
11
- import { MutationKey, QueryKey } from './components'
12
- import { mutationGenerator, queryGenerator } from './generators'
13
- import type { PluginSwr } from './types.ts'
14
-
15
- export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
16
-
17
- export const pluginSwr = createPlugin<PluginSwr>((options) => {
18
- const {
19
- output = { path: 'hooks', barrelType: 'named' },
20
- group,
21
- exclude = [],
22
- include,
23
- override = [],
24
- parser = 'client',
25
- transformers = {},
26
- query,
27
- mutation,
28
- client,
29
- paramsType = 'inline',
30
- pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
31
- mutationKey = MutationKey.getTransformer,
32
- queryKey = QueryKey.getTransformer,
33
- generators = [queryGenerator, mutationGenerator].filter(Boolean),
34
- paramsCasing,
35
- contentType,
36
- } = options
37
-
38
- const clientName = client?.client ?? 'axios'
39
- const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)
40
-
41
- return {
42
- name: pluginSwrName,
43
- options: {
44
- output,
45
- client: {
46
- bundle: client?.bundle,
47
- baseURL: client?.baseURL,
48
- client: clientName,
49
- clientType: client?.clientType ?? 'function',
50
- importPath: clientImportPath,
51
- dataReturnType: client?.dataReturnType ?? 'data',
52
- paramsCasing,
53
- },
54
- queryKey,
55
- query:
56
- query === false
57
- ? false
58
- : {
59
- importPath: 'swr',
60
- methods: ['get'],
61
- ...query,
62
- },
63
- mutationKey,
64
- mutation:
65
- mutation === false
66
- ? false
67
- : {
68
- importPath: 'swr/mutation',
69
- methods: ['post', 'put', 'delete', 'patch'],
70
- ...mutation,
71
- },
72
- parser,
73
- paramsType,
74
- pathParamsType,
75
- paramsCasing,
76
- group,
77
- },
78
- pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
79
- resolvePath(baseName, pathMode, options) {
80
- const root = path.resolve(this.config.root, this.config.output.path)
81
- const mode = pathMode ?? getMode(path.resolve(root, output.path))
82
-
83
- if (mode === 'single') {
84
- /**
85
- * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
86
- * Other plugins then need to call addOrAppend instead of just add from the fileManager class
87
- */
88
- return path.resolve(root, output.path)
89
- }
90
-
91
- if (group && (options?.group?.path || options?.group?.tag)) {
92
- const groupName: Group['name'] = group?.name
93
- ? group.name
94
- : (ctx) => {
95
- if (group?.type === 'path') {
96
- return `${ctx.group.split('/')[1]}`
97
- }
98
- return `${camelCase(ctx.group)}Controller`
99
- }
100
-
101
- return path.resolve(
102
- root,
103
- output.path,
104
- groupName({
105
- group: group.type === 'path' ? options.group.path! : options.group.tag!,
106
- }),
107
- baseName,
108
- )
109
- }
110
-
111
- return path.resolve(root, output.path, baseName)
112
- },
113
- resolveName(name, type) {
114
- let resolvedName = camelCase(name)
115
-
116
- if (type === 'file' || type === 'function') {
117
- resolvedName = camelCase(name, {
118
- isFile: type === 'file',
119
- })
120
- }
121
-
122
- if (type === 'type') {
123
- resolvedName = pascalCase(name)
124
- }
125
-
126
- if (type) {
127
- return transformers?.name?.(resolvedName, type) || resolvedName
128
- }
129
-
130
- return resolvedName
131
- },
132
- async install() {
133
- const root = path.resolve(this.config.root, this.config.output.path)
134
- const mode = getMode(path.resolve(root, output.path))
135
- const oas = await this.getOas()
136
- const baseURL = await this.getBaseURL()
137
-
138
- if (baseURL) {
139
- this.plugin.options.client.baseURL = baseURL
140
- }
141
-
142
- const hasClientPlugin = !!this.driver.getPluginByName(pluginClientName)
143
-
144
- if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) {
145
- // pre add bundled fetch
146
- await this.upsertFile({
147
- baseName: 'fetch.ts',
148
- path: path.resolve(root, '.kubb/fetch.ts'),
149
- sources: [
150
- {
151
- name: 'fetch',
152
- value: this.plugin.options.client.client === 'fetch' ? fetchClientSource : axiosClientSource,
153
- isExportable: true,
154
- isIndexable: true,
155
- },
156
- ],
157
- imports: [],
158
- exports: [],
159
- })
160
- }
161
-
162
- if (!hasClientPlugin) {
163
- await this.addFile({
164
- baseName: 'config.ts',
165
- path: path.resolve(root, '.kubb/config.ts'),
166
- sources: [
167
- {
168
- name: 'config',
169
- value: configSource,
170
- isExportable: false,
171
- isIndexable: false,
172
- },
173
- ],
174
- imports: [],
175
- exports: [],
176
- })
177
- }
178
-
179
- const operationGenerator = new OperationGenerator(this.plugin.options, {
180
- fabric: this.fabric,
181
- oas,
182
- driver: this.driver,
183
- events: this.events,
184
- plugin: this.plugin,
185
- contentType,
186
- exclude,
187
- include,
188
- override,
189
- mode,
190
- })
191
-
192
- const files = await operationGenerator.build(...generators)
193
- await this.upsertFile(...files)
194
-
195
- const barrelFiles = await getBarrelFiles(this.fabric.files, {
196
- type: output.barrelType ?? 'named',
197
- root,
198
- output,
199
- meta: {
200
- pluginName: this.plugin.name,
201
- },
202
- })
203
-
204
- await this.upsertFile(...barrelFiles)
205
- },
206
- }
207
- })
package/src/types.ts DELETED
@@ -1,141 +0,0 @@
1
- import type { Transformer } from '@internals/tanstack-query'
2
- import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
3
- import type { contentType, HttpMethod, Oas } from '@kubb/oas'
4
- import type { ClientImportPath, PluginClient } from '@kubb/plugin-client'
5
- import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
6
- import type { Generator } from '@kubb/plugin-oas/generators'
7
-
8
- export type { Transformer } from '@internals/tanstack-query'
9
-
10
- /**
11
- * Customize the queryKey
12
- */
13
- type QueryKey = Transformer
14
-
15
- /**
16
- * Customize the mutationKey
17
- */
18
- type MutationKey = Transformer
19
-
20
- type Query = {
21
- /**
22
- * Define which HttpMethods can be used for queries
23
- * @default ['get']
24
- */
25
- methods?: Array<HttpMethod>
26
- /**
27
- * Path to the useQuery hook for useQuery functionality.
28
- * Used as `import { useQuery } from '${importPath}'`.
29
- * Accepts relative and absolute paths.
30
- * Path is used as-is; relative paths are based on the generated file location.
31
- * @default 'swr'
32
- */
33
- importPath?: string
34
- }
35
-
36
- type Mutation = {
37
- /**
38
- * Define which HttpMethods can be used for queries
39
- * @default ['post', 'put', 'delete', 'patch']
40
- */
41
- methods?: Array<HttpMethod>
42
- /**
43
- * Path to the useQuery hook for useQuery functionality.
44
- * Used as `import { useQuery } from '${importPath}'`.
45
- * Accepts relative and absolute paths.
46
- * Path is used as-is; relative paths are based on the generated file location.
47
- * @default 'swr/mutation'
48
- */
49
- importPath?: string
50
- /**
51
- * When true, mutation parameters (path params, query params, headers, body) is passed via `trigger()` instead of as hook arguments.
52
- * This aligns with React Query's mutation pattern where variables are passed when triggering the mutation.
53
- * @default false
54
- * @deprecated This will become the default behavior in v5. Set to `true` to opt-in early.
55
- */
56
- paramsToTrigger?: boolean
57
- }
58
-
59
- export type Options = {
60
- /**
61
- * Specify the export location for the files and define the behavior of the output
62
- * @default { path: 'hooks', barrelType: 'named' }
63
- */
64
- output?: Output<Oas>
65
- /**
66
- * Define which contentType should be used.
67
- * By default, the first JSON valid mediaType is used
68
- */
69
- contentType?: contentType
70
- /**
71
- * Group the SWR hooks based on the provided name.
72
- */
73
- group?: Group
74
- /**
75
- * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
76
- */
77
- exclude?: Array<Exclude>
78
- /**
79
- * Array containing include parameters to include tags/operations/methods/paths.
80
- */
81
- include?: Array<Include>
82
- /**
83
- * Array containing override parameters to override `options` based on tags/operations/methods/paths.
84
- */
85
- override?: Array<Override<ResolvedOptions>>
86
- client?: ClientImportPath & Pick<PluginClient['options'], 'clientType' | 'dataReturnType' | 'baseURL' | 'bundle' | 'paramsCasing'>
87
- queryKey?: QueryKey
88
- query?: Query | false
89
- mutationKey?: MutationKey
90
- mutation?: Mutation | false
91
- /**
92
- * How to style your params, by default no casing is applied
93
- * - 'camelcase' uses camelcase for the params names
94
- */
95
- paramsCasing?: 'camelcase'
96
- /**
97
- * How to pass your params
98
- * - 'object' returns the params and pathParams as an object.
99
- * - 'inline' returns the params as comma separated params.
100
- * @default 'inline'
101
- */
102
- paramsType?: 'object' | 'inline'
103
- /**
104
- * How to pass your pathParams.
105
- * - 'object' returns the pathParams as an object.
106
- * - 'inline': returns the pathParams as comma separated params.
107
- * @default 'inline'
108
- */
109
- pathParamsType?: PluginClient['options']['pathParamsType']
110
- /**
111
- * Which parser should be used before returning the data to `swr`.
112
- * `'zod'` uses `@kubb/plugin-zod` to parse the data.
113
- */
114
- parser?: PluginClient['options']['parser']
115
- transformers?: {
116
- /**
117
- * Customize the names based on the type that is provided by the plugin.
118
- */
119
- name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
120
- }
121
- /**
122
- * Define some generators next to the swr generators
123
- */
124
- generators?: Array<Generator<PluginSwr>>
125
- }
126
-
127
- type ResolvedOptions = {
128
- output: Output<Oas>
129
- client: Pick<PluginClient['options'], 'client' | 'clientType' | 'dataReturnType' | 'importPath' | 'baseURL' | 'bundle' | 'paramsCasing'>
130
- parser: Required<NonNullable<Options['parser']>>
131
- queryKey: QueryKey | undefined
132
- query: NonNullable<Required<Query>> | false
133
- mutationKey: MutationKey | undefined
134
- mutation: (Required<Pick<Mutation, 'methods' | 'importPath'>> & Pick<Mutation, 'paramsToTrigger'>) | false
135
- paramsCasing: Options['paramsCasing']
136
- paramsType: NonNullable<Options['paramsType']>
137
- pathParamsType: NonNullable<Options['pathParamsType']>
138
- group: Options['group']
139
- }
140
-
141
- export type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, never, ResolvePathOptions>