/************************************************************************* * * 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 {IChassisPlugin, IChassisContext} from "../index"; import * as _ from "lodash"; import * as bodyParser from "body-parser"; /** * payload * ------- * * Handles parsing the payload body * * @type {{name: string, title: string, fn: module.exports.fn}} */ const SMALL_LIMIT = "10mb"; const LARGE_LIMIT = "1000mb"; export default class payload implements IChassisPlugin { name = "payload"; title = "parse body payloads (json, text, raw, urlencoded)"; install(context: IChassisContext, options: any) { let _options = options as any; context.log({ code: "api:plugin:payload", message: "body payloads" } ); // configure the set of supported body parsers if ( _options.json !== false ) { let _opts = _.defaults({ limit: SMALL_LIMIT }, _options.json ); context.api.use(bodyParser.json(_opts)); context.log({ code: "api:plugin:payload:json", message: "json payloads", options: _opts } ); } if ( _options.text !== false ) { let _opts = _.defaults({ type: "text/*", limit: SMALL_LIMIT }, _options.text); context.api.use(bodyParser.text( _opts )); context.log({ code: "api:plugin:payload:text", message: "plain text payloads", options: _opts } ); } if ( _options.urlencoded !== false ) { let _opts = _.defaults({ limit: SMALL_LIMIT, extended: true }, _options.urlencoded); context.api.use(bodyParser.urlencoded( _opts )); context.log({ code: "api:plugin:payload:urlencoded", message: "urlencoded payloads", options: _opts } ); } if ( _options.raw !== false ) { let _opts = _.defaults({ limit: LARGE_LIMIT }, _options.raw); context.api.use(bodyParser.raw( _opts )); context.log({ code: "api:plugin:payload:raw", message: "raw payloads", options: _opts } ); } } };