UNPKG

openapi-ts-mock-generator

Version:
383 lines (373 loc) 13 kB
var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __esm = (fn, res) => function __init() { return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/core/types.ts var HttpMethods, isNotNullish; var init_types = __esm({ "src/core/types.ts"() { "use strict"; HttpMethods = /* @__PURE__ */ ((HttpMethods2) => { HttpMethods2["GET"] = "get"; HttpMethods2["PUT"] = "put"; HttpMethods2["POST"] = "post"; HttpMethods2["DELETE"] = "delete"; HttpMethods2["OPTIONS"] = "options"; HttpMethods2["HEAD"] = "head"; HttpMethods2["PATCH"] = "patch"; HttpMethods2["TRACE"] = "trace"; return HttpMethods2; })(HttpMethods || {}); isNotNullish = (value) => { return value !== null && value !== void 0; }; } }); // src/core/config.ts import { Faker, ko } from "@faker-js/faker"; var defaultOptions, ARRAY_MIN_LENGTH, ARRAY_MAX_LENGTH, MIN_STRING_LENGTH, MAX_STRING_LENGTH, MIN_INTEGER, MAX_INTEGER, MIN_NUMBER, MAX_NUMBER, MIN_WORD_LENGTH, MAX_WORD_LENGTH, FAKER_SEED, faker, GEN_COMMENT; var init_config = __esm({ "src/core/config.ts"() { "use strict"; defaultOptions = { path: "", arrayMinLength: 1, arrayMaxLength: 3, includeCodes: void 0, baseDir: "./", specialPath: void 0, handlerUrl: "*", fakerLocale: "ko", generateTarget: "api,schema", clear: false, // TypeScriptCodeOptions isStatic: false, isOptional: false }; ARRAY_MIN_LENGTH = 1; ARRAY_MAX_LENGTH = 3; MIN_STRING_LENGTH = 3; MAX_STRING_LENGTH = 20; MIN_INTEGER = 1; MAX_INTEGER = 1e5; MIN_NUMBER = 0; MAX_NUMBER = 100; MIN_WORD_LENGTH = 0; MAX_WORD_LENGTH = 3; FAKER_SEED = 1; faker = new Faker({ locale: [ko] }); faker.seed(FAKER_SEED); GEN_COMMENT = "/* Do not edit this file. */\n/* This file generated by openapi-ts-mock-generator. */\n\n"; } }); // src/core/options.ts var mergeOptions, transformCliOptions, validateOptions; var init_options = __esm({ "src/core/options.ts"() { "use strict"; init_config(); mergeOptions = (userOptions) => { return __spreadValues(__spreadValues({}, defaultOptions), userOptions); }; transformCliOptions = (rawOptions) => { return { path: rawOptions.path || defaultOptions.path, baseDir: rawOptions.baseDir || defaultOptions.baseDir, arrayMinLength: parseInt(rawOptions.arrayMinLength) || defaultOptions.arrayMinLength, arrayMaxLength: parseInt(rawOptions.arrayMaxLength) || defaultOptions.arrayMaxLength, handlerUrl: rawOptions.handlerUrl || defaultOptions.handlerUrl, fakerLocale: rawOptions.locales || defaultOptions.fakerLocale, generateTarget: rawOptions.generateTarget || defaultOptions.generateTarget, specialPath: rawOptions.specialPath || defaultOptions.specialPath, clear: rawOptions.clear || defaultOptions.clear, includeCodes: rawOptions.includeCodes ? rawOptions.includeCodes.toString().split(",").map((code) => parseInt(code)) : void 0, // TypeScriptCodeOptions isStatic: rawOptions.static || defaultOptions.isStatic, isOptional: rawOptions.optional || defaultOptions.isOptional }; }; validateOptions = (options) => { const errors = []; if (!options.path) { errors.push("path is required"); } if (options.arrayMinLength && options.arrayMaxLength && options.arrayMinLength > options.arrayMaxLength) { errors.push("arrayMinLength should not be greater than arrayMaxLength"); } if (options.generateTarget && !options.generateTarget.split(",").every((target) => ["api", "schema"].includes(target.trim()))) { errors.push("generateTarget should contain only 'api' and/or 'schema'"); } return errors; }; } }); // src/core/index.ts var core_exports = {}; __export(core_exports, { ARRAY_MAX_LENGTH: () => ARRAY_MAX_LENGTH, ARRAY_MIN_LENGTH: () => ARRAY_MIN_LENGTH, GEN_COMMENT: () => GEN_COMMENT, HttpMethods: () => HttpMethods, MAX_INTEGER: () => MAX_INTEGER, MAX_NUMBER: () => MAX_NUMBER, MAX_STRING_LENGTH: () => MAX_STRING_LENGTH, MAX_WORD_LENGTH: () => MAX_WORD_LENGTH, MIN_INTEGER: () => MIN_INTEGER, MIN_NUMBER: () => MIN_NUMBER, MIN_STRING_LENGTH: () => MIN_STRING_LENGTH, MIN_WORD_LENGTH: () => MIN_WORD_LENGTH, defaultOptions: () => defaultOptions, faker: () => faker, isNotNullish: () => isNotNullish, mergeOptions: () => mergeOptions, transformCliOptions: () => transformCliOptions, validateOptions: () => validateOptions }); var init_core = __esm({ "src/core/index.ts"() { "use strict"; init_types(); init_config(); init_options(); } }); // src/generators/faker-generator.ts init_core(); // src/utils/code-utils.ts var toTypeScriptCode = (param, options) => { const { depth = 0, isStatic } = options; const prefixSpace = " ".repeat(depth * 2); if (param === null) { return "null"; } if (Array.isArray(param)) { const results = param.map((elem) => toTypeScriptCode(elem, __spreadProps(__spreadValues({}, options), { depth: depth + 1 }))).join(",\n" + prefixSpace); return ["[", results, "]"].join("\n" + prefixSpace); } if (typeof param === "object") { const results = Object.entries(param).map(([key, value]) => { return generateObjectProperty(key, value, options, prefixSpace); }).join("\n" + prefixSpace); return ["{", `${results}`, "}"].join("\n" + prefixSpace); } if (typeof param === "string") { if (isStatic === false && (param.startsWith("faker") || param.startsWith("Buffer.from(faker"))) { return param; } if (param.endsWith(" as const")) { return `"${param.slice(0, -" as const".length)}" as const`; } } return JSON.stringify(param); }; var shouldApplyNullableExtension = (value, isOptional) => { if (!isOptional) return false; if (value === null) return true; if (typeof value === "string" && value.includes(",null")) { return true; } return false; }; var generateObjectProperty = (key, value, options, prefixSpace) => { const { isOptional, depth = 0 } = options; const shouldApplyNullable = shouldApplyNullableExtension(value, isOptional); const nullableTypeExtensionStart = shouldApplyNullable ? `...(faker.datatype.boolean() ? { ${prefixSpace}` : ""; const nullableTypeExtensionEnd = shouldApplyNullable ? ` ${prefixSpace}} : {})` : ""; const propertyValue = toTypeScriptCode(value, __spreadProps(__spreadValues({}, options), { depth: depth + 1 })); return `${nullableTypeExtensionStart}${prefixSpace}${key}: ${propertyValue}${nullableTypeExtensionEnd},`; }; // src/utils/array-utils.ts init_core(); // src/utils/file-utils.ts import { existsSync, mkdirSync, writeFileSync, rmSync, readdirSync, readFileSync } from "fs"; import * as path from "path"; var ensureDir = (dirPath) => { if (!existsSync(dirPath)) { mkdirSync(dirPath, { recursive: true }); } }; var safeWriteFile = (filePath, content) => { const dir = path.dirname(filePath); ensureDir(dir); writeFileSync(filePath, content); }; // src/generators/faker-generator.ts import * as path2 from "path"; var generateFaker = (options) => { var _a, _b; const directory = path2.join((_a = options.baseDir) != null ? _a : ""); ensureDir(directory); const content = generateFakerFileContent(options); const outputFileName = path2.join((_b = options.baseDir) != null ? _b : "", "fakers.ts"); safeWriteFile(outputFileName, content); console.log(`Generated fakers ${outputFileName}`); }; var generateFakerFileContent = (options) => { const { GEN_COMMENT: GEN_COMMENT2 } = (init_core(), __toCommonJS(core_exports)); 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"); return GEN_COMMENT2 + importFaker + fakerDeclare; }; var generateSchemaFile = (schemas, options) => { var _a; const generatedVars = Object.entries(schemas).map(([varName, varValue]) => { return `export const ${varName}Mock = ${toTypeScriptCode(varValue, __spreadValues({ depth: 0 }, options))}`; }).join("\n\n"); const importFaker = options.isStatic ? "" : 'import { faker } from "./fakers"\n\n'; const content = GEN_COMMENT + importFaker + generatedVars; const outputFileName = path2.join((_a = options.baseDir) != null ? _a : "", "schemas.ts"); safeWriteFile(outputFileName, content); console.log(`Generated schema ${outputFileName}`); }; var generateCustomFakerConfig = (titleConfig, descriptionConfig, options) => { var _a; if (!options.specialPath) return; const specialDir = path2.join((_a = options.baseDir) != null ? _a : "", options.specialPath); ensureDir(specialDir); const titlesPath = path2.join(specialDir, "titles.json"); safeWriteFile(titlesPath, JSON.stringify(titleConfig, null, 2)); console.log(`Generated custom faker titles config: ${titlesPath}`); const descriptionsPath = path2.join(specialDir, "descriptions.json"); safeWriteFile(descriptionsPath, JSON.stringify(descriptionConfig, null, 2)); console.log(`Generated custom faker descriptions config: ${descriptionsPath}`); }; var validateFakerConfig = (config) => { const errors = []; Object.entries(config).forEach(([key, value]) => { if (typeof value !== "object") { errors.push(`Config for "${key}" must be an object`); return; } if ("module" in value && "type" in value) { if (typeof value.module !== "string") { errors.push(`Module for "${key}" must be a string`); } if (typeof value.type !== "string") { errors.push(`Type for "${key}" must be a string`); } } else if (!("value" in value)) { errors.push(`Config for "${key}" must have either "value" or "module"+"type"`); } }); return { isValid: errors.length === 0, errors }; }; var generateDefaultFakerTemplate = () => { return { titles: { "User ID": { module: "string", type: "uuid" }, Email: { module: "internet", type: "email" }, Name: { module: "person", type: "fullName" } }, descriptions: { "\uC0AC\uC6A9\uC790 \uC774\uBA54\uC77C": { module: "internet", type: "email" }, "\uC0AC\uC6A9\uC790 \uC774\uB984": { module: "person", type: "fullName", options: { locale: "ko" } }, \uC804\uD654\uBC88\uD638: { module: "phone", type: "number" } } }; }; var getLocaleSpecificModules = (locale) => { const commonModules = [ "datatype", "date", "finance", "git", "hacker", "helpers", "image", "internet", "lorem", "music", "person", "phone", "random", "system", "vehicle" ]; const localeModules = { ko: ["person", "phone", "address"], en: ["person", "phone", "address", "company"], ja: ["person", "phone", "address"] }; return [...commonModules, ...localeModules[locale] || []]; }; export { generateCustomFakerConfig, generateDefaultFakerTemplate, generateFaker, generateSchemaFile, getLocaleSpecificModules, validateFakerConfig }; //# sourceMappingURL=faker-generator.mjs.map