update devDependencies, add jsdoc lint, fix lint issues
[squeep-api-dingus] / lib / enum.js
1 'use strict';
2
3 const ContentType = {
4 TextHTML: 'text/html',
5 TextPlain: 'text/plain',
6 ApplicationForm: 'application/x-www-form-urlencoded',
7 ApplicationJson: 'application/json',
8 };
9
10 // Supported encoding types
11 const EncodingType = {
12 Brotli: 'br',
13 Gzip: 'gzip',
14 XGzip: 'x-gzip',
15 Identity: 'identity',
16 };
17
18 // Filename suffixes for serving pre-encoded files
19 const EncodingTypeSuffix = {
20 [EncodingType.Brotli]: '.br',
21 [EncodingType.Gzip]: '.gz',
22 [EncodingType.XGzip]: '.gz',
23 };
24
25 const HTTPStatusCode = {
26
27 Continue: 100,
28 SwitchingProtocols: 101,
29 Processing: 102,
30 EarlyHints: 103,
31
32 OK: 200,
33 Created: 201,
34 Accepted: 202,
35 NonAuthoritativeInformation: 203,
36 NoContent: 204,
37 ResetContent: 205,
38 PartialContent: 206,
39 MultiStatus: 207,
40 AlreadyReported: 208,
41 IMUsed: 226,
42
43 MultipleChoices: 300,
44 MovedPermanently: 301,
45 Found: 302,
46 SeeOther: 303,
47 NotModified: 304,
48 UseProxy: 305, // Deprecated, do not use
49 SwitchProxy: 306, // No longer used
50 TemporaryRedirect: 307,
51 PermanentRedirect: 308,
52
53 BadRequest: 400,
54 Unauthorized: 401,
55 PaymentRequired: 402,
56 Forbidden: 403,
57 NotFound: 404,
58 MethodNotAllowed: 405,
59 NotAcceptable: 406,
60 ProxyAuthenticationRequired: 407,
61 RequestTimeout: 408,
62 Conflict: 409,
63 Gone: 410,
64 LengthRequired: 411,
65 PreconditionFailed: 412,
66 ContentTooLarge: 413,
67 RequestEntityTooLarge: 413, // Alternate name
68 URITooLong: 414,
69 UnsupportedMediaType: 415,
70 RangeNotSatisfiable: 416,
71 ExpectationFailed: 417,
72 ImATeapot: 418,
73 MisdirectedRequest: 421,
74 UnprocessableContent: 422,
75 Locked: 423,
76 FailedDependency: 424,
77 TooEarly: 425,
78 UpgradeRequired: 426,
79 PreconditionRequired: 428,
80 TooManyRequests: 429,
81 RequestHeaderFieldsTooLarge: 431,
82 UnavailableForLegalReasons: 451,
83
84 InternalServerError: 500,
85 NotImplemented: 501,
86 BadGateway: 502,
87 ServiceUnavailable: 503,
88 GatewayTimeout: 504,
89 HTTPVersionNotSupported: 505,
90 VariantAlsoNegotiates: 506,
91 InsufficientStorage: 507,
92 LoopDetected: 508,
93 NotExtended: 510,
94 NetworkAuthenticationRequired: 511,
95
96 };
97
98 const HTTPStatusMessage = {
99
100 100: 'Continue',
101 101: 'Switching Protocols',
102 102: 'Processing',
103 103: 'Early Hints',
104
105 200: 'OK',
106 201: 'Created',
107 202: 'Accepted',
108 203: 'Non-Authoritative Information',
109 204: 'No Content',
110 205: 'Reset Content',
111 206: 'Partial Content',
112 207: 'Multi-Status',
113 208: 'Already Reported',
114 226: 'IM USed',
115
116 300: 'Multiple Choices',
117 301: 'Moved Permanently',
118 302: 'Found',
119 303: 'See Other',
120 304: 'Not Modified',
121 305: 'Use Proxy',
122 306: 'Switch Proxy',
123 307: 'Temporary Redirect',
124 308: 'Permanent Redirect',
125
126 400: 'Bad Request',
127 401: 'Unauthorized',
128 402: 'Payment Required',
129 403: 'Forbidden',
130 '403.zalgo': 'F̦̩̫̼͔̫͓̃ͤ̈̆̀͑o̖̟͙̫̯̗̳̽ͦ̆́ͨr̩͉̰̗͉b̬̂͘į̟̬̓d͂͗҉̟͈̜͙ͅd͎̜̺̝͇͑̒̋̾ë̴̳̺͓̦̘́ͮ̈́ǹ͈̦̫̙',
131 404: 'Not Found',
132 405: 'Method Not Allowed',
133 406: 'Not Acceptable',
134 407: 'Proxy Authentication Required',
135 408: 'Request Timeout',
136 409: 'Conflict',
137 410: 'Gone',
138 411: 'Length Required',
139 412: 'Precondition Failed',
140 413: 'Content Too Large',
141 414: 'URI Too Long',
142 415: 'Unsupported Media Type',
143 416: 'Range Not Satisfiable',
144 417: 'Expectation Failed',
145 418: 'I\'m A Teapot',
146 421: 'Misdirected Request',
147 422: 'Unprocessable Content',
148 423: 'Locked',
149 424: 'Failed Dependency',
150 425: 'Too Early',
151 426: 'Upgrade Required',
152 428: 'Precondition Required',
153 429: 'Too Many Requests',
154 431: 'Request Header Fields Too Large',
155 451: 'Unavailable For Legal Reasons',
156
157 500: 'Internal Server Error',
158 501: 'Not Implemented',
159 502: 'Bad Gateway',
160 503: 'Service Unavailable',
161 504: 'Gateway Timeout',
162 505: 'HTTP Version Not Supported',
163 506: 'Variant Also Negotiates',
164 507: 'Insufficient Storage',
165 508: 'Loop Detected',
166 510: 'Not Extended',
167 511: 'Network Authentication Required',
168
169 };
170
171 /**
172 * Build an object mapping known http status identifiers to their code and descriptive string.
173 * @returns {object} status mappings
174 */
175 function errorResponseGenerator () {
176 return Object.fromEntries(
177 Object.entries(HTTPStatusCode).map(([name, statusCode]) => {
178 const errorMessage = HTTPStatusMessage[statusCode]; // eslint-disable-line security/detect-object-injection
179 // istanbul ignore next
180 if (!errorMessage) {
181 throw new Error(`missing HTTPStatusMessage for ${statusCode} from HTTPStatusCode ${name}`);
182 }
183 return [name, { statusCode, errorMessage }];
184 }));
185 }
186
187 const ErrorResponse = errorResponseGenerator();
188
189 const ErrorResponseProxy = new Proxy(ErrorResponse, {
190 get: (target, property) => {
191 if (property in target) {
192 return target[property]; // eslint-disable-line security/detect-object-injection
193 } else {
194 const statusCode = HTTPStatusCode.InternalServerError;
195 const errorMessage = HTTPStatusMessage[statusCode]; // eslint-disable-line security/detect-object-injection
196 const details = [`undefined error response '${property}'`];
197 return { statusCode, errorMessage, details };
198 }
199 },
200 });
201
202 const Header = {
203 Accept: 'Accept',
204 AcceptEncoding: 'Accept-Encoding',
205 CacheControl: 'Cache-Control',
206 ContentEncoding: 'Content-Encoding',
207 ContentLength: 'Content-Length',
208 ContentType: 'Content-Type',
209 Cookie: 'Cookie',
210 ETag: 'ETag',
211 IfModifiedSince: 'If-Modified-Since',
212 IfNoneMatch: 'If-None-Match',
213 LastModified: 'Last-Modified',
214 Location: 'Location',
215 RequestId: 'Request-ID',
216 SetCookie: 'Set-Cookie',
217 Vary: 'Vary',
218 XCorrelationId: 'X-Correlation-ID',
219 XForwardedFor: 'X-Forwarded-For',
220 XForwardedProto: 'X-Forwarded-Proto',
221 XRealIP: 'X-Real-IP',
222 XRequestId: 'X-Request-ID',
223 };
224
225 module.exports = {
226 ContentType,
227 EncodingType,
228 EncodingTypeSuffix,
229 ErrorResponse: ErrorResponseProxy,
230 Header,
231 HTTPStatusCode,
232 HTTPStatusMessage,
233 };