UNPKG

@alova/wormhole

Version:

More modern openAPI generating solution for alova.js

155 lines (154 loc) 6.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.templateParser = exports.TemplateParser = void 0; const loader_1 = require("../../../core/loader"); const helper_1 = require("../../../helper"); const document_1 = require("../../../helper/document"); const openapi_1 = require("../../../utils/openapi"); const helper_2 = require("./helper"); class TemplateParser { constructor() { this.name = 'templateParser'; this.schemasMap = new Map(); this.operationIdSet = new Set(); this.pathMap = []; this.refNameMap = new Map(); this.openApiHelper = new document_1.OpenApiHelper(); } async parse(document, options) { this.document = document; this.options = options; const templateData = await this.parseBaseInfo(); await this.parseApiMethods(this.openApiHelper.load(document).getApiMethods(), templateData); this.clear(); return templateData; } clear() { this.schemasMap.clear(); this.operationIdSet.clear(); this.pathMap.splice(0, this.pathMap.length); this.refNameMap.clear(); } async parseBaseInfo() { const generatorHelper = await helper_1.GeneratorHelper.load(this.options.generatorConfig); const alovaVersion = generatorHelper.getAlovaVersion(this.options.projectPath); const templateType = generatorHelper.getTemplateType(this.options.projectPath); const config = generatorHelper.getConfig(); const commentText = await helper_1.TemplateHelper.load({ type: templateType, version: alovaVersion, }).readAndRenderTemplate('comment', this.document, { root: true, hasVersion: false, }); const templateData = { ...this.document, baseUrl: this.document.servers?.[0]?.url || '', pathsArr: [], pathApis: [], schemas: [], commentText, alovaVersion, global: config.global ?? 'Apis', globalHost: config.globalHost ?? 'globalThis', useImportType: config.useImportType ?? false, moduleType: helper_1.TemplateHelper.getModuleType(templateType), type: templateType, }; return templateData; } async transformApiMethods(apiMethod) { const { url, method, operationObject } = apiMethod; operationObject.operationId = loader_1.standardLoader.transformOperationId(operationObject, { url, method, map: this.operationIdSet, }); operationObject.tags = loader_1.standardLoader.transformTags(operationObject.tags); const result = await (0, helper_2.transformApiMethods)(apiMethod, { document: this.document, refNameMap: this.refNameMap, config: this.options.generatorConfig, map: this.pathMap, }); return result; } async parseApiMethods(apiMethods, templateData) { const apiMethodArray = (await Promise.all(apiMethods.map(apiMethod => this.transformApiMethods(apiMethod)))).filter(apiMethod => !!apiMethod); const refsMap = Object.fromEntries(this.refNameMap); const usedRefs = this.openApiHelper .saveApiMethods(apiMethodArray) .filterUsedReferences(Object.keys(refsMap)); this.refNameMap = new Map(Object.entries((0, openapi_1.optimizeRefsMap)(refsMap, usedRefs))); (await Promise.all(apiMethodArray.map(apiMethod => this.transformApis(apiMethod, templateData)))) .flat() .forEach((api) => { this.parseApi(api, templateData); }); templateData.schemas = [...new Set(this.schemasMap.values())]; } async transformApis(apiMethod, templateData) { const { method, url: path, operationObject } = apiMethod; const apis = []; const tags = operationObject.tags ?? []; for (const tag of tags) { const { queryParameters, queryParametersComment, pathParameters, pathParametersComment } = await (0, helper_2.parseParameters)(operationObject.parameters, { document: this.document, config: this.options.generatorConfig, schemasMap: this.schemasMap, refNameMap: this.refNameMap, }); const { requestComment, requestName } = await (0, helper_2.parseRequestBody)(operationObject.requestBody, { document: this.document, config: this.options.generatorConfig, schemasMap: this.schemasMap, refNameMap: this.refNameMap, }); const { responseComment, responseName } = await (0, helper_2.parseResponse)(operationObject.responses, { document: this.document, config: this.options.generatorConfig, schemasMap: this.schemasMap, refNameMap: this.refNameMap, }); const api = { tag, method: method.toUpperCase(), summary: operationObject.summary?.replace(/\n/g, '') ?? '', path, name: operationObject.operationId ?? '', responseName, requestName, pathKey: `${tag}.${operationObject.operationId}`, queryParameters, queryParametersComment, pathParameters, pathParametersComment, responseComment, requestComment, defaultValue: '', global: templateData.global, }; api.defaultValue = await loader_1.defaultValueLoader.transformApi(api); apis.push(api); } return apis; } parseApi(api, templateData) { templateData.pathsArr.push({ key: api.pathKey, method: api.method, path: api.path, }); let tagApis = templateData.pathApis.find(item => item.tag === api.tag); if (!tagApis) { tagApis = { tag: api.tag, apis: [], }; templateData.pathApis.push(tagApis); } tagApis.apis.push(api); } } exports.TemplateParser = TemplateParser; exports.templateParser = new TemplateParser();