UNPKG

vulcain-corejs

Version:
118 lines (116 loc) 5.35 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments)).next()); }); }; const common_1 = require('./common'); const os = require('os'); const annotations_1 = require('../di/annotations'); const system_1 = require('./../configurations/globals/system'); const commandRuntimeError_1 = require('./../errors/commandRuntimeError'); class QueryManager { constructor(container) { this.container = container; this._hostname = os.hostname(); } /** * Get the current domain model * @returns {Domain} */ get domain() { if (!this._domain) { this._domain = this.container.get(annotations_1.DefaultServiceNames.Domain); } return this._domain; } createResponse(ctx, query, error) { let res = { tenant: ctx.tenant, userContext: query.userContext, source: this._hostname, schema: query.schema, domain: query.domain, action: query.action, maxByPage: query.maxByPage, page: query.page }; if (error) res.error = { message: error.message, errors: error.errors }; return res; } getInfoHandler(command, container) { if (!this._serviceDescriptors) { this._serviceDescriptors = this.container.get(annotations_1.DefaultServiceNames.ServiceDescriptors); } let info = this._serviceDescriptors.getHandlerInfo(container, command.schema, command.action); return info; } validateRequestData(info, query) { return __awaiter(this, void 0, void 0, function* () { let errors; let inputSchema = info.metadata.inputSchema; if (inputSchema && inputSchema !== "none") { let schema = inputSchema && this.domain.getSchema(inputSchema); if (schema) { query.inputSchema = schema.name; // Custom binding if any query.params = schema.bind(query.params); errors = schema.validate(query.params); if (errors && !Array.isArray(errors)) errors = [errors]; } if (!errors || errors.length === 0) { // Search if a method naming validate<schema>[Async] exists let methodName = 'validate' + inputSchema; let altMethodName = methodName + 'Async'; errors = info.handler[methodName] && info.handler[methodName](query.params, query.action); if (!errors) errors = info.handler[altMethodName] && (yield info.handler[altMethodName](query.params, query.action)); if (errors && !Array.isArray(errors)) errors = [errors]; } } return errors; }); } runAsync(query, ctx) { return __awaiter(this, void 0, Promise, function* () { let info = this.getInfoHandler(query, ctx.container); if (info.kind !== "query") throw new Error("Action handler must be requested with POST."); system_1.System.log.write(ctx, { runQuery: query }); try { let errors = yield this.validateRequestData(info, query); if (errors && errors.length > 0) return this.createResponse(ctx, query, { message: "Validation errors", errors: errors }); if (ctx.user) query.userContext = { id: ctx.user.id, scopes: ctx.user.scopes, name: ctx.user.name, displayName: ctx.user.displayName, tenant: ctx.user.tenant }; query.schema = info.metadata.schema; query.correlationId = ctx.correlationId; query.correlationPath = ctx.correlationPath; info.handler.requestContext = ctx; info.handler.query = query; let result = yield info.handler[info.method](query.params); if (result instanceof common_1.HttpResponse) { return result; // skip normal process } let res = this.createResponse(ctx, query); res.value = common_1.HandlerFactory.obfuscateSensibleData(this.domain, this.container, result); if (result && Array.isArray(result)) { res.total = result.length; } return res; } catch (e) { let error = (e instanceof commandRuntimeError_1.CommandRuntimeError) ? e.error : e; return this.createResponse(ctx, query, error); } }); } } exports.QueryManager = QueryManager; //# sourceMappingURL=query.js.map