dino-express
Version:
DinO enabled REST framework based on express
110 lines • 4.72 kB
JavaScript
;
// Copyright 2018 Quirino Brizi [quirino.brizi@gmail.com]
//
// 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.RequestBodyParser = void 0;
const lodash_1 = require("lodash");
const Helper_1 = require("../Helper");
/**
* Parses the request body.
*
*/
class RequestBodyParser {
environment;
/**
* Create a new instance of RequestBodyParser
* @param {EnvironmentConfiguration} environment the environment configuration
*/
constructor(environment) {
this.environment = environment;
}
/**
* Parse the body of the received request. Set forceBase64Decode to true will cause inspection
* and deciding as required of the top level properties of the received message, if the
* received message contains the property isBase64Encoded this will take precedence above the
* configuration settings.
*
* @param {Request} req the express request
* @returns {object} the parsed body
*/
parse(req, method, def = {}) {
const forceBase64RequestDecode = Helper_1.Helper.getVariable(this.environment, 'dino:server:request:forceBase64Decode:enabled', false);
const base64RequestDecodePaths = Helper_1.Helper.getVariable(this.environment, 'dino:server:request:forceBase64Decode:paths', []);
let body = this.extractBody(req, method, def);
if (forceBase64RequestDecode) {
body = this.parseAndDecodeFromBase64(body, base64RequestDecodePaths);
}
return body;
}
/**
* Customisable hook that allows to extract the body from the received request
* @param {Request} req the received request
* @returns {any} the request body
* @protected
*/
extractBody(req, method, def = {}) {
if (Helper_1.Helper.requestHasBody(req) || Helper_1.Helper.isPayloadBeringMethod(method)) {
return req.body ?? Object.assign({}, req ?? def);
}
else {
return Object.assign({}, req ?? def);
}
}
/**
* Parse the request body decoding base64 string if present
* @param {any} body the request body
* @param {string[]} base64RequestDecodePaths an array of object pats that define the base64 strings to decode
* @returns {any} the parsed and decoded request body
* @private
*/
parseAndDecodeFromBase64(body, base64RequestDecodePaths) {
if (typeof body === 'string' && Helper_1.Helper.isBase64(body)) {
body = Helper_1.Helper.decodeFromBase64AndParse(body);
}
else {
if (base64RequestDecodePaths.length === 0) {
// decoding paths have not been defined, attempt to decode
// top level keys of the request
body = Object.keys(body).reduce((decodedMessage, key) => {
const element = body[key];
if (typeof element === 'string') {
// we inspect only the top level keys
decodedMessage[key] = Helper_1.Helper.decodeFromBase64AndParse(element);
}
else {
decodedMessage[key] = element;
}
return decodedMessage;
}, {});
}
else {
body = base64RequestDecodePaths.reduce((decodedMessage, path) => {
// we inspect only the top level keys
const element = (0, lodash_1.get)(body, path);
if (typeof element === 'string') {
(0, lodash_1.set)(decodedMessage, path, Helper_1.Helper.decodeFromBase64AndParse(element));
}
else {
(0, lodash_1.set)(decodedMessage, path, element);
}
return decodedMessage;
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
}, body);
}
}
return body;
}
}
exports.RequestBodyParser = RequestBodyParser;
//# sourceMappingURL=request.body.parser.js.map