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 // array of status codes
25 status.codes = populateStatusesMap(status, codes)
27 // status codes for redirects
38 // status codes for empty bodies
45 // status codes for when you should retry the request
53 * Populate the statuses map for given codes.
57 function populateStatusesMap (statuses, codes) {
60 Object.keys(codes).forEach(function forEachCode (code) {
61 var message = codes[code]
62 var status = Number(code)
64 // Populate properties
65 statuses[status] = message
66 statuses[message] = status
67 statuses[message.toLowerCase()] = status
77 * Get the status code.
79 * Given a number, this will throw if it is not a known status
80 * code, otherwise the code will be returned. Given a string,
81 * the string will be parsed for a number and return the code
82 * if valid, otherwise will lookup the code assuming this is
85 * @param {string|number} code
90 function status (code) {
91 if (typeof code === 'number') {
92 if (!status[code]) throw new Error('invalid status code: ' + code)
96 if (typeof code !== 'string') {
97 throw new TypeError('code must be a number or string')
101 var n = parseInt(code, 10)
103 if (!status[n]) throw new Error('invalid status code: ' + n)
107 n = status[code.toLowerCase()]
108 if (!n) throw new Error('invalid status message: "' + code + '"')