openapi-ts-mock-generator
Version:
typescript mock data generator based openapi
240 lines • 9.02 kB
JavaScript
;
function _array_like_to_array(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
return arr2;
}
function _array_with_holes(arr) {
if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _iterable_to_array_limit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _non_iterable_rest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _object_spread(target) {
for(var i = 1; i < arguments.length; i++){
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_define_property(target, key, source[key]);
});
}
return target;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function(sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _object_spread_props(target, source) {
source = source != null ? source : {};
if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function(key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
return target;
}
function _sliced_to_array(arr, i) {
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
if (!o) return;
if (typeof o === "string") return _array_like_to_array(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = function(target, all) {
for(var name in all)__defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = function(to, from, except, desc) {
if (from && typeof from === "object" || typeof from === "function") {
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
try {
var _loop = function() {
var key = _step.value;
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: function() {
return from[key];
},
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
};
for(var _iterator = __getOwnPropNames(from)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally{
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally{
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
return to;
};
var __toCommonJS = function(mod) {
return __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
};
// src/utils/code-utils.ts
var code_utils_exports = {};
__export(code_utils_exports, {
compressCode: function() {
return compressCode;
},
generateInterface: function() {
return generateInterface;
},
generateTypeAlias: function() {
return generateTypeAlias;
},
toTypeScriptCode: function() {
return toTypeScriptCode;
}
});
module.exports = __toCommonJS(code_utils_exports);
var toTypeScriptCode = function(param, options) {
var _options_depth = options.depth, depth = _options_depth === void 0 ? 0 : _options_depth, isStatic = options.isStatic;
var prefixSpace = " ".repeat(depth * 2);
if (param === null) {
return "null";
}
if (Array.isArray(param)) {
var results = param.map(function(elem) {
return toTypeScriptCode(elem, _object_spread_props(_object_spread({}, options), {
depth: depth + 1
}));
}).join(",\n" + prefixSpace);
return [
"[",
results,
"]"
].join("\n" + prefixSpace);
}
if (typeof param === "object") {
var results1 = Object.entries(param).map(function(param) {
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
return generateObjectProperty(key, value, options, prefixSpace);
}).join("\n" + prefixSpace);
return [
"{",
"".concat(results1),
"}"
].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 '"'.concat(param.slice(0, -" as const".length), '" as const');
}
}
return JSON.stringify(param);
};
var shouldApplyNullableExtension = function(value, isOptional) {
if (!isOptional) return false;
if (value === null) return true;
if (typeof value === "string" && value.includes(",null")) {
return true;
}
return false;
};
var generateObjectProperty = function(key, value, options, prefixSpace) {
var isOptional = options.isOptional, _options_depth = options.depth, depth = _options_depth === void 0 ? 0 : _options_depth;
var shouldApplyNullable = shouldApplyNullableExtension(value, isOptional);
var nullableTypeExtensionStart = shouldApplyNullable ? "...(faker.datatype.boolean() ? {\n".concat(prefixSpace) : "";
var nullableTypeExtensionEnd = shouldApplyNullable ? "\n".concat(prefixSpace, "} : {})") : "";
var propertyValue = toTypeScriptCode(value, _object_spread_props(_object_spread({}, options), {
depth: depth + 1
}));
return "".concat(nullableTypeExtensionStart).concat(prefixSpace).concat(key, ": ").concat(propertyValue).concat(nullableTypeExtensionEnd, ",");
};
var compressCode = function(code) {
return code.replace(/\n/g, " ").replace(/\s+/g, " ").replace(/\s\./g, ".").trim();
};
var generateInterface = function(name, properties) {
var props = Object.entries(properties).map(function(param) {
var _param = _sliced_to_array(param, 2), key = _param[0], type = _param[1];
return " ".concat(key, ": ").concat(type);
}).join("\n");
return "interface ".concat(name, " {\n").concat(props, "\n}");
};
var generateTypeAlias = function(name, type) {
return "type ".concat(name, " = ").concat(type);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
compressCode: compressCode,
generateInterface: generateInterface,
generateTypeAlias: generateTypeAlias,
toTypeScriptCode: toTypeScriptCode
});
//# sourceMappingURL=code-utils.js.map