UNPKG

@breautek/storm

Version:

Object-Oriented REST API framework

238 lines (235 loc) 7.11 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.Request = void 0; const tslib_1 = require("tslib"); const formidable_1 = tslib_1.__importDefault(require("formidable")); const instance_1 = require("./instance"); const Token_1 = require("./Token"); const JWTError_1 = require("./JWTError"); const ResponseData_1 = require("./ResponseData"); const StatusCode_1 = require("./StatusCode"); const InternalError_1 = require("./InternalError"); class Request { constructor(request) { this.$request = request; } getBody() { return this.$request.body; } getForm() { return new Promise((resolve, reject) => { let r = this.getRequestSource(); let form = (0, formidable_1.default)({ hashAlgorithm: 'md5' }); form.parse(r, (error, fields, files) => { if (error) { return reject(error); } return resolve({ fields, files: files }); }); }); } getHeaders() { return this.$request.headers; } getHeader(name) { let value = this.$request.headers[name.toLowerCase()]; if (typeof value === 'string') { return value; } else { return value && value[0] ? value[0] : null; } } getQueryVariables() { return this.$request.query; } getQueryVariable(name) { return this.$request.query[name]; } getQuerySingleVariable(name) { let v = this.getQueryVariable(name); if (v === undefined || v === null) return null; if (typeof v === 'string') { return v; } if (Array.isArray(v)) { v = v[0]; if (typeof v === 'string') { return v; } } return null; // Not parseable } getQueryMultiVariable(name) { let v = this.getQueryVariable(name); if (v === undefined || v === null) return null; if (typeof v === 'string') { return [v]; } if (Array.isArray(v)) { let out = []; for (let i = 0; i < v.length; i++) { if (typeof v[i] === 'string') { out.push(v[i]); } } return out; } return null; // Not parseable } getParams() { return this.$request.params; } getParam(name) { return this.$request.params[name]; } /** * Returns the matched route */ getRoute() { return this.$request.route.path; } /** * Gets a URL parameter as string. If the value is multiple * then only the first value is returned. * This is a convenience method over `getParam` which returns a union. */ getURLSingleParam(name) { let v = this.getParam(name); if (v === undefined || v === null) return null; if (typeof v === 'string') { return v; } else { return v[0]; } } /** * Gets a URL parameter as string[]. * This is a convenience method over `getParam` which returns a union. */ getURLMultiParam(name) { let v = this.getParam(name); if (v === undefined || v === null) return null; if (typeof v === 'string') { return [v]; } else { return v; } } getIP() { return this.$request.ip; } getForwardedIP() { return this.getHeader('X-Forwarded-For'); } getHostname() { return this.$request.hostname; } getMethod() { return this.$request.method; } getURL() { return this.$request.originalUrl; } isSecure() { return this.$request.secure; } pipe(destination) { return this.$request.pipe(destination); } unpipe(source) { this.$request.unpipe(source); } getRequestSource() { return this.$request; } /** * Reads the Page/Position DB headers if present * Will return null if headers are not parseable. * * @since 9.1.0 * @returns {IDatabasePosition} */ getDatabasePosition() { let pageStr = this.getHeader('X-DATABASE-PAGE'); let positionStr = this.getHeader('X-DATABASE-POSITION'); let page = null; let position = null; let out = null; if (pageStr && positionStr) { page = parseInt(pageStr); position = parseInt(positionStr); if (isNaN(page) || isNaN(position)) { page = null; position = null; } } if (page && position) { out = { page, position }; } return out; } async getAuthenticationToken() { let authHeader = (0, instance_1.getInstance)().getConfig().authentication_header; let tdata = null; try { tdata = (await (0, instance_1.getInstance)().getTokenManager().verify(new Token_1.Token(this.getHeader(authHeader)))); } catch (ex) { let error = null; if (ex && ex.name) { switch (ex.name) { case JWTError_1.JWTError.ERR_EXPIRED: error = new ResponseData_1.ResponseData(StatusCode_1.StatusCode.ERR_UNAUTHORIZED, { code: ex.name, reason: ex.message }); break; case JWTError_1.JWTError.ERR_GENERIC: error = new ResponseData_1.ResponseData(StatusCode_1.StatusCode.ERR_UNAUTHORIZED, { code: ex.name, reason: ex.message }); break; } } if (error === null) { let ie = new InternalError_1.InternalError(ex); error = new ResponseData_1.ResponseData(ie.getHTTPCode(), { code: ie.getCode(), reason: ie.getMessage() }); } throw error; } return tdata; } } exports.Request = Request; //# sourceMappingURL=Request.js.map