UNPKG

@breautek/storm

Version:

Object-Oriented REST API framework

145 lines (142 loc) 5.49 kB
"use strict"; /* Copyright 2017-2021 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 instance_1 = require("./instance"); 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; } _initMiddlewares() { return []; } getAccessToken(request) { let config = (0, instance_1.getInstance)().getConfig(); let authHeader = config.authentication_header; return request.getHeader(authHeader); } async $executeMiddlewares(request, response) { let result = { request, response }; let logger = (0, instance_1.getInstance)().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); } _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