vulcain-corejs
Version:
Vulcain micro-service framework
121 lines (119 loc) • 5.78 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 common_1 = require("./common");
const annotations_1 = require("../di/annotations");
const commandRuntimeError_1 = require("./../errors/commandRuntimeError");
const response_1 = require("./response");
class QueryManager {
constructor(container) {
this.container = container;
}
/**
* 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: undefined,
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(ctx, 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 = yield schema.validateAsync(ctx, 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, void 0, function* () {
let info = this.getInfoHandler(query, ctx.container);
if (info.kind !== "query")
return new response_1.BadRequestResponse("Action handler must be requested with POST.");
let logger = this.container.get(annotations_1.DefaultServiceNames.Logger);
logger.logAction(ctx, "Log", query.action, JSON.stringify(query));
try {
let errors = yield this.validateRequestData(ctx, info, query);
if (errors && errors.length > 0) {
return new response_1.BadRequestResponse(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);
let res = this.createResponse(ctx, query);
if (!(result instanceof response_1.HttpResponse)) {
if (result && Array.isArray(result)) {
res.total = result.length;
}
res.value = common_1.HandlerFactory.obfuscateSensibleData(this.domain, this.container, result);
return new response_1.VulcainResponse(res);
}
else if (result.contentType === response_1.HttpResponse.VulcainContentType) {
result.content = common_1.HandlerFactory.obfuscateSensibleData(this.domain, this.container, result.content);
}
return result;
}
catch (e) {
let error = (e instanceof commandRuntimeError_1.CommandRuntimeError) ? e.error : e;
let res = this.createResponse(ctx, query, error);
return new response_1.HttpResponse(res, e.statusCode || 500);
}
});
}
}
exports.QueryManager = QueryManager;
//# sourceMappingURL=query.js.map
;