@kubb/plugin-swr 5.0.0-alpha.34 → 5.0.0-alpha.35
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/components-BJSzUg7M.cjs +955 -0
- package/dist/components-BJSzUg7M.cjs.map +1 -0
- package/dist/components-JQ2KRFCa.js +877 -0
- package/dist/components-JQ2KRFCa.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +77 -37
- package/dist/components.js +1 -1
- package/dist/generators-17ulS9mu.cjs +537 -0
- package/dist/generators-17ulS9mu.cjs.map +1 -0
- package/dist/generators-Cl7nr-FB.js +526 -0
- package/dist/generators-Cl7nr-FB.js.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +4 -4
- package/dist/generators.js +1 -1
- package/dist/index.cjs +132 -110
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +22 -2
- package/dist/index.js +132 -110
- package/dist/index.js.map +1 -1
- package/dist/{types-BVDtH9S7.d.ts → types-FA5mH9Ch.d.ts} +46 -90
- package/package.json +7 -11
- package/src/components/Mutation.tsx +165 -170
- package/src/components/MutationKey.tsx +50 -1
- package/src/components/Query.tsx +122 -126
- package/src/components/QueryKey.tsx +65 -1
- package/src/components/QueryOptions.tsx +38 -93
- package/src/generators/mutationGenerator.tsx +194 -117
- package/src/generators/queryGenerator.tsx +205 -139
- package/src/plugin.ts +117 -152
- package/src/resolvers/resolverSwr.ts +26 -0
- package/src/resolvers/resolverSwrLegacy.ts +17 -0
- package/src/types.ts +55 -18
- package/src/utils.ts +209 -0
- package/dist/components-DaCTPplv.js +0 -756
- package/dist/components-DaCTPplv.js.map +0 -1
- package/dist/components-Qs8_faOt.cjs +0 -834
- package/dist/components-Qs8_faOt.cjs.map +0 -1
- package/dist/generators-0YayIrse.js +0 -400
- package/dist/generators-0YayIrse.js.map +0 -1
- package/dist/generators-Bd4rCa3l.cjs +0 -411
- package/dist/generators-Bd4rCa3l.cjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,145 +1,167 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_components = require("./components-
|
|
3
|
-
const require_generators = require("./generators-
|
|
2
|
+
const require_components = require("./components-BJSzUg7M.cjs");
|
|
3
|
+
const require_generators = require("./generators-17ulS9mu.cjs");
|
|
4
4
|
let node_path = require("node:path");
|
|
5
|
-
node_path = require_components.__toESM(node_path);
|
|
6
|
-
let _kubb_ast = require("@kubb/ast");
|
|
5
|
+
node_path = require_components.__toESM(node_path, 1);
|
|
7
6
|
let _kubb_core = require("@kubb/core");
|
|
8
7
|
let _kubb_plugin_client = require("@kubb/plugin-client");
|
|
9
8
|
let _kubb_plugin_client_templates_clients_axios_source = require("@kubb/plugin-client/templates/clients/axios.source");
|
|
10
9
|
let _kubb_plugin_client_templates_clients_fetch_source = require("@kubb/plugin-client/templates/clients/fetch.source");
|
|
11
10
|
let _kubb_plugin_client_templates_config_source = require("@kubb/plugin-client/templates/config.source");
|
|
12
|
-
let _kubb_plugin_oas = require("@kubb/plugin-oas");
|
|
13
11
|
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
14
12
|
let _kubb_plugin_zod = require("@kubb/plugin-zod");
|
|
15
|
-
//#region
|
|
16
|
-
|
|
13
|
+
//#region src/resolvers/resolverSwr.ts
|
|
14
|
+
/**
|
|
15
|
+
* Resolver for `@kubb/plugin-swr` that provides the default naming
|
|
16
|
+
* and path-resolution helpers used by the plugin.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { resolverSwr } from '@kubb/plugin-swr'
|
|
21
|
+
*
|
|
22
|
+
* resolverSwr.default('list pets', 'function') // -> 'listPets'
|
|
23
|
+
* resolverSwr.resolveName('show pet by id') // -> 'showPetById'
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
const resolverSwr = (0, _kubb_core.defineResolver)(() => ({
|
|
27
|
+
name: "default",
|
|
28
|
+
pluginName: "plugin-swr",
|
|
29
|
+
default(name, type) {
|
|
30
|
+
return require_components.camelCase(name, { isFile: type === "file" });
|
|
31
|
+
},
|
|
32
|
+
resolveName(name) {
|
|
33
|
+
return this.default(name, "function");
|
|
34
|
+
}
|
|
35
|
+
}));
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/resolvers/resolverSwrLegacy.ts
|
|
38
|
+
/**
|
|
39
|
+
* Legacy resolver for `@kubb/plugin-swr` that reproduces the naming conventions
|
|
40
|
+
* used in Kubb v4. Enable via `compatibilityPreset: 'kubbV4'`.
|
|
41
|
+
*
|
|
42
|
+
* The naming logic is identical to the default resolver — the only difference
|
|
43
|
+
* is the `name` field (`'kubbV4'` vs `'default'`) so the driver can
|
|
44
|
+
* distinguish presets.
|
|
45
|
+
*/
|
|
46
|
+
const resolverSwrLegacy = (0, _kubb_core.defineResolver)(() => ({
|
|
47
|
+
...resolverSwr,
|
|
48
|
+
name: "kubbV4",
|
|
49
|
+
pluginName: "plugin-swr"
|
|
50
|
+
}));
|
|
17
51
|
//#endregion
|
|
18
52
|
//#region src/plugin.ts
|
|
53
|
+
/**
|
|
54
|
+
* Canonical plugin name for `@kubb/plugin-swr`, used to identify the plugin
|
|
55
|
+
* in driver lookups and warnings.
|
|
56
|
+
*/
|
|
19
57
|
const pluginSwrName = "plugin-swr";
|
|
20
|
-
|
|
58
|
+
/**
|
|
59
|
+
* The `@kubb/plugin-swr` plugin factory.
|
|
60
|
+
*
|
|
61
|
+
* Generates SWR hooks from an OpenAPI/AST `RootNode`.
|
|
62
|
+
* Walks operations, delegates rendering to the active generators,
|
|
63
|
+
* and writes barrel files based on `output.barrelType`.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { pluginSwr } from '@kubb/plugin-swr'
|
|
68
|
+
*
|
|
69
|
+
* export default defineConfig({
|
|
70
|
+
* plugins: [pluginSwr({ output: { path: 'hooks' } })],
|
|
71
|
+
* })
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
const pluginSwr = (0, _kubb_core.definePlugin)((options) => {
|
|
21
75
|
const { output = {
|
|
22
76
|
path: "hooks",
|
|
23
77
|
barrelType: "named"
|
|
24
|
-
}, group, exclude = [], include, override = [], parser = "client", transformers = {}, query, mutation, client, paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", mutationKey = require_components.MutationKey.getTransformer, queryKey = require_components.QueryKey.getTransformer, generators = [
|
|
78
|
+
}, group, exclude = [], include, override = [], parser = "client", transformers = {}, query, mutation, client, paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", mutationKey = require_components.MutationKey.getTransformer, queryKey = require_components.QueryKey.getTransformer, paramsCasing, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [], compatibilityPreset = "default" } = options;
|
|
79
|
+
const defaultResolver = compatibilityPreset === "kubbV4" ? resolverSwrLegacy : resolverSwr;
|
|
25
80
|
const clientName = client?.client ?? "axios";
|
|
26
81
|
const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : void 0);
|
|
82
|
+
const selectedGenerators = options.generators ?? [require_generators.queryGenerator, require_generators.mutationGenerator].filter(Boolean);
|
|
83
|
+
const groupConfig = group ? {
|
|
84
|
+
...group,
|
|
85
|
+
name: group.name ? group.name : (ctx) => {
|
|
86
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
87
|
+
return `${require_components.camelCase(ctx.group)}Controller`;
|
|
88
|
+
}
|
|
89
|
+
} : void 0;
|
|
27
90
|
return {
|
|
28
91
|
name: pluginSwrName,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
83
|
-
return `${require_components.camelCase(ctx.group)}Controller`;
|
|
84
|
-
};
|
|
85
|
-
return node_path.default.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
|
|
86
|
-
}
|
|
87
|
-
return node_path.default.resolve(root, output.path, baseName);
|
|
88
|
-
},
|
|
89
|
-
resolveName(name, type) {
|
|
90
|
-
let resolvedName = require_components.camelCase(name);
|
|
91
|
-
if (type === "file" || type === "function") resolvedName = require_components.camelCase(name, { isFile: type === "file" });
|
|
92
|
-
if (type === "type") resolvedName = require_components.pascalCase(name);
|
|
93
|
-
if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
|
|
94
|
-
return resolvedName;
|
|
95
|
-
},
|
|
96
|
-
async buildStart() {
|
|
97
|
-
const root = this.root;
|
|
98
|
-
const mode = this.getMode(output);
|
|
99
|
-
const oas = await this.getOas();
|
|
100
|
-
const baseURL = await this.getBaseURL();
|
|
101
|
-
if (baseURL) this.plugin.options.client.baseURL = baseURL;
|
|
102
|
-
const hasClientPlugin = !!this.getPlugin(_kubb_plugin_client.pluginClientName);
|
|
103
|
-
if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) await this.upsertFile((0, _kubb_ast.createFile)({
|
|
92
|
+
options,
|
|
93
|
+
dependencies: [_kubb_plugin_ts.pluginTsName, parser === "zod" ? _kubb_plugin_zod.pluginZodName : void 0].filter(Boolean),
|
|
94
|
+
hooks: { "kubb:plugin:setup"(ctx) {
|
|
95
|
+
const resolver = userResolver ? {
|
|
96
|
+
...defaultResolver,
|
|
97
|
+
...userResolver
|
|
98
|
+
} : defaultResolver;
|
|
99
|
+
ctx.setOptions({
|
|
100
|
+
output,
|
|
101
|
+
transformers,
|
|
102
|
+
client: {
|
|
103
|
+
bundle: client?.bundle,
|
|
104
|
+
baseURL: client?.baseURL,
|
|
105
|
+
client: clientName,
|
|
106
|
+
clientType: client?.clientType ?? "function",
|
|
107
|
+
importPath: clientImportPath,
|
|
108
|
+
dataReturnType: client?.dataReturnType ?? "data",
|
|
109
|
+
paramsCasing
|
|
110
|
+
},
|
|
111
|
+
queryKey,
|
|
112
|
+
query: query === false ? false : {
|
|
113
|
+
importPath: "swr",
|
|
114
|
+
methods: ["get"],
|
|
115
|
+
...query
|
|
116
|
+
},
|
|
117
|
+
mutationKey,
|
|
118
|
+
mutation: mutation === false ? false : {
|
|
119
|
+
importPath: "swr/mutation",
|
|
120
|
+
methods: [
|
|
121
|
+
"post",
|
|
122
|
+
"put",
|
|
123
|
+
"delete",
|
|
124
|
+
"patch"
|
|
125
|
+
],
|
|
126
|
+
...mutation
|
|
127
|
+
},
|
|
128
|
+
parser,
|
|
129
|
+
paramsType,
|
|
130
|
+
pathParamsType,
|
|
131
|
+
paramsCasing,
|
|
132
|
+
group: groupConfig,
|
|
133
|
+
exclude,
|
|
134
|
+
include,
|
|
135
|
+
override,
|
|
136
|
+
resolver
|
|
137
|
+
});
|
|
138
|
+
ctx.setResolver(resolver);
|
|
139
|
+
if (userTransformer) ctx.setTransformer(userTransformer);
|
|
140
|
+
for (const gen of selectedGenerators) ctx.addGenerator(gen);
|
|
141
|
+
for (const gen of userGenerators) ctx.addGenerator(gen);
|
|
142
|
+
const root = node_path.default.resolve(ctx.config.root, ctx.config.output.path);
|
|
143
|
+
const hasClientPlugin = !!ctx.config.plugins?.some((p) => p.name === _kubb_plugin_client.pluginClientName);
|
|
144
|
+
if (client?.bundle && !hasClientPlugin && !clientImportPath) ctx.injectFile({
|
|
104
145
|
baseName: "fetch.ts",
|
|
105
146
|
path: node_path.default.resolve(root, ".kubb/fetch.ts"),
|
|
106
|
-
sources: [
|
|
147
|
+
sources: [_kubb_core.ast.createSource({
|
|
107
148
|
name: "fetch",
|
|
108
|
-
nodes: [
|
|
149
|
+
nodes: [_kubb_core.ast.createText(clientName === "fetch" ? _kubb_plugin_client_templates_clients_fetch_source.source : _kubb_plugin_client_templates_clients_axios_source.source)],
|
|
109
150
|
isExportable: true,
|
|
110
151
|
isIndexable: true
|
|
111
152
|
})]
|
|
112
|
-
})
|
|
113
|
-
if (!hasClientPlugin)
|
|
153
|
+
});
|
|
154
|
+
if (!hasClientPlugin) ctx.injectFile({
|
|
114
155
|
baseName: "config.ts",
|
|
115
156
|
path: node_path.default.resolve(root, ".kubb/config.ts"),
|
|
116
|
-
sources: [
|
|
157
|
+
sources: [_kubb_core.ast.createSource({
|
|
117
158
|
name: "config",
|
|
118
|
-
nodes: [
|
|
159
|
+
nodes: [_kubb_core.ast.createText(_kubb_plugin_client_templates_config_source.source)],
|
|
119
160
|
isExportable: false,
|
|
120
161
|
isIndexable: false
|
|
121
162
|
})]
|
|
122
|
-
}));
|
|
123
|
-
const files = await new _kubb_plugin_oas.OperationGenerator(this.plugin.options, {
|
|
124
|
-
oas,
|
|
125
|
-
driver: this.driver,
|
|
126
|
-
events: this.events,
|
|
127
|
-
plugin: this.plugin,
|
|
128
|
-
contentType,
|
|
129
|
-
exclude,
|
|
130
|
-
include,
|
|
131
|
-
override,
|
|
132
|
-
mode
|
|
133
|
-
}).build(...generators);
|
|
134
|
-
await this.upsertFile(...files);
|
|
135
|
-
const barrelFiles = await (0, _kubb_core.getBarrelFiles)(this.driver.fileManager.files, {
|
|
136
|
-
type: output.barrelType ?? "named",
|
|
137
|
-
root,
|
|
138
|
-
output,
|
|
139
|
-
meta: { pluginName: this.plugin.name }
|
|
140
163
|
});
|
|
141
|
-
|
|
142
|
-
}
|
|
164
|
+
} }
|
|
143
165
|
};
|
|
144
166
|
});
|
|
145
167
|
//#endregion
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["MutationKey","QueryKey","queryGenerator","mutationGenerator","pluginOasName","pluginTsName","pluginZodName","path","camelCase","pascalCase","pluginClientName","fetchClientSource","axiosClientSource","configSource","OperationGenerator"],"sources":["../package.json","../src/plugin.ts"],"sourcesContent":["","import path from 'node:path'\nimport { camelCase, pascalCase } from '@internals/utils'\nimport { createFile, createSource, createText } from '@kubb/ast'\nimport type { FileNode } from '@kubb/ast/types'\nimport { createPlugin, getBarrelFiles, type UserGroup } from '@kubb/core'\nimport { pluginClientName } from '@kubb/plugin-client'\nimport { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'\nimport { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'\nimport { source as configSource } from '@kubb/plugin-client/templates/config.source'\nimport { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { pluginZodName } from '@kubb/plugin-zod'\nimport { version } from '../package.json'\nimport { MutationKey, QueryKey } from './components'\nimport { mutationGenerator, queryGenerator } from './generators'\nimport type { PluginSwr } from './types.ts'\n\nexport const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']\n\nexport const pluginSwr = createPlugin<PluginSwr>((options) => {\n const {\n output = { path: 'hooks', barrelType: 'named' },\n group,\n exclude = [],\n include,\n override = [],\n parser = 'client',\n transformers = {},\n query,\n mutation,\n client,\n paramsType = 'inline',\n pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',\n mutationKey = MutationKey.getTransformer,\n queryKey = QueryKey.getTransformer,\n generators = [queryGenerator, mutationGenerator].filter(Boolean),\n paramsCasing,\n contentType,\n } = options\n\n const clientName = client?.client ?? 'axios'\n const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)\n\n return {\n name: pluginSwrName,\n version,\n options: {\n output,\n client: {\n bundle: client?.bundle,\n baseURL: client?.baseURL,\n client: clientName,\n clientType: client?.clientType ?? 'function',\n importPath: clientImportPath,\n dataReturnType: client?.dataReturnType ?? 'data',\n paramsCasing,\n },\n queryKey,\n query:\n query === false\n ? false\n : {\n importPath: 'swr',\n methods: ['get'],\n ...query,\n },\n mutationKey,\n mutation:\n mutation === false\n ? false\n : {\n importPath: 'swr/mutation',\n methods: ['post', 'put', 'delete', 'patch'],\n ...mutation,\n },\n parser,\n paramsType,\n pathParamsType,\n paramsCasing,\n group,\n exclude,\n include,\n override,\n },\n pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),\n resolvePath(baseName, pathMode, options) {\n const root = this.root\n const mode = pathMode ?? this.getMode(output)\n\n if (mode === 'single') {\n /**\n * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend\n * Other plugins then need to call addOrAppend instead of just add from the fileManager class\n */\n return path.resolve(root, output.path)\n }\n\n if (group && (options?.group?.path || options?.group?.tag)) {\n const groupName: UserGroup['name'] = group?.name\n ? group.name\n : (ctx) => {\n if (group?.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n return `${camelCase(ctx.group)}Controller`\n }\n\n return path.resolve(\n root,\n output.path,\n groupName({\n group: group.type === 'path' ? options.group.path! : options.group.tag!,\n }),\n baseName,\n )\n }\n\n return path.resolve(root, output.path, baseName)\n },\n resolveName(name, type) {\n let resolvedName = camelCase(name)\n\n if (type === 'file' || type === 'function') {\n resolvedName = camelCase(name, {\n isFile: type === 'file',\n })\n }\n\n if (type === 'type') {\n resolvedName = pascalCase(name)\n }\n\n if (type) {\n return transformers?.name?.(resolvedName, type) || resolvedName\n }\n\n return resolvedName\n },\n async buildStart() {\n const root = this.root\n const mode = this.getMode(output)\n const oas = await this.getOas()\n const baseURL = await this.getBaseURL()\n\n if (baseURL) {\n this.plugin.options.client.baseURL = baseURL\n }\n\n const hasClientPlugin = !!this.getPlugin(pluginClientName)\n\n if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) {\n // pre add bundled fetch\n await this.upsertFile(\n createFile({\n baseName: 'fetch.ts',\n path: path.resolve(root, '.kubb/fetch.ts'),\n sources: [\n createSource({\n name: 'fetch',\n nodes: [createText(this.plugin.options.client.client === 'fetch' ? fetchClientSource : axiosClientSource)],\n isExportable: true,\n isIndexable: true,\n }),\n ],\n }),\n )\n }\n\n if (!hasClientPlugin) {\n await this.addFile(\n createFile({\n baseName: 'config.ts',\n path: path.resolve(root, '.kubb/config.ts'),\n sources: [\n createSource({\n name: 'config',\n nodes: [createText(configSource)],\n isExportable: false,\n isIndexable: false,\n }),\n ],\n }),\n )\n }\n\n const operationGenerator = new OperationGenerator(this.plugin.options, {\n oas,\n driver: this.driver,\n events: this.events,\n plugin: this.plugin,\n contentType,\n exclude,\n include,\n override,\n mode,\n })\n\n const files = await operationGenerator.build(...generators)\n await this.upsertFile(...files)\n\n const barrelFiles = await getBarrelFiles(this.driver.fileManager.files as unknown as FileNode[], {\n type: output.barrelType ?? 'named',\n root,\n output,\n meta: {\n pluginName: this.plugin.name,\n },\n })\n\n await this.upsertFile(...barrelFiles)\n },\n }\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;ACiBA,MAAa,gBAAgB;AAE7B,MAAa,aAAA,GAAA,WAAA,eAAqC,YAAY;CAC5D,MAAM,EACJ,SAAS;EAAE,MAAM;EAAS,YAAY;EAAS,EAC/C,OACA,UAAU,EAAE,EACZ,SACA,WAAW,EAAE,EACb,SAAS,UACT,eAAe,EAAE,EACjB,OACA,UACA,QACA,aAAa,UACb,iBAAiB,eAAe,WAAW,WAAW,QA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AAA,GAAA,UAAA,cACM;KACX,MAAM;KACN,OAAO,EAAA,GAAA,UAAA,YAAYM,4CAAAA,OAAa,CAAC;KACjC,cAAc;KACd,aAAa;KACd,CAAC,CACH;IACF,CAAC,CACH;GAeH,MAAM,QAAQ,MAZa,IAAIC,iBAAAA,mBAAmB,KAAK,OAAO,SAAS;IACrE;IACA,QAAQ,KAAK;IACb,QAAQ,KAAK;IACb,QAAQ,KAAK;IACb;IACA;IACA;IACA;IACA;IACD,CAAC,CAEqC,MAAM,GAAG,WAAW;AAC3D,SAAM,KAAK,WAAW,GAAG,MAAM;GAE/B,MAAM,cAAc,OAAA,GAAA,WAAA,gBAAqB,KAAK,OAAO,YAAY,OAAgC;IAC/F,MAAM,OAAO,cAAc;IAC3B;IACA;IACA,MAAM,EACJ,YAAY,KAAK,OAAO,MACzB;IACF,CAAC;AAEF,SAAM,KAAK,WAAW,GAAG,YAAY;;EAExC;EACD"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["camelCase","MutationKey","QueryKey","queryGenerator","mutationGenerator","camelCase","pluginTsName","pluginZodName","path","pluginClientName","ast","fetchClientSource","axiosClientSource","configSource"],"sources":["../src/resolvers/resolverSwr.ts","../src/resolvers/resolverSwrLegacy.ts","../src/plugin.ts"],"sourcesContent":["import { camelCase } from '@internals/utils'\nimport { defineResolver } from '@kubb/core'\nimport type { PluginSwr } from '../types.ts'\n\n/**\n * Resolver for `@kubb/plugin-swr` that provides the default naming\n * and path-resolution helpers used by the plugin.\n *\n * @example\n * ```ts\n * import { resolverSwr } from '@kubb/plugin-swr'\n *\n * resolverSwr.default('list pets', 'function') // -> 'listPets'\n * resolverSwr.resolveName('show pet by id') // -> 'showPetById'\n * ```\n */\nexport const resolverSwr = defineResolver<PluginSwr>(() => ({\n name: 'default',\n pluginName: 'plugin-swr',\n default(name, type) {\n return camelCase(name, { isFile: type === 'file' })\n },\n resolveName(name) {\n return this.default(name, 'function')\n },\n}))\n","import { defineResolver } from '@kubb/core'\nimport type { PluginSwr } from '../types.ts'\nimport { resolverSwr } from './resolverSwr.ts'\n\n/**\n * Legacy resolver for `@kubb/plugin-swr` that reproduces the naming conventions\n * used in Kubb v4. Enable via `compatibilityPreset: 'kubbV4'`.\n *\n * The naming logic is identical to the default resolver — the only difference\n * is the `name` field (`'kubbV4'` vs `'default'`) so the driver can\n * distinguish presets.\n */\nexport const resolverSwrLegacy = defineResolver<PluginSwr>(() => ({\n ...resolverSwr,\n name: 'kubbV4',\n pluginName: 'plugin-swr',\n}))\n","import path from 'node:path'\nimport { camelCase } from '@internals/utils'\n\nimport { ast, definePlugin, type Group } from '@kubb/core'\nimport { pluginClientName } from '@kubb/plugin-client'\nimport { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'\nimport { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'\nimport { source as configSource } from '@kubb/plugin-client/templates/config.source'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { pluginZodName } from '@kubb/plugin-zod'\nimport { MutationKey, QueryKey } from './components'\nimport { mutationGenerator, queryGenerator } from './generators'\nimport { resolverSwr } from './resolvers/resolverSwr.ts'\nimport { resolverSwrLegacy } from './resolvers/resolverSwrLegacy.ts'\nimport type { PluginSwr } from './types.ts'\n\n/**\n * Canonical plugin name for `@kubb/plugin-swr`, used to identify the plugin\n * in driver lookups and warnings.\n */\nexport const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']\n\n/**\n * The `@kubb/plugin-swr` plugin factory.\n *\n * Generates SWR hooks from an OpenAPI/AST `RootNode`.\n * Walks operations, delegates rendering to the active generators,\n * and writes barrel files based on `output.barrelType`.\n *\n * @example\n * ```ts\n * import { pluginSwr } from '@kubb/plugin-swr'\n *\n * export default defineConfig({\n * plugins: [pluginSwr({ output: { path: 'hooks' } })],\n * })\n * ```\n */\nexport const pluginSwr = definePlugin<PluginSwr>((options) => {\n const {\n output = { path: 'hooks', barrelType: 'named' },\n group,\n exclude = [],\n include,\n override = [],\n parser = 'client',\n transformers = {},\n query,\n mutation,\n client,\n paramsType = 'inline',\n pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',\n mutationKey = MutationKey.getTransformer,\n queryKey = QueryKey.getTransformer,\n paramsCasing,\n resolver: userResolver,\n transformer: userTransformer,\n generators: userGenerators = [],\n compatibilityPreset = 'default',\n } = options\n\n const defaultResolver = compatibilityPreset === 'kubbV4' ? resolverSwrLegacy : resolverSwr\n\n const clientName = client?.client ?? 'axios'\n const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)\n\n const selectedGenerators = options.generators ?? [queryGenerator, mutationGenerator].filter(Boolean)\n\n const groupConfig = group\n ? ({\n ...group,\n name: group.name\n ? group.name\n : (ctx: { group: string }) => {\n if (group.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n return `${camelCase(ctx.group)}Controller`\n },\n } satisfies Group)\n : undefined\n\n return {\n name: pluginSwrName,\n options,\n dependencies: [pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),\n hooks: {\n 'kubb:plugin:setup'(ctx) {\n const resolver = userResolver ? { ...defaultResolver, ...userResolver } : defaultResolver\n\n ctx.setOptions({\n output,\n transformers,\n client: {\n bundle: client?.bundle,\n baseURL: client?.baseURL,\n client: clientName,\n clientType: client?.clientType ?? 'function',\n importPath: clientImportPath,\n dataReturnType: client?.dataReturnType ?? 'data',\n paramsCasing,\n },\n queryKey,\n query:\n query === false\n ? false\n : {\n importPath: 'swr',\n methods: ['get'],\n ...query,\n },\n mutationKey,\n mutation:\n mutation === false\n ? false\n : {\n importPath: 'swr/mutation',\n methods: ['post', 'put', 'delete', 'patch'],\n ...mutation,\n },\n parser,\n paramsType,\n pathParamsType,\n paramsCasing,\n group: groupConfig,\n exclude,\n include,\n override,\n resolver,\n })\n ctx.setResolver(resolver)\n if (userTransformer) {\n ctx.setTransformer(userTransformer)\n }\n for (const gen of selectedGenerators) {\n ctx.addGenerator(gen)\n }\n for (const gen of userGenerators) {\n ctx.addGenerator(gen)\n }\n\n const root = path.resolve(ctx.config.root, ctx.config.output.path)\n\n const hasClientPlugin = !!ctx.config.plugins?.some((p) => (p as { name?: string }).name === pluginClientName)\n\n if (client?.bundle && !hasClientPlugin && !clientImportPath) {\n ctx.injectFile({\n baseName: 'fetch.ts',\n path: path.resolve(root, '.kubb/fetch.ts'),\n sources: [\n ast.createSource({\n name: 'fetch',\n nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],\n isExportable: true,\n isIndexable: true,\n }),\n ],\n })\n }\n\n if (!hasClientPlugin) {\n ctx.injectFile({\n baseName: 'config.ts',\n path: path.resolve(root, '.kubb/config.ts'),\n sources: [\n ast.createSource({\n name: 'config',\n nodes: [ast.createText(configSource)],\n isExportable: false,\n isIndexable: false,\n }),\n ],\n })\n }\n },\n },\n }\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,MAAa,eAAA,GAAA,WAAA,uBAA+C;CAC1D,MAAM;CACN,YAAY;CACZ,QAAQ,MAAM,MAAM;AAClB,SAAOA,mBAAAA,UAAU,MAAM,EAAE,QAAQ,SAAS,QAAQ,CAAC;;CAErD,YAAY,MAAM;AAChB,SAAO,KAAK,QAAQ,MAAM,WAAW;;CAExC,EAAE;;;;;;;;;;;ACbH,MAAa,qBAAA,GAAA,WAAA,uBAAqD;CAChE,GAAG;CACH,MAAM;CACN,YAAY;CACb,EAAE;;;;;;;ACIH,MAAa,gBAAgB;;;;;;;;;;;;;;;;;AAkB7B,MAAa,aAAA,GAAA,WAAA,eAAqC,YAAY;CAC5D,MAAM,EACJ,SAAS;EAAE,MAAM;EAAS,YAAY;EAAS,EAC/C,OACA,UAAU,EAAE,EACZ,SACA,WAAW,EAAE,EACb,SAAS,UACT,eAAe,EAAE,EACjB,OACA,UACA,QACA,aAAa,UACb,iBAAiB,eAAe,WAAW,WAAW,QAAQ,kBAAkB,UAChF,cAAcC,mBAAAA,YAAY,gBAC1B,WA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BACF,KAAI,eAAe,gBAAgB;AAErC,QAAK,MAAM,OAAO,mBAChB,KAAI,aAAa,IAAI;AAEvB,QAAK,MAAM,OAAO,eAChB,KAAI,aAAa,IAAI;GAGvB,MAAM,OAAOC,UAAAA,QAAK,QAAQ,IAAI,OAAO,MAAM,IAAI,OAAO,OAAO,KAAK;GAElE,MAAM,kBAAkB,CAAC,CAAC,IAAI,OAAO,SAAS,MAAM,MAAO,EAAwB,SAASC,oBAAAA,iBAAiB;AAE7G,OAAI,QAAQ,UAAU,CAAC,mBAAmB,CAAC,iBACzC,KAAI,WAAW;IACb,UAAU;IACV,MAAMD,UAAAA,QAAK,QAAQ,MAAM,iBAAiB;IAC1C,SAAS,CACPE,WAAAA,IAAI,aAAa;KACf,MAAM;KACN,OAAO,CAACA,WAAAA,IAAI,WAAW,eAAe,UAAUC,mDAAAA,SAAoBC,mDAAAA,OAAkB,CAAC;KACvF,cAAc;KACd,aAAa;KACd,CAAC,CACH;IACF,CAAC;AAGJ,OAAI,CAAC,gBACH,KAAI,WAAW;IACb,UAAU;IACV,MAAMJ,UAAAA,QAAK,QAAQ,MAAM,kBAAkB;IAC3C,SAAS,CACPE,WAAAA,IAAI,aAAa;KACf,MAAM;KACN,OAAO,CAACA,WAAAA,IAAI,WAAWG,4CAAAA,OAAa,CAAC;KACrC,cAAc;KACd,aAAa;KACd,CAAC,CACH;IACF,CAAC;KAGP;EACF;EACD"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { n as PluginSwr, t as Options } from "./types-
|
|
2
|
+
import { n as PluginSwr, t as Options } from "./types-FA5mH9Ch.js";
|
|
3
3
|
import * as _$_kubb_core0 from "@kubb/core";
|
|
4
4
|
|
|
5
5
|
//#region src/plugin.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Canonical plugin name for `@kubb/plugin-swr`, used to identify the plugin
|
|
8
|
+
* in driver lookups and warnings.
|
|
9
|
+
*/
|
|
6
10
|
declare const pluginSwrName = "plugin-swr";
|
|
7
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The `@kubb/plugin-swr` plugin factory.
|
|
13
|
+
*
|
|
14
|
+
* Generates SWR hooks from an OpenAPI/AST `RootNode`.
|
|
15
|
+
* Walks operations, delegates rendering to the active generators,
|
|
16
|
+
* and writes barrel files based on `output.barrelType`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { pluginSwr } from '@kubb/plugin-swr'
|
|
21
|
+
*
|
|
22
|
+
* export default defineConfig({
|
|
23
|
+
* plugins: [pluginSwr({ output: { path: 'hooks' } })],
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare const pluginSwr: (options?: Options | undefined) => _$_kubb_core0.HookStylePlugin<PluginSwr>;
|
|
8
28
|
//#endregion
|
|
9
29
|
export { type PluginSwr, pluginSwr, pluginSwrName };
|
|
10
30
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,144 +1,166 @@
|
|
|
1
1
|
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import {
|
|
3
|
-
import { n as mutationGenerator, t as queryGenerator } from "./generators-
|
|
2
|
+
import { i as MutationKey, r as QueryKey, s as camelCase } from "./components-JQ2KRFCa.js";
|
|
3
|
+
import { n as mutationGenerator, t as queryGenerator } from "./generators-Cl7nr-FB.js";
|
|
4
4
|
import path from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
import { createPlugin, getBarrelFiles } from "@kubb/core";
|
|
5
|
+
import { ast, definePlugin, defineResolver } from "@kubb/core";
|
|
7
6
|
import { pluginClientName } from "@kubb/plugin-client";
|
|
8
7
|
import { source } from "@kubb/plugin-client/templates/clients/axios.source";
|
|
9
8
|
import { source as source$1 } from "@kubb/plugin-client/templates/clients/fetch.source";
|
|
10
9
|
import { source as source$2 } from "@kubb/plugin-client/templates/config.source";
|
|
11
|
-
import { OperationGenerator, pluginOasName } from "@kubb/plugin-oas";
|
|
12
10
|
import { pluginTsName } from "@kubb/plugin-ts";
|
|
13
11
|
import { pluginZodName } from "@kubb/plugin-zod";
|
|
14
|
-
//#region
|
|
15
|
-
|
|
12
|
+
//#region src/resolvers/resolverSwr.ts
|
|
13
|
+
/**
|
|
14
|
+
* Resolver for `@kubb/plugin-swr` that provides the default naming
|
|
15
|
+
* and path-resolution helpers used by the plugin.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { resolverSwr } from '@kubb/plugin-swr'
|
|
20
|
+
*
|
|
21
|
+
* resolverSwr.default('list pets', 'function') // -> 'listPets'
|
|
22
|
+
* resolverSwr.resolveName('show pet by id') // -> 'showPetById'
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
const resolverSwr = defineResolver(() => ({
|
|
26
|
+
name: "default",
|
|
27
|
+
pluginName: "plugin-swr",
|
|
28
|
+
default(name, type) {
|
|
29
|
+
return camelCase(name, { isFile: type === "file" });
|
|
30
|
+
},
|
|
31
|
+
resolveName(name) {
|
|
32
|
+
return this.default(name, "function");
|
|
33
|
+
}
|
|
34
|
+
}));
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region src/resolvers/resolverSwrLegacy.ts
|
|
37
|
+
/**
|
|
38
|
+
* Legacy resolver for `@kubb/plugin-swr` that reproduces the naming conventions
|
|
39
|
+
* used in Kubb v4. Enable via `compatibilityPreset: 'kubbV4'`.
|
|
40
|
+
*
|
|
41
|
+
* The naming logic is identical to the default resolver — the only difference
|
|
42
|
+
* is the `name` field (`'kubbV4'` vs `'default'`) so the driver can
|
|
43
|
+
* distinguish presets.
|
|
44
|
+
*/
|
|
45
|
+
const resolverSwrLegacy = defineResolver(() => ({
|
|
46
|
+
...resolverSwr,
|
|
47
|
+
name: "kubbV4",
|
|
48
|
+
pluginName: "plugin-swr"
|
|
49
|
+
}));
|
|
16
50
|
//#endregion
|
|
17
51
|
//#region src/plugin.ts
|
|
52
|
+
/**
|
|
53
|
+
* Canonical plugin name for `@kubb/plugin-swr`, used to identify the plugin
|
|
54
|
+
* in driver lookups and warnings.
|
|
55
|
+
*/
|
|
18
56
|
const pluginSwrName = "plugin-swr";
|
|
19
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The `@kubb/plugin-swr` plugin factory.
|
|
59
|
+
*
|
|
60
|
+
* Generates SWR hooks from an OpenAPI/AST `RootNode`.
|
|
61
|
+
* Walks operations, delegates rendering to the active generators,
|
|
62
|
+
* and writes barrel files based on `output.barrelType`.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { pluginSwr } from '@kubb/plugin-swr'
|
|
67
|
+
*
|
|
68
|
+
* export default defineConfig({
|
|
69
|
+
* plugins: [pluginSwr({ output: { path: 'hooks' } })],
|
|
70
|
+
* })
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
const pluginSwr = definePlugin((options) => {
|
|
20
74
|
const { output = {
|
|
21
75
|
path: "hooks",
|
|
22
76
|
barrelType: "named"
|
|
23
|
-
}, group, exclude = [], include, override = [], parser = "client", transformers = {}, query, mutation, client, paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", mutationKey = MutationKey.getTransformer, queryKey = QueryKey.getTransformer, generators = [
|
|
77
|
+
}, group, exclude = [], include, override = [], parser = "client", transformers = {}, query, mutation, client, paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", mutationKey = MutationKey.getTransformer, queryKey = QueryKey.getTransformer, paramsCasing, resolver: userResolver, transformer: userTransformer, generators: userGenerators = [], compatibilityPreset = "default" } = options;
|
|
78
|
+
const defaultResolver = compatibilityPreset === "kubbV4" ? resolverSwrLegacy : resolverSwr;
|
|
24
79
|
const clientName = client?.client ?? "axios";
|
|
25
80
|
const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : void 0);
|
|
81
|
+
const selectedGenerators = options.generators ?? [queryGenerator, mutationGenerator].filter(Boolean);
|
|
82
|
+
const groupConfig = group ? {
|
|
83
|
+
...group,
|
|
84
|
+
name: group.name ? group.name : (ctx) => {
|
|
85
|
+
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
86
|
+
return `${camelCase(ctx.group)}Controller`;
|
|
87
|
+
}
|
|
88
|
+
} : void 0;
|
|
26
89
|
return {
|
|
27
90
|
name: pluginSwrName,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
82
|
-
return `${camelCase(ctx.group)}Controller`;
|
|
83
|
-
};
|
|
84
|
-
return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
|
|
85
|
-
}
|
|
86
|
-
return path.resolve(root, output.path, baseName);
|
|
87
|
-
},
|
|
88
|
-
resolveName(name, type) {
|
|
89
|
-
let resolvedName = camelCase(name);
|
|
90
|
-
if (type === "file" || type === "function") resolvedName = camelCase(name, { isFile: type === "file" });
|
|
91
|
-
if (type === "type") resolvedName = pascalCase(name);
|
|
92
|
-
if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
|
|
93
|
-
return resolvedName;
|
|
94
|
-
},
|
|
95
|
-
async buildStart() {
|
|
96
|
-
const root = this.root;
|
|
97
|
-
const mode = this.getMode(output);
|
|
98
|
-
const oas = await this.getOas();
|
|
99
|
-
const baseURL = await this.getBaseURL();
|
|
100
|
-
if (baseURL) this.plugin.options.client.baseURL = baseURL;
|
|
101
|
-
const hasClientPlugin = !!this.getPlugin(pluginClientName);
|
|
102
|
-
if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) await this.upsertFile(createFile({
|
|
91
|
+
options,
|
|
92
|
+
dependencies: [pluginTsName, parser === "zod" ? pluginZodName : void 0].filter(Boolean),
|
|
93
|
+
hooks: { "kubb:plugin:setup"(ctx) {
|
|
94
|
+
const resolver = userResolver ? {
|
|
95
|
+
...defaultResolver,
|
|
96
|
+
...userResolver
|
|
97
|
+
} : defaultResolver;
|
|
98
|
+
ctx.setOptions({
|
|
99
|
+
output,
|
|
100
|
+
transformers,
|
|
101
|
+
client: {
|
|
102
|
+
bundle: client?.bundle,
|
|
103
|
+
baseURL: client?.baseURL,
|
|
104
|
+
client: clientName,
|
|
105
|
+
clientType: client?.clientType ?? "function",
|
|
106
|
+
importPath: clientImportPath,
|
|
107
|
+
dataReturnType: client?.dataReturnType ?? "data",
|
|
108
|
+
paramsCasing
|
|
109
|
+
},
|
|
110
|
+
queryKey,
|
|
111
|
+
query: query === false ? false : {
|
|
112
|
+
importPath: "swr",
|
|
113
|
+
methods: ["get"],
|
|
114
|
+
...query
|
|
115
|
+
},
|
|
116
|
+
mutationKey,
|
|
117
|
+
mutation: mutation === false ? false : {
|
|
118
|
+
importPath: "swr/mutation",
|
|
119
|
+
methods: [
|
|
120
|
+
"post",
|
|
121
|
+
"put",
|
|
122
|
+
"delete",
|
|
123
|
+
"patch"
|
|
124
|
+
],
|
|
125
|
+
...mutation
|
|
126
|
+
},
|
|
127
|
+
parser,
|
|
128
|
+
paramsType,
|
|
129
|
+
pathParamsType,
|
|
130
|
+
paramsCasing,
|
|
131
|
+
group: groupConfig,
|
|
132
|
+
exclude,
|
|
133
|
+
include,
|
|
134
|
+
override,
|
|
135
|
+
resolver
|
|
136
|
+
});
|
|
137
|
+
ctx.setResolver(resolver);
|
|
138
|
+
if (userTransformer) ctx.setTransformer(userTransformer);
|
|
139
|
+
for (const gen of selectedGenerators) ctx.addGenerator(gen);
|
|
140
|
+
for (const gen of userGenerators) ctx.addGenerator(gen);
|
|
141
|
+
const root = path.resolve(ctx.config.root, ctx.config.output.path);
|
|
142
|
+
const hasClientPlugin = !!ctx.config.plugins?.some((p) => p.name === pluginClientName);
|
|
143
|
+
if (client?.bundle && !hasClientPlugin && !clientImportPath) ctx.injectFile({
|
|
103
144
|
baseName: "fetch.ts",
|
|
104
145
|
path: path.resolve(root, ".kubb/fetch.ts"),
|
|
105
|
-
sources: [createSource({
|
|
146
|
+
sources: [ast.createSource({
|
|
106
147
|
name: "fetch",
|
|
107
|
-
nodes: [createText(
|
|
148
|
+
nodes: [ast.createText(clientName === "fetch" ? source$1 : source)],
|
|
108
149
|
isExportable: true,
|
|
109
150
|
isIndexable: true
|
|
110
151
|
})]
|
|
111
|
-
})
|
|
112
|
-
if (!hasClientPlugin)
|
|
152
|
+
});
|
|
153
|
+
if (!hasClientPlugin) ctx.injectFile({
|
|
113
154
|
baseName: "config.ts",
|
|
114
155
|
path: path.resolve(root, ".kubb/config.ts"),
|
|
115
|
-
sources: [createSource({
|
|
156
|
+
sources: [ast.createSource({
|
|
116
157
|
name: "config",
|
|
117
|
-
nodes: [createText(source$2)],
|
|
158
|
+
nodes: [ast.createText(source$2)],
|
|
118
159
|
isExportable: false,
|
|
119
160
|
isIndexable: false
|
|
120
161
|
})]
|
|
121
|
-
}));
|
|
122
|
-
const files = await new OperationGenerator(this.plugin.options, {
|
|
123
|
-
oas,
|
|
124
|
-
driver: this.driver,
|
|
125
|
-
events: this.events,
|
|
126
|
-
plugin: this.plugin,
|
|
127
|
-
contentType,
|
|
128
|
-
exclude,
|
|
129
|
-
include,
|
|
130
|
-
override,
|
|
131
|
-
mode
|
|
132
|
-
}).build(...generators);
|
|
133
|
-
await this.upsertFile(...files);
|
|
134
|
-
const barrelFiles = await getBarrelFiles(this.driver.fileManager.files, {
|
|
135
|
-
type: output.barrelType ?? "named",
|
|
136
|
-
root,
|
|
137
|
-
output,
|
|
138
|
-
meta: { pluginName: this.plugin.name }
|
|
139
162
|
});
|
|
140
|
-
|
|
141
|
-
}
|
|
163
|
+
} }
|
|
142
164
|
};
|
|
143
165
|
});
|
|
144
166
|
//#endregion
|