1 | /*!
|
---|
2 | * statuses
|
---|
3 | * Copyright(c) 2014 Jonathan Ong
|
---|
4 | * Copyright(c) 2016 Douglas Christopher Wilson
|
---|
5 | * MIT Licensed
|
---|
6 | */
|
---|
7 |
|
---|
8 | 'use strict'
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Module dependencies.
|
---|
12 | * @private
|
---|
13 | */
|
---|
14 |
|
---|
15 | var codes = require('./codes.json')
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Module exports.
|
---|
19 | * @public
|
---|
20 | */
|
---|
21 |
|
---|
22 | module.exports = status
|
---|
23 |
|
---|
24 | // status code to message map
|
---|
25 | status.STATUS_CODES = codes
|
---|
26 |
|
---|
27 | // array of status codes
|
---|
28 | status.codes = populateStatusesMap(status, codes)
|
---|
29 |
|
---|
30 | // status codes for redirects
|
---|
31 | status.redirect = {
|
---|
32 | 300: true,
|
---|
33 | 301: true,
|
---|
34 | 302: true,
|
---|
35 | 303: true,
|
---|
36 | 305: true,
|
---|
37 | 307: true,
|
---|
38 | 308: true
|
---|
39 | }
|
---|
40 |
|
---|
41 | // status codes for empty bodies
|
---|
42 | status.empty = {
|
---|
43 | 204: true,
|
---|
44 | 205: true,
|
---|
45 | 304: true
|
---|
46 | }
|
---|
47 |
|
---|
48 | // status codes for when you should retry the request
|
---|
49 | status.retry = {
|
---|
50 | 502: true,
|
---|
51 | 503: true,
|
---|
52 | 504: true
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Populate the statuses map for given codes.
|
---|
57 | * @private
|
---|
58 | */
|
---|
59 |
|
---|
60 | function populateStatusesMap (statuses, codes) {
|
---|
61 | var arr = []
|
---|
62 |
|
---|
63 | Object.keys(codes).forEach(function forEachCode (code) {
|
---|
64 | var message = codes[code]
|
---|
65 | var status = Number(code)
|
---|
66 |
|
---|
67 | // Populate properties
|
---|
68 | statuses[status] = message
|
---|
69 | statuses[message] = status
|
---|
70 | statuses[message.toLowerCase()] = status
|
---|
71 |
|
---|
72 | // Add to array
|
---|
73 | arr.push(status)
|
---|
74 | })
|
---|
75 |
|
---|
76 | return arr
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Get the status code.
|
---|
81 | *
|
---|
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
|
---|
86 | * the status message.
|
---|
87 | *
|
---|
88 | * @param {string|number} code
|
---|
89 | * @returns {number}
|
---|
90 | * @public
|
---|
91 | */
|
---|
92 |
|
---|
93 | function status (code) {
|
---|
94 | if (typeof code === 'number') {
|
---|
95 | if (!status[code]) throw new Error('invalid status code: ' + code)
|
---|
96 | return code
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (typeof code !== 'string') {
|
---|
100 | throw new TypeError('code must be a number or string')
|
---|
101 | }
|
---|
102 |
|
---|
103 | // '403'
|
---|
104 | var n = parseInt(code, 10)
|
---|
105 | if (!isNaN(n)) {
|
---|
106 | if (!status[n]) throw new Error('invalid status code: ' + n)
|
---|
107 | return n
|
---|
108 | }
|
---|
109 |
|
---|
110 | n = status[code.toLowerCase()]
|
---|
111 | if (!n) throw new Error('invalid status message: "' + code + '"')
|
---|
112 | return n
|
---|
113 | }
|
---|