@alova/wormhole
Version:
More modern openAPI generating solution for alova.js
129 lines (128 loc) • 5.42 kB
JavaScript
"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 helper_2 = require("./helper");
class TemplateParser {
constructor() {
this.name = 'templateParser';
this.schemasMap = new Map();
this.operationIdSet = new Set();
this.pathMap = [];
}
async parse(document, options) {
this.document = document;
this.options = options;
const templateData = await this.parseBaseInfo();
await this.parseApiMethods(helper_1.OpenApiHelper.load(document).getApiMethods(), templateData);
this.clear();
return templateData;
}
clear() {
this.schemasMap.clear();
this.operationIdSet.clear();
this.pathMap.splice(0, this.pathMap.length);
}
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,
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);
(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, this.document, this.options.generatorConfig, this.schemasMap);
const { requestComment, requestName } = await (0, helper_2.parseRequestBody)(operationObject.requestBody, this.document, this.options.generatorConfig, this.schemasMap);
const { responseComment, responseName } = await (0, helper_2.parseResponse)(operationObject.responses, this.document, this.options.generatorConfig, this.schemasMap);
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();