├── .gitignore ├── .travis.yml ├── README.md ├── index.js ├── isEmpty.js ├── package.json └── test ├── deleteTest.js ├── getTest.js ├── patchTest.js ├── postTest.js ├── putTest.js └── staticOptionsTest.js /.gitignore: -------------------------------------------------------------------------------- 1 | *sublime* 2 | *DS_Store* 3 | *.log 4 | node_modules/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | script: "npm test" 3 | node_js: 4 | - "0.10" 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/B1naryStudio/express-api-response.svg)](https://travis-ci.org/B1naryStudio/express-api-response) 2 | 3 | ## Install 4 | > npm install express-api-response 5 | 6 | [![npm version](https://badge.fury.io/js/express-api-response.svg)](https://badge.fury.io/js/express-api-response) 7 | 8 | ## Overview 9 | Middleware for serving json responses with correct REST API / HTTP status codes without pain. 10 | Works with [Express](https://github.com/visionmedia/express). 11 | 12 | **express-api-response** creates 5 new fields on express response object: 13 | - res.data - object which will be transferred to the client. 14 | - res.err - error which occured within route handler. 15 | - res.shouldNotHaveData - indicates, whether empty res.data field should lead to 16 | failure status code. 17 | - res.shouldSendErrorMessage - indicates, whether error message should be sent with status 18 | code. 19 | - res.successStatus - status code which will be added to the response in case of success. 20 | - res.failureStatus - status code which will be added to the response in case of failure. 21 | These two status parameters, the shouldNotHaveData and shouldHaveErrorMessage are optional and most of the time 22 | you will not use them. Here are the default values for different methods: 23 | 24 | | Method | Error present | No error, no data | No error, data | shouldNotHaveData| 25 | |--------|---------------|-------------------|----------------|------------------| 26 | | get | 400 | 404 | 200 | false | 27 | | post | 400 | 201 | 201 | true | 28 | | put | 400 | 204 | 200 | true | 29 | | delete | 400 | 204 | 200 | true | 30 | | patch | 400 | 204 | 200 | true | 31 | 32 | 33 | ### Static middleware options 34 | 35 | You can set static options to the middleware which will define its behaviour throught the whole application. 36 | 37 | Currently there is only 1 static option available: 38 | - emptyArrayIsOk - if **true**, treats empty array in data as success. False by default. 39 | 40 | 41 | ## Usage 42 | 43 | ```js 44 | var express = require('express'); 45 | var app = express(); 46 | var apiResponse = require('express-api-response'); 47 | 48 | app.get('/', function(req, res, next){ 49 | res.data = {data: 'myjson'}; 50 | next(); 51 | }, apiResponse); 52 | ``` 53 | 54 | ```js 55 | var express = require('express'); 56 | var app = express(); 57 | var apiResponse = require('express-api-response'); 58 | 59 | apiResponse.options({ 60 | emptyArrayIsOk: true 61 | }); 62 | 63 | app.get('/', function(req, res, next){ 64 | res.data = []; 65 | next(); 66 | }, apiResponse); 67 | ``` 68 | 69 | ```js 70 | var express = require('express'); 71 | var app = express(); 72 | var apiResponse = require('express-api-response'); 73 | 74 | app.post('/route', function(req, res, next){ 75 | asyncFunction(function(err, data){ 76 | res.data = data; 77 | res.err = { 78 | message: 'Something went wrong', 79 | }; 80 | next(); 81 | }); 82 | }, apiResponse); 83 | ``` 84 | 85 | ```js 86 | var express = require('express'); 87 | var app = express(); 88 | var apiResponse = require('express-api-response'); 89 | 90 | app.delete('/route', function(req, res, next){ 91 | asyncFunction(function(err, data){ 92 | res.data = data; 93 | res.err = err; 94 | res.shouldSendErrorMessage = false 95 | res.shouldNotHaveData = false; 96 | res.failureStatus = 702; 97 | next(); 98 | }); 99 | }, apiResponse); 100 | ``` 101 | 102 | ## Contributing 103 | Feel free to open issues and send PRs, though make sure that you create tests 104 | for new functionality and amend ones for fixes and changes. 105 | 106 | ## Running tests 107 | `npm test` 108 | 109 | ## License 110 | 111 | The MIT License (MIT) 112 | 113 | [![Binary Studio](https://d3ot0t2g92r1ra.cloudfront.net/img/binary-small-logo.png)](http://binary-studio.com) 114 | Copyright (c) 2014-2019 Semenistyi Mykyta nikeiwe@gmail.com 115 | 116 | Permission is hereby granted, free of charge, to any person obtaining a copy 117 | of this software and associated documentation files (the "Software"), to deal 118 | in the Software without restriction, including without limitation the rights 119 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 120 | copies of the Software, and to permit persons to whom the Software is 121 | furnished to do so, subject to the following conditions: 122 | 123 | The above copyright notice and this permission notice shall be included in 124 | all copies or substantial portions of the Software. 125 | 126 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 127 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 128 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 129 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 130 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 131 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 132 | THE SOFTWARE. 133 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var isEmpty = require('./isEmpty'); 2 | var staticOptions = {}; 3 | 4 | function apiResponse(req, res, next) { 5 | var successStatus; 6 | var failureStatus; 7 | var shouldNotHaveData; 8 | var shouldSendErrorMessage; 9 | switch (req.method.toLowerCase()) { 10 | case 'get': 11 | successStatus = 200; 12 | failureStatus = res.err ? 400 : 404; 13 | shouldNotHaveData = res.shouldNotHaveData; 14 | break; 15 | case 'post': 16 | shouldNotHaveData = typeof res.shouldNotHaveData !== 'undefined' ? 17 | res.shouldNotHaveData : true; 18 | successStatus = 201; 19 | failureStatus = res.err ? 400 : 404; 20 | break; 21 | case 'put': 22 | case 'patch': 23 | case 'delete': 24 | shouldNotHaveData = typeof res.shouldNotHaveData !== 'undefined' ? 25 | res.shouldNotHaveData : true; 26 | successStatus = shouldNotHaveData && isEmpty(res.data, staticOptions) ? 204 : 200; 27 | failureStatus = res.err ? 400 : 404; 28 | break; 29 | default: 30 | successStatus = 200; 31 | failureStatus = res.err ? 400 : 404; 32 | } 33 | 34 | if (res.err !== undefined && res.err !== null && res.err.message !== undefined && res.err.message !== null) { 35 | if(res.shouldSendErrorMessage === undefined) { 36 | shouldSendErrorMessage = true; 37 | } else { 38 | shouldSendErrorMessage = res.shouldSendErrorMessage; 39 | } 40 | } else { 41 | shouldSendErrorMessage = false; 42 | } 43 | 44 | successStatus = res.successStatus || successStatus; 45 | failureStatus = res.failureStatus || failureStatus; 46 | if (res.err) { 47 | if (shouldSendErrorMessage) { 48 | return res.status(failureStatus).send(res.err.message); 49 | } else { 50 | return res.status(failureStatus).end(); 51 | } 52 | } else if ((isEmpty(res.data, staticOptions))) { 53 | if (!shouldNotHaveData) { 54 | return res.status(failureStatus).end(); 55 | } else { 56 | return res.status(successStatus).end(); 57 | } 58 | } else { 59 | return res.status(successStatus).json(res.data); 60 | } 61 | }; 62 | 63 | apiResponse.options = function(options){ 64 | if (typeof options === 'object'){ 65 | staticOptions = options; 66 | } 67 | }; 68 | 69 | module.exports = apiResponse; -------------------------------------------------------------------------------- /isEmpty.js: -------------------------------------------------------------------------------- 1 | module.exports = function(obj, staticOptions) { 2 | 3 | if (obj == null || typeof obj !== 'object') { 4 | return true; 5 | } 6 | 7 | if (obj instanceof Array) { 8 | if (staticOptions.emptyArrayIsOk){ 9 | return false; 10 | } 11 | 12 | return obj.length === 0; 13 | } 14 | 15 | for (var key in obj) { 16 | if (obj.hasOwnProperty(key)) { 17 | return false; 18 | } 19 | } 20 | 21 | return true; 22 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express-api-response", 3 | "version": "0.4.1", 4 | "description": "Middleware for picking REST API HTTP status code", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node ./node_modules/mocha/bin/mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/B1naryStudio/express-api-response.git" 12 | }, 13 | "keywords": [ 14 | "express", 15 | "rest", 16 | "api", 17 | "response", 18 | "http" 19 | ], 20 | "author": "msemenistyi", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/B1naryStudio/express-api-response/issues" 24 | }, 25 | "homepage": "https://github.com/B1naryStudio/express-api-response", 26 | "devDependencies": { 27 | "mocha": "~1.21.5", 28 | "supertest": "~0.14.0", 29 | "express": "~4.9.8" 30 | }, 31 | "dependencies": { 32 | "should": "^5.2.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/deleteTest.js: -------------------------------------------------------------------------------- 1 | var request = require('supertest'); 2 | var express = require('express'); 3 | var apiResponse = require('../'); 4 | 5 | describe('express api middleware on *delete* should', function(){ 6 | var app; 7 | beforeEach(function(){ 8 | app = express(); 9 | }); 10 | 11 | it('return 204 code on no data', function(done){ 12 | app.delete('/', apiResponse); 13 | 14 | request(app) 15 | .delete('/') 16 | .expect(204) 17 | .end(done); 18 | }); 19 | 20 | it('return 400 code on error', function(done){ 21 | app.delete('/', function(req, res, next){ 22 | res.err = 'asd'; 23 | next(); 24 | }, apiResponse); 25 | 26 | request(app) 27 | .delete('/') 28 | .expect(400) 29 | .end(done); 30 | }); 31 | 32 | it('return 200 code on object data', function(done){ 33 | app.delete('/', function(req, res, next){ 34 | res.data = {asd: '1'}; 35 | next(); 36 | }, apiResponse); 37 | 38 | request(app) 39 | .delete('/') 40 | .expect(200) 41 | .end(done); 42 | }); 43 | 44 | it('return 400 code on empty object data', function(done){ 45 | app.delete('/', function(req, res, next){ 46 | res.data = {}; 47 | next(); 48 | }, apiResponse); 49 | 50 | request(app) 51 | .delete('/') 52 | .expect(204) 53 | .end(done); 54 | }); 55 | 56 | it('return 200 code on array data', function(done){ 57 | app.delete('/', function(req, res, next){ 58 | res.data = [1, 2, 3]; 59 | next(); 60 | }, apiResponse); 61 | 62 | request(app) 63 | .delete('/') 64 | .expect(200) 65 | .end(done); 66 | }); 67 | 68 | it('return 204 code on empty array data', function(done){ 69 | app.delete('/', function(req, res, next){ 70 | res.data = []; 71 | next(); 72 | }, apiResponse); 73 | 74 | request(app) 75 | .delete('/') 76 | .expect(204) 77 | .end(done); 78 | }); 79 | 80 | it('return 204 code on empty data and should not have data', function(done){ 81 | app.delete('/', function(req, res, next){ 82 | res.data = []; 83 | res.shouldNotHaveData = true; 84 | next(); 85 | }, apiResponse); 86 | 87 | request(app) 88 | .delete('/') 89 | .expect(204) 90 | .end(done); 91 | }); 92 | 93 | it('return 400 code on wrong data and should have error message', function(done) { 94 | app.delete('/', function(req, res, next) { 95 | res.err = { 96 | message: 'Something went wrong' 97 | }; 98 | next(); 99 | }, apiResponse); 100 | 101 | request(app) 102 | .delete('/') 103 | .expect(400) 104 | .end(function(err, res) { 105 | res.error.text.should.equal('Something went wrong'); 106 | done(); 107 | }); 108 | }); 109 | 110 | it('return 400 code on wrong data and should not have error message', function(done) { 111 | app.delete('/', function(req, res, next) { 112 | res.err = { 113 | message: 'Something went wrong' 114 | }; 115 | res.shouldSendErrorMessage = false; 116 | next(); 117 | }, apiResponse); 118 | 119 | request(app) 120 | .delete('/') 121 | .expect(400) 122 | .end(function(err, res) { 123 | res.error.text.should.equal(''); 124 | done(); 125 | }); 126 | }); 127 | 128 | }); 129 | 130 | -------------------------------------------------------------------------------- /test/getTest.js: -------------------------------------------------------------------------------- 1 | var request = require('supertest'); 2 | var express = require('express'); 3 | var apiResponse = require('../'); 4 | 5 | describe('express api middleware on *get* should', function(){ 6 | var app; 7 | beforeEach(function(){ 8 | app = express(); 9 | }); 10 | 11 | it('return 404 code on no data', function(done){ 12 | app.get('/', apiResponse); 13 | 14 | request(app) 15 | .get('/') 16 | .expect(404) 17 | .end(done); 18 | }); 19 | 20 | it('return 400 code on error', function(done){ 21 | app.get('/', function(req, res, next){ 22 | res.err = 'asd'; 23 | next(); 24 | }, apiResponse); 25 | 26 | request(app) 27 | .get('/') 28 | .expect(400) 29 | .end(done); 30 | }); 31 | 32 | it('return 404 code on non-object data', function(done){ 33 | app.get('/', function(req, res, next){ 34 | res.data = 'asd'; 35 | next(); 36 | }, apiResponse); 37 | 38 | request(app) 39 | .get('/') 40 | .expect(404) 41 | .end(done); 42 | }); 43 | 44 | it('return 200 code on object data', function(done){ 45 | app.get('/', function(req, res, next){ 46 | res.data = {asd: '1'}; 47 | next(); 48 | }, apiResponse); 49 | 50 | request(app) 51 | .get('/') 52 | .expect(200) 53 | .end(done); 54 | }); 55 | 56 | it('return 400 code on empty object data', function(done){ 57 | app.get('/', function(req, res, next){ 58 | res.data = {}; 59 | next(); 60 | }, apiResponse); 61 | 62 | request(app) 63 | .get('/') 64 | .expect(404) 65 | .end(done); 66 | }); 67 | 68 | it('return 200 code on array data', function(done){ 69 | app.get('/', function(req, res, next){ 70 | res.data = [1, 2, 3]; 71 | next(); 72 | }, apiResponse); 73 | 74 | request(app) 75 | .get('/') 76 | .expect(200) 77 | .end(done); 78 | }); 79 | 80 | it('return 404 code on empty array data with {emptyArrayIsOk: true}', function(done){ 81 | apiResponse.options({emptyArrayIsOk: true}); 82 | 83 | app.get('/', function(req, res, next){ 84 | res.data = []; 85 | next(); 86 | }, apiResponse); 87 | 88 | request(app) 89 | .get('/') 90 | .expect(404) 91 | .end(done); 92 | 93 | apiResponse.options({emptyArrayIsOk: false}); 94 | }); 95 | 96 | it('return 404 code on empty array data', function(done){ 97 | app.get('/', function(req, res, next){ 98 | res.data = []; 99 | next(); 100 | }, apiResponse); 101 | 102 | request(app) 103 | .get('/') 104 | .expect(404) 105 | .end(done); 106 | }); 107 | 108 | it('return 200 code on empty data and should not have data', function(done){ 109 | app.get('/', function(req, res, next){ 110 | res.data = []; 111 | res.shouldNotHaveData = true; 112 | next(); 113 | }, apiResponse); 114 | 115 | request(app) 116 | .get('/') 117 | .expect(200) 118 | .end(done); 119 | }); 120 | 121 | it('return 400 code on wrong data and should have error message', function(done) { 122 | app.get('/', function(req, res, next) { 123 | res.err = { 124 | message: 'Something went wrong' 125 | }; 126 | next(); 127 | }, apiResponse); 128 | 129 | request(app) 130 | .get('/') 131 | .expect(400) 132 | .end(function(err, res) { 133 | res.error.text.should.equal('Something went wrong'); 134 | done(); 135 | }); 136 | }); 137 | 138 | it('return 400 code on wrong data and should not have error message', function(done) { 139 | app.get('/', function(req, res, next) { 140 | res.err = { 141 | message: 'Something went wrong' 142 | }; 143 | res.shouldSendErrorMessage = false; 144 | next(); 145 | }, apiResponse); 146 | 147 | request(app) 148 | .get('/') 149 | .expect(400) 150 | .end(function(err, res) { 151 | res.error.text.should.equal(''); 152 | done(); 153 | }); 154 | }); 155 | 156 | }); 157 | 158 | -------------------------------------------------------------------------------- /test/patchTest.js: -------------------------------------------------------------------------------- 1 | var request = require('supertest'); 2 | var express = require('express'); 3 | var apiResponse = require('../'); 4 | 5 | describe('express api middleware on *patch* should', function(){ 6 | var app; 7 | beforeEach(function(){ 8 | app = express(); 9 | }); 10 | 11 | it('return 204 code on no data', function(done){ 12 | app.patch('/', apiResponse); 13 | 14 | request(app) 15 | .patch('/') 16 | .expect(204) 17 | .end(done); 18 | }); 19 | 20 | it('return 400 code on error', function(done){ 21 | app.patch('/', function(req, res, next){ 22 | res.err = 'asd'; 23 | next(); 24 | }, apiResponse); 25 | 26 | request(app) 27 | .patch('/') 28 | .expect(400) 29 | .end(done); 30 | }); 31 | 32 | it('return 204 code on non-object data', function(done){ 33 | app.patch('/', function(req, res, next){ 34 | res.data = 'asd'; 35 | next(); 36 | }, apiResponse); 37 | 38 | request(app) 39 | .patch('/') 40 | .expect(204) 41 | .end(done); 42 | }); 43 | 44 | it('return 200 code on object data', function(done){ 45 | app.patch('/', function(req, res, next){ 46 | res.data = {asd: '1'}; 47 | next(); 48 | }, apiResponse); 49 | 50 | request(app) 51 | .patch('/') 52 | .expect(200) 53 | .end(done); 54 | }); 55 | 56 | it('return 204 code on empty object data', function(done){ 57 | app.patch('/', function(req, res, next){ 58 | res.data = {}; 59 | next(); 60 | }, apiResponse); 61 | 62 | request(app) 63 | .patch('/') 64 | .expect(204) 65 | .end(done); 66 | }); 67 | 68 | it('return 200 code on array data', function(done){ 69 | app.patch('/', function(req, res, next){ 70 | res.data = [1, 2, 3]; 71 | next(); 72 | }, apiResponse); 73 | 74 | request(app) 75 | .patch('/') 76 | .expect(200) 77 | .end(done); 78 | }); 79 | 80 | it('return 204 code on empty array data', function(done){ 81 | app.patch('/', function(req, res, next){ 82 | res.data = []; 83 | next(); 84 | }, apiResponse); 85 | 86 | request(app) 87 | .patch('/') 88 | .expect(204) 89 | .end(done); 90 | }); 91 | 92 | it('return 204 code on empty data and should not have data', function(done){ 93 | app.patch('/', function(req, res, next){ 94 | res.data = []; 95 | res.shouldNotHaveData = true; 96 | next(); 97 | }, apiResponse); 98 | 99 | request(app) 100 | .patch('/') 101 | .expect(204) 102 | .end(done); 103 | }); 104 | 105 | }); 106 | 107 | -------------------------------------------------------------------------------- /test/postTest.js: -------------------------------------------------------------------------------- 1 | var request = require('supertest'); 2 | var express = require('express'); 3 | var apiResponse = require('../'); 4 | var should = require('should'); 5 | 6 | describe('express api middleware on *post* should', function(){ 7 | var app; 8 | beforeEach(function(){ 9 | app = express(); 10 | }); 11 | 12 | it('return 201 code on no data', function(done){ 13 | app.post('/', apiResponse); 14 | 15 | request(app) 16 | .post('/') 17 | .expect(201) 18 | .end(done); 19 | }); 20 | 21 | it('return 400 code on error', function(done){ 22 | app.post('/', function(req, res, next){ 23 | res.err = 'asd'; 24 | next(); 25 | }, apiResponse); 26 | 27 | request(app) 28 | .post('/') 29 | .expect(400) 30 | .end(done); 31 | }); 32 | 33 | it('return 201 code on non-object data', function(done){ 34 | app.post('/', function(req, res, next){ 35 | res.data = 'asd'; 36 | next(); 37 | }, apiResponse); 38 | 39 | request(app) 40 | .post('/') 41 | .expect(201) 42 | .end(done); 43 | }); 44 | 45 | it('return 201 code on object data', function(done){ 46 | app.post('/', function(req, res, next){ 47 | res.data = {asd: '1'}; 48 | next(); 49 | }, apiResponse); 50 | 51 | request(app) 52 | .post('/') 53 | .expect(201) 54 | .end(done); 55 | }); 56 | 57 | it('return 201 code on empty object data', function(done){ 58 | app.post('/', function(req, res, next){ 59 | res.data = {}; 60 | next(); 61 | }, apiResponse); 62 | 63 | request(app) 64 | .post('/') 65 | .expect(201) 66 | .end(done); 67 | }); 68 | 69 | it('return 201 code on array data', function(done){ 70 | app.post('/', function(req, res, next){ 71 | res.data = [1, 2, 3]; 72 | next(); 73 | }, apiResponse); 74 | 75 | request(app) 76 | .post('/') 77 | .expect(201) 78 | .end(done); 79 | }); 80 | 81 | it('return 201 code on empty array data', function(done){ 82 | app.post('/', function(req, res, next){ 83 | res.data = []; 84 | next(); 85 | }, apiResponse); 86 | 87 | request(app) 88 | .post('/') 89 | .expect(201) 90 | .end(done); 91 | }); 92 | 93 | it('return 201 code on empty data and should not have data', function(done){ 94 | app.post('/', function(req, res, next){ 95 | res.data = []; 96 | res.shouldNotHaveData = true; 97 | next(); 98 | }, apiResponse); 99 | 100 | request(app) 101 | .post('/') 102 | .expect(201) 103 | .end(done); 104 | }); 105 | 106 | it('return 400 code on wrong data and should have error message', function(done) { 107 | app.post('/', function(req, res, next) { 108 | res.err = { 109 | message: 'Something went wrong' 110 | }; 111 | next(); 112 | }, apiResponse); 113 | 114 | request(app) 115 | .post('/') 116 | .expect(400) 117 | .end(function(err, res) { 118 | res.error.text.should.equal('Something went wrong'); 119 | done(); 120 | }); 121 | }); 122 | 123 | it('return 400 code on wrong data and should not have error message', function(done) { 124 | app.post('/', function(req, res, next) { 125 | res.err = { 126 | message: 'Something went wrong' 127 | }; 128 | res.shouldSendErrorMessage = false; 129 | next(); 130 | }, apiResponse); 131 | 132 | request(app) 133 | .post('/') 134 | .expect(400) 135 | .end(function(err, res) { 136 | res.error.text.should.equal(''); 137 | done(); 138 | }); 139 | }); 140 | 141 | }); 142 | 143 | -------------------------------------------------------------------------------- /test/putTest.js: -------------------------------------------------------------------------------- 1 | var request = require('supertest'); 2 | var express = require('express'); 3 | var apiResponse = require('../'); 4 | 5 | describe('express api middleware on *put* should', function(){ 6 | var app; 7 | beforeEach(function(){ 8 | app = express(); 9 | }); 10 | 11 | it('return 204 code on no data', function(done){ 12 | app.put('/', apiResponse); 13 | 14 | request(app) 15 | .put('/') 16 | .expect(204) 17 | .end(done); 18 | }); 19 | 20 | it('return 400 code on error', function(done){ 21 | app.put('/', function(req, res, next){ 22 | res.err = 'asd'; 23 | next(); 24 | }, apiResponse); 25 | 26 | request(app) 27 | .put('/') 28 | .expect(400) 29 | .end(done); 30 | }); 31 | 32 | it('return 204 code on non-object data', function(done){ 33 | app.put('/', function(req, res, next){ 34 | res.data = 'asd'; 35 | next(); 36 | }, apiResponse); 37 | 38 | request(app) 39 | .put('/') 40 | .expect(204) 41 | .end(done); 42 | }); 43 | 44 | it('return 200 code on object data', function(done){ 45 | app.put('/', function(req, res, next){ 46 | res.data = {asd: '1'}; 47 | next(); 48 | }, apiResponse); 49 | 50 | request(app) 51 | .put('/') 52 | .expect(200) 53 | .end(done); 54 | }); 55 | 56 | it('return 204 code on empty object data', function(done){ 57 | app.put('/', function(req, res, next){ 58 | res.data = {}; 59 | next(); 60 | }, apiResponse); 61 | 62 | request(app) 63 | .put('/') 64 | .expect(204) 65 | .end(done); 66 | }); 67 | 68 | it('return 200 code on array data', function(done){ 69 | app.put('/', function(req, res, next){ 70 | res.data = [1, 2, 3]; 71 | next(); 72 | }, apiResponse); 73 | 74 | request(app) 75 | .put('/') 76 | .expect(200) 77 | .end(done); 78 | }); 79 | 80 | it('return 204 code on empty array data', function(done){ 81 | app.put('/', function(req, res, next){ 82 | res.data = []; 83 | next(); 84 | }, apiResponse); 85 | 86 | request(app) 87 | .put('/') 88 | .expect(204) 89 | .end(done); 90 | }); 91 | 92 | it('return 204 code on empty data and should not have data', function(done){ 93 | app.put('/', function(req, res, next){ 94 | res.data = []; 95 | res.shouldNotHaveData = true; 96 | next(); 97 | }, apiResponse); 98 | 99 | request(app) 100 | .put('/') 101 | .expect(204) 102 | .end(done); 103 | }); 104 | 105 | it('return 400 code on wrong data and should have error message', function(done) { 106 | app.put('/', function(req, res, next) { 107 | res.err = { 108 | message: 'Something went wrong' 109 | }; 110 | next(); 111 | }, apiResponse); 112 | 113 | request(app) 114 | .put('/') 115 | .expect(400) 116 | .end(function(err, res) { 117 | res.error.text.should.equal('Something went wrong'); 118 | done(); 119 | }); 120 | }); 121 | 122 | it('return 400 code on wrong data and should not have error message', function(done) { 123 | app.put('/', function(req, res, next) { 124 | res.err = { 125 | message: 'Something went wrong' 126 | }; 127 | res.shouldSendErrorMessage = false; 128 | next(); 129 | }, apiResponse); 130 | 131 | request(app) 132 | .put('/') 133 | .expect(400) 134 | .end(function(err, res) { 135 | res.error.text.should.equal(''); 136 | done(); 137 | }); 138 | }); 139 | 140 | }); 141 | 142 | -------------------------------------------------------------------------------- /test/staticOptionsTest.js: -------------------------------------------------------------------------------- 1 | var request = require('supertest'); 2 | var express = require('express'); 3 | var apiResponse = require('../'); 4 | 5 | describe('express api middleware options should', function(){ 6 | var app; 7 | 8 | beforeEach(function(){ 9 | app = express(); 10 | }); 11 | 12 | it('accept empty object without breaking the app', function(done){ 13 | apiResponse.options({}); 14 | app.get('/', apiResponse); 15 | 16 | request(app) 17 | .get('/') 18 | .expect(404) 19 | .end(done); 20 | }); 21 | 22 | 23 | it('accept empty array without breaking the app', function(done){ 24 | apiResponse.options([]); 25 | app.get('/', apiResponse); 26 | 27 | request(app) 28 | .get('/') 29 | .expect(404) 30 | .end(done); 31 | }); 32 | 33 | it('accept no arguments without breaking the app', function(done){ 34 | apiResponse.options(); 35 | app.get('/', apiResponse); 36 | 37 | request(app) 38 | .get('/') 39 | .expect(404) 40 | .end(done); 41 | }); 42 | 43 | }); --------------------------------------------------------------------------------