custom-fields-middleware

1.0.8 • Public • Published

Custom Fields Middleware Build Status

Adds and transforms custom fields on the response

Usage

In Express, pass the middleware to app.use before the router

app.use(require('custom-fields-middleware'));

Assign a model to customfields from anywhere in your app

res.customfields = {
  id: 'foo',
  value: 'bar'
};

Assign a collection

res.customfields = [{id: 'foo', value: 'bar'}, {id: 'bar', value: 'bar'}];

Read the value(s)

res.customfields.foo // 'bar'
res.customfields.bar // 'baz'

Params

  • id: {String} required. The name of the value.
  • value: {*} required. The value to be assigned to the id.
  • transforms: {Array|String|Function} options. A function or list of functions to transforms the value.

Transforms

A transform is a function that takes two arguments, the value and the request object.

var square (value, req) => value * value;

res.customfields = {
  id: 'foo',
  value: 'bar',
  transforms: [square]
};

Built-in transforms include:

  • baseurl: prepends the value with req.baseUrl

Use a built-in transform by passing the name to the transforms field.

res.customfields = {
  id: 'foo',
  value: 'bar',
  transforms: ['baseurl']
};

Install

$ npm install custom-fields-middleware --save

Scripts

$ npm test
$ npm lint

Readme

Keywords

Package Sidebar

Install

npm i custom-fields-middleware

Weekly Downloads

4

Version

1.0.8

License

MIT

Last publish

Collaborators

  • chrisns
  • josephchapman