From cd303ab3ca72f8d20c05f9c3a91546e256e4d70a Mon Sep 17 00:00:00 2001 From: Baterka Date: Sun, 4 Aug 2019 14:33:24 +0200 Subject: [PATCH 1/2] Added HTTP status codes --- README.md | 1 + index.js | 278 ++++---- test/jsend.test.js | 1611 ++++++++++++++++++++++---------------------- 3 files changed, 948 insertions(+), 942 deletions(-) diff --git a/README.md b/README.md index fa74296..2a38586 100755 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # jsend Utilities and middleware to assist with sending and handling [jsend](https://github.com/omniti-labs/jsend) responses. +Edited original [Prestaul/jsend](https://github.com/Prestaul/jsend) (added HTTP status codes) ### Installation diff --git a/index.js b/index.js index ef7ceb9..714e00f 100755 --- a/index.js +++ b/index.js @@ -1,149 +1,155 @@ var STATUSES = { - success: { required: ['status', 'data'], allowed:['status', 'data'] }, - fail: { required: ['status', 'data'], allowed:['status', 'data'] }, - error: { required: ['status', 'message'], allowed:['status', 'message', 'data', 'code'] } + success: {required: ['status', 'data'], allowed: ['status', 'data']}, + fail: {required: ['status', 'data'], allowed: ['status', 'data']}, + error: {required: ['status', 'message'], allowed: ['status', 'message', 'data', 'code']} }; function requireKeys(keys, json) { - return keys.every(function(key) { - return key in json; - }); + return keys.every(function (key) { + return key in json; + }); } function allowKeys(keys, json) { - return Object.keys(json).every(function(key) { - return ~keys.indexOf(key); - }); + return Object.keys(json).every(function (key) { + return ~keys.indexOf(key); + }); } function jsend(config, host) { - config = config || {}; - host = host || {}; - - function isValid(json) { - var spec = STATUSES[json && json.status], - valid = !!spec && requireKeys(spec.required, json); - - if(config.strict) valid = valid && allowKeys(spec.allowed, json); - - return valid; - } - - function forward(json, done) { - if(!isValid(json)) - json = { - status: 'error', - message: 'Invalid jsend object.', - data: { originalObject: json } - }; - - if(json.status === 'success') - done(null, json.data); - else { - var err = new Error(json.message || ('Jsend response status: ' + json.status)); - if('code' in json) err.code = json.code; - if('data' in json) err.data = json.data; - done(err, json.data); - } - } - - function fromArguments(err, json) { - if(arguments.length === 1 && err.length === 2) { - json = err[1]; - err = err[0]; - } - - if(err) { - json = { - status: 'error', - message: (typeof err === 'string') - ? err - : err && err.message || 'Unknown error. (jsend)' - }; - if(err && err.stack) json.data = { stack:err.stack }; - } else if(json === undefined) { - json = { - status: 'error', - message: 'No data returned.' - }; - } else if(!isValid(json)) { - json = { - status: 'success', - data: json - }; - } - - return json; - } - - function success(data) { - if(data === undefined) throw new Error('"data" must be defined when calling jsend.success. (jsend)'); - return { - status: 'success', - data: (data && data.status === 'success' && isValid(data)) - ? data.data - : data - }; - } - - function fail(data) { - if(data === undefined) throw new Error('"data" must be defined when calling jsend.fail. (jsend)'); - return{ - status: 'fail', - data: (data && data.status === 'fail' && isValid(data)) - ? data.data - : data - }; - } - - function error(message, rest) { - var json = { - status: 'error' - }; - - if(typeof message === 'string') { - json.message = message; - if(rest) { - if(rest.code !== undefined) json.code = rest.code; - if(rest.data !== undefined) json.data = rest.data; - } - } else if(message && message.message) { - json.message = message.message; - if(message.code !== undefined) json.code = message.code; - if(message.data !== undefined) json.data = message.data; - } else { - throw new Error('"message" must be defined when calling jsend.error. (jsend)'); - } - - return json; - } - - host.isValid = isValid; - host.forward = forward; - host.fromArguments = fromArguments; - host.success = success; - host.fail = fail; - host.error = error; - - host.middleware = function(req, res, next) { - var middleware = res.jsend = function(err, json) { - res.json(fromArguments(err, json)); - }; - - middleware.success = function(data) { - res.json(success(data)); - }; - middleware.fail = function(data) { - res.json(fail(data)); - }; - middleware.error = function(message) { - res.json(error(message)); - }; - - next(); - }; - - return host; + config = config || {}; + host = host || {}; + + function isValid(json) { + var spec = STATUSES[json && json.status], + valid = !!spec && requireKeys(spec.required, json); + + if (config.strict) valid = valid && allowKeys(spec.allowed, json); + + return valid; + } + + function forward(json, done) { + if (!isValid(json)) + json = { + status: 'error', + message: 'Invalid jsend object.', + data: {originalObject: json} + }; + + if (json.status === 'success') + done(null, json.data); + else { + var err = new Error(json.message || ('Jsend response status: ' + json.status)); + if ('code' in json) err.code = json.code; + if ('data' in json) err.data = json.data; + done(err, json.data); + } + } + + function fromArguments(err, json) { + if (arguments.length === 1 && err.length === 2) { + json = err[1]; + err = err[0]; + } + + if (err) { + json = { + status: 'error', + message: (typeof err === 'string') + ? err + : err && err.message || 'Unknown error. (jsend)' + }; + if (err && err.stack) json.data = {stack: err.stack}; + } else if (json === undefined) { + json = { + status: 'error', + message: 'No data returned.' + }; + } else if (!isValid(json)) { + json = { + status: 'success', + data: json + }; + } + + return json; + } + + function success(data) { + if (data === undefined) throw new Error('"data" must be defined when calling jsend.success. (jsend)'); + return { + status: 'success', + data: (data && data.status === 'success' && isValid(data)) + ? data.data + : data + }; + } + + function fail(data) { + if (data === undefined) throw new Error('"data" must be defined when calling jsend.fail. (jsend)'); + return { + status: 'fail', + data: (data && data.status === 'fail' && isValid(data)) + ? data.data + : data + }; + } + + function error(message, rest) { + var json = { + status: 'error' + }; + + if (typeof message === 'string') { + json.message = message; + if (rest) { + if (rest.code !== undefined) json.code = rest.code; + if (rest.data !== undefined) json.data = rest.data; + } + } else if (message && message.message) { + json.message = message.message; + if (message.code !== undefined) json.code = message.code; + if (message.data !== undefined) json.data = message.data; + } else { + throw new Error('"message" must be defined when calling jsend.error. (jsend)'); + } + + return json; + } + + host.isValid = isValid; + host.forward = forward; + host.fromArguments = fromArguments; + host.success = success; + host.fail = fail; + host.error = error; + + host.middleware = function (req, res, next) { + var middleware = res.jsend = function (err, json) { + res.json(fromArguments(err, json)); + }; + + middleware.success = function (data, statusCode) { + if (typeof statusCode === 'undefined') + statusCode = 200; + res.status(statusCode).json(success(data)); + }; + middleware.fail = function (data, statusCode) { + if (typeof statusCode === 'undefined') + statusCode = 400; + res.status(statusCode).json(fail(data)); + }; + middleware.error = function (message, statusCode) { + if (typeof statusCode === 'undefined') + statusCode = 500; + res.status(statusCode).json(error(message)); + }; + + next(); + }; + + return host; } module.exports = jsend(null, jsend); diff --git a/test/jsend.test.js b/test/jsend.test.js index 39aa9cb..3ae6179 100755 --- a/test/jsend.test.js +++ b/test/jsend.test.js @@ -1,845 +1,844 @@ var assert = require('chai').assert, - jsend = require('../index'); - - -describe('jsend', function() { - - function basicTests(jsendInstance) { - - describe('- isValid', function() { - describe('should validate', function() { - describe('"success" status', function() { - it('with object data', function() { - assert(jsendInstance.isValid({ - status: 'success', - data: { foo:'bar' } - })); - }); - - it('with array data', function() { - assert(jsendInstance.isValid({ - status: 'success', - data: [1, 2, 3] - })); - }); - - it('with null data', function() { - assert(jsendInstance.isValid({ - status: 'success', - data: null - })); - }); - }); - - describe('"fail" status', function() { - it('with object data', function() { - assert(jsendInstance.isValid({ - status: 'fail', - data: { foo:'bar' } - })); - }); - - it('with array data', function() { - assert(jsendInstance.isValid({ - status: 'fail', - data: [1, 2, 3] - })); - }); - - it('with null data', function() { - assert(jsendInstance.isValid({ - status: 'fail', - data: null - })); - }); - }); - - describe('"error" status', function() { - it('with a message', function() { - assert(jsendInstance.isValid({ - status: 'error', - message: 'something is wrong' - })); - }); - - it('with a message and a code', function() { - assert(jsendInstance.isValid({ - status: 'error', - message: 'something is wrong', - code: 123 - })); - }); - - it('with a message and data', function() { - assert(jsendInstance.isValid({ - status: 'error', - message: 'something is wrong', - data: { stack:'this -> that -> the other' } - })); - }); - - it('with a message, a code, and data', function() { - assert(jsendInstance.isValid({ - status: 'error', - message: 'something is wrong', - code: 123, - data: { stack:'this -> that -> the other' } - })); - }); - }); - }); - - describe('should invalidate', function() { - it('object with no status', function() { - assert.isFalse(jsendInstance.isValid({ - data: { foo:'bar' } - })); - }); - - it('"success" status without data', function() { - assert.isFalse(jsendInstance.isValid({ - status: 'success' - })); - }); - - it('"fail" status without data', function() { - assert.isFalse(jsendInstance.isValid({ - status: 'fail' - })); - }); - - it('"error" status without message', function() { - assert.isFalse(jsendInstance.isValid({ - status: 'error', - data: { foo:'bar' } - })); - }); - }); - }); - - - - describe('- fromArguments', function() { - describe('should generate "success"', function() { - it('with object data', function() { - var json = { status:'success', data:{ foo:'bar' } }; - assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); - }); - - it('with array data', function() { - var json = { status:'success', data:[1,2,3] }; - assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); - }); - - it('with string data', function() { - var json = { status:'success', data:'you got it' }; - assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); - }); - - it('with numeric data', function() { - var json = { status:'success', data:123 }; - assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); - }); - - it('with null data', function() { - var json = { status:'success', data:null }; - assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); - }); - }); - - describe('should generate "error"', function() { - it('with error message as first arg', function() { - var json = { status:'error', message:'something bad' }; - assert.deepEqual(jsendInstance.fromArguments(json.message), json); - }); - - it('with Error object as first arg', function() { - var json = { status:'error', message:'something bad' }, - output = jsendInstance.fromArguments(new Error(json.message)); - assert.isObject(output.data); - assert.isString(output.data.stack); - delete output.data; - assert.deepEqual(output, json); - }); - - it('with jsend error object as first arg', function() { - var json = { status:'error', message:'something bad' }; - assert.deepEqual(jsendInstance.fromArguments(json), json); - }); - - it('with jsend fail object as first arg', function() { - var json = { status:'fail', data:{ something:'bad' } }; - assert.deepEqual(jsendInstance.fromArguments(json), { status:'error', message:'Unknown error. (jsend)' }); - }); - - it('with jsend fail object as first arg and preserve message', function() { - var json = { status:'fail', data:{ something:'bad' }, message:'Really bad!' }; - assert.deepEqual(jsendInstance.fromArguments(json), { status:'error', message:'Really bad!' }); - }); - - it('with jsend success object as first arg', function() { - var json = { status:'success', data:{ something:'bad' } }; - assert.deepEqual(jsendInstance.fromArguments(json), { status:'error', message:'Unknown error. (jsend)' }); - }); - }); - }); - - - - describe('- forward', function() { - function assertCall(expectedErr, expectedData) { - return function(err, data) { - if(expectedErr) { - assert.typeOf(err, 'error'); - assert.isString(err.message); - if(expectedErr.message) assert.equal(err.message, expectedErr.message); - if(expectedErr.code) assert.equal(err.code, expectedErr.code); - } - - if(expectedData !== undefined) { - assert.deepEqual(data, expectedData); - } - }; - } - - describe('for "success"', function() { - it('should pass object data', function() { - var json = { status:'success', data:{ foo:'bar' } }; - jsendInstance.forward(json, assertCall(null, json.data)); - }); - - it('should pass array data', function() { - var json = { status:'success', data:[1,2,3] }; - jsendInstance.forward(json, assertCall(null, json.data)); - }); - - it('should pass string data', function() { - var json = { status:'success', data:'you got it' }; - jsendInstance.forward(json, assertCall(null, json.data)); - }); - - it('should pass numeric data', function() { - var json = { status:'success', data:123 }; - jsendInstance.forward(json, assertCall(null, json.data)); - }); - - it('should pass null data', function() { - var json = { status:'success', data:null }; - jsendInstance.forward(json, assertCall(null, json.data)); - }); - }); - - describe('for "fail"', function() { - it('should pass an error and data', function() { - var json = { status:'fail', data:{ validation:false } }; - jsendInstance.forward(json, assertCall(true, json.data)); - }); - }); - - describe('for "error"', function() { - it('should pass an error', function() { - var json = { status:'error', message:'something bad' }; - jsendInstance.forward(json, assertCall(json)); - }); - - it('with code should pass an error with code', function() { - var json = { status:'error', message:'something bad', code:123 }; - jsendInstance.forward(json, assertCall(json)); - }); - - it('with data should pass the data', function() { - var json = { status:'error', message:'something bad', code:123, data:{ foo:'bar' } }; - jsendInstance.forward(json, assertCall(json, json.data)); - }); - }); - - describe('for invalid jsend responses', function() { - it('passes string responses back as the error message', function() { - var html = '414 Request-URI Too Large'; - jsendInstance.forward(html, function(err, data) { - assert.equal(err.data.originalObject, html); - }); - }); + jsend = require('../index'); + + +describe('jsend', function () { + + function basicTests(jsendInstance) { + + describe('- isValid', function () { + describe('should validate', function () { + describe('"success" status', function () { + it('with object data', function () { + assert(jsendInstance.isValid({ + status: 'success', + data: {foo: 'bar'} + })); + }); + + it('with array data', function () { + assert(jsendInstance.isValid({ + status: 'success', + data: [1, 2, 3] + })); + }); + + it('with null data', function () { + assert(jsendInstance.isValid({ + status: 'success', + data: null + })); + }); + }); + + describe('"fail" status', function () { + it('with object data', function () { + assert(jsendInstance.isValid({ + status: 'fail', + data: {foo: 'bar'} + })); + }); + + it('with array data', function () { + assert(jsendInstance.isValid({ + status: 'fail', + data: [1, 2, 3] + })); + }); + + it('with null data', function () { + assert(jsendInstance.isValid({ + status: 'fail', + data: null + })); + }); + }); + + describe('"error" status', function () { + it('with a message', function () { + assert(jsendInstance.isValid({ + status: 'error', + message: 'something is wrong' + })); + }); + + it('with a message and a code', function () { + assert(jsendInstance.isValid({ + status: 'error', + message: 'something is wrong', + code: 123 + })); + }); + + it('with a message and data', function () { + assert(jsendInstance.isValid({ + status: 'error', + message: 'something is wrong', + data: {stack: 'this -> that -> the other'} + })); + }); + + it('with a message, a code, and data', function () { + assert(jsendInstance.isValid({ + status: 'error', + message: 'something is wrong', + code: 123, + data: {stack: 'this -> that -> the other'} + })); + }); + }); + }); + + describe('should invalidate', function () { + it('object with no status', function () { + assert.isFalse(jsendInstance.isValid({ + data: {foo: 'bar'} + })); + }); + + it('"success" status without data', function () { + assert.isFalse(jsendInstance.isValid({ + status: 'success' + })); + }); + + it('"fail" status without data', function () { + assert.isFalse(jsendInstance.isValid({ + status: 'fail' + })); + }); + + it('"error" status without message', function () { + assert.isFalse(jsendInstance.isValid({ + status: 'error', + data: {foo: 'bar'} + })); + }); + }); + }); + + + describe('- fromArguments', function () { + describe('should generate "success"', function () { + it('with object data', function () { + var json = {status: 'success', data: {foo: 'bar'}}; + assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); + }); + + it('with array data', function () { + var json = {status: 'success', data: [1, 2, 3]}; + assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); + }); + + it('with string data', function () { + var json = {status: 'success', data: 'you got it'}; + assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); + }); + + it('with numeric data', function () { + var json = {status: 'success', data: 123}; + assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); + }); + + it('with null data', function () { + var json = {status: 'success', data: null}; + assert.deepEqual(jsendInstance.fromArguments(null, json.data), json); + }); + }); + + describe('should generate "error"', function () { + it('with error message as first arg', function () { + var json = {status: 'error', message: 'something bad'}; + assert.deepEqual(jsendInstance.fromArguments(json.message), json); + }); + + it('with Error object as first arg', function () { + var json = {status: 'error', message: 'something bad'}, + output = jsendInstance.fromArguments(new Error(json.message)); + assert.isObject(output.data); + assert.isString(output.data.stack); + delete output.data; + assert.deepEqual(output, json); + }); + + it('with jsend error object as first arg', function () { + var json = {status: 'error', message: 'something bad'}; + assert.deepEqual(jsendInstance.fromArguments(json), json); + }); + + it('with jsend fail object as first arg', function () { + var json = {status: 'fail', data: {something: 'bad'}}; + assert.deepEqual(jsendInstance.fromArguments(json), {status: 'error', message: 'Unknown error. (jsend)'}); + }); + + it('with jsend fail object as first arg and preserve message', function () { + var json = {status: 'fail', data: {something: 'bad'}, message: 'Really bad!'}; + assert.deepEqual(jsendInstance.fromArguments(json), {status: 'error', message: 'Really bad!'}); + }); + + it('with jsend success object as first arg', function () { + var json = {status: 'success', data: {something: 'bad'}}; + assert.deepEqual(jsendInstance.fromArguments(json), {status: 'error', message: 'Unknown error. (jsend)'}); + }); + }); + }); + + + describe('- forward', function () { + function assertCall(expectedErr, expectedData) { + return function (err, data) { + if (expectedErr) { + assert.typeOf(err, 'error'); + assert.isString(err.message); + if (expectedErr.message) assert.equal(err.message, expectedErr.message); + if (expectedErr.code) assert.equal(err.code, expectedErr.code); + } + + if (expectedData !== undefined) { + assert.deepEqual(data, expectedData); + } + }; + } + + describe('for "success"', function () { + it('should pass object data', function () { + var json = {status: 'success', data: {foo: 'bar'}}; + jsendInstance.forward(json, assertCall(null, json.data)); + }); + + it('should pass array data', function () { + var json = {status: 'success', data: [1, 2, 3]}; + jsendInstance.forward(json, assertCall(null, json.data)); + }); + + it('should pass string data', function () { + var json = {status: 'success', data: 'you got it'}; + jsendInstance.forward(json, assertCall(null, json.data)); + }); + + it('should pass numeric data', function () { + var json = {status: 'success', data: 123}; + jsendInstance.forward(json, assertCall(null, json.data)); + }); + + it('should pass null data', function () { + var json = {status: 'success', data: null}; + jsendInstance.forward(json, assertCall(null, json.data)); + }); + }); + + describe('for "fail"', function () { + it('should pass an error and data', function () { + var json = {status: 'fail', data: {validation: false}}; + jsendInstance.forward(json, assertCall(true, json.data)); + }); + }); + + describe('for "error"', function () { + it('should pass an error', function () { + var json = {status: 'error', message: 'something bad'}; + jsendInstance.forward(json, assertCall(json)); + }); + + it('with code should pass an error with code', function () { + var json = {status: 'error', message: 'something bad', code: 123}; + jsendInstance.forward(json, assertCall(json)); + }); + + it('with data should pass the data', function () { + var json = {status: 'error', message: 'something bad', code: 123, data: {foo: 'bar'}}; + jsendInstance.forward(json, assertCall(json, json.data)); + }); + }); + + describe('for invalid jsend responses', function () { + it('passes string responses back as the error message', function () { + var html = '414 Request-URI Too Large'; + jsendInstance.forward(html, function (err, data) { + assert.equal(err.data.originalObject, html); + }); + }); + + it('passes object responses back as the error message', function () { + var html = {"invalid-jsend": true}; + jsendInstance.forward(html, function (err, data) { + assert.equal(err.data.originalObject["invalid-jsend"], html["invalid-jsend"]); + }); + }); + }); + }); - it('passes object responses back as the error message', function() { - var html = {"invalid-jsend": true}; - jsendInstance.forward(html, function(err, data) { - assert.equal(err.data.originalObject["invalid-jsend"], html["invalid-jsend"]); - }); - }); - }); - }); + describe('- success', function () { - describe('- success', function() { + it('with jsend object', function () { + var json = {status: 'success', data: {foo: 'bar'}}; - it('with jsend object', function() { - var json = { status:'success', data:{ foo:'bar' } }; + assert.deepEqual(json, jsendInstance.success(json)); + }); - assert.deepEqual(json, jsendInstance.success(json)); - }); + it('with object data', function () { + var json = {status: 'success', data: {foo: 'bar'}}; - it('with object data', function() { - var json = { status:'success', data:{ foo:'bar' } }; + assert.deepEqual(json, jsendInstance.success(json.data)); + }); - assert.deepEqual(json, jsendInstance.success(json.data)); - }); + it('with array data', function () { + var json = {status: 'success', data: [1, 2, 3]}; - it('with array data', function() { - var json = { status:'success', data:[1,2,3] }; + assert.deepEqual(json, jsendInstance.success(json.data)); + }); - assert.deepEqual(json, jsendInstance.success(json.data)); - }); + it('with string data', function () { + var json = {status: 'success', data: 'you got it'}; - it('with string data', function() { - var json = { status:'success', data:'you got it' }; + assert.deepEqual(json, jsendInstance.success(json.data)); + }); - assert.deepEqual(json, jsendInstance.success(json.data)); - }); + it('with numeric data', function () { + var json = {status: 'success', data: 123}; - it('with numeric data', function() { - var json = { status:'success', data:123 }; + assert.deepEqual(json, jsendInstance.success(json.data)); + }); - assert.deepEqual(json, jsendInstance.success(json.data)); - }); + it('with null data', function () { + var json = {status: 'success', data: null}; - it('with null data', function() { - var json = { status:'success', data:null }; + assert.deepEqual(json, jsendInstance.success(json.data)); + }); - assert.deepEqual(json, jsendInstance.success(json.data)); - }); + it('should throw error with no data', function () { + assert.throws(jsendInstance.success); + }); + }); - it('should throw error with no data', function() { - assert.throws(jsendInstance.success); - }); - }); + describe('- fail', function () { - describe('- fail', function() { + it('with jsend object', function () { + var json = {status: 'fail', data: {foo: 'bar'}}; - it('with jsend object', function() { - var json = { status:'fail', data:{ foo:'bar' } }; + assert.deepEqual(json, jsendInstance.fail(json)); + }); - assert.deepEqual(json, jsendInstance.fail(json)); - }); + it('with object data', function () { + var json = {status: 'fail', data: {foo: 'bar'}}; - it('with object data', function() { - var json = { status:'fail', data:{ foo:'bar' } }; + assert.deepEqual(json, jsendInstance.fail(json.data)); + }); - assert.deepEqual(json, jsendInstance.fail(json.data)); - }); + it('with array data', function () { + var json = {status: 'fail', data: [1, 2, 3]}; - it('with array data', function() { - var json = { status:'fail', data:[1,2,3] }; + assert.deepEqual(json, jsendInstance.fail(json.data)); + }); - assert.deepEqual(json, jsendInstance.fail(json.data)); - }); + it('with string data', function () { + var json = {status: 'fail', data: 'you got it'}; - it('with string data', function() { - var json = { status:'fail', data:'you got it' }; + assert.deepEqual(json, jsendInstance.fail(json.data)); + }); - assert.deepEqual(json, jsendInstance.fail(json.data)); - }); + it('with numeric data', function () { + var json = {status: 'fail', data: 123}; - it('with numeric data', function() { - var json = { status:'fail', data:123 }; + assert.deepEqual(json, jsendInstance.fail(json.data)); + }); - assert.deepEqual(json, jsendInstance.fail(json.data)); - }); + it('with null data', function () { + var json = {status: 'fail', data: null}; - it('with null data', function() { - var json = { status:'fail', data:null }; + assert.deepEqual(json, jsendInstance.fail(json.data)); + }); - assert.deepEqual(json, jsendInstance.fail(json.data)); - }); + it('should throw error with no data', function () { + assert.throws(jsendInstance.fail); + }); - it('should throw error with no data', function() { - assert.throws(jsendInstance.fail); - }); + }); - }); + describe('- error', function () { - describe('- error', function() { + it('with message', function () { + var json = {status: 'error', message: 'something bad'}; - it('with message', function() { - var json = { status:'error', message:'something bad' }; + assert.deepEqual(json, jsendInstance.error(json.message)); + }); - assert.deepEqual(json, jsendInstance.error(json.message)); - }); + it('with message and code', function () { + var json = {status: 'error', message: 'something bad', code: 'BAD_THINGS'}; - it('with message and code', function() { - var json = { status:'error', message:'something bad', code:'BAD_THINGS' }; + assert.deepEqual(json, jsendInstance.error(json)); + }); - assert.deepEqual(json, jsendInstance.error(json)); - }); + it('with message and data', function () { + var json = {status: 'error', message: 'something bad', data: {foo: 'bar'}}; - it('with message and data', function() { - var json = { status:'error', message:'something bad', data:{ foo:'bar' } }; - - assert.deepEqual(json, jsendInstance.error(json)); - }); - - it('with message and data and code', function() { - var json = { status:'error', message:'something bad', code:'BAD_THINGS', data:{ foo:'bar' } }; - - assert.deepEqual(json, jsendInstance.error(json)); - }); - - it('with message as first argument, and data and code as second', function () { - var json = { status:'error', message:'something bad', code:'BAD_THINGS', data:{ foo:'bar' } }; - var { message, ...rest } = json; - - assert.deepEqual(json, jsendInstance.error(message, rest)); - }); - - it('with message as first argument, and null as second', function () { - var json = { status:'error', message:'something bad' }; - - assert.deepEqual(json, jsendInstance.error(json.message, null)); - }); - - it('should throw error with no message', function() { - var json = { status:'error', code:'BAD_THINGS', data:{ foo:'bar' } }; - - assert.throws(jsendInstance.error.bind(jsendInstance, json)); - }); - - }); - - describe('- middleware', function() { - var req = {}; - - it('should call "next" callback', function(done) { - jsendInstance.middleware({}, {}, done); - }); - - describe('should respond with "error"', function() { - it('with error message as first arg', function(done) { - var json = { status:'error', message:'something bad' }, - res = { - json: function(output) { - assert.deepEqual(output, { status:'error', message:'something bad' }); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(json.message); - }); - }); - - it('with Error object as first arg', function(done) { - var json = { status:'error', message:'something bad' }, - res = { - json: function(output) { - assert.isObject(output.data); - assert.isString(output.data.stack); - delete output.data; - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(new Error(json.message)); - }); - }); - - it('with jsend error object as first arg', function(done) { - var json = { status:'error', message:'something bad' }, - res = { - json: function(output) { - assert.deepEqual(jsendInstance.fromArguments(json), json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(json); - }); - }); - - it('with jsend fail object as first arg', function(done) { - var json = { status:'fail', data:{ something:'bad' } }, - res = { - json: function(output) { - assert.deepEqual(jsendInstance.fromArguments(json), { status:'error', message:'Unknown error. (jsend)' }); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(json); - }); - }); - - it('with jsend fail object as first arg and preserve message', function(done) { - var json = { status:'fail', data:{ something:'bad' }, message:'Really bad!' }, - res = { - json: function(output) { - assert.deepEqual(jsendInstance.fromArguments(json), { status:'error', message:'Really bad!' }); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(json); - }); - }); - - it('with jsend success object as first arg', function(done) { - var json = { status:'success', data:{ something:'bad' } }, - res = { - json: function(output) { - assert.deepEqual(jsendInstance.fromArguments(json), { status:'error', message:'Unknown error. (jsend)' }); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(json); - }); - }); - - }); - - describe('should respond with "success"', function() { - - it('with jsend object', function(done) { - var json = { status:'success', data:{ foo:'bar' } }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(null, json); - }); - }); - - it('with object data', function(done) { - var json = { status:'success', data:{ foo:'bar' } }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(null, json.data); - }); - }); - - it('with array data', function(done) { - var json = { status:'success', data:[1,2,3] }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(null, json.data); - }); - }); - - it('with string data', function(done) { - var json = { status:'success', data:'you got it' }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(null, json.data); - }); - }); - - it('with numeric data', function(done) { - var json = { status:'success', data:123 }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(null, json.data); - }); - }); - - it('with null data', function(done) { - var json = { status:'success', data:null }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend(null, json.data); - }); - }); - - }); - - describe('.success method', function() { - - it('with jsend object', function(done) { - var json = { status:'success', data:{ foo:'bar' } }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.success(json); - }); - }); - - it('with object data', function(done) { - var json = { status:'success', data:{ foo:'bar' } }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.success(json.data); - }); - }); - - it('with array data', function(done) { - var json = { status:'success', data:[1,2,3] }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.success(json.data); - }); - }); - - it('with string data', function(done) { - var json = { status:'success', data:'you got it' }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.success(json.data); - }); - }); - - it('with numeric data', function(done) { - var json = { status:'success', data:123 }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.success(json.data); - }); - }); - - it('with null data', function(done) { - var json = { status:'success', data:null }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.success(json.data); - }); - }); - - it('should throw error with no data', function(done) { - var res = {}; - jsendInstance.middleware(req, res, function() { - assert.throws(function() { - res.jsend.success(); - }); - done(); - }); - }); - - }); - - describe('.fail method', function() { - - it('with jsend object', function(done) { - var json = { status:'fail', data:{ foo:'bar' } }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.fail(json); - }); - }); - - it('with object data', function(done) { - var json = { status:'fail', data:{ foo:'bar' } }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.fail(json.data); - }); - }); - - it('with array data', function(done) { - var json = { status:'fail', data:[1,2,3] }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.fail(json.data); - }); - }); - - it('with string data', function(done) { - var json = { status:'fail', data:'you got it' }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.fail(json.data); - }); - }); - - it('with numeric data', function(done) { - var json = { status:'fail', data:123 }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.fail(json.data); - }); - }); - - it('with null data', function(done) { - var json = { status:'fail', data:null }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.fail(json.data); - }); - }); - - it('should throw error with no data', function(done) { - var res = {}; - jsendInstance.middleware(req, res, function() { - assert.throws(function() { - res.jsend.fail(); - }); - done(); - }); - }); - - }); - - describe('.error method', function() { - - it('with message', function(done) { - var json = { status:'error', message:'something bad' }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.error(json.message); - }); - }); - - it('with message and code', function(done) { - var json = { status:'error', message:'something bad', code:'BAD_THINGS' }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.error(json); - }); - }); - - it('with message and data', function(done) { - var json = { status:'error', message:'something bad', data:{ foo:'bar' } }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.error(json); - }); - }); - - it('with message and data and code', function(done) { - var json = { status:'error', message:'something bad', code:'BAD_THINGS', data:{ foo:'bar' } }, - res = { - json: function(output) { - assert.deepEqual(output, json); - done(); - } - }; - jsendInstance.middleware(req, res, function() { - res.jsend.error(json); - }); - }); - - it('should throw error with no message', function(done) { - var json = { status:'error', code:'BAD_THINGS', data:{ foo:'bar' } }, - res = {}; - jsendInstance.middleware(req, res, function() { - assert.throws(function() { - res.jsend.error(json); - }); - done(); - }); - }); - - }); - }); - } - - describe('without strict flag', function() { - var jsendInstance = jsend; - - basicTests(jsendInstance); - }); - - describe('with strict flag', function() { - var jsendInstance = jsend({ strict:true }); - - basicTests(jsendInstance); - }); + assert.deepEqual(json, jsendInstance.error(json)); + }); + + it('with message and data and code', function () { + var json = {status: 'error', message: 'something bad', code: 'BAD_THINGS', data: {foo: 'bar'}}; + + assert.deepEqual(json, jsendInstance.error(json)); + }); + + it('with message as first argument, and data and code as second', function () { + var json = {status: 'error', message: 'something bad', code: 'BAD_THINGS', data: {foo: 'bar'}}; + var {message, ...rest} = json; + + assert.deepEqual(json, jsendInstance.error(message, rest)); + }); + + it('with message as first argument, and null as second', function () { + var json = {status: 'error', message: 'something bad'}; + + assert.deepEqual(json, jsendInstance.error(json.message, null)); + }); + + it('should throw error with no message', function () { + var json = {status: 'error', code: 'BAD_THINGS', data: {foo: 'bar'}}; + + assert.throws(jsendInstance.error.bind(jsendInstance, json)); + }); + + }); + + describe('- middleware', function () { + var req = {}; + + it('should call "next" callback', function (done) { + jsendInstance.middleware({}, {}, done); + }); + + describe('should respond with "error"', function () { + it('with error message as first arg', function (done) { + var json = {status: 'error', message: 'something bad'}, + res = { + json: function (output) { + assert.deepEqual(output, {status: 'error', message: 'something bad'}); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(json.message); + }); + }); + + it('with Error object as first arg', function (done) { + var json = {status: 'error', message: 'something bad'}, + res = { + json: function (output) { + assert.isObject(output.data); + assert.isString(output.data.stack); + delete output.data; + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(new Error(json.message)); + }); + }); + + it('with jsend error object as first arg', function (done) { + var json = {status: 'error', message: 'something bad'}, + res = { + json: function (output) { + assert.deepEqual(jsendInstance.fromArguments(json), json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(json); + }); + }); + + it('with jsend fail object as first arg', function (done) { + var json = {status: 'fail', data: {something: 'bad'}}, + res = { + json: function (output) { + assert.deepEqual(jsendInstance.fromArguments(json), {status: 'error', message: 'Unknown error. (jsend)'}); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(json); + }); + }); + + it('with jsend fail object as first arg and preserve message', function (done) { + var json = {status: 'fail', data: {something: 'bad'}, message: 'Really bad!'}, + res = { + json: function (output) { + assert.deepEqual(jsendInstance.fromArguments(json), {status: 'error', message: 'Really bad!'}); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(json); + }); + }); + + it('with jsend success object as first arg', function (done) { + var json = {status: 'success', data: {something: 'bad'}}, + res = { + json: function (output) { + assert.deepEqual(jsendInstance.fromArguments(json), {status: 'error', message: 'Unknown error. (jsend)'}); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(json); + }); + }); + + }); + + describe('should respond with "success"', function () { + + it('with jsend object', function (done) { + var json = {status: 'success', data: {foo: 'bar'}}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(null, json); + }); + }); + + it('with object data', function (done) { + var json = {status: 'success', data: {foo: 'bar'}}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(null, json.data); + }); + }); + + it('with array data', function (done) { + var json = {status: 'success', data: [1, 2, 3]}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(null, json.data); + }); + }); + + it('with string data', function (done) { + var json = {status: 'success', data: 'you got it'}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(null, json.data); + }); + }); + + it('with numeric data', function (done) { + var json = {status: 'success', data: 123}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(null, json.data); + }); + }); + + it('with null data', function (done) { + var json = {status: 'success', data: null}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend(null, json.data); + }); + }); + + }); + + describe('.success method', function () { + + it('with jsend object', function (done) { + var json = {status: 'success', data: {foo: 'bar'}}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.success(json); + }); + }); + + it('with object data', function (done) { + var json = {status: 'success', data: {foo: 'bar'}}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.success(json.data); + }); + }); + + it('with array data', function (done) { + var json = {status: 'success', data: [1, 2, 3]}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.success(json.data); + }); + }); + + it('with string data', function (done) { + var json = {status: 'success', data: 'you got it'}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.success(json.data); + }); + }); + + it('with numeric data', function (done) { + var json = {status: 'success', data: 123}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.success(json.data); + }); + }); + + it('with null data', function (done) { + var json = {status: 'success', data: null}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.success(json.data); + }); + }); + + it('should throw error with no data', function (done) { + var res = {}; + jsendInstance.middleware(req, res, function () { + assert.throws(function () { + res.jsend.success(); + }); + done(); + }); + }); + + }); + + describe('.fail method', function () { + + it('with jsend object', function (done) { + var json = {status: 'fail', data: {foo: 'bar'}}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.fail(json); + }); + }); + + it('with object data', function (done) { + var json = {status: 'fail', data: {foo: 'bar'}}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.fail(json.data); + }); + }); + + it('with array data', function (done) { + var json = {status: 'fail', data: [1, 2, 3]}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.fail(json.data); + }); + }); + + it('with string data', function (done) { + var json = {status: 'fail', data: 'you got it'}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.fail(json.data); + }); + }); + + it('with numeric data', function (done) { + var json = {status: 'fail', data: 123}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.fail(json.data); + }); + }); + + it('with null data', function (done) { + var json = {status: 'fail', data: null}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.fail(json.data); + }); + }); + + it('should throw error with no data', function (done) { + var res = {}; + jsendInstance.middleware(req, res, function () { + assert.throws(function () { + res.jsend.fail(); + }); + done(); + }); + }); + + }); + + describe('.error method', function () { + + it('with message', function (done) { + var json = {status: 'error', message: 'something bad'}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.error(json.message); + }); + }); + + it('with message and code', function (done) { + var json = {status: 'error', message: 'something bad', code: 'BAD_THINGS'}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.error(json); + }); + }); + + it('with message and data', function (done) { + var json = {status: 'error', message: 'something bad', data: {foo: 'bar'}}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.error(json); + }); + }); + + it('with message and data and code', function (done) { + var json = {status: 'error', message: 'something bad', code: 'BAD_THINGS', data: {foo: 'bar'}}, + res = { + json: function (output) { + assert.deepEqual(output, json); + done(); + } + }; + jsendInstance.middleware(req, res, function () { + res.jsend.error(json); + }); + }); + + it('should throw error with no message', function (done) { + var json = {status: 'error', code: 'BAD_THINGS', data: {foo: 'bar'}}, + res = {}; + jsendInstance.middleware(req, res, function () { + assert.throws(function () { + res.jsend.error(json); + }); + done(); + }); + }); + + }); + }); + } + + describe('without strict flag', function () { + var jsendInstance = jsend; + + + basicTests(jsendInstance); + }); + + describe('with strict flag', function () { + var jsendInstance = jsend({strict: true}); + + basicTests(jsendInstance); + }); }); From 3b3adc4088aa288f69b6c7ba3fc80b8b6682f142 Mon Sep 17 00:00:00 2001 From: Baterka Date: Sun, 4 Aug 2019 14:33:41 +0200 Subject: [PATCH 2/2] Added HTTP status codes --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 82aa882..6753d34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jsend", - "version": "1.1.0", + "version": "1.1.1", "description": "Utilities and middleware to assist with sending and handling jsend responses.", "keywords": [ "jsend",