├── .gitignore ├── .travis.yml ├── .github └── config.yml ├── .istanbul.yml ├── normalized.js ├── package.json ├── LICENSE ├── README.md └── tests └── normalized.js /.gitignore: -------------------------------------------------------------------------------- 1 | artifacts 2 | node_modules 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '0.12' 5 | - '0.10' 6 | -------------------------------------------------------------------------------- /.github/config.yml: -------------------------------------------------------------------------------- 1 | repository: 2 | url: https://github.com/yahoo/express-normalized 3 | travis: enabled 4 | -------------------------------------------------------------------------------- /.istanbul.yml: -------------------------------------------------------------------------------- 1 | reporting: 2 | reports: 3 | - lcov 4 | - text 5 | - text-summary 6 | -------------------------------------------------------------------------------- /normalized.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015, Yahoo Inc. 3 | * Licensed under the MIT License. 4 | * See LICENSE file for details 5 | */ 6 | /*jshint forin: false*/ 7 | 'use strict'; 8 | 9 | var normalize = function(options) { 10 | options = options || {}; 11 | var base = options.base || 'normalized', 12 | post = options.post || 'body', 13 | get = options.get || 'query'; 14 | 15 | return function(req, res, next) { 16 | var normal = Object.create(null), 17 | i, body = req[post], query = req[get]; 18 | 19 | for (i in query) { 20 | normal[i] = query[i]; 21 | } 22 | 23 | for (i in body) { 24 | normal[i] = body[i]; 25 | } 26 | 27 | req[base] = normal; 28 | 29 | next(); 30 | }; 31 | }; 32 | 33 | module.exports = normalize; 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-normalized", 3 | "author": "Dav Glass ", 4 | "version": "1.0.0", 5 | "description": "Normalize get/post vars", 6 | "main": "normalized.js", 7 | "scripts": { 8 | "pretest": "jshint *.js ./tests/*.js", 9 | "test": "jenkins-mocha ./tests/*.js", 10 | "posttest": "node_modules/jenkins-mocha/node_modules/.bin/istanbul check-coverage --statements 100 --branches 100 --functions 100 --lines 100" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "http://github.com/yahoo/express-normalized.git" 15 | }, 16 | "bugs": { 17 | "url": "http://github.com/yahoo/express-normalized/issues" 18 | }, 19 | "jshintConfig": { 20 | "node": true 21 | }, 22 | "keywords": [ 23 | "express", 24 | "params", 25 | "vars", 26 | "normalized" 27 | ], 28 | "license": "MIT", 29 | "devDependencies": { 30 | "chai": "^3.3.0", 31 | "jenkins-mocha": "^2.4.0", 32 | "jshint": "^2.8.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Yahoo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | express-normalized 2 | ================== 3 | 4 | Similar to PHP's `$_REQUEST` object, this mixes the `GET` vars and `POST` vars into a normalized `Object` always choosing `POST` over `GET`. 5 | 6 | By default it creates a new `Object`, then walks the `req.query` and adds the `key/value` to it, then it will walk the `req.body` and do the same. 7 | It will always use the `POST` version of the value if it is available. It will then add this new `Object` to the `req` as `normalized`. See the `configuration` section 8 | below for changing these values. 9 | 10 | [![npm Version](https://img.shields.io/npm/v/express-normalized.svg?style=flat-square)](https://www.npmjs.org/package/express-normalized) 11 | [![Build Status](http://img.shields.io/travis/yahoo/express-normalized.svg?style=flat-square)](https://travis-ci.org/yahoo/express-normalized) 12 | 13 | usage 14 | ----- 15 | 16 | ```js 17 | 18 | var app = express(), 19 | normalized = require('express-normalized'); 20 | 21 | //Add your normalize handlers for get/post here 22 | //busboy, body-parser, etc 23 | 24 | app.use(normalized()); 25 | 26 | //Then in your app: 27 | 28 | function(req, res) { 29 | console.log(req.normalized); 30 | } 31 | ``` 32 | 33 | configuration 34 | ------------- 35 | 36 | You can configure where it get's the `GET` & `POST` from as well as the namespace that it applies it to: 37 | 38 | ```js 39 | var app = express(), 40 | normalized = require('express-normalized'); 41 | 42 | //Add your normalize handlers for get/post here 43 | //busboy, body-parser, etc 44 | 45 | //Maybe you are using a custom variable parser and it uses 46 | // queries as get vars and bodies as post vars 47 | // and you want it to populate the req.normal variable 48 | 49 | app.use(normalized({ 50 | base: 'normal', 51 | get: 'queries', 52 | post: 'bodies' 53 | })); 54 | 55 | //Then in your app: 56 | 57 | function(req, res) { 58 | console.log(req.normal); 59 | } 60 | ``` 61 | 62 | -------------------------------------------------------------------------------- /tests/normalized.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 'use strict'; 3 | 4 | var chai = require('chai'), 5 | assert = chai.assert, 6 | pathlib = require('path'), 7 | normalize = require(pathlib.join(__dirname, '../normalized.js')); 8 | 9 | describe('express-normalized', function () { 10 | 11 | it('normalize should return a function.', function () { 12 | assert.isFunction(normalize); 13 | }); 14 | 15 | it('should create an empty object if given no data', function() { 16 | var req = { body: {}, query: {} }; 17 | normalize()(req, {}, function() { 18 | assert.ok(req.normalized); 19 | assert.equal(Object.keys(req.normalized).length, 0); 20 | }); 21 | }); 22 | 23 | it('should create an object if given query', function() { 24 | var req = { body: {}, query: { foo: true } }; 25 | normalize()(req, {}, function() { 26 | assert.ok(req.normalized); 27 | assert.equal(Object.keys(req.normalized).length, 1); 28 | assert.equal(req.normalized.foo, true); 29 | }); 30 | }); 31 | 32 | it('should create an object if given body', function() { 33 | var req = { body: { bar: true }, query: {} }; 34 | normalize()(req, {}, function() { 35 | assert.ok(req.normalized); 36 | assert.equal(Object.keys(req.normalized).length, 1); 37 | assert.equal(req.normalized.bar, true); 38 | }); 39 | }); 40 | 41 | it('should create a mixed object if given body and query of different keys', function() { 42 | var req = { body: { bar: true }, query: { foo: true } }; 43 | normalize()(req, {}, function() { 44 | assert.ok(req.normalized); 45 | assert.equal(Object.keys(req.normalized).length, 2); 46 | assert.equal(req.normalized.bar, true); 47 | assert.equal(req.normalized.foo, true); 48 | assert.isUndefined(req.query.bar); 49 | assert.isUndefined(req.body.foo); 50 | }); 51 | }); 52 | 53 | it('should create a mixed object if given body and query of matching keys', function() { 54 | var req = { body: { bar: true, baz: 'two' }, query: { foo: true, baz: 'one' } }; 55 | normalize()(req, {}, function() { 56 | assert.ok(req.normalized); 57 | assert.equal(Object.keys(req.normalized).length, 3); 58 | assert.equal(req.normalized.bar, true); 59 | assert.equal(req.normalized.foo, true); 60 | assert.equal(req.normalized.baz, 'two'); 61 | }); 62 | }); 63 | 64 | it('should create a mixed object if given body and query of matching keys - with config', function() { 65 | var req = { bodies: { bar: true, baz: 'two' }, queries: { foo: true, baz: 'one' } }; 66 | normalize({ 67 | base: 'normal', 68 | get: 'queries', 69 | post: 'bodies' 70 | })(req, {}, function() { 71 | assert.ok(req.normal); 72 | assert.equal(Object.keys(req.normal).length, 3); 73 | assert.equal(req.normal.bar, true); 74 | assert.equal(req.normal.foo, true); 75 | assert.equal(req.normal.baz, 'two'); 76 | }); 77 | }); 78 | 79 | }); 80 | 81 | --------------------------------------------------------------------------------