openapi-ts-mock-generator
Version:
typescript mock data generator based openapi
1,290 lines (1,282 loc) • 55.2 kB
JavaScript
#!/usr/bin/env node
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
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 __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
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, 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_defaults = __esm({
"src/defaults.ts"() {
"use strict";
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/writer.ts
import SwaggerParser from "@apidevtools/swagger-parser";
import { camelCase, pascalCase } from "change-case-all";
import { existsSync, mkdirSync, writeFileSync, rmSync, readdirSync } from "fs";
import { isReference } from "oazapfts/generate";
import * as path from "path";
var writeHandlers, writeResponses, writeSchema, writeFaker, toUnquotedJSON, multiLineStr;
var init_writer = __esm({
"src/writer.ts"() {
"use strict";
init_parser();
init_defaults();
writeHandlers = (paths, options) => {
var _a;
const firstTags = Array.from(new Set(paths.map((path3) => path3.tags[0])));
const handlersPerTag = firstTags.reduce(
(acc, tag) => {
acc[tag] = [];
return acc;
},
{}
);
paths.forEach((path3) => {
var _a2;
const codeBaseArray = [` http.${path3.method}(\`\${handlerUrl}${path3.pathname}\`, () => {`];
if (path3.responses.length === 1) {
const res = path3.responses[0];
if (((_a2 = res.schema) == null ? void 0 : _a2.type) === "ref") {
const schemaName = pascalCase(res.schema.value.$ref.replace("#/components/schemas/", ""));
codeBaseArray.push(` // Schema is ${schemaName}`);
}
const outputResName = `get${pascalCase(path3.operationId)}${res.statusCode}`;
codeBaseArray.push(` return HttpResponse.json(${outputResName}(), {`);
codeBaseArray.push(` status: ${res.statusCode},`);
codeBaseArray.push(` })`);
} else if (path3.responses.length > 1) {
codeBaseArray.push(` const responses = [`);
path3.responses.forEach((res) => {
var _a3;
const schemaName = ((_a3 = res.schema) == null ? void 0 : _a3.type) === "ref" ? pascalCase(res.schema.value.$ref.replace("#/components/schemas/", "")) : "";
const schemaComment = schemaName ? ` // Schema is ${schemaName}` : "";
const outputResName = `get${pascalCase(path3.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[path3.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 (!existsSync(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}`);
};
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((path3) => path3.tags[0])));
const codeBasePerTag = firstTags.reduce(
(acc, tag) => {
acc[tag] = [];
return acc;
},
{}
);
const specialFakers = specialFakerParser(options);
paths.forEach((path3) => {
const pathResponses = path3.responses.map((res) => {
var _a2, _b2, _c, _d, _e;
const codeBaseArray = [
`export const get${pascalCase(path3.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 (isReference(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 (isReference(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 = `// ${path3.operationId}
` + pathResponses.join("\n\n");
codeBasePerTag[path3.tags[0]].push(pathResponsesWithComment);
});
const directory = path.join((_b = options.baseDir) != null ? _b : "", "response");
if (!existsSync(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}`);
});
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}`);
};
writeFaker = (options) => {
var _a;
const directory = path.join((_a = options.baseDir) != null ? _a : "");
if (!existsSync(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}`);
};
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);
};
multiLineStr = (str) => {
return str.replace(/\n/g, " ").replace(/\s+/g, " ").replace(/\s\./g, ".").trim();
};
}
});
// src/parser.ts
import { pascalCase as pascalCase2 } from "change-case-all";
import { existsSync as existsSync2, readFileSync } from "fs";
import { isReference as isReference2 } from "oazapfts/generate";
import { join as join2 } from "path";
var parseSchema, uuidToB64, valueGenerator, getRandomLengthArray, refSchemaParser, getFakerValue, specialFakerParser;
var init_parser = __esm({
"src/parser.ts"() {
"use strict";
init_defaults();
init_writer();
parseSchema = (schemaValue, specialSchema, options, outputSchema = {}) => {
if (isReference2(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);
};
uuidToB64 = (uuid) => {
const uuidBuffer = Buffer.from(uuid.replace(/-/g, ""), "hex");
const base64Uuid = uuidBuffer.toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
return base64Uuid;
};
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()";
};
getRandomLengthArray = (min = ARRAY_MIN_LENGTH, max = ARRAY_MAX_LENGTH) => {
const length = faker.number.int({
min,
max
});
return Array.from({ length }, (_, i) => i);
};
refSchemaParser = (ref, refs) => {
const schemaName = pascalCase2(ref.replace("#/components/schemas/", ""));
const schemaValue = refs.get(ref);
return { name: schemaName, value: schemaValue };
};
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;
};
specialFakerParser = (options) => {
var _a, _b;
if (options.specialPath === void 0)
return {
titleSpecial: {},
descriptionSpecial: {}
};
const titlePath = join2((_a = options.baseDir) != null ? _a : "", options.specialPath, "titles.json");
const descPath = join2((_b = options.baseDir) != null ? _b : "", options.specialPath, "descriptions.json");
const titleSpecialKey = existsSync2(titlePath) ? JSON.parse(readFileSync(titlePath, "utf-8")) : {};
const descriptionSpecialKey = existsSync2(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/types.ts
var HttpMethods, isNotNullish;
var init_types = __esm({
"src/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/generate.ts
import SwaggerParser2 from "@apidevtools/swagger-parser";
import { isReference as isReference3 } from "oazapfts/generate";
import * as path2 from "path";
var getOpenAPIDocsDeref, getOpenAPIDocsBundle, generateSchema, generateAPI;
var init_generate = __esm({
"src/generate.ts"() {
"use strict";
init_parser();
init_types();
getOpenAPIDocsDeref = (path3) => __async(void 0, null, function* () {
const doc = yield SwaggerParser2.dereference(path3);
const isOpenApiV3 = "openapi" in doc && doc.openapi.startsWith("3");
if (isOpenApiV3)
return doc;
return;
});
getOpenAPIDocsBundle = (path3) => __async(void 0, null, function* () {
const doc = yield SwaggerParser2.bundle(path3);
const isOpenApiV3 = "openapi" in doc && doc.openapi.startsWith("3");
if (isOpenApiV3)
return doc;
return;
});
generateSchema = (options) => __async(void 0, null, function* () {
var _a, _b;
const openapiPath = options.path.startsWith("http") ? options.path : path2.join((_a = options.baseDir) != null ? _a : "", options.path);
const doc = yield getOpenAPIDocsDeref(openapiPath);
const sampleSchemas = (_b = doc == null ? void 0 : doc.components) == null ? void 0 : _b.schemas;
if (sampleSchemas === void 0) {
console.warn("No schemas found");
return;
}
const specialFakers = specialFakerParser(options);
return Object.entries(sampleSchemas).reduce(
(acc, [schemaName, schema]) => {
acc[schemaName] = parseSchema(schema, specialFakers, options, {});
return acc;
},
{}
);
});
generateAPI = (options) => __async(void 0, null, function* () {
var _a;
const openapiPath = options.path.startsWith("http") ? options.path : path2.join((_a = options.baseDir) != null ? _a : "", options.path);
const doc = yield getOpenAPIDocsBundle(openapiPath);
const samplePaths = doc == null ? void 0 : doc.paths;
if (samplePaths === void 0) {
console.warn("No paths found");
return;
}
const specialFakers = specialFakerParser(options);
const normalizedPaths = Object.entries(samplePaths).reduce((acc, [apiName, api]) => {
if (api === void 0)
return acc;
const paths = Object.values(HttpMethods).map((method) => {
var _a2, _b, _c, _d, _e, _f, _g, _h;
if (api[method] === void 0)
return;
const responses = Object.entries((_b = (_a2 = api[method]) == null ? void 0 : _a2.responses) != null ? _b : []).map(([statusCode, response]) => {
var _a3, _b2, _c2;
if (isReference3(response))
return void 0;
if (options.includeCodes && !options.includeCodes.includes(parseInt(statusCode)))
return void 0;
const schema = (_c2 = (_b2 = (_a3 = response.content) == null ? void 0 : _a3["application/json"]) == null ? void 0 : _b2.schema) != null ? _c2 : {};
const compositeSchema = (() => {
if ("oneOf" in schema) {
return {
type: "oneOf",
value: schema
};
}
if ("anyOf" in schema) {
return {
type: "anyOf",
value: schema
};
}
if ("type" in schema && "items" in schema && schema.type === "array") {
return {
type: "array",
value: schema.items
};
}
if (isReference3(schema))
return { type: "ref", value: schema };
if (Object.keys(schema).length === 0) {
return void 0;
}
return parseSchema(schema != null ? schema : {}, specialFakers, options, {});
})();
return {
statusCode: parseInt(statusCode),
description: response.description,
schema: compositeSchema
};
}).filter(isNotNullish);
return {
pathname: apiName.replace(/{/g, ":").replace(/}/g, ""),
operationId: (_d = (_c = api[method]) == null ? void 0 : _c.operationId) != null ? _d : "",
summary: (_f = (_e = api[method]) == null ? void 0 : _e.summary) != null ? _f : "",
tags: (_h = (_g = api[method]) == null ? void 0 : _g.tags) != null ? _h : ["default"],
method,
responses
};
}).filter(isNotNullish);
return [...acc, ...paths];
}, []);
return normalizedPaths;
});
}
});
// src/index.ts
import { existsSync as existsSync3, mkdirSync as mkdirSync2 } from "fs";
var main;
var init_src = __esm({
"src/index.ts"() {
"use strict";
init_generate();
init_writer();
main = (options) => __async(void 0, null, function* () {
if (options.baseDir && !existsSync3(options.baseDir)) {
mkdirSync2(options.baseDir, { recursive: true });
}
if (options.generateTarget.includes("api")) {
const generatedAPI = yield generateAPI(options);
if (generatedAPI === void 0) {
console.warn("generate api fail");
return;
}
writeHandlers(generatedAPI, options);
writeResponses(generatedAPI, options);
}
if (options.generateTarget.includes("schema")) {
const generatedSchema = yield generateSchema(options);
if (generatedSchema === void 0) {
console.warn("generate schema fail");
return;
}
writeSchema(generatedSchema, options);
}
if (options.isStatic === false)
writeFaker(options);
});
}
});
// node_modules/cac/dist/index.mjs
import { EventEmitter } from "events";
function toArr(any) {
return any == null ? [] : Array.isArray(any) ? any : [any];
}
function toVal(out, key, val, opts) {
var x, old = out[key], nxt = !!~opts.string.indexOf(key) ? val == null || val === true ? "" : String(val) : typeof val === "boolean" ? val : !!~opts.boolean.indexOf(key) ? val === "false" ? false : val === "true" || (out._.push((x = +val, x * 0 === 0) ? x : val), !!val) : (x = +val, x * 0 === 0) ? x : val;
out[key] = old == null ? nxt : Array.isArray(old) ? old.concat(nxt) : [old, nxt];
}
function mri2(args, opts) {
args = args || [];
opts = opts || {};
var k, arr, arg, name, val, out = { _: [] };
var i = 0, j = 0, idx = 0, len = args.length;
const alibi = opts.alias !== void 0;
const strict = opts.unknown !== void 0;
const defaults = opts.default !== void 0;
opts.alias = opts.alias || {};
opts.string = toArr(opts.string);
opts.boolean = toArr(opts.boolean);
if (alibi) {
for (k in opts.alias) {
arr = opts.alias[k] = toArr(opts.alias[k]);
for (i = 0; i < arr.length; i++) {
(opts.alias[arr[i]] = arr.concat(k)).splice(i, 1);
}
}
}
for (i = opts.boolean.length; i-- > 0; ) {
arr = opts.alias[opts.boolean[i]] || [];
for (j = arr.length; j-- > 0; )
opts.boolean.push(arr[j]);
}
for (i = opts.string.length; i-- > 0; ) {
arr = opts.alias[opts.string[i]] || [];
for (j = arr.length; j-- > 0; )
opts.string.push(arr[j]);
}
if (defaults) {
for (k in opts.default) {
name = typeof opts.default[k];
arr = opts.alias[k] = opts.alias[k] || [];
if (opts[name] !== void 0) {
opts[name].push(k);
for (i = 0; i < arr.length; i++) {
opts[name].push(arr[i]);
}
}
}
}
const keys = strict ? Object.keys(opts.alias) : [];
for (i = 0; i < len; i++) {
arg = args[i];
if (arg === "--") {
out._ = out._.concat(args.slice(++i));
break;
}
for (j = 0; j < arg.length; j++) {
if (arg.charCodeAt(j) !== 45)
break;
}
if (j === 0) {
out._.push(arg);
} else if (arg.substring(j, j + 3) === "no-") {
name = arg.substring(j + 3);
if (strict && !~keys.indexOf(name)) {
return opts.unknown(arg);
}
out[name] = false;
} else {
for (idx = j + 1; idx < arg.length; idx++) {
if (arg.charCodeAt(idx) === 61)
break;
}
name = arg.substring(j, idx);
val = arg.substring(++idx) || (i + 1 === len || ("" + args[i + 1]).charCodeAt(0) === 45 || args[++i]);
arr = j === 2 ? [name] : name;
for (idx = 0; idx < arr.length; idx++) {
name = arr[idx];
if (strict && !~keys.indexOf(name))
return opts.unknown("-".repeat(j) + name);
toVal(out, name, idx + 1 < arr.length || val, opts);
}
}
}
if (defaults) {
for (k in opts.default) {
if (out[k] === void 0) {
out[k] = opts.default[k];
}
}
}
if (alibi) {
for (k in out) {
arr = opts.alias[k] || [];
while (arr.length > 0) {
out[arr.shift()] = out[k];
}
}
}
return out;
}
var removeBrackets, findAllBrackets, getMriOptions, findLongest, padRight, camelcase, setDotProp, setByType, getFileName, camelcaseOptionName, CACError, Option, processArgs, platformInfo, Command, GlobalCommand, __assign, CAC, cac, dist_default;
var init_dist = __esm({
"node_modules/cac/dist/index.mjs"() {
"use strict";
removeBrackets = (v) => v.replace(/[<[].+/, "").trim();
findAllBrackets = (v) => {
const ANGLED_BRACKET_RE_GLOBAL = /<([^>]+)>/g;
const SQUARE_BRACKET_RE_GLOBAL = /\[([^\]]+)\]/g;
const res = [];
const parse = (match) => {
let variadic = false;
let value = match[1];
if (value.startsWith("...")) {
value = value.slice(3);
variadic = true;
}
return {
required: match[0].startsWith("<"),
value,
variadic
};
};
let angledMatch;
while (angledMatch = ANGLED_BRACKET_RE_GLOBAL.exec(v)) {
res.push(parse(angledMatch));
}
let squareMatch;
while (squareMatch = SQUARE_BRACKET_RE_GLOBAL.exec(v)) {
res.push(parse(squareMatch));
}
return res;
};
getMriOptions = (options) => {
const result = { alias: {}, boolean: [] };
for (const [index, option] of options.entries()) {
if (option.names.length > 1) {
result.alias[option.names[0]] = option.names.slice(1);
}
if (option.isBoolean) {
if (option.negated) {
const hasStringTypeOption = options.some((o, i) => {
return i !== index && o.names.some((name) => option.names.includes(name)) && typeof o.required === "boolean";
});
if (!hasStringTypeOption) {
result.boolean.push(option.names[0]);
}
} else {
result.boolean.push(option.names[0]);
}
}
}
return result;
};
findLongest = (arr) => {
return arr.sort((a, b) => {
return a.length > b.length ? -1 : 1;
})[0];
};
padRight = (str, length) => {
return str.length >= length ? str : `${str}${" ".repeat(length - str.length)}`;
};
camelcase = (input) => {
return input.replace(/([a-z])-([a-z])/g, (_, p1, p2) => {
return p1 + p2.toUpperCase();
});
};
setDotProp = (obj, keys, val) => {
let i = 0;
let length = keys.length;
let t = obj;
let x;
for (; i < length; ++i) {
x = t[keys[i]];
t = t[keys[i]] = i === length - 1 ? val : x != null ? x : !!~keys[i + 1].indexOf(".") || !(+keys[i + 1] > -1) ? {} : [];
}
};
setByType = (obj, transforms) => {
for (const key of Object.keys(transforms)) {
const transform = transforms[key];
if (transform.shouldTransform) {
obj[key] = Array.prototype.concat.call([], obj[key]);
if (typeof transform.transformFunction === "function") {
obj[key] = obj[key].map(transform.transformFunction);
}
}
}
};
getFileName = (input) => {
const m = /([^\\\/]+)$/.exec(input);
return m ? m[1] : "";
};
camelcaseOptionName = (name) => {
return name.split(".").map((v, i) => {
return i === 0 ? camelcase(v) : v;
}).join(".");
};
CACError = class extends Error {
constructor(message) {
super(message);
this.name = this.constructor.name;
if (typeof Error.captureStackTrace === "function") {
Error.captureStackTrace(this, this.constructor);
} else {
this.stack = new Error(message).stack;
}
}
};
Option = class {
constructor(rawName, description, config) {
this.rawName = rawName;
this.description = description;
this.config = Object.assign({}, config);
rawName = rawName.replace(/\.\*/g, "");
this.negated = false;
this.names = removeBrackets(rawName).split(",").map((v) => {
let name = v.trim().replace(/^-{1,2}/, "");
if (name.startsWith("no-")) {
this.negated = true;
name = name.replace(/^no-/, "");
}
return camelcaseOptionName(name);
}).sort((a, b) => a.length > b.length ? 1 : -1);
this.name = this.names[this.names.length - 1];
if (this.negated && this.config.default == null) {
this.config.default = true;
}
if (rawName.includes("<")) {
this.required = true;
} else if (rawName.includes("[")) {
this.required = false;
} else {
this.isBoolean = true;
}
}
};
processArgs = process.argv;
platformInfo = `${process.platform}-${process.arch} node-${process.version}`;
Command = class {
constructor(rawName, description, config = {}, cli) {
this.rawName = rawName;
this.description = description;
this.config = config;
this.cli = cli;
this.options = [];
this.aliasNames = [];
this.name = removeBrackets(rawName);
this.args = findAllBrackets(rawName);
this.examples = [];
}
usage(text) {
this.usageText = text;
return this;
}
allowUnknownOptions() {
this.config.allowUnknownOptions = true;
return this;
}
ignoreOptionDefaultValue() {
this.config.ignoreOptionDefaultValue = true;
return this;
}
version(version, customFlags = "-v, --version") {
this.versionNumber = version;
this.option(customFlags, "Display version number");
return this;
}
example(example) {
this.examples.push(example);
return this;
}
option(rawName, description, config) {
const option = new Option(rawName, description, config);
this.options.push(option);
return this;
}
alias(name) {
this.aliasNames.push(name);
return this;
}
action(callback) {
this.commandAction = callback;
return this;
}
isMatched(name) {
return this.name === name || this.aliasNames.includes(name);
}
get isDefaultCommand() {
return this.name === "" || this.aliasNames.includes("!");
}
get isGlobalCommand() {
return this instanceof GlobalCommand;
}
hasOption(name) {
name = name.split(".")[0];
return this.options.find((option) => {
return option.names.includes(name);
});
}
outputHelp() {
const { name, commands } = this.cli;
const {
versionNumber,
options: globalOptions,
helpCallback
} = this.cli.globalCommand;
let sections = [
{
body: `${name}${versionNumber ? `/${versionNumber}` : ""}`
}
];
sections.push({
title: "Usage",
body: ` $ ${name} ${this.usageText || this.rawName}`
});
const showCommands = (this.isGlobalCommand || this.isDefaultCommand) && commands.length > 0;
if (showCommands) {
const longestCommandName = findLongest(commands.map((command) => command.rawName));
sections.push({
title: "Commands",
body: commands.map((command) => {
return ` ${padRight(command.rawName, longestCommandName.length)} ${command.description}`;
}).join("\n")
});
sections.push({
title: `For more info, run any command with the \`--help\` flag`,
body: commands.map((command) => ` $ ${name}${command.name === "" ? "" : ` ${command.name}`} --help`).join("\n")
});
}
let options = this.isGlobalCommand ? globalOptions : [...this.options, ...globalOptions || []];
if (!this.isGlobalCommand && !this.isDefaultCommand) {
options = options.filter((option) => option.name !== "version");
}
if (options.length > 0) {
const longestOptionName = findLongest(options.map((option) => option.rawName));
sections.push({
title: "Options",
body: options.map((option) => {
return ` ${padRight(option.rawName, longestOptionName.length)} ${option.description} ${option.config.default === void 0 ? "" : `(default: ${option.config.default})`}`;
}).join("\n")
});
}
if (this.examples.length > 0) {
sections.push({
title: "Examples",
body: this.examples.map((example) => {
if (typeof example === "function") {
return example(name);
}
return example;
}).join("\n")
});
}
if (helpCallback) {
sections = helpCallback(sections) || sections;
}
console.log(sections.map((section) => {
return section.title ? `${section.title}:
${section.body}` : section.body;
}).join("\n\n"));
}
outputVersion() {
const { name } = this.cli;
const { versionNumber } = this.cli.globalCommand;
if (versionNumber) {
console.log(`${name}/${versionNumber} ${platformInfo}`);
}
}
checkRequiredArgs() {
const minimalArgsCount = this.args.filter((arg) => arg.required).length;
if (this.cli.args.length < minimalArgsCount) {
throw new CACError(`missing required args for command \`${this.rawName}\``);
}
}
checkUnknownOptions() {
const { options, globalCommand } = this.cli;
if (!this.config.allowUnknownOptions) {
for (const name of Object.keys(options)) {
if (name !== "--" && !this.hasOption(name) && !globalCommand.hasOption(name)) {
throw new CACError(`Unknown option \`${name.length > 1 ? `--${name}` : `-${name}`}\``);
}
}
}
}
checkOptionValue() {
const { options: parsedOptions, globalCommand } = this.cli;
const options = [...globalCommand.options, ...this.options];
for (const option of options) {
const value = parsedOptions[option.name.split(".")[0]];
if (option.required) {
const hasNegated = options.some((o) => o.negated && o.names.includes(option.name));
if (value === true || value === false && !hasNegated) {
throw new CACError(`option \`${option.rawName}\` value is missing`);
}
}
}
}
};
GlobalCommand = class extends Command {
constructor(cli) {
super("@@global@@", "", {}, cli);
}
};
__assign = Object.assign;
CAC = class extends EventEmitter {
constructor(name = "") {
super();
this.name = name;
this.commands = [];
this.rawArgs = [];
this.args = [];
this.options = {};
this.globalCommand = new GlobalCommand(this);
this.globalCommand.usage("<command> [options]");
}
usage(text) {
this.globalCommand.usage(text);
return this;
}
command(rawName, description, config) {
const command = new Command(rawName, description || "", config, this);
command.globalCommand = this.globalCommand;
this.commands.push(command);
return command;
}
option(rawName, description, config) {
this.globalCommand.option(rawName, description, config);
return this;
}
help(callback) {
this.globalCommand.option("-h, --help", "Display this message");
this.globalCommand.helpCallback = callback;
this.showHelpOnExit = true;
return this;
}
version(version, customFlags = "-v, --version") {
this.globalCommand.version(version, customFlags);
this.showVersionOnExit = true;
return this;
}
example(example) {
this.globalCommand.example(example);
return this;
}
outputHelp() {
if (this.matchedCommand) {
this.matchedCommand.outputHelp();
} else {
this.globalCommand.outputHelp();
}
}
outputVersion() {
this.globalCommand.outputVersion();
}
setParsedInfo({ args, options }, matchedCommand, matchedCommandName) {
this.args = args;
this.options = options;
if (matchedCommand) {
this.matchedCommand = matchedCommand;
}
if (matchedCommandName) {
this.matchedCommandName = matchedCommandName;
}
return this;
}
unsetMatchedCommand() {
this.matchedCommand = void 0;
this.matchedCommandName = void 0;
}
parse(argv = processArgs, {
run = true
} = {}) {
this.rawArgs = argv;
if (!this.name) {
this.name = argv[1] ? getFileName(argv[1]) : "cli";
}
let shouldParse = true;
for (const command of this.commands) {
const parsed = this.mri(argv.slice(2), command);
const commandName = parsed.args[0];
if (command.isMatched(commandName)) {
shouldParse = false;
const parsedInfo = __assign(__assign({}, parsed), {
args: parsed.args.slice(1)
});
this.setParsedInfo(parsedInfo, command, commandName);
this.emit(`command:${commandName}`, command);
}
}
if (shouldParse) {
for (const command of this.commands) {
if (command.name === "") {
shouldParse = false;
const parsed = this.mri(argv.slice(2), command);
this.setParsedInfo(parsed, command);
this.emit(`command:!`, command);
}
}
}
if (shouldParse) {
const parsed = this.mri(argv.slice(2));
this.setParsedInfo(parsed);
}
if (this.options.help && this.showHelpOnExit) {
this.outputHelp();
run = false;
this.