infinity-forge
Version:
85 lines • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getParams = getParams;
function formatType(schema, isRequired) {
if (schema.type === 'array') {
var itemsType = formatType(schema.items, true);
return isRequired ? "".concat(itemsType, "[]") : "".concat(itemsType, "[] | undefined");
}
if (schema.type === 'object') {
if (schema.additionalProperties) {
var valueType = formatType(schema.additionalProperties, true);
return isRequired ? "Record<string, ".concat(valueType, ">") : "Record<string, ".concat(valueType, "> | undefined");
}
return isRequired ? 'object' : 'object | undefined';
}
switch (schema.type) {
case 'integer':
// Handle integer formats like int32
if (schema.format === 'int32') {
return isRequired ? 'number' : 'number | undefined';
}
return isRequired ? 'number' : 'number | undefined';
case 'string':
return isRequired ? 'string' : 'string | undefined';
case 'boolean':
return isRequired ? 'boolean' : 'boolean | undefined';
default:
return isRequired ? 'any' : 'any | undefined';
}
}
function setNestedValue(obj, path, value) {
var parts = path.split('.');
var current = obj;
for (var i = 0; i < parts.length - 1; i++) {
var part = parts[i];
if (!current[part]) {
current[part] = {};
}
current = current[part];
}
current[parts[parts.length - 1]] = value;
}
// Função para transformar notação de colchetes em objetos aninhados
function parseBracketNotation(name) {
var bracketMatch = name.match(/^([^\[]+)\[([^\]]+)\]$/);
if (bracketMatch) {
return {
parent: bracketMatch[1],
child: bracketMatch[2]
};
}
return {
parent: null,
child: name
};
}
function getParams(methodInfo) {
var params = (methodInfo === null || methodInfo === void 0 ? void 0 : methodInfo.parameters) || [];
var result = {};
params.forEach(function (param) {
var _a;
var paramObject = param;
var typedParam = paramObject === null || paramObject === void 0 ? void 0 : paramObject.schema;
var isRequired = (_a = paramObject.required) !== null && _a !== void 0 ? _a : false;
var formattedType = formatType(typedParam, isRequired);
// Handle bracket notation (ex: price[priceBRLMin])
var _b = parseBracketNotation(param.name), parent = _b.parent, child = _b.child;
if (parent) {
// Se tem notação de colchetes, cria objeto aninhado
if (!result[parent]) {
result[parent] = {};
}
result[parent][child] = formattedType;
}
else if (param.name.includes('.')) {
// Handle nested properties with dot notation
setNestedValue(result, param.name, formattedType);
}
else {
result[param.name] = formattedType;
}
});
return result;
}
//# sourceMappingURL=get-params.js.map