UNPKG

@kubb/plugin-msw

Version:

Mock Service Worker (MSW) handlers generator plugin for Kubb, creating API mocks from OpenAPI specifications for frontend development and testing.

162 lines (159 loc) 4.82 kB
import { i as Handlers, n as MockWithFaker, r as Mock, t as Response } from "./components-C5_yzPIo.js"; import { pluginFakerName } from "@kubb/plugin-faker"; import { pluginTsName } from "@kubb/plugin-ts"; import { usePluginManager } from "@kubb/core/hooks"; import { createReactGenerator } from "@kubb/plugin-oas/generators"; import { useOas, useOperationManager } from "@kubb/plugin-oas/hooks"; import { getBanner, getFooter } from "@kubb/plugin-oas/utils"; import { File } from "@kubb/react-fabric"; import { jsx, jsxs } from "@kubb/react-fabric/jsx-runtime"; //#region src/generators/handlersGenerator.tsx const handlersGenerator = createReactGenerator({ name: "plugin-msw", Operations({ operations, generator, plugin }) { const pluginManager = usePluginManager(); const oas = useOas(); const { getName, getFile } = 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__ */ jsx(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__ */ jsxs(File, { baseName: file.baseName, path: file.path, meta: file.meta, banner: getBanner({ oas, output: plugin.options.output, config: pluginManager.config }), footer: getFooter({ oas, output: plugin.options.output }), children: [imports, /* @__PURE__ */ jsx(Handlers, { name: "handlers", handlers })] }); } }); //#endregion //#region src/generators/mswGenerator.tsx const mswGenerator = createReactGenerator({ name: "msw", Operation({ operation, generator, plugin }) { const { options: { output, parser, baseURL } } = plugin; const pluginManager = usePluginManager(); const oas = useOas(); const { getSchemas, getName, getFile } = useOperationManager(generator); const mock = { name: getName(operation, { type: "function" }), file: getFile(operation) }; const faker = { file: getFile(operation, { pluginKey: [pluginFakerName] }), schemas: getSchemas(operation, { pluginKey: [pluginFakerName], type: "function" }) }; const type = { file: getFile(operation, { pluginKey: [pluginTsName] }), schemas: getSchemas(operation, { pluginKey: [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__ */ jsxs(File, { baseName: mock.file.baseName, path: mock.file.path, meta: mock.file.meta, banner: getBanner({ oas, output, config: pluginManager.config }), footer: getFooter({ oas, output }), children: [ /* @__PURE__ */ jsx(File.Import, { name: ["http"], path: "msw" }), /* @__PURE__ */ jsx(File.Import, { name: ["ResponseResolver"], isTypeOnly: true, path: "msw" }), /* @__PURE__ */ jsx(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__ */ jsx(File.Import, { name: [faker.schemas.response.name], root: mock.file.path, path: faker.file.path }), types.filter(([code]) => code !== "default").map(([code, typeName]) => /* @__PURE__ */ jsx(Response, { typeName, operation, name: mock.name, statusCode: code })), parser === "faker" && /* @__PURE__ */ jsx(MockWithFaker, { name: mock.name, typeName: type.schemas.response.name, fakerName: faker.schemas.response.name, operation, baseURL }), parser === "data" && /* @__PURE__ */ jsx(Mock, { name: mock.name, typeName: type.schemas.response.name, fakerName: faker.schemas.response.name, operation, baseURL }) ] }); } }); //#endregion export { handlersGenerator as n, mswGenerator as t }; //# sourceMappingURL=generators-D36jd0kA.js.map