@kubb/plugin-swr
Version:
SWR hooks generator plugin for Kubb, creating type-safe data fetching hooks from OpenAPI specifications for React and Next.js applications.
146 lines (144 loc) • 5.35 kB
JavaScript
import { t as __name } from "./chunk-DKWOrOAv.js";
import { a as MutationKey, r as QueryKey } from "./Query-DDIFmxNc.js";
import { n as mutationGenerator, t as queryGenerator } from "./queryGenerator-CEsWWhOe.js";
import path from "node:path";
import { definePlugin, getBarrelFiles, getMode } from "@kubb/core";
import { camelCase, pascalCase } from "@kubb/core/transformers";
import { pluginClientName } from "@kubb/plugin-client";
import { source } from "@kubb/plugin-client/templates/clients/axios.source";
import { source as source$1 } from "@kubb/plugin-client/templates/clients/fetch.source";
import { source as source$2 } from "@kubb/plugin-client/templates/config.source";
import { OperationGenerator, pluginOasName } from "@kubb/plugin-oas";
import { pluginTsName } from "@kubb/plugin-ts";
import { pluginZodName } from "@kubb/plugin-zod";
//#region src/plugin.ts
const pluginSwrName = "plugin-swr";
const pluginSwr = definePlugin((options) => {
const { output = {
path: "hooks",
barrelType: "named"
}, 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 = [queryGenerator, mutationGenerator].filter(Boolean), paramsCasing, contentType } = options;
const clientName = client?.client ?? "axios";
const clientImportPath = client?.importPath ?? (!client?.bundle ? `/plugin-client/clients/${clientName}` : void 0);
return {
name: pluginSwrName,
options: {
output,
client: {
bundle: client?.bundle,
baseURL: client?.baseURL,
client: clientName,
clientType: client?.clientType ?? "function",
importPath: clientImportPath,
dataReturnType: client?.dataReturnType ?? "data",
paramsCasing
},
queryKey,
query: query === false ? false : {
importPath: "swr",
methods: ["get"],
...query
},
mutationKey,
mutation: mutation === false ? false : {
importPath: "swr/mutation",
methods: [
"post",
"put",
"delete",
"patch"
],
...mutation
},
parser,
paramsType,
pathParamsType,
paramsCasing,
group
},
pre: [
pluginOasName,
pluginTsName,
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) {
let resolvedName = camelCase(name);
if (type === "file" || type === "function") resolvedName = camelCase(name, { isFile: type === "file" });
if (type === "type") resolvedName = pascalCase(name);
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 (baseURL) this.plugin.options.client.baseURL = baseURL;
const hasClientPlugin = !!this.pluginManager.getPluginByKey([pluginClientName]);
if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) await this.upsertFile({
baseName: "fetch.ts",
path: path.resolve(root, ".kubb/fetch.ts"),
sources: [{
name: "fetch",
value: this.plugin.options.client.client === "fetch" ? source$1 : source,
isExportable: true,
isIndexable: true
}],
imports: [],
exports: []
});
if (!hasClientPlugin) 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(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 { pluginSwr, pluginSwrName };
//# sourceMappingURL=index.js.map