UNPKG

openapi-ts-mock-generator

Version:
589 lines (585 loc) 23.6 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; // src/defaults.ts import { Faker, ko } from "@faker-js/faker"; var ARRAY_MIN_LENGTH = 1; var ARRAY_MAX_LENGTH = 3; var MIN_STRING_LENGTH = 3; var MAX_STRING_LENGTH = 20; var MIN_INTEGER = 1; var MAX_INTEGER = 1e5; var MIN_NUMBER = 0; var MAX_NUMBER = 100; var MIN_WORD_LENGTH = 0; var MAX_WORD_LENGTH = 3; var FAKER_SEED = 1; var faker = new Faker({ locale: [ko] }); faker.seed(FAKER_SEED); var GEN_COMMENT = "/* Do not edit this file. */\n/* This file generated by openapi-ts-mock-generator. */\n\n"; // src/parser.ts import { pascalCase } from "change-case-all"; import { existsSync, readFileSync } from "fs"; import { isReference } from "oazapfts/generate"; import { join } from "path"; var parseSchema = (schemaValue, specialSchema, options, outputSchema = {}) => { if (isReference(schemaValue)) { console.warn("can't parse reference schema", schemaValue, schemaValue.$ref); return; } if (schemaValue.type === "object") { if (schemaValue.properties === void 0) return {}; return Object.entries(schemaValue.properties).reduce((acc, [key, field]) => { acc[key] = parseSchema(field, specialSchema, options, outputSchema); return acc; }, {}); } else if (schemaValue.enum !== void 0) { const enumValue = options.isStatic ? faker.helpers.arrayElement(schemaValue.enum) : `faker.helpers.arrayElement<${schemaValue.enum.map((item) => `"${item}"`).join(" | ")}>(${toUnquotedJSON(schemaValue.enum, { depth: 0, isStatic: options.isStatic, singleLine: true })})`; if (options.isStatic && typeof enumValue === "string") return enumValue + " as const"; return enumValue; } else if (schemaValue.allOf !== void 0) { const allOfValue = schemaValue.allOf; return faker.helpers.arrayElement( allOfValue.map((field) => { return parseSchema(field, specialSchema, options, outputSchema); }) ); } else if (schemaValue.anyOf !== void 0) { const anyOfValue = schemaValue.anyOf; return options.isStatic ? faker.helpers.arrayElement( anyOfValue.map((field) => { return parseSchema(field, specialSchema, options, outputSchema); }) ) : multiLineStr(` faker.helpers.arrayElement([ ${anyOfValue.map( (field) => toUnquotedJSON(parseSchema(field, specialSchema, options, {}), { depth: 0, isStatic: options.isStatic, singleLine: true }) )} ]) `); } else if (schemaValue.oneOf !== void 0) { const oneOfValue = schemaValue.oneOf; return options.isStatic ? faker.helpers.arrayElement( oneOfValue.map((field) => { return parseSchema(field, specialSchema, options, outputSchema); }) ) : multiLineStr(` faker.helpers.arrayElement([ ${oneOfValue.map( (field) => toUnquotedJSON(parseSchema(field, specialSchema, options, {}), { depth: 0, isStatic: options.isStatic, singleLine: true }) )} ]) `); } else if (schemaValue.type === "array") { if ("prefixItems" in schemaValue) { const length = faker.number.int({ min: schemaValue.minItems, max: schemaValue.maxItems }); return schemaValue.prefixItems.slice(0, length).map((field) => parseSchema(field, specialSchema, options, outputSchema)); } const arrayValue = schemaValue.items; return getRandomLengthArray(options.arrayMinLength, options.arrayMaxLength).map( () => parseSchema(arrayValue, specialSchema, options, outputSchema) ); } return valueGenerator(schemaValue, specialSchema, options.isStatic); }; var uuidToB64 = (uuid) => { const uuidBuffer = Buffer.from(uuid.replace(/-/g, ""), "hex"); const base64Uuid = uuidBuffer.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ""); return base64Uuid; }; var valueGenerator = (schemaValue, specialSchema, isStatic) => { var _a, _b, _c, _d, _e, _f, _g, _h, _i; const { titleSpecial, descriptionSpecial } = specialSchema; if (schemaValue.title && titleSpecial[schemaValue.title]) { return titleSpecial[schemaValue.title]; } else if (schemaValue.description && descriptionSpecial[schemaValue.description]) { return descriptionSpecial[schemaValue.description]; } if (schemaValue.type === "string" && schemaValue.format === "date-time") { return isStatic ? faker.date.between({ from: "2020-01-01T00:00:00.000Z", to: "2030-12-31T23:59:59.999Z" }).toISOString() : multiLineStr(` faker.date.between({ from: "2020-01-01T00:00:00.000Z", to: "2030-12-31T23:59:59.999Z", }) .toISOString() `); } else if (schemaValue.type === "string" && schemaValue.format === "date") { return isStatic ? faker.date.between({ from: "2020-01-01T00:00:00.000Z", to: "2030-12-31T23:59:59.999Z" }).toISOString().split("T")[0] : multiLineStr(` faker.date.between({ from: "2020-01-01T00:00:00.000Z", to: "2030-12-31T23:59:59.999Z", }) .toISOString() .split("T")[0] `); } else if (schemaValue.type === "string" && schemaValue.pattern) { return isStatic ? faker.helpers.fromRegExp(schemaValue.pattern) : `faker.helpers.fromRegExp(/${schemaValue.pattern}/)`; } else if (schemaValue.type === "string" && ((_a = schemaValue.title) == null ? void 0 : _a.toLowerCase()) === "b64uuid") { const baseUuid = faker.string.uuid(); return isStatic ? uuidToB64(baseUuid) : multiLineStr(` Buffer.from(faker.string.uuid().replace(/-/g, ""), "hex") .toString("base64") .replace(/\\+/g, "-") .replace(/\\//g, "_") .replace(/=/g, "") `); } else if (schemaValue.type === "string") { const minLength = (_c = schemaValue.minLength) != null ? _c : Math.min(MIN_STRING_LENGTH, (_b = schemaValue.maxLength) != null ? _b : MAX_STRING_LENGTH); const maxLength = (_e = schemaValue.maxLength) != null ? _e : Math.max(MAX_STRING_LENGTH, (_d = schemaValue.minLength) != null ? _d : MIN_STRING_LENGTH); return isStatic ? faker.string.alphanumeric({ length: { min: minLength, max: maxLength } }) : multiLineStr(` faker.string.alphanumeric({ length: { min: ${minLength}, max: ${maxLength} }, }) `); } else if (schemaValue.type === "integer") { return isStatic ? faker.number.int({ min: MIN_INTEGER, max: MAX_INTEGER }) : multiLineStr(` faker.number.int({ min: ${MIN_INTEGER}, max: ${MAX_INTEGER} }) `); } else if (schemaValue.type === "number") { const minNumber = (_g = schemaValue.minimum) != null ? _g : Math.min(MIN_NUMBER, (_f = schemaValue.maximum) != null ? _f : MAX_NUMBER); const maxNumber = (_i = schemaValue.maximum) != null ? _i : Math.max(MAX_NUMBER, (_h = schemaValue.minimum) != null ? _h : MIN_NUMBER); return isStatic ? faker.number.float({ min: minNumber, max: maxNumber, fractionDigits: 2 }) : multiLineStr(` faker.number.float({ min: ${minNumber}, max: ${maxNumber}, fractionDigits: 2, }) `); } else if (schemaValue.type === "boolean") { return isStatic ? faker.datatype.boolean() : "faker.datatype.boolean()"; } else if (schemaValue.type === "null") { return null; } else if (Object.keys(schemaValue).length === 0) { return isStatic ? faker.word.words({ count: { min: MIN_WORD_LENGTH, max: MAX_WORD_LENGTH } }) : multiLineStr(` faker.word.words({ count: { min: ${MIN_WORD_LENGTH}, max: ${MAX_WORD_LENGTH}, }, }) `); } return isStatic ? faker.word.adjective() : "faker.word.adjective()"; }; var getRandomLengthArray = (min = ARRAY_MIN_LENGTH, max = ARRAY_MAX_LENGTH) => { const length = faker.number.int({ min, max }); return Array.from({ length }, (_, i) => i); }; var refSchemaParser = (ref, refs) => { const schemaName = pascalCase(ref.replace("#/components/schemas/", "")); const schemaValue = refs.get(ref); return { name: schemaName, value: schemaValue }; }; var getFakerValue = (value, options) => { if ("value" in value) { return value.value; } if ("module" in value && "type" in value) { if (options.isStatic === false) { const fakerOption = "options" in value ? toUnquotedJSON(value.options, { depth: 0, isStatic: options.isStatic, singleLine: true }) : ""; return `faker.${value.module}.${value.type}(${fakerOption})`; } const fakerModule = faker[value.module]; if (fakerModule === void 0) { console.warn("can't find faker module", fakerModule); return void 0; } const fakerFunc = fakerModule[value.type]; if (fakerFunc === void 0 || typeof fakerFunc !== "function") { console.warn("can't find faker function", fakerFunc); return void 0; } return "options" in value ? fakerFunc(value.options) : fakerFunc(); } return void 0; }; var specialFakerParser = (options) => { var _a, _b; if (options.specialPath === void 0) return { titleSpecial: {}, descriptionSpecial: {} }; const titlePath = join((_a = options.baseDir) != null ? _a : "", options.specialPath, "titles.json"); const descPath = join((_b = options.baseDir) != null ? _b : "", options.specialPath, "descriptions.json"); const titleSpecialKey = existsSync(titlePath) ? JSON.parse(readFileSync(titlePath, "utf-8")) : {}; const descriptionSpecialKey = existsSync(descPath) ? JSON.parse(readFileSync(descPath, "utf-8")) : {}; const titleSpecial = Object.entries(titleSpecialKey).reduce((acc, [key, value]) => { const fakerValue = getFakerValue(value, { isStatic: options.isStatic }); acc[key] = fakerValue; return acc; }, {}); const descriptionSpecial = Object.entries(descriptionSpecialKey).reduce((acc, [key, value]) => { const fakerValue = getFakerValue(value, { isStatic: options.isStatic }); acc[key] = fakerValue; return acc; }, {}); return { titleSpecial, descriptionSpecial }; }; // src/writer.ts import SwaggerParser from "@apidevtools/swagger-parser"; import { camelCase, pascalCase as pascalCase2 } from "change-case-all"; import { existsSync as existsSync2, mkdirSync, writeFileSync, rmSync, readdirSync } from "fs"; import { isReference as isReference2 } from "oazapfts/generate"; import * as path from "path"; var writeHandlers = (paths, options) => { var _a; const firstTags = Array.from(new Set(paths.map((path2) => path2.tags[0]))); const handlersPerTag = firstTags.reduce( (acc, tag) => { acc[tag] = []; return acc; }, {} ); paths.forEach((path2) => { var _a2; const codeBaseArray = [` http.${path2.method}(\`\${handlerUrl}${path2.pathname}\`, () => {`]; if (path2.responses.length === 1) { const res = path2.responses[0]; if (((_a2 = res.schema) == null ? void 0 : _a2.type) === "ref") { const schemaName = pascalCase2(res.schema.value.$ref.replace("#/components/schemas/", "")); codeBaseArray.push(` // Schema is ${schemaName}`); } const outputResName = `get${pascalCase2(path2.operationId)}${res.statusCode}`; codeBaseArray.push(` return HttpResponse.json(${outputResName}(), {`); codeBaseArray.push(` status: ${res.statusCode},`); codeBaseArray.push(` })`); } else if (path2.responses.length > 1) { codeBaseArray.push(` const responses = [`); path2.responses.forEach((res) => { var _a3; const schemaName = ((_a3 = res.schema) == null ? void 0 : _a3.type) === "ref" ? pascalCase2(res.schema.value.$ref.replace("#/components/schemas/", "")) : ""; const schemaComment = schemaName ? ` // Schema is ${schemaName}` : ""; const outputResName = `get${pascalCase2(path2.operationId)}${res.statusCode}`; codeBaseArray.push( ` [${outputResName}(), { status: ${res.statusCode} }],${schemaComment}` ); return outputResName; }); codeBaseArray.push(` ]`); codeBaseArray.push(` const randomIndex = Math.floor(Math.random() * responses.length)`); codeBaseArray.push(` return HttpResponse.json(...responses[randomIndex])`); } else { codeBaseArray.push(` return HttpResponse.json()`); } codeBaseArray.push(` }),`); const handler = codeBaseArray.join("\n"); handlersPerTag[path2.tags[0]].push(handler); }); Object.entries(handlersPerTag).forEach(([tag, handlers]) => { var _a2; const importMSW = `import { http, HttpResponse } from 'msw'`; const responseNames = handlers.reduce((acc, handler) => { const matched = handler.match(/get[A-Z]\w+/g); if (matched === null) return acc; return [...acc, ...matched]; }, []).join(", "); const importResponses = responseNames.length > 0 ? `import { ${responseNames} } from "../response" ` : ""; const handlerUrl = `const handlerUrl = "${options.handlerUrl}"`; const handlerName = camelCase(tag); const mockHandlers2 = [ `${importMSW}`, `${importResponses}`, `${handlerUrl}`, ``, `export const ${handlerName}Handlers = [`, `${handlers.join("\n\n")}`, `]` ].join("\n"); const directory = path.join((_a2 = options.baseDir) != null ? _a2 : "", "handlers"); if (!existsSync2(directory)) { mkdirSync(directory, { recursive: true }); } else if (options.clear) { readdirSync(directory).forEach((file) => { rmSync(path.join(directory, file)); }); } const fileName2 = path.join(directory, `${tag}.ts`); writeFileSync(fileName2, GEN_COMMENT + mockHandlers2); console.log(`Generated Handler ${fileName2}`); }); const handlersImport = Object.keys(handlersPerTag).map((tag) => { const handlerName = `${camelCase(tag)}Handlers`; return `import { ${handlerName} } from "./handlers/${tag}"`; }).join("\n"); const handlersArrayItem = Object.keys(handlersPerTag).map((tag) => { const handlerName = `${camelCase(tag)}Handlers`; return ` ...${handlerName},`; }).join("\n"); const mockHandlers = [ `${handlersImport}`, ``, `export const handlers = [`, `${handlersArrayItem}`, `]` ].join("\n"); const fileName = path.join((_a = options.baseDir) != null ? _a : "", "mockHandlers.ts"); writeFileSync(fileName, GEN_COMMENT + mockHandlers); console.log(`Generated mock handlers ${fileName}`); }; var writeResponses = (paths, options) => __async(void 0, null, function* () { var _a, _b; const parser = new SwaggerParser(); const openapiPath = options.path.startsWith("http") ? options.path : path.join((_a = options.baseDir) != null ? _a : "", options.path); yield parser.dereference(openapiPath); const refs = parser.$refs; const firstTags = Array.from(new Set(paths.map((path2) => path2.tags[0]))); const codeBasePerTag = firstTags.reduce( (acc, tag) => { acc[tag] = []; return acc; }, {} ); const specialFakers = specialFakerParser(options); paths.forEach((path2) => { const pathResponses = path2.responses.map((res) => { var _a2, _b2, _c, _d, _e; const codeBaseArray = [ `export const get${pascalCase2(path2.operationId)}${res.statusCode} = () => {` ]; if (((_a2 = res.schema) == null ? void 0 : _a2.type) === "ref") { const { name, value } = refSchemaParser(res.schema.value.$ref, refs); const outputSchema = parseSchema(value, specialFakers, options); codeBaseArray.push(` // Schema is ${name}`); codeBaseArray.push( ` return ${toUnquotedJSON(outputSchema, { depth: 1, isStatic: options.isStatic })}` ); } else if (((_b2 = res.schema) == null ? void 0 : _b2.type) === "array") { if (isReference2(res.schema.value)) { const { name, value } = refSchemaParser(res.schema.value.$ref, refs); const outputSchema = getRandomLengthArray( options.arrayMinLength, options.arrayMaxLength ).map(() => parseSchema(value, specialFakers, options)); codeBaseArray.push(` // Schema is ${name} array`); codeBaseArray.push( ` return ${toUnquotedJSON(outputSchema, { depth: 1, isStatic: options.isStatic })}` ); } else { const outputSchema = getRandomLengthArray( options.arrayMinLength, options.arrayMaxLength ).map(() => res.schema && parseSchema(res.schema.value, specialFakers, options)); codeBaseArray.push( ` return ${toUnquotedJSON(outputSchema, { depth: 1, isStatic: options.isStatic })}` ); } } else if (((_c = res.schema) == null ? void 0 : _c.type) === "anyOf") { const firstSchema = (_d = res.schema.value.anyOf) == null ? void 0 : _d[0]; if (isReference2(firstSchema)) { const { name, value } = refSchemaParser(firstSchema.$ref, refs); const outputSchema = parseSchema(value, specialFakers, options); codeBaseArray.push(` // Schema is ${name}`); codeBaseArray.push( ` return ${toUnquotedJSON(outputSchema, { depth: 1, isStatic: options.isStatic })}` ); } else { codeBaseArray.push(` return ${res.schema.value}`); } } else { codeBaseArray.push(` return ${(_e = res.schema) == null ? void 0 : _e.value}`); } return [...codeBaseArray, `}`].join("\n"); }); const pathResponsesWithComment = `// ${path2.operationId} ` + pathResponses.join("\n\n"); codeBasePerTag[path2.tags[0]].push(pathResponsesWithComment); }); const directory = path.join((_b = options.baseDir) != null ? _b : "", "response"); if (!existsSync2(directory)) { mkdirSync(directory, { recursive: true }); } else if (options.clear) { readdirSync(directory).forEach((file) => { rmSync(path.join(directory, file)); }); } Object.entries(codeBasePerTag).forEach(([tag, responses]) => { const needImportFaker = responses.some((res) => res.includes("faker.")); const importFaker = options.isStatic || !needImportFaker ? "" : 'import { faker } from "../fakers"\n\n'; const fileName2 = `${directory}/${tag}.ts`; writeFileSync(fileName2, GEN_COMMENT + importFaker + responses.join("\n\n")); console.log(`Generated ${fileName2}`); }); const importResponses = Object.entries(codeBasePerTag).map(([tag, responses]) => { const responseNames = responses.reduce((acc, handler) => { const matched = handler.match(/get[A-Z]\w+/g); if (matched === null) return acc; return [...acc, ...matched]; }, []).join(",\n "); return ["export {", " " + responseNames, '} from "./' + tag + '"'].join("\n"); }); const fileName = `${directory}/index.ts`; writeFileSync(fileName, GEN_COMMENT + importResponses.join("\n")); console.log(`Generated ${fileName}`); }); var writeSchema = (schemas, options) => { const generatedVars = Object.entries(schemas).map(([varName, varValue]) => { return `export const ${varName}Mock = ${toUnquotedJSON(varValue, { isStatic: options.isStatic })}`; }).join("\n\n"); const importFaker = options.isStatic ? "" : 'import { faker } from "./fakers"\n\n'; const outputFileName = path.join(`${options.baseDir}`, "schemas.ts"); writeFileSync(outputFileName, GEN_COMMENT + importFaker + generatedVars); console.log(`Generated schema ${outputFileName}`); }; var writeFaker = (options) => { var _a; const directory = path.join((_a = options.baseDir) != null ? _a : ""); if (!existsSync2(directory)) { mkdirSync(directory, { recursive: true }); } const localeOption = options.fakerLocale.replace(",", ", "); const importFaker = `import { Faker, ${localeOption} } from "@faker-js/faker" `; const fakerDeclare = [ "export const faker = new Faker({", ` locale: [${localeOption}]`, "})" ].join("\n"); const outputFileName = path.join(`${options.baseDir}`, "fakers.ts"); writeFileSync(outputFileName, GEN_COMMENT + importFaker + fakerDeclare); console.log(`Generated fakers ${outputFileName}`); }; var toUnquotedJSON = (param, options) => { const { depth, isStatic, singleLine } = __spreadValues({ depth: 0, isStatic: false, singleLine: false }, options); const prefixSpace = " ".repeat(depth * 2); const lineBreak = singleLine ? "" : "\n"; if (param === null) { return "null"; } else if (Array.isArray(param)) { const results = param.map((elem) => toUnquotedJSON(elem, __spreadProps(__spreadValues({}, options), { depth: depth + 1 }))); const firstElementSpace = singleLine ? "" : " "; return ["[", firstElementSpace + results.join(", "), "]"].join(lineBreak + prefixSpace); } else if (typeof param === "object") { const firstElementSpace = singleLine ? " " : " "; const lastComma = singleLine ? ", " : ","; const results = Object.entries(param).map( ([key, value]) => `${firstElementSpace}${key}: ${toUnquotedJSON(value, __spreadProps(__spreadValues({}, options), { depth: depth + 1 }))}${lastComma}` ).join(lineBreak + prefixSpace); return ["{", `${results}`, "}"].join(lineBreak + prefixSpace); } else if (typeof param === "string" && isStatic === false && (param.startsWith("faker") || param.startsWith("Buffer.from(faker"))) { return param; } else if (typeof param === "string" && param.endsWith(" as const")) { return `"${param.slice(0, -" as const".length)}" as const`; } return JSON.stringify(param); }; var multiLineStr = (str) => { return str.replace(/\n/g, " ").replace(/\s+/g, " ").replace(/\s\./g, ".").trim(); }; export { multiLineStr, toUnquotedJSON, writeFaker, writeHandlers, writeResponses, writeSchema }; //# sourceMappingURL=writer.mjs.map