JSPM

  • Created
  • Published
  • Downloads 1764
  • Score
    100M100P100Q107799F
  • License MIT

Parse requests in the Browser and Node (with added support for Passport). Made for Cabin.

Package Exports

  • parse-request

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (parse-request) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

parse-request

build status code coverage code style styled with prettier made with lass license

Parse requests in the Browser and Node (with added support for Passport). Made for Cabin.

Table of Contents

Install

npm:

npm install parse-request

yarn:

yarn add parse-request

How does it work

This package exports a function that accepts two arguments (req, userFields).

  • req (Object) - an HTTP request
  • userFields (Array) - defaults to [ 'id', 'email', 'full_name', 'ip_address' ], list of fields to cherry-pick from the user object parsed out of req.user

It automatically detects whether the request is from the Browser, Koa, or Express, and return a parsed object with these fields populated:

{
  request: {
    method: 'GET',
    query: {},
    headers: {},
    cookies: {},
    body: '',
    url: ''
  },
  user: {}
}

Note that there is a user object returned, which will be parsed from req.user automatically.

The user object will also have a ip_address property added, but only if one does not already exists and if an IP address was actually detected.

Usage

We highly recommend to simply use Cabin as this package is built-in!

VanillaJS

The example below uses xhook which is used to intercept HTTP requests made in the browser.

<script src="https://unpkg.com/xhook"></script>
<script src="https://unpkg.com/parse-request"></script>
<script type="text/javascript">
  (function() {
    xhook.after(function(req, res) {
      var req = parseRequest(req);
      console.log('req', req);
      // ...
    });
  })();
</script>

Koa

const parseRequest = require('parse-request');

// ...

app.get('/', (ctx, next) => {
  const req = parseRequest(ctx);
  console.log('req', req);
  // ...
});

Express

const parseRequest = require('parse-request');

// ...

app.get('/', (req, res, next) => {
  const req = parseRequest(req);
  console.log('req', req);
  // ...
});

Contributors

Name Website
Nick Baugh http://niftylettuce.com/

License

MIT © Nick Baugh