UNPKG

@lcap/asl

Version:

NetEase Application Specific Language

357 lines 12.9 kB
"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.Param = void 0; const decorators_1 = require("../decorators"); const __1 = require(".."); const logic_1 = require("../../service/logic"); const dataTypeUtils_1 = require("../data/dataTypeUtils"); const basicTypes_1 = require("../data/basicTypes"); const common_1 = require("../../service/common"); /** * 逻辑输入参数 */ class Param extends __1.BaseVariable { /** * @param source 需要合并的部分参数 */ constructor(source) { super(); /** * 概念类型 */ this.level = __1.LEVEL_ENUM.param; /** * 请求类型 */ this.in = undefined; /** * 实体字段 Id */ this.entityFieldId = undefined; /** * 默认值 * 按 JSON string 处理 * - string: 666 -> '666' * - string: true -> 'true' * - number: 666 -> 666 * - boolean: true -> true */ this.defaultValue = undefined; source && this.assign(source); } /** * 添加逻辑输入参数 */ async create(none, actionOptions) { __1.config.defaultApp?.emit('saving'); const body = this.toJSON(); // convert2SchemaType(body.schema); body._posIndex = this.logic.params.indexOf(this); __1.utils.logger.debug('添加逻辑输入参数', body); const result = await logic_1.paramService.create({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Param.create', operationDesc: actionOptions?.actionDesc || `添加逻辑"${this.logic.name}"输入参数"${this.name}"`, }, body, }); // convert2RefType(result.schema); this.deepPick(result, ['id']); this.assign({ code: this.genCode() }); if (this.logic.interface) { const interfaceItem = __1.Vertex.getVertexByRef(this.logic.interface.id); // this.logic.interface.service.emit('interfacesChange'); interfaceItem.setParamAndReturn(); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 删除逻辑输入参数 */ async delete(none, actionOptions) { __1.config.defaultApp?.emit('saving'); if (this.id) { try { await logic_1.paramService.delete({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Param.create', operationDesc: actionOptions?.actionDesc || `删除逻辑"${this.logic.name}"输入参数"${this.name}"`, }, query: { loValId: this.id, logicId: this.logic.id, }, }); } catch (err) { await __1.config.defaultApp?.history.load(); throw err; } } const index = this.logic.params.indexOf(this); ~index && this.logic.params.splice(index, 1); this.destroy(); if (this.logic.interface) { // this.logic.interface.service.emit('interfacesChange'); this.logic.interface.setParamAndReturn(); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); } /** * 修改逻辑输入参数 */ async update(none, actionOptions, then) { __1.config.defaultApp?.emit('saving'); const body = this.toJSON(); // convert2SchemaType(body.schema); __1.utils.logger.debug('修改逻辑输入参数', body); const result = await logic_1.paramService.update({ headers: { appId: __1.config.defaultApp?.id, operationAction: actionOptions?.actionName || 'Param.update', operationDesc: actionOptions?.actionDesc || `修改逻辑输入参数"${this.name}"`, }, body, }); this.schema.id = result.schema.id; // convert2RefType(result.schema); // this.plainAssign(result); await then?.(); await __1.config.defaultApp?.history.load(); __1.config.defaultApp?.emit('saved'); return this; } /** * 设置逻辑输入参数名称 * @param name 名称 */ async setName(name) { const oldName = this.name; this.assign({ name }); await this.update(undefined, { actionDesc: `设置逻辑"${this.logic.name}"的输入参数"${oldName}"的名称为"${name}"`, }); if (this.logic.interface) { // this.logic.interface.service.emit('interfacesChange'); this.logic.interface.setParamAndReturn(); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } } /** * 设置逻辑输入参数描述 * @param description 描述 */ async setDescription(description) { this.assign({ description }); await this.update(undefined, { actionDesc: `设置逻辑"${this.logic.name}"的输入参数"${this.name}"的描述为"${description}"`, }); if (this.logic.interface) { this.logic.interface.service.emit('interfacesChange'); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } } /** * 查找schema 顶点被引用的逻辑顶点列表 */ async getSchemaUsage() { if (this.schema) { const schemaId = this.schema.type === 'genericType' ? this.id : this.schema.id; const result = await common_1.schemaService.getSchemaUsage({ query: { schemaId, valSource: 'logic', }, }); return result; } } /** * 设置逻辑输入参数的数据类型 */ async setDataType(schema) { // 用于update失败后还原数据 const originalData = { schema: Object.assign({}, this.schema), defaultValue: this.defaultValue, }; Object.assign(this.schema, { $ref: undefined, type: undefined, format: undefined, typeInstantiation: undefined, }); Object.assign(this.schema, schema); // if (this.defaultValue) this.assign({ defaultValue: basicTypes_1.getBasicTypeDefaultValue() }); try { await this.update(undefined, { actionDesc: `设置逻辑"${this.logic.name}"的输入参数"${this.name}"的数据类型为"${this.schema.typeKey}"`, }); this.genSchemaChildren(); if (this.logic.interface) { // this.logic.interface.service.emit('interfacesChange'); this.logic.interface.setParamAndReturn(); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } } catch (err) { this.assign(originalData); __1.config.defaultApp?.emit('saved'); throw err; } } /** * 设置逻辑输入参数为列表 * @param isArray */ async setAsList(isArray) { this.schema.isArray = isArray; this.assign({ defaultValue: basicTypes_1.getBasicTypeDefaultValue() }); await this.update(undefined, { actionDesc: `设置逻辑"${this.logic.name}"的输入参数"${this.name}"${isArray ? '为' : '不为'}列表`, }); this.genSchemaChildren(); if (this.logic.interface) { // this.logic.interface.service.emit('interfacesChange'); this.logic.interface.setParamAndReturn(); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } } /** * 设置逻辑输入参数的默认值 */ async setDefaultValue(defaultValue) { this.assign({ defaultValue }); await this.update(undefined, { actionDesc: `设置逻辑输入参数的默认值`, }); if (this.logic.interface) { this.logic.interface.service.emit('interfacesChange'); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } } /** * 设置逻辑输入参数的请求类型 * @param in 必须 */ async setIn(requestIn) { this.assign({ in: requestIn }); await this.update(undefined, { actionDesc: `设置逻辑输入参数"${this.logic.name}"的输入参数"${this.name}"的请求类型为"${requestIn}"`, }); if (this.logic.interface) { // this.logic.interface.service.emit('interfacesChange'); this.logic.interface.setParamAndReturn(); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } } /** * 设置逻辑输入参数是否必须 * @param required 必须 */ async setRequired(required) { this.assign({ required }); await this.update(undefined, { actionDesc: `设置逻辑"${this.logic.name}"的输入参数"${this.name}""${required ? '必须' : '不必须'}"`, }); if (this.logic.interface) { // this.logic.interface.service.emit('interfacesChange'); this.logic.interface.setParamAndReturn(); } else if (this.logic.view) { this.logic.view.page.service.emit('pageTreeChange'); this.logic.emit('change'); } } /** * 从后端 JSON 生成规范的 Param 对象 */ static from(source, logic) { dataTypeUtils_1.convert2RefType(source.schema); source.logic = logic; source.code = 'ID_' + source.id; const param = new Param(source); param.genSchemaChildren(); return param; } } __decorate([ decorators_1.immutable() ], Param.prototype, "level", void 0); __decorate([ decorators_1.immutable() ], Param.prototype, "in", void 0); __decorate([ decorators_1.immutable() ], Param.prototype, "entityFieldId", void 0); __decorate([ decorators_1.immutable() ], Param.prototype, "defaultValue", void 0); __decorate([ decorators_1.action('添加逻辑输入参数') ], Param.prototype, "create", null); __decorate([ decorators_1.action('删除逻辑输入参数') ], Param.prototype, "delete", null); __decorate([ decorators_1.action('设置逻辑输入参数名称') ], Param.prototype, "setName", null); __decorate([ decorators_1.action('设置逻辑输入参数描述') ], Param.prototype, "setDescription", null); __decorate([ decorators_1.action('设置逻辑输入参数的数据类型') ], Param.prototype, "setDataType", null); __decorate([ decorators_1.action('设置逻辑输入参数是否为列表') ], Param.prototype, "setAsList", null); __decorate([ decorators_1.action('设置逻辑输入参数的默认值') ], Param.prototype, "setDefaultValue", null); __decorate([ decorators_1.action('设置逻辑输入参数的请求类型') ], Param.prototype, "setIn", null); __decorate([ decorators_1.action('设置逻辑输入参数是否必须') ], Param.prototype, "setRequired", null); exports.Param = Param; exports.default = Param; //# sourceMappingURL=Param.js.map