json-inflector

middleware for changing the inflection of json keys in requests and responses in express/connect applications


Keywords
inflection, camelcase, snakecase, express, connect, middleware
License
MIT
Install
npm install json-inflector@1.1.0

Documentation

json-inflector

The JSON Inflector is a node.js package for providing a Connect/Express middleware that can be used modify json requests and responses with with inflection rules. This lets you change the formatting of the json's keys on it's way in and out of your app.

For example maybe your server usings postgresql which has case insistive column names leading most people to use snake_case field names, but want your api to have cameCased field names. You can do a lot of manual parsing or you can setup a rule per app or per route and deal with the data in their "native" field formatting as they come from your clients and database.

It works by parsing req.body if it's an object, and overiding res.json to parse a json responses if applicable.

Credit to the cors library and it's authors for providing the project structure to make an express middleware.

NPM

build status

Installation (via npm)

Requires NodeJS 4+ and we test with the latest 2 major and latest 3 minor patches.

$ npm install --save json-inflector

Usage

Default Usage (camelCase <=> snake_case)

Snake case on the server, camel case on the client.

let express = require('express');
let inflector = require('json-inflector');
let bodyParser = require('body-parser');
let app = express();
let request = require('request');

app.use(bodyParser.json());
app.use(inflector());

app.post('/products', (req, res) => {
  let product = req.body;
  console.log('Request:', product);
  product.product_price = 100;
  res.json(product);
});

app.listen(8080, () => {
  request.post({
    url: 'http://localhost:8080/products',
    json: true,
    body: {productName: 'Race car'}
  }, (err, res) => {
    console.log('Response:', res.body);
    process.exit();
  });
});

// Request: { product_name: 'Race car' }
// Response: { productName: 'Race car', productPrice: 100 }

Enable for a Single Route

let express = require('express');
let inflector = require('json-inflector');
let bodyParser = require('body-parser');
let app = express();
app.use(bodyParser.json());

app.get('/products/:id', inflector(), function(req, res, next){
  res.json({status_message: 'statusMessage for all products'});
});

Configuring JSON Inflector

let express = require('express');
let inflector = require('json-inflector');
let bodyParser = require('body-parser');
let app = express();
app.use(bodyParser.json());

let options = {
  response: 'dasherize'
};

app.get('/products/:id', inflector(options), function(req, res, next){
  res.json({status_message: 'status-message for all products'});
});

Configuration Options

We use inflection to do the key inflecting. You can pass the name of any of it's functions to be used. We've added camelizeLower which is the same as camelize with lower first letters by default.

  • request: Configures the request processing.
  • response: Configures the request processing.

Both methods are optional and take the same kinds of arguments.

  • String - The name of a inflection function to processes the keys of a request.
  • Array - The names of functions to be passed to inflection's transform() function.

The default configuration is the equivalent of:

{
  request: 'underscore',
  response: 'camelizeLower'
}

Transform

The transform function is also available to use directly

let inflector = require('json-inflector');
var obj = {
  fullName: "Bob Sanders"
};
inflector.transform(obj, 'underscore');
// { full_name: "Bob Sanders"}

License

MIT License

Author

Francis Gulotta (wizard@roborooter.com)