vulcain-corejs
Version:
Vulcain micro-service framework
111 lines (109 loc) • 4.99 kB
JavaScript
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 vulcain_configurationsjs_1 = require('vulcain-configurationsjs');
const common_1 = require('./common');
const os = require('os');
const command_1 = require('../commands/command/command');
const annotations_1 = require('../di/annotations');
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 = error;
return res;
}
getMetadata(command) {
let info = QueryManager.handlerFactory.getInfo(null, command.domain, command.schema, command.action);
return info.metadata;
}
validateRequestData(info, query) {
return __awaiter(this, void 0, void 0, function* () {
let errors;
let inputSchema = info.metadata.inputSchema;
if (inputSchema) {
let schema = inputSchema && this.domain.getSchema(inputSchema);
if (schema) {
query.inputSchema = schema.name;
// Custom binding if any
query.data = schema.bind(query.data);
errors = this.domain.validate(query.data, schema);
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.data, query.action);
if (!errors)
errors = info.handler[altMethodName] && (yield info.handler[altMethodName](query.data, query.action));
if (errors && !Array.isArray(errors))
errors = [errors];
}
}
return errors;
});
}
runAsync(query, ctx) {
return __awaiter(this, void 0, void 0, function* () {
let info = QueryManager.handlerFactory.getInfo(ctx.container, query.domain, query.schema, query.action);
vulcain_configurationsjs_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.data);
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 command_1.CommandRuntimeError) ? e.error : e;
return this.createResponse(ctx, query, { message: error.message });
}
});
}
}
QueryManager.handlerFactory = new common_1.HandlerFactory();
exports.QueryManager = QueryManager;
//# sourceMappingURL=query.js.map
;