ts-trim-request
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

ts-trim-request

Typescript Express middleware to trim all incoming request object

Installation

npm install ts-trim-request

Usage

ts-trim-request is a fork of trim-request. It implements typescript declaration files and can trim deeper the arrays and sub-objects. It's very simple to use, just import the package and use one of its methods:

trimRequest.all trim body, params and query objects

trimRequest.body trim only the body object

trimRequest.param trim the params object

trimRequest.query trim the query string object

import express from "express";
import trimRequest from "ts-trim-request";
const app: express.Application = express();

// For all routes
app.use(trimRequest.all);

// OR for a single route
app.post('/person', trimRequest.all, (req, res, next) => { 
    res.json(req.body);
});

If the client post:

{
    name: '  foo   ',
    age: 20,
    arr: ['     bar    ', '    foo         '],
    obj: {prop1: '    bar   ', prop2: '    foo   '}
}

The code above will return this object:

{
    name: 'foo',
    age: 20,
    arr: ['bar', 'foo'],
    obj: {prop1: 'bar', prop2: 'foo'}
}

License

MIT License

Package Sidebar

Install

npm i ts-trim-request

Weekly Downloads

92

Version

1.0.1

License

MIT

Unpacked Size

7.13 kB

Total Files

6

Last publish

Collaborators

  • johngtrs