@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
55 lines (54 loc) • 1.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tupleTypeGenerator = tupleTypeGenerator;
const type_1 = require("../../../../type");
const utils_1 = require("./utils");
function tupleTypeGenerator(ast, ctx) {
const result = {
name: ast.keyName ?? '',
comment: (0, utils_1.setComment)(ast, ctx.options),
type: 'type',
code: '',
};
const params = [...ast.params];
const minItems = Math.max(ast.minItems ?? 0, params.length);
const maxItems = Math.max(ast.maxItems ?? minItems - 1, params.length);
const spreadParam = ast.spreadParam ?? {
type: type_1.ASTType.ANY,
};
while (params.length < minItems) {
params.push(spreadParam);
}
const lines = [`[`];
params.forEach((param, idx, arr) => {
ctx.pathKey = param.keyName;
const value = (0, utils_1.getValue)(ctx.next(param, ctx.options), ctx.options);
const endText = idx === arr.length - 1 ? '' : ',';
`${value}${endText}`.split('\n').forEach(line => lines.push(` ${line}`));
});
if (maxItems < minItems) {
ctx.pathKey = spreadParam.keyName;
lines.push(`,\n...Array<${(0, utils_1.getValue)(ctx.next(spreadParam, ctx.options), ctx.options)}>`);
}
lines.push(`]`);
if (maxItems > minItems) {
const nextParams = [...params];
for (let i = 0; i < maxItems - minItems; i += 1) {
nextParams.push(spreadParam);
const nextResult = tupleTypeGenerator({
...ast,
keyName: '',
params: nextParams,
minItems: nextParams.length,
maxItems: nextParams.length,
}, ctx);
lines.push(`\n| ${(0, utils_1.getValue)(nextResult, ctx.options)}`);
}
}
result.code = lines.join('\n');
return result;
}
exports.default = {
type: type_1.ASTType.TUPLE,
generate: tupleTypeGenerator,
};