@kubb/plugin-client 4.17.0 → 4.18.0
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/{Operations-DZrmy4LE.cjs → StaticClassClient-Doy7nyLp.cjs} +140 -20
- package/dist/StaticClassClient-Doy7nyLp.cjs.map +1 -0
- package/dist/{Operations-CKIsAPFo.js → StaticClassClient-yRrX6THy.js} +136 -21
- package/dist/StaticClassClient-yRrX6THy.js.map +1 -0
- package/dist/{chunk-CbDLau6x.cjs → chunk-C1_xRkKa.cjs} +10 -0
- package/dist/chunk-iVr_oF3V.js +9 -0
- package/dist/clients/axios.cjs +1 -1
- package/dist/clients/axios.cjs.map +1 -1
- package/dist/clients/axios.d.cts +1 -0
- package/dist/clients/axios.d.ts +1 -0
- package/dist/clients/axios.js +1 -0
- package/dist/clients/axios.js.map +1 -1
- package/dist/clients/fetch.cjs +1 -0
- package/dist/clients/fetch.cjs.map +1 -1
- package/dist/clients/fetch.d.cts +2 -0
- package/dist/clients/fetch.d.ts +2 -0
- package/dist/clients/fetch.js +2 -0
- package/dist/clients/fetch.js.map +1 -1
- package/dist/components.cjs +6 -5
- package/dist/components.d.cts +55 -6
- package/dist/components.d.ts +55 -6
- package/dist/components.js +2 -2
- package/dist/{generators-DLZfbw6J.cjs → generators-BPQFq13R.cjs} +203 -7
- package/dist/generators-BPQFq13R.cjs.map +1 -0
- package/dist/{generators-B0pg5PUA.js → generators-CtXa-3Fh.js} +194 -3
- package/dist/generators-CtXa-3Fh.js.map +1 -0
- package/dist/generators.cjs +3 -2
- package/dist/generators.d.cts +6 -2
- package/dist/generators.d.ts +6 -2
- package/dist/generators.js +2 -2
- package/dist/index.cjs +10 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +10 -5
- package/dist/index.js.map +1 -1
- package/dist/{types-Bw1u7Glb.d.ts → types-Chp-emXk.d.cts} +5 -3
- package/dist/{types-CLOc9nuD.d.cts → types-aqsuGVmg.d.ts} +5 -3
- package/package.json +8 -8
- package/src/components/Client.tsx +33 -29
- package/src/components/StaticClassClient.tsx +224 -0
- package/src/components/Url.tsx +2 -2
- package/src/components/index.ts +1 -0
- package/src/generators/__snapshots__/static/Pet.ts +186 -0
- package/src/generators/__snapshots__/static/Store.ts +105 -0
- package/src/generators/__snapshots__/static/User.ts +143 -0
- package/src/generators/index.ts +1 -0
- package/src/generators/staticClassClientGenerator.tsx +231 -0
- package/src/plugin.ts +8 -2
- package/src/types.ts +2 -1
- package/dist/Operations-CKIsAPFo.js.map +0 -1
- package/dist/Operations-DZrmy4LE.cjs.map +0 -1
- package/dist/generators-B0pg5PUA.js.map +0 -1
- package/dist/generators-DLZfbw6J.cjs.map +0 -1
package/dist/generators.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __name } from "./chunk-DlpkT3g-.cjs";
|
|
2
|
+
import { n as PluginClient, r as ReactGenerator } from "./types-Chp-emXk.cjs";
|
|
2
3
|
|
|
3
4
|
//#region src/generators/classClientGenerator.d.ts
|
|
4
5
|
declare const classClientGenerator: ReactGenerator<PluginClient>;
|
|
@@ -12,5 +13,8 @@ declare const groupedClientGenerator: ReactGenerator<PluginClient>;
|
|
|
12
13
|
//#region src/generators/operationsGenerator.d.ts
|
|
13
14
|
declare const operationsGenerator: ReactGenerator<PluginClient>;
|
|
14
15
|
//#endregion
|
|
15
|
-
|
|
16
|
+
//#region src/generators/staticClassClientGenerator.d.ts
|
|
17
|
+
declare const staticClassClientGenerator: ReactGenerator<PluginClient>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { classClientGenerator, clientGenerator, groupedClientGenerator, operationsGenerator, staticClassClientGenerator };
|
|
16
20
|
//# sourceMappingURL=generators.d.cts.map
|
package/dist/generators.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __name } from "./chunk-iVr_oF3V.js";
|
|
2
|
+
import { n as PluginClient, r as ReactGenerator } from "./types-aqsuGVmg.js";
|
|
2
3
|
|
|
3
4
|
//#region src/generators/classClientGenerator.d.ts
|
|
4
5
|
declare const classClientGenerator: ReactGenerator<PluginClient>;
|
|
@@ -12,5 +13,8 @@ declare const groupedClientGenerator: ReactGenerator<PluginClient>;
|
|
|
12
13
|
//#region src/generators/operationsGenerator.d.ts
|
|
13
14
|
declare const operationsGenerator: ReactGenerator<PluginClient>;
|
|
14
15
|
//#endregion
|
|
15
|
-
|
|
16
|
+
//#region src/generators/staticClassClientGenerator.d.ts
|
|
17
|
+
declare const staticClassClientGenerator: ReactGenerator<PluginClient>;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { classClientGenerator, clientGenerator, groupedClientGenerator, operationsGenerator, staticClassClientGenerator };
|
|
16
20
|
//# sourceMappingURL=generators.d.ts.map
|
package/dist/generators.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as classClientGenerator, i as clientGenerator, n as operationsGenerator, r as groupedClientGenerator, t as staticClassClientGenerator } from "./generators-CtXa-3Fh.js";
|
|
2
2
|
|
|
3
|
-
export { classClientGenerator, clientGenerator, groupedClientGenerator, operationsGenerator };
|
|
3
|
+
export { classClientGenerator, clientGenerator, groupedClientGenerator, operationsGenerator, staticClassClientGenerator };
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const require_chunk = require('./chunk-
|
|
2
|
-
const require_generators = require('./generators-
|
|
1
|
+
const require_chunk = require('./chunk-C1_xRkKa.cjs');
|
|
2
|
+
const require_generators = require('./generators-BPQFq13R.cjs');
|
|
3
3
|
let node_path = require("node:path");
|
|
4
4
|
node_path = require_chunk.__toESM(node_path);
|
|
5
5
|
let _kubb_core = require("@kubb/core");
|
|
@@ -17,8 +17,8 @@ const pluginClient = (0, _kubb_core.definePlugin)((options) => {
|
|
|
17
17
|
}, group, urlType = false, exclude = [], include, override = [], transformers = {}, dataReturnType = "data", paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", operations = false, baseURL, paramsCasing, clientType = "function", parser = "client", client = "axios", importPath, contentType, bundle = false } = options;
|
|
18
18
|
const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : void 0);
|
|
19
19
|
const defaultGenerators = [
|
|
20
|
-
clientType === "class" ? require_generators.classClientGenerator : require_generators.clientGenerator,
|
|
21
|
-
group && clientType
|
|
20
|
+
clientType === "staticClass" ? require_generators.staticClassClientGenerator : clientType === "class" ? require_generators.classClientGenerator : require_generators.clientGenerator,
|
|
21
|
+
group && clientType === "function" ? require_generators.groupedClientGenerator : void 0,
|
|
22
22
|
operations ? require_generators.operationsGenerator : void 0
|
|
23
23
|
].filter(Boolean);
|
|
24
24
|
const generators = options.generators ?? defaultGenerators;
|
|
@@ -75,7 +75,9 @@ const pluginClient = (0, _kubb_core.definePlugin)((options) => {
|
|
|
75
75
|
value: (0, _kubb_core_utils.resolveModuleSource)(this.plugin.options.client === "fetch" ? "@kubb/plugin-client/templates/clients/fetch" : "@kubb/plugin-client/templates/clients/axios").source,
|
|
76
76
|
isExportable: true,
|
|
77
77
|
isIndexable: true
|
|
78
|
-
}]
|
|
78
|
+
}],
|
|
79
|
+
imports: [],
|
|
80
|
+
exports: []
|
|
79
81
|
});
|
|
80
82
|
await this.addFile({
|
|
81
83
|
baseName: "config.ts",
|
|
@@ -85,7 +87,9 @@ const pluginClient = (0, _kubb_core.definePlugin)((options) => {
|
|
|
85
87
|
value: (0, _kubb_core_utils.resolveModuleSource)("@kubb/plugin-client/templates/config").source,
|
|
86
88
|
isExportable: false,
|
|
87
89
|
isIndexable: false
|
|
88
|
-
}]
|
|
90
|
+
}],
|
|
91
|
+
imports: [],
|
|
92
|
+
exports: []
|
|
89
93
|
});
|
|
90
94
|
const files = await new _kubb_plugin_oas.OperationGenerator(baseURL$1 ? {
|
|
91
95
|
...this.plugin.options,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["classClientGenerator","clientGenerator","groupedClientGenerator","operationsGenerator","pluginOasName","pluginZodName","path","options","
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["staticClassClientGenerator","classClientGenerator","clientGenerator","groupedClientGenerator","operationsGenerator","pluginOasName","pluginZodName","path","options","baseURL","OperationGenerator"],"sources":["../src/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { definePlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'\nimport { camelCase } from '@kubb/core/transformers'\nimport { resolveModuleSource } from '@kubb/core/utils'\nimport { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'\nimport { pluginZodName } from '@kubb/plugin-zod'\nimport { classClientGenerator, operationsGenerator } from './generators'\nimport { clientGenerator } from './generators/clientGenerator.tsx'\nimport { groupedClientGenerator } from './generators/groupedClientGenerator.tsx'\nimport { staticClassClientGenerator } from './generators/staticClassClientGenerator.tsx'\nimport type { PluginClient } from './types.ts'\n\nexport const pluginClientName = 'plugin-client' satisfies PluginClient['name']\n\nexport const pluginClient = definePlugin<PluginClient>((options) => {\n const {\n output = { path: 'clients', barrelType: 'named' },\n group,\n urlType = false,\n exclude = [],\n include,\n override = [],\n transformers = {},\n dataReturnType = 'data',\n paramsType = 'inline',\n pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',\n operations = false,\n baseURL,\n paramsCasing,\n clientType = 'function',\n parser = 'client',\n client = 'axios',\n importPath,\n contentType,\n bundle = false,\n } = options\n\n const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : undefined)\n\n const defaultGenerators = [\n clientType === 'staticClass' ? staticClassClientGenerator : clientType === 'class' ? classClientGenerator : clientGenerator,\n group && clientType === 'function' ? groupedClientGenerator : undefined,\n operations ? operationsGenerator : undefined,\n ].filter(Boolean)\n\n const generators = options.generators ?? defaultGenerators\n\n return {\n name: pluginClientName,\n options: {\n client,\n clientType,\n bundle,\n output,\n group,\n parser,\n dataReturnType,\n importPath: resolvedImportPath,\n paramsType,\n paramsCasing,\n pathParamsType,\n baseURL,\n urlType,\n },\n pre: [pluginOasName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),\n resolvePath(baseName, pathMode, options) {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = pathMode ?? getMode(path.resolve(root, output.path))\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: Group['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 const resolvedName = camelCase(name, { isFile: type === 'file' })\n\n if (type) {\n return transformers?.name?.(resolvedName, type) || resolvedName\n }\n\n return resolvedName\n },\n async install() {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = getMode(path.resolve(root, output.path))\n const oas = await this.getOas()\n const baseURL = await this.getBaseURL()\n\n // pre add bundled fetch\n if (bundle && !this.plugin.options.importPath) {\n await this.addFile({\n baseName: 'fetch.ts',\n path: path.resolve(root, '.kubb/fetch.ts'),\n sources: [\n {\n name: 'fetch',\n value: resolveModuleSource(\n this.plugin.options.client === 'fetch' ? '@kubb/plugin-client/templates/clients/fetch' : '@kubb/plugin-client/templates/clients/axios',\n ).source,\n isExportable: true,\n isIndexable: true,\n },\n ],\n imports: [],\n exports: [],\n })\n }\n\n await this.addFile({\n baseName: 'config.ts',\n path: path.resolve(root, '.kubb/config.ts'),\n sources: [\n {\n name: 'config',\n value: resolveModuleSource('@kubb/plugin-client/templates/config').source,\n isExportable: false,\n isIndexable: false,\n },\n ],\n imports: [],\n exports: [],\n })\n\n const operationGenerator = new OperationGenerator(\n baseURL\n ? {\n ...this.plugin.options,\n baseURL,\n }\n : this.plugin.options,\n {\n fabric: this.fabric,\n oas,\n pluginManager: this.pluginManager,\n events: this.events,\n plugin: this.plugin,\n contentType,\n exclude,\n include,\n override,\n mode,\n },\n )\n\n const files = await operationGenerator.build(...generators)\n\n await this.upsertFile(...files)\n\n const barrelFiles = await getBarrelFiles(this.fabric.files, {\n type: output.barrelType ?? 'named',\n root,\n output,\n meta: {\n pluginKey: this.plugin.key,\n },\n })\n\n await this.upsertFile(...barrelFiles)\n },\n }\n})\n"],"mappings":"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"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __name } from "./chunk-DlpkT3g-.cjs";
|
|
2
|
+
import { a as UserPluginWithLifeCycle, n as PluginClient, t as Options } from "./types-Chp-emXk.cjs";
|
|
2
3
|
|
|
3
4
|
//#region src/plugin.d.ts
|
|
4
5
|
declare const pluginClientName = "plugin-client";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __name } from "./chunk-iVr_oF3V.js";
|
|
2
|
+
import { a as UserPluginWithLifeCycle, n as PluginClient, t as Options } from "./types-aqsuGVmg.js";
|
|
2
3
|
|
|
3
4
|
//#region src/plugin.d.ts
|
|
4
5
|
declare const pluginClientName = "plugin-client";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __name } from "./chunk-iVr_oF3V.js";
|
|
2
|
+
import { a as classClientGenerator, i as clientGenerator, n as operationsGenerator, r as groupedClientGenerator, t as staticClassClientGenerator } from "./generators-CtXa-3Fh.js";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import { definePlugin, getBarrelFiles, getMode } from "@kubb/core";
|
|
4
5
|
import { camelCase } from "@kubb/core/transformers";
|
|
@@ -15,8 +16,8 @@ const pluginClient = definePlugin((options) => {
|
|
|
15
16
|
}, group, urlType = false, exclude = [], include, override = [], transformers = {}, dataReturnType = "data", paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline", operations = false, baseURL, paramsCasing, clientType = "function", parser = "client", client = "axios", importPath, contentType, bundle = false } = options;
|
|
16
17
|
const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : void 0);
|
|
17
18
|
const defaultGenerators = [
|
|
18
|
-
clientType === "class" ? classClientGenerator : clientGenerator,
|
|
19
|
-
group && clientType
|
|
19
|
+
clientType === "staticClass" ? staticClassClientGenerator : clientType === "class" ? classClientGenerator : clientGenerator,
|
|
20
|
+
group && clientType === "function" ? groupedClientGenerator : void 0,
|
|
20
21
|
operations ? operationsGenerator : void 0
|
|
21
22
|
].filter(Boolean);
|
|
22
23
|
const generators = options.generators ?? defaultGenerators;
|
|
@@ -73,7 +74,9 @@ const pluginClient = definePlugin((options) => {
|
|
|
73
74
|
value: resolveModuleSource(this.plugin.options.client === "fetch" ? "@kubb/plugin-client/templates/clients/fetch" : "@kubb/plugin-client/templates/clients/axios").source,
|
|
74
75
|
isExportable: true,
|
|
75
76
|
isIndexable: true
|
|
76
|
-
}]
|
|
77
|
+
}],
|
|
78
|
+
imports: [],
|
|
79
|
+
exports: []
|
|
77
80
|
});
|
|
78
81
|
await this.addFile({
|
|
79
82
|
baseName: "config.ts",
|
|
@@ -83,7 +86,9 @@ const pluginClient = definePlugin((options) => {
|
|
|
83
86
|
value: resolveModuleSource("@kubb/plugin-client/templates/config").source,
|
|
84
87
|
isExportable: false,
|
|
85
88
|
isIndexable: false
|
|
86
|
-
}]
|
|
89
|
+
}],
|
|
90
|
+
imports: [],
|
|
91
|
+
exports: []
|
|
87
92
|
});
|
|
88
93
|
const files = await new OperationGenerator(baseURL$1 ? {
|
|
89
94
|
...this.plugin.options,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["options","
|
|
1
|
+
{"version":3,"file":"index.js","names":["options","baseURL"],"sources":["../src/plugin.ts"],"sourcesContent":["import path from 'node:path'\nimport { definePlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'\nimport { camelCase } from '@kubb/core/transformers'\nimport { resolveModuleSource } from '@kubb/core/utils'\nimport { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'\nimport { pluginZodName } from '@kubb/plugin-zod'\nimport { classClientGenerator, operationsGenerator } from './generators'\nimport { clientGenerator } from './generators/clientGenerator.tsx'\nimport { groupedClientGenerator } from './generators/groupedClientGenerator.tsx'\nimport { staticClassClientGenerator } from './generators/staticClassClientGenerator.tsx'\nimport type { PluginClient } from './types.ts'\n\nexport const pluginClientName = 'plugin-client' satisfies PluginClient['name']\n\nexport const pluginClient = definePlugin<PluginClient>((options) => {\n const {\n output = { path: 'clients', barrelType: 'named' },\n group,\n urlType = false,\n exclude = [],\n include,\n override = [],\n transformers = {},\n dataReturnType = 'data',\n paramsType = 'inline',\n pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',\n operations = false,\n baseURL,\n paramsCasing,\n clientType = 'function',\n parser = 'client',\n client = 'axios',\n importPath,\n contentType,\n bundle = false,\n } = options\n\n const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : undefined)\n\n const defaultGenerators = [\n clientType === 'staticClass' ? staticClassClientGenerator : clientType === 'class' ? classClientGenerator : clientGenerator,\n group && clientType === 'function' ? groupedClientGenerator : undefined,\n operations ? operationsGenerator : undefined,\n ].filter(Boolean)\n\n const generators = options.generators ?? defaultGenerators\n\n return {\n name: pluginClientName,\n options: {\n client,\n clientType,\n bundle,\n output,\n group,\n parser,\n dataReturnType,\n importPath: resolvedImportPath,\n paramsType,\n paramsCasing,\n pathParamsType,\n baseURL,\n urlType,\n },\n pre: [pluginOasName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),\n resolvePath(baseName, pathMode, options) {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = pathMode ?? getMode(path.resolve(root, output.path))\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: Group['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 const resolvedName = camelCase(name, { isFile: type === 'file' })\n\n if (type) {\n return transformers?.name?.(resolvedName, type) || resolvedName\n }\n\n return resolvedName\n },\n async install() {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = getMode(path.resolve(root, output.path))\n const oas = await this.getOas()\n const baseURL = await this.getBaseURL()\n\n // pre add bundled fetch\n if (bundle && !this.plugin.options.importPath) {\n await this.addFile({\n baseName: 'fetch.ts',\n path: path.resolve(root, '.kubb/fetch.ts'),\n sources: [\n {\n name: 'fetch',\n value: resolveModuleSource(\n this.plugin.options.client === 'fetch' ? '@kubb/plugin-client/templates/clients/fetch' : '@kubb/plugin-client/templates/clients/axios',\n ).source,\n isExportable: true,\n isIndexable: true,\n },\n ],\n imports: [],\n exports: [],\n })\n }\n\n await this.addFile({\n baseName: 'config.ts',\n path: path.resolve(root, '.kubb/config.ts'),\n sources: [\n {\n name: 'config',\n value: resolveModuleSource('@kubb/plugin-client/templates/config').source,\n isExportable: false,\n isIndexable: false,\n },\n ],\n imports: [],\n exports: [],\n })\n\n const operationGenerator = new OperationGenerator(\n baseURL\n ? {\n ...this.plugin.options,\n baseURL,\n }\n : this.plugin.options,\n {\n fabric: this.fabric,\n oas,\n pluginManager: this.pluginManager,\n events: this.events,\n plugin: this.plugin,\n contentType,\n exclude,\n include,\n override,\n mode,\n },\n )\n\n const files = await operationGenerator.build(...generators)\n\n await this.upsertFile(...files)\n\n const barrelFiles = await getBarrelFiles(this.fabric.files, {\n type: output.barrelType ?? 'named',\n root,\n output,\n meta: {\n pluginKey: this.plugin.key,\n },\n })\n\n await this.upsertFile(...barrelFiles)\n },\n }\n})\n"],"mappings":"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"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as __name } from "./chunk-DlpkT3g-.cjs";
|
|
2
2
|
import * as oas_normalize_lib_types0 from "oas-normalize/lib/types";
|
|
3
3
|
import BaseOas from "oas";
|
|
4
4
|
import { Operation } from "oas/operation";
|
|
5
5
|
import { DiscriminatorObject, HttpMethods, OASDocument, SchemaObject } from "oas/types";
|
|
6
6
|
import { KubbFile } from "@kubb/fabric-core/types";
|
|
7
|
+
import { Fabric } from "@kubb/react-fabric";
|
|
7
8
|
import { KubbNode } from "@kubb/react-fabric/types";
|
|
8
9
|
|
|
9
10
|
//#region ../oas/src/types.d.ts
|
|
@@ -1269,9 +1270,10 @@ type Options = {
|
|
|
1269
1270
|
* How to generate the client code
|
|
1270
1271
|
* - 'function' will generate standalone functions for each operation.
|
|
1271
1272
|
* - 'class' will generate a class with methods for each operation.
|
|
1273
|
+
* - 'staticClass' will generate a class with static methods for each operation.
|
|
1272
1274
|
* @default 'function'
|
|
1273
1275
|
*/
|
|
1274
|
-
clientType?: 'function' | 'class';
|
|
1276
|
+
clientType?: 'function' | 'class' | 'staticClass';
|
|
1275
1277
|
/**
|
|
1276
1278
|
* Bundle the selected client into the generated `.kubb` directory.
|
|
1277
1279
|
* When disabled the generated clients will import the shared runtime from `@kubb/plugin-client/clients/*`.
|
|
@@ -1308,4 +1310,4 @@ type ResolvedOptions = {
|
|
|
1308
1310
|
type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, never, ResolvePathOptions>;
|
|
1309
1311
|
//#endregion
|
|
1310
1312
|
export { UserPluginWithLifeCycle as a, OperationSchemas as i, PluginClient as n, Operation$1 as o, ReactGenerator as r, Options as t };
|
|
1311
|
-
//# sourceMappingURL=types-
|
|
1313
|
+
//# sourceMappingURL=types-Chp-emXk.d.cts.map
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { t as __name } from "./chunk-iVr_oF3V.js";
|
|
2
|
+
import { Fabric } from "@kubb/react-fabric";
|
|
1
3
|
import * as oas_normalize_lib_types0 from "oas-normalize/lib/types";
|
|
2
4
|
import BaseOas from "oas";
|
|
3
5
|
import { Operation } from "oas/operation";
|
|
4
6
|
import { DiscriminatorObject, HttpMethods, OASDocument, SchemaObject } from "oas/types";
|
|
5
7
|
import { KubbFile } from "@kubb/fabric-core/types";
|
|
6
|
-
import { Fabric } from "@kubb/react-fabric";
|
|
7
8
|
import { KubbNode } from "@kubb/react-fabric/types";
|
|
8
9
|
|
|
9
10
|
//#region ../oas/src/types.d.ts
|
|
@@ -1269,9 +1270,10 @@ type Options = {
|
|
|
1269
1270
|
* How to generate the client code
|
|
1270
1271
|
* - 'function' will generate standalone functions for each operation.
|
|
1271
1272
|
* - 'class' will generate a class with methods for each operation.
|
|
1273
|
+
* - 'staticClass' will generate a class with static methods for each operation.
|
|
1272
1274
|
* @default 'function'
|
|
1273
1275
|
*/
|
|
1274
|
-
clientType?: 'function' | 'class';
|
|
1276
|
+
clientType?: 'function' | 'class' | 'staticClass';
|
|
1275
1277
|
/**
|
|
1276
1278
|
* Bundle the selected client into the generated `.kubb` directory.
|
|
1277
1279
|
* When disabled the generated clients will import the shared runtime from `@kubb/plugin-client/clients/*`.
|
|
@@ -1308,4 +1310,4 @@ type ResolvedOptions = {
|
|
|
1308
1310
|
type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, never, ResolvePathOptions>;
|
|
1309
1311
|
//#endregion
|
|
1310
1312
|
export { UserPluginWithLifeCycle as a, OperationSchemas as i, PluginClient as n, Operation$1 as o, ReactGenerator as r, Options as t };
|
|
1311
|
-
//# sourceMappingURL=types-
|
|
1313
|
+
//# sourceMappingURL=types-aqsuGVmg.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-client",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.18.0",
|
|
4
4
|
"description": "API client generator plugin for Kubb, creating type-safe HTTP clients (Axios, Fetch) from OpenAPI specifications for making API requests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api-client",
|
|
@@ -86,18 +86,18 @@
|
|
|
86
86
|
}
|
|
87
87
|
],
|
|
88
88
|
"dependencies": {
|
|
89
|
-
"@kubb/react-fabric": "0.
|
|
90
|
-
"@kubb/core": "4.
|
|
91
|
-
"@kubb/oas": "4.
|
|
92
|
-
"@kubb/plugin-oas": "4.
|
|
93
|
-
"@kubb/plugin-ts": "4.
|
|
94
|
-
"@kubb/plugin-zod": "4.
|
|
89
|
+
"@kubb/react-fabric": "0.11.8",
|
|
90
|
+
"@kubb/core": "4.18.0",
|
|
91
|
+
"@kubb/oas": "4.18.0",
|
|
92
|
+
"@kubb/plugin-oas": "4.18.0",
|
|
93
|
+
"@kubb/plugin-ts": "4.18.0",
|
|
94
|
+
"@kubb/plugin-zod": "4.18.0"
|
|
95
95
|
},
|
|
96
96
|
"devDependencies": {
|
|
97
97
|
"axios": "^1.13.2"
|
|
98
98
|
},
|
|
99
99
|
"peerDependencies": {
|
|
100
|
-
"@kubb/react-fabric": "0.
|
|
100
|
+
"@kubb/react-fabric": "0.11.8",
|
|
101
101
|
"axios": "^1.7.2"
|
|
102
102
|
},
|
|
103
103
|
"peerDependenciesMeta": {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { URLPath } from '@kubb/core/utils'
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { getDefaultValue, isOptional, type Operation } from '@kubb/oas'
|
|
4
4
|
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
5
5
|
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
6
6
|
import { File, Function, FunctionParams } from '@kubb/react-fabric'
|
|
@@ -90,7 +90,7 @@ function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas, isCo
|
|
|
90
90
|
? {
|
|
91
91
|
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
92
92
|
children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
|
|
93
|
-
default:
|
|
93
|
+
default: getDefaultValue(typeSchemas.pathParams?.schema),
|
|
94
94
|
}
|
|
95
95
|
: undefined,
|
|
96
96
|
data: typeSchemas.request?.name
|
|
@@ -206,33 +206,37 @@ export function Client({
|
|
|
206
206
|
)
|
|
207
207
|
|
|
208
208
|
return (
|
|
209
|
-
|
|
210
|
-
<
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
209
|
+
<>
|
|
210
|
+
<br />
|
|
211
|
+
|
|
212
|
+
<File.Source name={name} isExportable={isExportable} isIndexable={isIndexable}>
|
|
213
|
+
<Function
|
|
214
|
+
name={name}
|
|
215
|
+
async
|
|
216
|
+
export={isExportable}
|
|
217
|
+
params={params.toConstructor()}
|
|
218
|
+
JSDoc={{
|
|
219
|
+
comments: getComments(operation),
|
|
220
|
+
}}
|
|
221
|
+
returnType={returnType}
|
|
222
|
+
>
|
|
223
|
+
{isConfigurable ? 'const { client: request = fetch, ...requestConfig } = config' : ''}
|
|
224
|
+
<br />
|
|
225
|
+
<br />
|
|
226
|
+
{parser === 'zod' && zodSchemas?.request?.name
|
|
227
|
+
? `const requestData = ${zodSchemas.request.name}.parse(data)`
|
|
228
|
+
: typeSchemas?.request?.name && 'const requestData = data'}
|
|
229
|
+
<br />
|
|
230
|
+
{isFormData && typeSchemas?.request?.name && 'const formData = buildFormData(requestData)'}
|
|
231
|
+
<br />
|
|
232
|
+
{isConfigurable
|
|
233
|
+
? `const res = await request<${generics.join(', ')}>(${clientParams.toCall()})`
|
|
234
|
+
: `const res = await fetch<${generics.join(', ')}>(${clientParams.toCall()})`}
|
|
235
|
+
<br />
|
|
236
|
+
{childrenElement}
|
|
237
|
+
</Function>
|
|
238
|
+
</File.Source>
|
|
239
|
+
</>
|
|
236
240
|
)
|
|
237
241
|
}
|
|
238
242
|
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { buildJSDoc, URLPath } from '@kubb/core/utils'
|
|
2
|
+
import type { Operation } from '@kubb/oas'
|
|
3
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
4
|
+
import { getComments } from '@kubb/plugin-oas/utils'
|
|
5
|
+
import { File, FunctionParams } from '@kubb/react-fabric'
|
|
6
|
+
import type { KubbNode } from '@kubb/react-fabric/types'
|
|
7
|
+
import type { PluginClient } from '../types.ts'
|
|
8
|
+
import { Client } from './Client.tsx'
|
|
9
|
+
|
|
10
|
+
type Props = {
|
|
11
|
+
name: string
|
|
12
|
+
isExportable?: boolean
|
|
13
|
+
isIndexable?: boolean
|
|
14
|
+
operations: Array<{
|
|
15
|
+
operation: Operation
|
|
16
|
+
name: string
|
|
17
|
+
typeSchemas: OperationSchemas
|
|
18
|
+
zodSchemas: OperationSchemas | undefined
|
|
19
|
+
}>
|
|
20
|
+
baseURL: string | undefined
|
|
21
|
+
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
22
|
+
paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
|
|
23
|
+
paramsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
24
|
+
pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
25
|
+
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
26
|
+
children?: KubbNode
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type GenerateMethodProps = {
|
|
30
|
+
operation: Operation
|
|
31
|
+
name: string
|
|
32
|
+
typeSchemas: OperationSchemas
|
|
33
|
+
zodSchemas: OperationSchemas | undefined
|
|
34
|
+
baseURL: string | undefined
|
|
35
|
+
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
36
|
+
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
37
|
+
paramsType: PluginClient['resolvedOptions']['paramsType']
|
|
38
|
+
paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
|
|
39
|
+
pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function buildHeaders(contentType: string, hasHeaderParams: boolean): Array<string> {
|
|
43
|
+
return [
|
|
44
|
+
contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` : undefined,
|
|
45
|
+
hasHeaderParams ? '...headers' : undefined,
|
|
46
|
+
].filter(Boolean) as Array<string>
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildGenerics(typeSchemas: OperationSchemas): Array<string> {
|
|
50
|
+
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
51
|
+
return [typeSchemas.response.name, TError, typeSchemas.request?.name || 'unknown'].filter(Boolean)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function buildClientParams({
|
|
55
|
+
operation,
|
|
56
|
+
path,
|
|
57
|
+
baseURL,
|
|
58
|
+
typeSchemas,
|
|
59
|
+
isFormData,
|
|
60
|
+
headers,
|
|
61
|
+
}: {
|
|
62
|
+
operation: Operation
|
|
63
|
+
path: URLPath
|
|
64
|
+
baseURL: string | undefined
|
|
65
|
+
typeSchemas: OperationSchemas
|
|
66
|
+
isFormData: boolean
|
|
67
|
+
headers: Array<string>
|
|
68
|
+
}) {
|
|
69
|
+
return FunctionParams.factory({
|
|
70
|
+
config: {
|
|
71
|
+
mode: 'object',
|
|
72
|
+
children: {
|
|
73
|
+
method: {
|
|
74
|
+
value: JSON.stringify(operation.method.toUpperCase()),
|
|
75
|
+
},
|
|
76
|
+
url: {
|
|
77
|
+
value: path.template,
|
|
78
|
+
},
|
|
79
|
+
baseURL: baseURL
|
|
80
|
+
? {
|
|
81
|
+
value: JSON.stringify(baseURL),
|
|
82
|
+
}
|
|
83
|
+
: undefined,
|
|
84
|
+
params: typeSchemas.queryParams?.name ? {} : undefined,
|
|
85
|
+
data: typeSchemas.request?.name
|
|
86
|
+
? {
|
|
87
|
+
value: isFormData ? 'formData as FormData' : 'requestData',
|
|
88
|
+
}
|
|
89
|
+
: undefined,
|
|
90
|
+
requestConfig: {
|
|
91
|
+
mode: 'inlineSpread',
|
|
92
|
+
},
|
|
93
|
+
headers: headers.length
|
|
94
|
+
? {
|
|
95
|
+
value: `{ ${headers.join(', ')}, ...requestConfig.headers }`,
|
|
96
|
+
}
|
|
97
|
+
: undefined,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function buildRequestDataLine({
|
|
104
|
+
parser,
|
|
105
|
+
zodSchemas,
|
|
106
|
+
typeSchemas,
|
|
107
|
+
}: {
|
|
108
|
+
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
109
|
+
zodSchemas: OperationSchemas | undefined
|
|
110
|
+
typeSchemas: OperationSchemas
|
|
111
|
+
}): string {
|
|
112
|
+
if (parser === 'zod' && zodSchemas?.request?.name) {
|
|
113
|
+
return `const requestData = ${zodSchemas.request.name}.parse(data)`
|
|
114
|
+
}
|
|
115
|
+
if (typeSchemas?.request?.name) {
|
|
116
|
+
return 'const requestData = data'
|
|
117
|
+
}
|
|
118
|
+
return ''
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function buildFormDataLine(isFormData: boolean, hasRequest: boolean): string {
|
|
122
|
+
return isFormData && hasRequest ? 'const formData = buildFormData(requestData)' : ''
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function buildReturnStatement({
|
|
126
|
+
dataReturnType,
|
|
127
|
+
parser,
|
|
128
|
+
zodSchemas,
|
|
129
|
+
}: {
|
|
130
|
+
dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
|
|
131
|
+
parser: PluginClient['resolvedOptions']['parser'] | undefined
|
|
132
|
+
zodSchemas: OperationSchemas | undefined
|
|
133
|
+
}): string {
|
|
134
|
+
if (dataReturnType === 'full' && parser === 'zod' && zodSchemas) {
|
|
135
|
+
return `return {...res, data: ${zodSchemas.response.name}.parse(res.data)}`
|
|
136
|
+
}
|
|
137
|
+
if (dataReturnType === 'data' && parser === 'zod' && zodSchemas) {
|
|
138
|
+
return `return ${zodSchemas.response.name}.parse(res.data)`
|
|
139
|
+
}
|
|
140
|
+
if (dataReturnType === 'full' && parser === 'client') {
|
|
141
|
+
return 'return res'
|
|
142
|
+
}
|
|
143
|
+
return 'return res.data'
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function generateMethod({
|
|
147
|
+
operation,
|
|
148
|
+
name,
|
|
149
|
+
typeSchemas,
|
|
150
|
+
zodSchemas,
|
|
151
|
+
baseURL,
|
|
152
|
+
dataReturnType,
|
|
153
|
+
parser,
|
|
154
|
+
paramsType,
|
|
155
|
+
paramsCasing,
|
|
156
|
+
pathParamsType,
|
|
157
|
+
}: GenerateMethodProps): string {
|
|
158
|
+
const path = new URLPath(operation.path, { casing: paramsCasing })
|
|
159
|
+
const contentType = operation.getContentType()
|
|
160
|
+
const isFormData = contentType === 'multipart/form-data'
|
|
161
|
+
const headers = buildHeaders(contentType, !!typeSchemas.headerParams?.name)
|
|
162
|
+
const generics = buildGenerics(typeSchemas)
|
|
163
|
+
const params = Client.getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas, isConfigurable: true })
|
|
164
|
+
const clientParams = buildClientParams({ operation, path, baseURL, typeSchemas, isFormData, headers })
|
|
165
|
+
const jsdoc = buildJSDoc(getComments(operation))
|
|
166
|
+
|
|
167
|
+
const requestDataLine = buildRequestDataLine({ parser, zodSchemas, typeSchemas })
|
|
168
|
+
const formDataLine = buildFormDataLine(isFormData, !!typeSchemas?.request?.name)
|
|
169
|
+
const returnStatement = buildReturnStatement({ dataReturnType, parser, zodSchemas })
|
|
170
|
+
|
|
171
|
+
const methodBody = [
|
|
172
|
+
'const { client: request = this.#client, ...requestConfig } = config',
|
|
173
|
+
'',
|
|
174
|
+
requestDataLine,
|
|
175
|
+
formDataLine,
|
|
176
|
+
`const res = await request<${generics.join(', ')}>(${clientParams.toCall()})`,
|
|
177
|
+
returnStatement,
|
|
178
|
+
]
|
|
179
|
+
.filter(Boolean)
|
|
180
|
+
.map((line) => ` ${line}`)
|
|
181
|
+
.join('\n')
|
|
182
|
+
|
|
183
|
+
// Indent static method by 2 spaces, body by 4 spaces (matching snapshot)
|
|
184
|
+
return `${jsdoc} static async ${name}(${params.toConstructor()}) {\n${methodBody}\n }`
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function StaticClassClient({
|
|
188
|
+
name,
|
|
189
|
+
isExportable = true,
|
|
190
|
+
isIndexable = true,
|
|
191
|
+
operations,
|
|
192
|
+
baseURL,
|
|
193
|
+
dataReturnType,
|
|
194
|
+
parser,
|
|
195
|
+
paramsType,
|
|
196
|
+
paramsCasing,
|
|
197
|
+
pathParamsType,
|
|
198
|
+
children,
|
|
199
|
+
}: Props): KubbNode {
|
|
200
|
+
const methods = operations.map(({ operation, name: methodName, typeSchemas, zodSchemas }) =>
|
|
201
|
+
generateMethod({
|
|
202
|
+
operation,
|
|
203
|
+
name: methodName,
|
|
204
|
+
typeSchemas,
|
|
205
|
+
zodSchemas,
|
|
206
|
+
baseURL,
|
|
207
|
+
dataReturnType,
|
|
208
|
+
parser,
|
|
209
|
+
paramsType,
|
|
210
|
+
paramsCasing,
|
|
211
|
+
pathParamsType,
|
|
212
|
+
}),
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
const classCode = `export class ${name} {\n static #client: typeof fetch = fetch\n\n${methods.join('\n\n')}\n}`
|
|
216
|
+
|
|
217
|
+
return (
|
|
218
|
+
<File.Source name={name} isExportable={isExportable} isIndexable={isIndexable}>
|
|
219
|
+
{classCode}
|
|
220
|
+
{children}
|
|
221
|
+
</File.Source>
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
StaticClassClient.getParams = Client.getParams
|