/************************************************************************* * * Troven CONFIDENTIAL * __________________ * * (c) 2017-2020 Troven Ventures Pty Ltd * All Rights Reserved. * * NOTICE: All information contained herein is, and remains * the property of Troven Pty Ltd and its licensors, * if any. The intellectual and technical concepts contained * herein are proprietary to Troven Pty Ltd * and its suppliers and may be covered by International and Regional Patents, * patents in process, and are protected by trade secret or copyright law. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from Troven Pty Ltd. */ import * as _ from "lodash"; import {IChassisFeature, IOperation} from "../interfaces"; /** * headers * --------- * Set / Deny Headers * * @type {{name: string, title: string, fn: module.exports.fn}} */ export default class headers implements IChassisFeature { name = "api.headers"; title = "Set/Deny HTTP headers"; fn(_operation: IOperation, _options: any): Function { let options = _.extend({ request: { set: {}, deny: { "X-REQUEST-ID": true } }, response: { set: {}, deny: {} } }, _options); return function (req: any, res: any, next: any) { // request if (options.request) { _.each(options.request.deny, (name, denied) => { denied && delete req.headers[name]; }); _.each(options.request.set, (name, value) => { req.headers[name] = value; }); } // response if (options.response) { _.each(options.response.deny, (deny, denied) => { denied && delete res.headers[deny]; }); _.each(options.response.set, (name, value) => { res.headers[name] = value; }); } next(); }; } }