nralcm
Version:
This is a framework based on NodeJs to manage rest api request lifecycle
137 lines (136 loc) • 6.98 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
require("reflect-metadata/Reflect");
const functions_1 = require("../../common/functions");
const route_mapping_1 = require("../route-mapping");
const route_mapping_2 = require("../route-mapping");
const dependency_resolver_1 = require("../dependency-resolver");
const __2 = require("..");
const filter_1 = require("../filter");
const exceptions_1 = require("../../exceptions");
const Observable_1 = require("rxjs/Observable");
const enums_1 = require("../../common/enums");
/**
* Handler for rest api
*/
class RestApiHandler {
constructor(restApiConfiguration) {
this.restApiConfiguration = restApiConfiguration;
if (!this.restApiConfiguration.HttpResponseHandler) {
this.restApiConfiguration.HttpResponseHandler = new __1.DefaultHttpResponseHandler();
}
if (!this.restApiConfiguration.ExceptionHandler) {
this.restApiConfiguration.ExceptionHandler = new __1.ExceptionHandler();
}
if (!this.restApiConfiguration.AuthHandler) {
this.restApiConfiguration.AuthHandler = new __1.AuthHandler(this.restApiConfiguration);
}
if (!this.restApiConfiguration.ModelValidationHandler) {
this.restApiConfiguration.ModelValidationHandler = new __1.ModelValidationHandler();
}
}
/**
* Process request of rest api
* @param req Request object
* @param res Response object
*/
processRequest(req, res) {
const context = functions_1.getContext(req, res);
try {
// passing HttpContext to HttpRequestHandler
if (this.restApiConfiguration.HttpRequestHandler) {
this.restApiConfiguration.HttpRequestHandler.handle(context);
// checking that request sent from HttpRequestHandler. If sent return from here
if (context.response.headersSent) {
return;
}
}
// Mapping request with controller
const route = route_mapping_1.ControllerMapper(context, this.restApiConfiguration);
if (route) {
// injecting controller and controller object in HttpContext object
context.controller = route.controller;
context.controllerObject = new route.controller();
}
// Mapping request with api method
const routeDescriptor = route_mapping_2.ApiMethodMapper(context, this.restApiConfiguration);
context.routeDescriptor = routeDescriptor;
// sending request to Auth handler
if (this.restApiConfiguration.AuthHandler) {
const authResult = this.restApiConfiguration.AuthHandler.handle(context);
if (!authResult) {
throw new exceptions_1.UnAuthenticateException();
}
}
// validating request body, params and query string
if (this.restApiConfiguration.ModelValidationHandler) {
const modelValidationHandlerResult = this.restApiConfiguration.ModelValidationHandler.validate(context, routeDescriptor);
if (modelValidationHandlerResult.length && !context.response.headersSent) {
// throw new BadRequestException(context, modelValidationHandlerResult, this.restApiConfiguration);
throw new exceptions_1.BadRequestException(modelValidationHandlerResult.map(val => val.errorMessage));
}
}
// custom filters execution
const filterExecuter = new filter_1.FilterExecuter(context, routeDescriptor, this.restApiConfiguration.Filters);
filterExecuter.executeBeforeActionExceduted();
let httpResponse = functions_1.getHttpResponse(context);
// Resolve Dependency of Controller
let di = new dependency_resolver_1.DependencyInjection(context, httpResponse);
di.inject();
// get arugments array to call api method
const args = Reflect.getMetadata(__2.Constants.metadata.args, context.controllerObject);
const method = routeDescriptor.descriptor.value;
const data = method.apply(context.controllerObject, args);
if (!context.response.headersSent && !(data instanceof Observable_1.Observable)) {
filterExecuter.executeAfterActionExceduted();
if (!context.response.headersSent) {
if (!context.httpResponseMessage) {
context.controllerObject.response.send(data);
}
this.restApiConfiguration.HttpResponseHandler.sendResponse(context);
}
}
else if (data instanceof Observable_1.Observable) {
let sub$ = data.subscribe(data => {
if (!context.httpResponseMessage) {
context.httpResponseMessage = new __1.HttpResponseMessage();
context.httpResponseMessage.body = data;
context.httpResponseMessage.statusCode = enums_1.StatusCode.Ok;
}
this.restApiConfiguration.HttpResponseHandler.sendResponse(context);
sub$.unsubscribe();
}, error => {
if (error && error.constructor && error.constructor.name === "Error") {
if (this.restApiConfiguration.ExceptionHandler && !context.response.headersSent) {
this.restApiConfiguration.ExceptionHandler.handleException(context, error);
}
}
else {
if (error && error.httpResponseMessage && !context.response.headersSent) {
context.httpResponseMessage = error.httpResponseMessage;
this.restApiConfiguration.HttpResponseHandler.sendResponse(context);
}
}
// this.restApiConfiguration.ExceptionHandler.handleException(context, error);
sub$.unsubscribe();
});
}
return;
}
catch (e) {
if (e && e.constructor && e.constructor.name === "Error") {
if (this.restApiConfiguration.ExceptionHandler && !context.response.headersSent) {
this.restApiConfiguration.ExceptionHandler.handleException(context, e);
}
}
else {
if (e && e.httpResponseMessage && !context.response.headersSent) {
context.httpResponseMessage = e.httpResponseMessage;
this.restApiConfiguration.HttpResponseHandler.sendResponse(context);
}
}
}
}
}
exports.RestApiHandler = RestApiHandler;