UNPKG

@kubb/plugin-client

Version:

API client generator plugin for Kubb, creating type-safe HTTP clients (Axios, Fetch) from OpenAPI specifications for making API requests.

124 lines (122 loc) 4.72 kB
import { t as __name } from "./chunk-DKWOrOAv.js"; import { a as classClientGenerator, i as clientGenerator, n as operationsGenerator, r as groupedClientGenerator, t as staticClassClientGenerator } from "./staticClassClientGenerator-BUGBMkNO.js"; import { source } from "./templates/clients/axios.source.js"; import { source as source$1 } from "./templates/clients/fetch.source.js"; import { source as source$2 } from "./templates/config.source.js"; import path from "node:path"; import { definePlugin, getBarrelFiles, getMode } from "@kubb/core"; import { camelCase } from "@kubb/core/transformers"; import { OperationGenerator, pluginOasName } from "@kubb/plugin-oas"; import { pluginZodName } from "@kubb/plugin-zod"; //#region src/plugin.ts const pluginClientName = "plugin-client"; const pluginClient = definePlugin((options) => { const { output = { path: "clients", barrelType: "named" }, 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; const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : void 0); const defaultGenerators = [ clientType === "staticClass" ? staticClassClientGenerator : clientType === "class" ? classClientGenerator : clientGenerator, group && clientType === "function" ? groupedClientGenerator : void 0, operations ? operationsGenerator : void 0 ].filter(Boolean); const generators = options.generators ?? defaultGenerators; return { name: pluginClientName, options: { client, clientType, bundle, output, group, parser, dataReturnType, importPath: resolvedImportPath, paramsType, paramsCasing, pathParamsType, baseURL, urlType }, pre: [pluginOasName, parser === "zod" ? pluginZodName : void 0].filter(Boolean), resolvePath(baseName, pathMode, options) { const root = path.resolve(this.config.root, this.config.output.path); if ((pathMode ?? getMode(path.resolve(root, output.path))) === "single") /** * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend * Other plugins then need to call addOrAppend instead of just add from the fileManager class */ return path.resolve(root, output.path); if (group && (options?.group?.path || options?.group?.tag)) { const groupName = group?.name ? group.name : (ctx) => { if (group?.type === "path") return `${ctx.group.split("/")[1]}`; return `${camelCase(ctx.group)}Controller`; }; return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName); } return path.resolve(root, output.path, baseName); }, resolveName(name, type) { const resolvedName = camelCase(name, { isFile: type === "file" }); if (type) return transformers?.name?.(resolvedName, type) || resolvedName; return resolvedName; }, async install() { const root = path.resolve(this.config.root, this.config.output.path); const mode = getMode(path.resolve(root, output.path)); const oas = await this.getOas(); const baseURL = await this.getBaseURL(); if (bundle && !this.plugin.options.importPath) await this.addFile({ baseName: "fetch.ts", path: path.resolve(root, ".kubb/fetch.ts"), sources: [{ name: "fetch", value: this.plugin.options.client === "fetch" ? source$1 : source, isExportable: true, isIndexable: true }], imports: [], exports: [] }); await this.addFile({ baseName: "config.ts", path: path.resolve(root, ".kubb/config.ts"), sources: [{ name: "config", value: source$2, isExportable: false, isIndexable: false }], imports: [], exports: [] }); const files = await new OperationGenerator(baseURL ? { ...this.plugin.options, baseURL } : this.plugin.options, { fabric: this.fabric, oas, pluginManager: this.pluginManager, events: this.events, plugin: this.plugin, contentType, exclude, include, override, mode }).build(...generators); await this.upsertFile(...files); const barrelFiles = await getBarrelFiles(this.fabric.files, { type: output.barrelType ?? "named", root, output, meta: { pluginKey: this.plugin.key } }); await this.upsertFile(...barrelFiles); } }; }); //#endregion export { pluginClient, pluginClientName }; //# sourceMappingURL=index.js.map