hapi-status
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -6,3 +6,8 @@ 'use strict'; | ||
HTTPStatusCode = require('http-status-code'), | ||
camelcase = require('camelcase'); | ||
camelcase = require('camelcase'), | ||
submerge = require('submerge'), | ||
delegate = {}, | ||
defaults = { | ||
'content-type': 'application/json' | ||
}; | ||
@@ -15,25 +20,39 @@ function init() { | ||
Object.defineProperty(status, name, { | ||
enumerable: true, | ||
value: function(reply, result, headers) { | ||
var body = { | ||
statusCode: +key | ||
}, | ||
response; | ||
status[name] = create(+key, definitions[key]); | ||
status[name].displayName = definitions[key]; | ||
}); | ||
} | ||
if (result) | ||
body[Math.floor(+key * 0.01) >= 4 ? 'error' : 'result'] = result; | ||
function applyHeaders(headers, response) { | ||
Object.keys(headers).forEach(function(header) { | ||
response.header(header, headers[header]); | ||
}); | ||
response = reply(body).code(+key).type('application/json'); | ||
return response; | ||
} | ||
if (headers) { | ||
Object.keys(headers).forEach(function(header) { | ||
response.header(header, headers[header]); | ||
}); | ||
} | ||
delegate.applicationJson = function applicationJson(result, code, message) { | ||
var body = { | ||
statusCode: code, | ||
result: result | ||
}; | ||
return response; | ||
} | ||
}); | ||
}); | ||
if (Math.floor(code * 0.01) >= 4) { | ||
body.error = message; | ||
delete body.result; | ||
} | ||
return body; | ||
}; | ||
function create(code, message) { | ||
return function(reply, result, headers) { | ||
var body, type; | ||
headers = submerge(headers || {}, defaults); | ||
type = camelcase(headers['content-type'].replace(/\//, ' ')); | ||
body = type in delegate ? delegate[type].apply(status, [result, code, message]) : result; | ||
return applyHeaders(headers, reply(body).code(code)); | ||
}; | ||
} | ||
@@ -40,0 +59,0 @@ |
{ | ||
"name": "hapi-status", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Easily add status code and headers to a response in Hapi", | ||
@@ -27,3 +27,4 @@ "main": "index.js", | ||
"camelcase": "^1.0.2", | ||
"http-status-code": "^1.0.3" | ||
"http-status-code": "^1.0.3", | ||
"submerge": "^1.0.2" | ||
}, | ||
@@ -30,0 +31,0 @@ "devDependencies": { |
@@ -14,4 +14,4 @@ 'use strict'; | ||
reply.code = function code(status) { | ||
reply.statusCode = status; | ||
reply.code = function code(statusCode) { | ||
reply.statusCode = statusCode; | ||
return reply; | ||
@@ -42,3 +42,3 @@ }; | ||
lab.test('should serve the right statusCode', function(done) { | ||
var response = status[key].apply(null, [new Reply(), 'test']); | ||
var response = status[key].apply(null, [new Reply()]); | ||
@@ -65,6 +65,7 @@ Code.expect(response.result.statusCode).to.equal(response.statusCode); | ||
lab.test('should return only statusCode when no result present', function(done) { | ||
var response = status[key].apply(null, [new Reply()]).result; | ||
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'}]); | ||
Code.expect(!('error' in response) && !('result' in response)).to.equal(true); | ||
Code.expect('Cache-Control' in response.headers).to.equal(true); | ||
Code.expect(response.headers['Cache-Control']).to.equal('max-age=0, no-cache, no-store'); | ||
@@ -74,7 +75,7 @@ done(); | ||
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 serve the right content type', function(done) { | ||
var response = status[key].apply(null, [new Reply(), '<h1>Test</h1>', {'content-type': 'text/html'}]); | ||
Code.expect('Cache-Control' in response.headers).to.equal(true); | ||
Code.expect(response.headers['Cache-Control']).to.equal('max-age=0, no-cache, no-store'); | ||
Code.expect(response.headers['content-type']).to.equal('text/html'); | ||
Code.expect(response.result).to.equal('<h1>Test</h1>'); | ||
@@ -81,0 +82,0 @@ done(); |
12991
107
3
+ Addedsubmerge@^1.0.2
+ Addedsubmerge@1.1.4(transitive)