@breautek/storm
Version:
Object-Oriented REST API framework
159 lines (156 loc) • 5.64 kB
JavaScript
;
/*
Copyright 2017-2025 Norman Breau
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Handler = void 0;
const StormError_1 = require("./StormError");
const InternalError_1 = require("./InternalError");
const NotImplementedError_1 = require("./NotImplementedError");
const HTTPMethod_1 = require("./HTTPMethod");
const TAG = 'Handler';
class Handler {
constructor(app) {
this.$app = app;
this.$middlewares = this._initMiddlewares();
}
getApplication() {
return this.$app;
}
/**
* @deprecated
*/
_initMiddlewares() {
return [];
}
getAccessToken(request) {
let config = this.$app.getConfig();
let authHeader = config.authentication_header;
return request.getHeader(authHeader);
}
/**
* @deprecated
* @param request
* @param response
* @returns
*/
async $executeMiddlewares(request, response) {
let result = {
request,
response
};
let logger = this.$app.getLogger();
try {
for (let i = 0; i < this.$middlewares.length; i++) {
let middleware = this.$middlewares[i];
logger.info(TAG, `executing middleware ${i}`);
result = await middleware.execute(result.request, result.response);
}
}
catch (ex) {
logger.error(TAG, ex);
let error = null;
if (!(ex instanceof StormError_1.StormError)) {
error = new InternalError_1.InternalError(ex);
}
else {
error = ex;
}
this._onMiddlewareReject(request, response, error);
return Promise.reject(error);
}
if (!result) {
result = {
request: null,
response: null
};
}
if (!result.request) {
result.request = request;
}
if (!result.response) {
result.response = response;
}
return Promise.resolve(result);
}
/**
* @deprecated
* @param request
* @param response
* @param error
*/
_onMiddlewareReject(request, response, error) { }
$handleResponse(response, data) {
response.send(data);
}
$handleResponseError(response, error) {
response.error(error);
}
async get(request, response) {
this.getApplication().getLogger().info(TAG, `${request.getForwardedIP()} (${request.getIP()}) - ${request.getMethod()} ${request.getURL()} - UA(${request.getHeader('user-agent')})`);
try {
let result = await this.$executeMiddlewares(request, response);
let req = await this._get(result.request);
this.$handleResponse(response, req);
}
catch (ex) {
this.$handleResponseError(response, ex);
}
}
async put(request, response) {
this.getApplication().getLogger().info(TAG, `${request.getForwardedIP()} (${request.getIP()}) - ${request.getMethod()} ${request.getURL()} - UA(${request.getHeader('user-agent')})`);
try {
let result = await this.$executeMiddlewares(request, response);
let req = await this._put(result.request);
this.$handleResponse(response, req);
}
catch (ex) {
this.$handleResponseError(response, ex);
}
}
async post(request, response) {
this.getApplication().getLogger().info(TAG, `${request.getForwardedIP()} (${request.getIP()}) - ${request.getMethod()} ${request.getURL()} - UA(${request.getHeader('user-agent')})`);
try {
let result = await this.$executeMiddlewares(request, response);
let req = await this._post(result.request);
this.$handleResponse(response, req);
}
catch (ex) {
this.$handleResponseError(response, ex);
}
}
async delete(request, response) {
this.getApplication().getLogger().info(TAG, `${request.getForwardedIP()} (${request.getIP()}) - ${request.getMethod()} ${request.getURL()} - UA(${request.getHeader('user-agent')})`);
try {
let result = await this.$executeMiddlewares(request, response);
let req = await this._delete(result.request);
this.$handleResponse(response, req);
}
catch (ex) {
this.$handleResponseError(response, ex);
}
}
async _get(request) {
throw new NotImplementedError_1.NotImplementedError(HTTPMethod_1.HTTPMethod.GET);
}
async _post(request) {
throw new NotImplementedError_1.NotImplementedError(HTTPMethod_1.HTTPMethod.POST);
}
async _put(request) {
throw new NotImplementedError_1.NotImplementedError(HTTPMethod_1.HTTPMethod.PUT);
}
async _delete(request) {
throw new NotImplementedError_1.NotImplementedError(HTTPMethod_1.HTTPMethod.DELETE);
}
}
exports.Handler = Handler;
//# sourceMappingURL=Handler.js.map