3 * Copyright(c) 2014 Jonathan Ong
4 * Copyright(c) 2016 Douglas Christopher Wilson
11 * Module dependencies.
15 var codes = require('./codes.json')
22 module.exports = status
24 // status code to message map
25 status.STATUS_CODES = codes
27 // array of status codes
28 status.codes = populateStatusesMap(status, codes)
30 // status codes for redirects
41 // status codes for empty bodies
48 // status codes for when you should retry the request
56 * Populate the statuses map for given codes.
60 function populateStatusesMap (statuses, codes) {
63 Object.keys(codes).forEach(function forEachCode (code) {
64 var message = codes[code]
65 var status = Number(code)
67 // Populate properties
68 statuses[status] = message
69 statuses[message] = status
70 statuses[message.toLowerCase()] = status
80 * Get the status code.
82 * Given a number, this will throw if it is not a known status
83 * code, otherwise the code will be returned. Given a string,
84 * the string will be parsed for a number and return the code
85 * if valid, otherwise will lookup the code assuming this is
88 * @param {string|number} code
93 function status (code) {
94 if (typeof code === 'number') {
95 if (!status[code]) throw new Error('invalid status code: ' + code)
99 if (typeof code !== 'string') {
100 throw new TypeError('code must be a number or string')
104 var n = parseInt(code, 10)
106 if (!status[n]) throw new Error('invalid status code: ' + n)
110 n = status[code.toLowerCase()]
111 if (!n) throw new Error('invalid status message: "' + code + '"')