@kubb/plugin-msw
Version:
Mock Service Worker (MSW) handlers generator plugin for Kubb, creating API mocks from OpenAPI specifications for frontend development and testing.
174 lines (171 loc) • 6.07 kB
JavaScript
const require_components = require('./components-N4uAuUW1.cjs');
const require_index = require('./index.cjs');
let _kubb_plugin_faker = require("@kubb/plugin-faker");
let _kubb_plugin_ts = require("@kubb/plugin-ts");
let _kubb_core_hooks = require("@kubb/core/hooks");
let _kubb_plugin_oas_generators = require("@kubb/plugin-oas/generators");
let _kubb_plugin_oas_hooks = require("@kubb/plugin-oas/hooks");
let _kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils");
let _kubb_react_fabric = require("@kubb/react-fabric");
let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime");
//#region src/generators/handlersGenerator.tsx
const handlersGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({
name: "plugin-msw",
Operations({ operations, generator, plugin }) {
const pluginManager = (0, _kubb_core_hooks.usePluginManager)();
const oas = (0, _kubb_plugin_oas_hooks.useOas)();
const { getName, getFile } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator);
const file = pluginManager.getFile({
name: "handlers",
extname: ".ts",
pluginKey: plugin.key
});
const imports = operations.map((operation) => {
const operationFile = getFile(operation, { pluginKey: plugin.key });
const operationName = getName(operation, {
pluginKey: plugin.key,
type: "function"
});
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: [operationName],
root: file.path,
path: operationFile.path
}, operationFile.path);
});
const handlers = operations.map((operation) => `${getName(operation, {
type: "function",
pluginKey: plugin.key
})}()`);
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
baseName: file.baseName,
path: file.path,
meta: file.meta,
banner: (0, _kubb_plugin_oas_utils.getBanner)({
oas,
output: plugin.options.output,
config: pluginManager.config
}),
footer: (0, _kubb_plugin_oas_utils.getFooter)({
oas,
output: plugin.options.output
}),
children: [imports, /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_components.Handlers, {
name: "handlers",
handlers
})]
});
}
});
//#endregion
//#region src/generators/mswGenerator.tsx
const mswGenerator = (0, _kubb_plugin_oas_generators.createReactGenerator)({
name: "msw",
Operation({ operation, generator, plugin }) {
const { options: { output, parser, baseURL } } = plugin;
const pluginManager = (0, _kubb_core_hooks.usePluginManager)();
const oas = (0, _kubb_plugin_oas_hooks.useOas)();
const { getSchemas, getName, getFile } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator);
const mock = {
name: getName(operation, { type: "function" }),
file: getFile(operation)
};
const faker = {
file: getFile(operation, { pluginKey: [_kubb_plugin_faker.pluginFakerName] }),
schemas: getSchemas(operation, {
pluginKey: [_kubb_plugin_faker.pluginFakerName],
type: "function"
})
};
const type = {
file: getFile(operation, { pluginKey: [_kubb_plugin_ts.pluginTsName] }),
schemas: getSchemas(operation, {
pluginKey: [_kubb_plugin_ts.pluginTsName],
type: "type"
})
};
const responseStatusCodes = operation.getResponseStatusCodes();
const types = [];
for (const code of responseStatusCodes) {
if (code === "default") {
types.push(["default", type.schemas.response.name]);
continue;
}
if (code.startsWith("2")) {
types.push([Number(code), type.schemas.response.name]);
continue;
}
const codeType = type.schemas.errors?.find((err) => err.statusCode === Number(code));
if (codeType) types.push([Number(code), codeType.name]);
}
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
baseName: mock.file.baseName,
path: mock.file.path,
meta: mock.file.meta,
banner: (0, _kubb_plugin_oas_utils.getBanner)({
oas,
output,
config: pluginManager.config
}),
footer: (0, _kubb_plugin_oas_utils.getFooter)({
oas,
output
}),
children: [
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["http"],
path: "msw"
}),
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: ["ResponseResolver"],
isTypeOnly: true,
path: "msw"
}),
/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: Array.from(new Set([type.schemas.response.name, ...types.map((t) => t[1])])),
path: type.file.path,
root: mock.file.path,
isTypeOnly: true
}),
parser === "faker" && faker.file && faker.schemas.response && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
name: [faker.schemas.response.name],
root: mock.file.path,
path: faker.file.path
}),
types.filter(([code]) => code !== "default").map(([code, typeName]) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_components.Response, {
typeName,
operation,
name: mock.name,
statusCode: code
})),
parser === "faker" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_components.MockWithFaker, {
name: mock.name,
typeName: type.schemas.response.name,
fakerName: faker.schemas.response.name,
operation,
baseURL
}),
parser === "data" && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_components.Mock, {
name: mock.name,
typeName: type.schemas.response.name,
fakerName: faker.schemas.response.name,
operation,
baseURL
})
]
});
}
});
//#endregion
Object.defineProperty(exports, 'handlersGenerator', {
enumerable: true,
get: function () {
return handlersGenerator;
}
});
Object.defineProperty(exports, 'mswGenerator', {
enumerable: true,
get: function () {
return mswGenerator;
}
});
//# sourceMappingURL=generators-CTtbmbWc.cjs.map