@kubb/plugin-msw
Version:
Mock Service Worker (MSW) handlers generator plugin for Kubb, creating API mocks from OpenAPI specifications for frontend development and testing.
140 lines (137 loc) • 3.9 kB
JavaScript
import { Handlers, Mock, MockWithFaker } from "./components-ByUOezvw.js";
import { createReactGenerator } from "@kubb/plugin-oas";
import { pluginFakerName } from "@kubb/plugin-faker";
import { pluginTsName } from "@kubb/plugin-ts";
import { useOas, useOperationManager } from "@kubb/plugin-oas/hooks";
import { getBanner, getFooter } from "@kubb/plugin-oas/utils";
import { File, useApp } from "@kubb/react";
import { jsx, jsxs } from "@kubb/react/jsx-runtime";
//#region src/generators/mswGenerator.tsx
const mswGenerator = createReactGenerator({
name: "msw",
Operation({ operation }) {
const { pluginManager, plugin: { options: { output, parser, baseURL } } } = useApp();
const oas = useOas();
const { getSchemas, getName, getFile } = useOperationManager();
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"
})
};
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: [type.schemas.response.name],
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
}),
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
//#region src/generators/handlersGenerator.tsx
const handlersGenerator = createReactGenerator({
name: "plugin-msw",
Operations({ operations }) {
const { pluginManager, plugin } = useApp();
const oas = useOas();
const { getName, getFile } = useOperationManager();
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
export { handlersGenerator, mswGenerator };
//# sourceMappingURL=generators-BjQcx0SS.js.map