@lcap/asl
Version:
NetEase Application Specific Language
480 lines • 15.1 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Interface = void 0;
const decorators_1 = require("../decorators");
const __1 = require("..");
const data_1 = require("../../service/data");
const common_1 = require("../../service/common");
/**
* 接口类
*/
class Interface extends __1.Vertex {
/**
* @param source 需要合并的部分参数
*/
constructor(source) {
super();
/**
* 概念类型
*/
this.level = __1.LEVEL_ENUM.interface;
/**
* Id
*/
this.id = undefined;
/**
* InterfaceKey
*/
this.key = undefined;
/**
* 名称
*/
this.name = undefined;
/**
* 协议
*/
this.protocol = undefined;
/**
* 地址
*/
this.host = undefined;
/**
* 端口
*/
this.port = undefined;
/**
* 路径
*/
this.path = undefined;
/**
* 方法
*/
this.method = undefined;
/**
* 描述
*/
this.description = undefined;
/**
* 请求数据
*/
this.exportedInterface = undefined;
/**
* 逻辑 Id
*/
this.logicId = undefined;
/**
* 逻辑
* @TODO 使用 excludedInJSON 有点问题,不同 class 相同字段会串
*/
this.logic = undefined;
/**
* Service 类型
*/
this.serviceType = undefined;
/**
* Service Id
*/
this.serviceId = undefined;
/**
* Service Name
*/
this.category = undefined;
/**
* 所属实体 Id
*/
this.entityId = undefined;
/**
* Service 名称
*/
this.serviceName = undefined;
/**
* Service
*/
this.service = undefined;
/**
* 参数
*/
this.parameters = undefined;
/**
* 返回值
*/
this.responses = undefined;
/**
* 请求数据
*/
this.requestBody = undefined;
/**
* 树组件的子节点字段
*/
this.moreChildrenFields = ['logic.params', 'logic.returns', 'logic.variables'];
/**
* 周边存在的名称
*/
this.existingNames = [];
source && this.assign(source);
}
/**
* 按当前 id 加载接口数据
* @requires this.id
*/
async load() {
const result = await data_1.interfaceService.loadDetail({
path: {
id: this.id,
},
});
this.assign(result);
return this;
}
/**
* 添加接口
*/
async create(none, actionOptions, then) {
__1.config.defaultApp?.emit('saving');
if (!this.logicId) {
this.logic.assign({ name: this.name });
await this.logic.create();
this.assign({
path: this.path || '/api/' + this.name,
logicId: this.logic.id,
});
}
const body = this.toPlainJSON();
body.logicId = this.logicId || this.logic.id;
__1.utils.logger.debug('添加接口', body);
const result = await data_1.interfaceService.create({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Interface.create',
operationDesc: actionOptions?.actionDesc || `添加接口"${this.name}"`,
},
body,
});
this.pick(result, ['id']);
this.assign({
key: `#/${this.service.id}/${this.id}`,
});
await then?.();
this.service.emit('interfacesChange');
this.service.emit('vertexIdToNameChange', this.id, this.name);
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
return this;
}
/**
* 删除接口
*/
async delete(none, actionOptions) {
__1.config.defaultApp?.emit('saving');
if (this.logic)
await this.logic.delete();
if (this.id) {
await data_1.interfaceService.delete({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Interface.delete',
operationDesc: actionOptions?.actionDesc || `删除接口"${this.name}"`,
},
path: {
id: this.id,
},
});
const index = this.service.interfaces.indexOf(this);
~index && this.service.interfaces.splice(index, 1);
this.service.globalLogic.removeInterface(this);
this.destroy();
this.service.syncStructures(); // 处理 CallQueryComponent 嵌套的情形
this.service.emit('interfacesChange');
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
}
else {
const index = this.service.interfaces.indexOf(this);
~index && this.service.interfaces.splice(index, 1);
this.service.globalLogic.removeInterface(this);
}
}
/**
* 更新接口
*/
async update(none, actionOptions, then) {
__1.config.defaultApp?.emit('saving');
const body = this.toPlainJSON();
await data_1.interfaceService.update({
headers: {
appId: __1.config.defaultApp?.id,
operationAction: actionOptions?.actionName || 'Interface.update',
operationDesc: actionOptions?.actionDesc || `修改接口"${this.name}"`,
},
body,
});
await then?.();
await __1.config.defaultApp?.history.load();
__1.config.defaultApp?.emit('saved');
return this;
}
/**
* 设置接口名称
*/
async setName(name) {
const originalData = {
name: this.name,
path: this.path,
};
this.assign({ name, path: '/api/' + name });
try {
await this.update(undefined, {
actionDesc: '设置接口名称',
});
this.service.emit('interfacesChange');
this.service.emit('vertexIdToNameChange', this.id, this.name);
}
catch (err) {
this.assign(originalData);
throw err;
}
}
/**
* 设置接口描述
*/
async setDescription(description) {
this.assign({ description });
await this.update(undefined, {
actionDesc: '设置接口描述',
});
this.service.emit('interfacesChange');
}
/**
* 设置接口路径
*/
async setPath(path) {
this.assign({ path });
await this.update(undefined, {
actionDesc: '设置接口路径',
});
this.service.emit('interfacesChange');
}
/**
* 设置接口方法
*/
async setMethod(method) {
this.assign({ method });
await this.update(undefined, {
actionDesc: '设置接口方法',
});
this.service.emit('interfacesChange');
}
/**
* 设置接口参数和返回值类型
*/
async setParamAndReturn() {
if (this.serviceType === 'micro' && this.exportedInterface) {
const logic = this.logic;
const exportedInterface = this.exportedInterface;
let bodyParameters = [];
if (exportedInterface.requestBody) {
const body = JSON.parse(exportedInterface.requestBody);
const schema = body?.content['application/json']?.schema;
const name = body?.description?.name || 'content';
const required = body?.required;
bodyParameters = [{
name,
schema,
in: 'body',
required,
defaultValue: '',
}];
}
const params = Object.values(JSON.parse(exportedInterface.parameters || '{}')).map((p) => ({
name: p.name,
schema: p.schema,
in: p.in,
required: true,
defaultValue: '',
}));
const parameters = params.concat(bodyParameters).filter((p) => !!p);
const finnalParameter = [];
if (logic) {
logic.params.forEach((p) => {
const finded = parameters.find((param) => param.name === p.name);
const isBasic = __1.isBasicType(p.schema);
if (finded) {
finnalParameter.push({
name: p.name,
schema: p.schema,
in: isBasic ? 'query' : 'body',
required: finded.required,
defaultValue: finded.defaultValue,
});
}
else {
finnalParameter.push({
name: p.name,
schema: p.schema,
in: isBasic ? 'query' : 'body',
required: !isBasic,
defaultValue: '',
});
}
});
}
let finnalResponses = '';
if (logic.returns.length > 0) {
const p = logic.returns[0];
const payload = {
description: {
name: p.name,
},
content: {
'application/json': {
schema: p.schema,
},
},
};
finnalResponses = JSON.stringify({
200: payload,
});
}
const { parameters: finnalP, requestBody: finnalB, } = __1.utils.resolveParameter(finnalParameter);
exportedInterface.parameters = finnalP;
exportedInterface.requestBody = finnalB;
exportedInterface.responses = finnalResponses;
await exportedInterface.update();
}
this.service.emit('interfacesChange');
}
/**
* 查找引用
*/
async findUsage() {
let id = this.id;
if (['entity', 'micro', 'process', 'processComponent'].includes(this.serviceType)) {
id = this.logicId;
}
const result = await common_1.findUsageService.findUsage({
query: {
id,
},
});
return result;
}
/**
* 从后端 JSON 生成规范的 Interface 对象
*/
static from(source, service) {
const item = new Interface(source);
item.assign({
service,
expanded: false,
key: `#/${service.id}/${item.id}`,
});
item.logic && item.assign({ logic: __1.Logic.from(item.logic, item) });
return item;
}
}
__decorate([
decorators_1.immutable()
], Interface.prototype, "level", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "id", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "key", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "name", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "protocol", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "host", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "port", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "path", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "method", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "description", void 0);
__decorate([
decorators_1.excludedInJSON(),
decorators_1.immutable()
], Interface.prototype, "exportedInterface", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "logicId", void 0);
__decorate([
decorators_1.excludedInJSON(),
decorators_1.immutable()
], Interface.prototype, "logic", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "serviceType", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "serviceId", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "category", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "serviceName", void 0);
__decorate([
decorators_1.circular(),
decorators_1.immutable()
], Interface.prototype, "service", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "parameters", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "responses", void 0);
__decorate([
decorators_1.immutable()
], Interface.prototype, "requestBody", void 0);
__decorate([
decorators_1.excludedInJSON(),
decorators_1.immutable()
], Interface.prototype, "moreChildrenFields", void 0);
__decorate([
decorators_1.excludedInJSON()
], Interface.prototype, "existingNames", void 0);
__decorate([
decorators_1.action('添加接口')
], Interface.prototype, "create", null);
__decorate([
decorators_1.action('删除接口')
], Interface.prototype, "delete", null);
__decorate([
decorators_1.action('设置接口名称')
], Interface.prototype, "setName", null);
__decorate([
decorators_1.action('设置接口描述')
], Interface.prototype, "setDescription", null);
__decorate([
decorators_1.action('设置接口路径')
], Interface.prototype, "setPath", null);
__decorate([
decorators_1.action('设置接口方法')
], Interface.prototype, "setMethod", null);
__decorate([
decorators_1.action('设置接口参数和返回值')
], Interface.prototype, "setParamAndReturn", null);
__decorate([
decorators_1.action('查找引用')
], Interface.prototype, "findUsage", null);
exports.Interface = Interface;
exports.default = Interface;
//# sourceMappingURL=Interface.js.map