@kubb/plugin-msw
Version:
Mock Service Worker (MSW) handlers generator plugin for Kubb, creating API mocks from OpenAPI specifications for frontend development and testing.
149 lines (142 loc) • 5.58 kB
JavaScript
const require_index = require('./index.cjs');
let _kubb_react_fabric = require("@kubb/react-fabric");
let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime");
let _kubb_core_utils = require("@kubb/core/utils");
//#region src/components/Handlers.tsx
function Handlers({ name, handlers }) {
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isIndexable: true,
isExportable: true,
children: `export const ${name} = ${JSON.stringify(handlers).replaceAll(`"`, "")} as const`
});
}
//#endregion
//#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 _kubb_core_utils.URLPath(operation.path).toURLPath().replace(/([^/]):/g, "$1\\\\:");
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
const dataType = contentType && responseObject?.content?.[contentType]?.schema !== void 0 ? typeName : "string | number | boolean | null | object";
const params = _kubb_react_fabric.FunctionParams.factory({ data: {
type: `${dataType} | ((
info: Parameters<Parameters<typeof http.${method}>[1]>[0],
) => Response | Promise<Response>)`,
optional: true
} });
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isIndexable: true,
isExportable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.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/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 _kubb_core_utils.URLPath(operation.path).toURLPath().replace(/([^/]):/g, "$1\\\\:");
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
const params = _kubb_react_fabric.FunctionParams.factory({ data: {
type: `${typeName} | ((
info: Parameters<Parameters<typeof http.${method}>[1]>[0],
) => Response | Promise<Response>)`,
optional: true
} });
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name,
isIndexable: true,
isExportable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.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
//#region src/components/Response.tsx
function Response({ name, typeName, operation, statusCode }) {
const responseObject = operation.getResponseByStatusCode(statusCode);
const contentType = Object.keys(responseObject.content || {})?.[0];
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
const hasResponseSchema = contentType && responseObject?.content?.[contentType]?.schema !== void 0;
const params = _kubb_react_fabric.FunctionParams.factory({ data: {
type: `${typeName}`,
optional: !hasResponseSchema
} });
const responseName = `${name}Response${statusCode}`;
return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
name: responseName,
isIndexable: true,
isExportable: true,
children: /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.Function, {
name: responseName,
export: true,
params: params.toConstructor(),
children: `
return new Response(JSON.stringify(data), {
status: ${statusCode},
${headers.length ? ` headers: {
${headers.join(", \n")}
},` : ""}
})`
})
});
}
//#endregion
Object.defineProperty(exports, 'Handlers', {
enumerable: true,
get: function () {
return Handlers;
}
});
Object.defineProperty(exports, 'Mock', {
enumerable: true,
get: function () {
return Mock;
}
});
Object.defineProperty(exports, 'MockWithFaker', {
enumerable: true,
get: function () {
return MockWithFaker;
}
});
Object.defineProperty(exports, 'Response', {
enumerable: true,
get: function () {
return Response;
}
});
//# sourceMappingURL=components-N4uAuUW1.cjs.map