hapi-status
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -11,3 +11,3 @@ 'use strict'; | ||
delegate = { | ||
delegates = { | ||
applicationJson: (result, code, message) => { | ||
@@ -19,3 +19,3 @@ let body = { | ||
if (Math.floor(code * 0.01) >= 4) { | ||
if (+(code + '')[0] >= 4) { | ||
body.error = message; | ||
@@ -73,2 +73,15 @@ delete body.result; | ||
/** | ||
* add/replace a delegation for response manipulation | ||
* | ||
* @method addDelegation | ||
* @return void | ||
* @public | ||
*/ | ||
addDelegation(type, delegation) { | ||
if (typeof delegation === 'function') { | ||
delegates[type] = delegation; | ||
} | ||
} | ||
/** | ||
* create a function based on the status code message | ||
@@ -90,3 +103,3 @@ * | ||
type = camelcase(headers['content-type'].replace(/\//, ' ')); | ||
body = type in delegate ? delegate[type].apply(status, [result, code, message]) : result; | ||
body = type in delegates ? delegates[type].apply(status, [result, code, message]) : result; | ||
@@ -93,0 +106,0 @@ return applyHeaders(headers, reply(body).code(code)); |
{ | ||
"name": "hapi-status", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Easily add status code and headers to a response in Hapi", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
'use strict'; | ||
var Lab = require('lab'), | ||
const Lab = require('lab'), | ||
lab = exports.lab = Lab.script(), | ||
@@ -9,7 +9,7 @@ Code = require('code'), | ||
function Reply() { | ||
var reply = this; | ||
let reply = this; | ||
reply.headers = {}; | ||
reply.code = function code(statusCode) { | ||
reply.code = (statusCode) => { | ||
reply.statusCode = statusCode; | ||
@@ -19,3 +19,3 @@ return reply; | ||
reply.type = function type(contentType) { | ||
reply.type = (contentType) => { | ||
reply.header('Content-Type', contentType); | ||
@@ -26,3 +26,3 @@ | ||
reply.header = function(key, value) { | ||
reply.header = (key, value) => { | ||
reply.headers[key] = value; | ||
@@ -33,3 +33,3 @@ | ||
return function body(result) { | ||
return (result) => { | ||
reply.result = result; | ||
@@ -41,6 +41,6 @@ | ||
Object.keys(status).forEach(function(key) { | ||
lab.experiment(key, function() { | ||
lab.test('should serve the right statusCode', function(done) { | ||
var response = status[key].apply(null, [new Reply()]); | ||
Object.keys(status).forEach((key) => { | ||
lab.experiment(key, () => { | ||
lab.test('should serve the right statusCode', (done) => { | ||
let response = status[key].apply(null, [new Reply()]); | ||
@@ -52,6 +52,6 @@ Code.expect(response.result.statusCode).to.equal(response.statusCode); | ||
lab.test('should contain the right key', function(done) { | ||
var response = status[key].apply(null, [new Reply(), 'test']).result; | ||
lab.test('should contain the right key', (done) => { | ||
let response = status[key].apply(null, [new Reply(), 'test']).result; | ||
if (Math.floor(response.statusCode * 0.01) >= 4) { | ||
if (+(response.statusCode + '')[0] >= 4) { | ||
Code.expect('error' in response).to.equal(true); | ||
@@ -68,4 +68,4 @@ Code.expect('result' in response).to.equal(false); | ||
lab.test('should be able to add headers', function(done) { | ||
var response = status[key].apply(null, [new Reply(), 'test', {'Cache-Control': 'max-age=0, no-cache, no-store'}]); | ||
lab.test('should be able to add headers', (done) => { | ||
let response = status[key].apply(null, [new Reply(), 'test', {'Cache-Control': 'max-age=0, no-cache, no-store'}]); | ||
@@ -78,4 +78,4 @@ Code.expect('Cache-Control' in response.headers).to.equal(true); | ||
lab.test('should serve the right content type', function(done) { | ||
var response = status[key].apply(null, [new Reply(), '<h1>Test</h1>', {'content-type': 'text/html'}]); | ||
lab.test('should serve the right content type', (done) => { | ||
let response = status[key].apply(null, [new Reply(), '<h1>Test</h1>', {'content-type': 'text/html'}]); | ||
@@ -89,1 +89,45 @@ Code.expect(response.headers['content-type']).to.equal('text/html'); | ||
}); | ||
lab.experiment('add a new delegation', () => { | ||
lab.test('should be skipped if it\'s not a function', (done) => { | ||
let response; | ||
status.addDelegation('textHtml', 'some-text'); | ||
response = status.ok.apply(null, [new Reply(), 'Test', {'content-type': 'text/html'}]); | ||
Code.expect(response.result).to.equal('Test'); | ||
done(); | ||
}); | ||
lab.test('should be able to alter the response.result', (done) => { | ||
let response; | ||
status.addDelegation('textHtml', () => 'Hi!'); | ||
response = status.ok.apply(null, [new Reply(), 'Test', {'content-type': 'text/html'}]); | ||
Code.expect(response.result).to.equal('Hi!'); | ||
done(); | ||
}); | ||
lab.test('should be able to alter an existing delegate', (done) => { | ||
let response; | ||
response = status.ok.apply(null, [new Reply(), ['Test']]); | ||
Code.expect(response.result).to.be.an.object(); | ||
Code.expect(response.result.statusCode).to.be.a.number(); | ||
Code.expect(response.result.result).to.be.an.array(); | ||
status.addDelegation('applicationJson', (result) => result); | ||
response = status.ok.apply(null, [new Reply(), ['Test']]); | ||
Code.expect(response.result).to.be.an.array(); | ||
done(); | ||
}); | ||
}); |
16774
176