@kubb/plugin-cypress
Version:
Cypress test generator plugin for Kubb, creating end-to-end tests from OpenAPI specifications for automated API testing.
79 lines (77 loc) • 3 kB
JavaScript
import { t as cypressGenerator } from "./generators-3ldN506W.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 { pluginTsName } from "@kubb/plugin-ts";
//#region src/plugin.ts
const pluginCypressName = "plugin-cypress";
const pluginCypress = definePlugin((options) => {
const { output = {
path: "cypress",
barrelType: "named"
}, group, dataReturnType = "data", exclude = [], include, override = [], transformers = {}, generators = [cypressGenerator].filter(Boolean), contentType, baseURL, paramsCasing = "camelcase", paramsType = "inline", pathParamsType = paramsType === "object" ? "object" : options.pathParamsType || "inline" } = options;
return {
name: pluginCypressName,
options: {
output,
dataReturnType,
group,
baseURL,
paramsCasing,
paramsType,
pathParamsType
},
pre: [pluginOasName, pluginTsName].filter(Boolean),
resolvePath(baseName, pathMode, options$1) {
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$1?.group?.path || options$1?.group?.tag)) {
const groupName = group?.name ? group.name : (ctx) => {
if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
return `${camelCase(ctx.group)}Requests`;
};
return path.resolve(root, output.path, groupName({ group: group.type === "path" ? options$1.group.path : options$1.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 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 { pluginCypress, pluginCypressName };
//# sourceMappingURL=index.js.map