UNPKG

@alova/wormhole

Version:

More modern openAPI generating solution for alova.js

60 lines (59 loc) 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.recordTypeGenerator = recordTypeGenerator; exports.normalInterfaceGenerator = normalInterfaceGenerator; exports.interfaceTypeGenerator = interfaceTypeGenerator; const loader_1 = require("../../../../core/loader"); const type_1 = require("../../../../type"); const utils_1 = require("./utils"); function recordTypeGenerator(ast, ctx) { const result = { name: ast.keyName ?? '', comment: (0, utils_1.setComment)(ast, ctx.options), type: 'type', code: '', }; let addParamsStr = 'any'; if (ast.addParams) { ctx.pathKey = '[key: string]'; const nextResult = ctx.next(ast.addParams, ctx.options); addParamsStr = (0, utils_1.getValue)(nextResult, ctx.options); } result.code = `Record<string, ${addParamsStr}>`; return result; } function normalInterfaceGenerator(ast, ctx) { const result = { name: ast.keyName ?? '', comment: (0, utils_1.setComment)(ast, ctx.options), type: 'interface', code: '', }; const lines = [`{`]; ast.params.forEach((param) => { const optionalFlag = param.isRequired ? '' : '?'; const keyName = loader_1.standardLoader.validate(param.keyName) ? param.keyName : `"${param.keyName}"`; ctx.pathKey = keyName; const nextResult = ctx.next(param.ast, ctx.options); const value = (0, utils_1.getValue)(nextResult, ctx.options); `${nextResult.comment ?? ''}${keyName}${optionalFlag}:${value}`.split('\n').forEach(line => lines.push(` ${line}`)); }); if (ast.addParams) { ctx.pathKey = '[key: string]'; const nextResult = ctx.next(ast.addParams, ctx.options); lines.push(`[key: string]:${(0, utils_1.getValue)(nextResult, ctx.options) || 'any'}`); } lines.push(`}`); result.code = lines.join('\n'); return result; } function interfaceTypeGenerator(ast, ctx) { if (ast.params.length > 0) { return normalInterfaceGenerator(ast, ctx); } return recordTypeGenerator(ast, ctx); } exports.default = { type: type_1.ASTType.INTERFACE, generate: interfaceTypeGenerator, };