npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-inflector

v1.1.0

Published

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

Downloads

3,294

Readme

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 ([email protected])