nralcm
Version:
This is a framework based on NodeJs to manage rest api request lifecycle
43 lines (42 loc) • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
/**
* HttpConfiguration class provides methods to configure application
*/
class HttpConfiguration {
constructor(restApiConfiguration) {
this.restApiConfiguration = restApiConfiguration;
this.handlers = [];
this.addHandler("/api/*", new __1.RestApiHandler(this.restApiConfiguration));
}
/**
* Method to add handler
* @param url string or regex
* @param handler IHttpHandler
*/
addHandler(url, handler) {
this.handlers.push([url, handler]);
}
/**
* Method to get handler
* @param url string or regex
*/
getHandler(url) {
return this.handlers.find(handler => url.match(handler[0]) ? true : false);
}
/**
* Removes registered handler
* @param insatanceOfHandler class instance that implemented IHttpHandler
* @returns boolean
*/
removeHandler(insatanceOfHandler) {
let handlerIndex = this.handlers.findIndex(handler => insatanceOfHandler.constructor.name === handler[1].constructor.name);
if (handlerIndex !== -1) {
this.handlers.splice(handlerIndex, 1);
return true;
}
return false;
}
}
exports.HttpConfiguration = HttpConfiguration;