openapi-ts-mock-generator
Version:
typescript mock data generator based openapi
88 lines (87 loc) • 3.28 kB
JavaScript
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));
// 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},`;
};
var compressCode = (code) => {
return code.replace(/\n/g, " ").replace(/\s+/g, " ").replace(/\s\./g, ".").trim();
};
var generateInterface = (name, properties) => {
const props = Object.entries(properties).map(([key, type]) => ` ${key}: ${type}`).join("\n");
return `interface ${name} {
${props}
}`;
};
var generateTypeAlias = (name, type) => {
return `type ${name} = ${type}`;
};
export {
compressCode,
generateInterface,
generateTypeAlias,
toTypeScriptCode
};
//# sourceMappingURL=code-utils.mjs.map