@kubb/plugin-msw
Version:
Mock Service Worker (MSW) handlers generator plugin for Kubb, creating API mocks from OpenAPI specifications for frontend development and testing.
93 lines (87 loc) • 3.38 kB
JavaScript
import { File, Function, FunctionParams } from "@kubb/react";
import { URLPath } from "@kubb/core/utils";
import { jsx } from "@kubb/react/jsx-runtime";
//#region src/components/Mock.tsx
function Mock({ baseURL = "", name, typeName, operation }) {
const method = operation.method;
const successStatusCodes = operation.getResponseStatusCodes().filter((code) => code.startsWith("2"));
const statusCode = successStatusCodes.length > 0 ? Number(successStatusCodes[0]) : 200;
const responseObject = operation.getResponseByStatusCode(statusCode);
const contentType = Object.keys(responseObject.content || {})?.[0];
const url = new URLPath(operation.path).toURLPath().replace(/([^/]):/g, "$1\\\\:");
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
const params = FunctionParams.factory({ data: {
type: `${typeName} | ((
info: Parameters<Parameters<typeof http.${method}>[1]>[0],
) => Response)`,
optional: true
} });
return /* @__PURE__ */ jsx(File.Source, {
name,
isIndexable: true,
isExportable: true,
children: /* @__PURE__ */ jsx(Function, {
name,
export: true,
params: params.toConstructor(),
children: `return http.${method}('${baseURL}${url.replace(/([^/]):/g, "$1\\\\:")}', function handler(info) {
if(typeof data === 'function') return data(info)
return new Response(JSON.stringify(data), {
status: ${statusCode},
${headers.length ? ` headers: {
${headers.join(", \n")}
},` : ""}
})
})`
})
});
}
//#endregion
//#region src/components/Handlers.tsx
function Handlers({ name, handlers }) {
return /* @__PURE__ */ jsx(File.Source, {
name,
isIndexable: true,
isExportable: true,
children: `export const ${name} = ${JSON.stringify(handlers).replaceAll(`"`, "")} as const`
});
}
//#endregion
//#region src/components/MockWithFaker.tsx
function MockWithFaker({ baseURL = "", name, fakerName, typeName, operation }) {
const method = operation.method;
const successStatusCodes = operation.getResponseStatusCodes().filter((code) => code.startsWith("2"));
const statusCode = successStatusCodes.length > 0 ? Number(successStatusCodes[0]) : 200;
const responseObject = operation.getResponseByStatusCode(statusCode);
const contentType = Object.keys(responseObject.content || {})?.[0];
const url = new URLPath(operation.path).toURLPath().replace(/([^/]):/g, "$1\\\\:");
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
const params = FunctionParams.factory({ data: {
type: `${typeName} | ((
info: Parameters<Parameters<typeof http.${method}>[1]>[0],
) => Response)`,
optional: true
} });
return /* @__PURE__ */ jsx(File.Source, {
name,
isIndexable: true,
isExportable: true,
children: /* @__PURE__ */ jsx(Function, {
name,
export: true,
params: params.toConstructor(),
children: `return http.${method}('${baseURL}${url.replace(/([^/]):/g, "$1\\\\:")}', function handler(info) {
if(typeof data === 'function') return data(info)
return new Response(JSON.stringify(data || ${fakerName}(data)), {
status: ${statusCode},
${headers.length ? ` headers: {
${headers.join(", \n")}
},` : ""}
})
})`
})
});
}
//#endregion
export { Handlers, Mock, MockWithFaker };
//# sourceMappingURL=components-ByUOezvw.js.map