infinity-forge
Version:
55 lines • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRequestBody = getRequestBody;
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 getRequestBody(methodInfo) {
var _a, _b;
var requestBody = methodInfo === null || methodInfo === void 0 ? void 0 : methodInfo.requestBody;
var schemaBody = (_b = (_a = requestBody === null || requestBody === void 0 ? void 0 : requestBody.content) === null || _a === void 0 ? void 0 : _a["application/json"]) === null || _b === void 0 ? void 0 : _b.schema;
if (!schemaBody) {
return null;
}
// If it's a reference, return it as is
if ('$ref' in schemaBody) {
return schemaBody;
}
// If it's an object with properties, format each property
if (schemaBody.type === 'object' && schemaBody.properties) {
var formattedProperties = Object.entries(schemaBody.properties).reduce(function (acc, _a) {
var _b, _c;
var key = _a[0], value = _a[1];
acc[key] = formatType(value, (_c = (_b = schemaBody.required) === null || _b === void 0 ? void 0 : _b.includes(key)) !== null && _c !== void 0 ? _c : false);
return acc;
}, {});
return formattedProperties;
}
// For other types, format the schema directly
return formatType(schemaBody, true);
}
//# sourceMappingURL=get-request-body.js.map