1.0.8 • Published 10 years ago

custom-fields-middleware v1.0.8

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

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