Skip to content

🎄 Decorate Node.js Request/Response object with custom properties the correct way.

License

Notifications You must be signed in to change notification settings

simonecorsi/decorate-request-response

Repository files navigation

decorate-request-response

About The Project

This package allow for decorating request and/or response objects without modifying the shape of the object on each requests.

Installation

npm i @scdev/decorate-request-response

Usage

NB Avoid overriding existing properties

const { createServer } = require('http');

const decorate = require('@scdev/decorate-request-response');
const { IncomingMessage, ServerResponse } = decorate({
  request: ['user'],
  response: [
    [
      'json',
      function (data) {
        this.end(JSON.stringify(data));
      },
    ],
  ],
});

createServer(
  {
    IncomingMessage,
    ServerResponse,
  },
  (req, res) => {
    // req.user === null
    // res.user === null
    res.end();
  }
).listen(3000);

Contributing

Project is pretty simple and straight forward for what is my needs, but if you have any idea you're welcome.

This projects uses commitizen so be sure to use standard commit format or PR won't be accepted

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'feat(scope): some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Simone Corsi - @im_simonecorsi

About

🎄 Decorate Node.js Request/Response object with custom properties the correct way.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published