├── LICENSE ├── README.md ├── codes.ts ├── maps.ts ├── mod.ts ├── status.ts └── status_test.ts /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020-Present Filippo Rossi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # status 2 | 3 | [![Open Issues](https://img.shields.io/github/issues/denosaurs/status)](https://github.com/denosaurs/status) 4 | [![GitHub license](https://img.shields.io/github/license/denosaurs/status)](https://github.com/denosaurs/denom/blob/master/LICENSE) 5 | [![Deno Version](https://img.shields.io/badge/deno-1.0.0-informational)](https://deno.land) 6 | [![Deno Doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/status/mod.ts) 7 | 8 | HTTP codes and status utility for Deno. Based on 9 | [Java Apache HttpStatus](http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpStatus.html) 10 | 11 | ## API 12 | 13 | ### status(code) and status.pretty(code) 14 | 15 | ```typescript 16 | import { status } from "https://deno.land/x/status/mod.ts"; 17 | 18 | status(403); // => "FORBIDDEN" 19 | status("403"); // => "FORBIDDEN" 20 | status.pretty(403); // => "Forbidden" 21 | status(306); // throws 22 | ``` 23 | 24 | ### status(message) 25 | 26 | ```typescript 27 | import { status } from "https://deno.land/x/status/mod.ts"; 28 | 29 | status("forbidden"); // => 403 30 | status("FoRbIdDeN"); // => 403 31 | status("foo"); // throws 32 | ``` 33 | 34 | ### status.codes 35 | 36 | Array of all the possible status codes. 37 | 38 | ```typescript 39 | import { status } from "https://deno.land/x/status/mod.ts"; 40 | 41 | status.codes; // => [202, 502, 400, ...] 42 | ``` 43 | 44 | ### status.code[code] 45 | 46 | Map of all the available codes. `message (string) -> code (number)` 47 | 48 | ```typescript 49 | import { status } from "https://deno.land/x/status/mod.ts"; 50 | 51 | status.code; // => { "ACCEPTED": 202, "BAD_GATEWAY": 502, "BAD_REQUEST": 400, ... } 52 | status.code["FORBIDDEN"] = 403; 53 | ``` 54 | 55 | ### status.message[msg] 56 | 57 | Map of all the available codes. `code (number) -> message (string)` 58 | 59 | ```typescript 60 | import { status } from "https://deno.land/x/status/mod.ts"; 61 | 62 | status.message; // => { 202: "ACCEPTED", 502: "BAD_GATEWAY, 400: "BAD_REQUEST", ... } 63 | status.message[403] = "FORBIDDEN"; 64 | ``` 65 | 66 | ### status.empty[code] 67 | 68 | Returns `true` if a status code expects an empty body. 69 | 70 | ```typescript 71 | import { status } from "https://deno.land/x/status/mod.ts"; 72 | 73 | status.empty[200]; // => undefined 74 | status.empty[204]; // => true 75 | ``` 76 | 77 | ### status.redirect[code] 78 | 79 | Returns `true` if a status code is a valid redirect status. 80 | 81 | ```typescript 82 | import { status } from "https://deno.land/x/status/mod.ts"; 83 | 84 | status.redirect[200]; // => undefined 85 | status.redirect[301]; // => true 86 | ``` 87 | 88 | ### status.retry[code] 89 | 90 | Returns `true` if a status code hints that the request might be retried. 91 | 92 | ```typescript 93 | import { status } from "https://deno.land/x/status/mod.ts"; 94 | 95 | status.retry[501]; // => undefined 96 | status.retry[503]; // => true 97 | ``` 98 | 99 | ## other 100 | 101 | ### contribution 102 | 103 | Pull request and issues are very welcome. Code style is formatted with 104 | `deno fmt`. 105 | 106 | ### inspiration 107 | 108 | The project is inspired by the [statuses](https://github.com/jshttp/statuses) 109 | project. 110 | -------------------------------------------------------------------------------- /codes.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Filippo Rossi. All rights reserved. MIT license. 2 | 3 | /** 4 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.3 5 | * 6 | * The request has been received but not yet acted upon. It is non-committal, meaning that there is no way in HTTP to later send an asynchronous response indicating the outcome of processing the request. It is intended for cases where another process or server handles the request, or for batch processing. 7 | */ 8 | export const ACCEPTED = 202; 9 | 10 | /** 11 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.3 12 | * 13 | * This error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response. 14 | */ 15 | export const BAD_GATEWAY = 502; 16 | 17 | /** 18 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.1 19 | * 20 | * This response means that server could not understand the request due to invalid syntax. 21 | */ 22 | export const BAD_REQUEST = 400; 23 | 24 | /** 25 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.8 26 | * 27 | * This response is sent when a request conflicts with the current state of the server. 28 | */ 29 | export const CONFLICT = 409; 30 | 31 | /** 32 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.2.1 33 | * 34 | * This interim response indicates that everything so far is OK and that the client should continue with the request or ignore it if it is already finished. 35 | */ 36 | export const CONTINUE = 100; 37 | 38 | /** 39 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.2 40 | * 41 | * The request has succeeded and a new resource has been created as a result of it. This is typically the response sent after a PUT request. 42 | */ 43 | export const CREATED = 201; 44 | 45 | /** 46 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.14 47 | * 48 | * This response code means the expectation indicated by the Expect request header field can't be met by the server. 49 | */ 50 | export const EXPECTATION_FAILED = 417; 51 | 52 | /** 53 | * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.5 54 | * 55 | * The request failed due to failure of a previous request. 56 | */ 57 | export const FAILED_DEPENDENCY = 424; 58 | 59 | /** 60 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.3 61 | * 62 | * The client does not have access rights to the content, i.e. they are unauthorized, so server is rejecting to give proper response. Unlike 401, the client's identity is known to the server. 63 | */ 64 | export const FORBIDDEN = 403; 65 | 66 | /** 67 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.5 68 | * 69 | * This error response is given when the server is acting as a gateway and cannot get a response in time. 70 | */ 71 | export const GATEWAY_TIMEOUT = 504; 72 | 73 | /** 74 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.9 75 | * 76 | * This response would be sent when the requested content has been permenantly deleted from server, with no forwarding address. Clients are expected to remove their caches and links to the resource. The HTTP specification intends this status code to be used for "limited-time, promotional services". APIs should not feel compelled to indicate resources that have been deleted with this status code. 77 | */ 78 | export const GONE = 410; 79 | 80 | /** 81 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.6 82 | * 83 | * The HTTP version used in the request is not supported by the server. 84 | */ 85 | export const HTTP_VERSION_NOT_SUPPORTED = 505; 86 | 87 | /** 88 | * Official Documentation @ https://tools.ietf.org/html/rfc2324#section-2.3.2 89 | * 90 | * Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout. 91 | */ 92 | export const IM_A_TEAPOT = 418; 93 | 94 | /** 95 | * UNOFFICIAL w/ NO DOCS 96 | */ 97 | export const INSUFFICIENT_SPACE_ON_RESOURCE = 419; 98 | 99 | /** 100 | * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.6 101 | * 102 | * The server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself, and is therefore not a proper end point in the negotiation process. 103 | */ 104 | export const INSUFFICIENT_STORAGE = 507; 105 | 106 | /** 107 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.1 108 | * 109 | * The server has encountered a situation it doesn't know how to handle. 110 | */ 111 | export const INTERNAL_SERVER_ERROR = 500; 112 | 113 | /** 114 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.10 115 | * 116 | * Server rejected the request because the Content-Length header field is not defined and the server requires it. 117 | */ 118 | export const LENGTH_REQUIRED = 411; 119 | 120 | /** 121 | * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.4 122 | * 123 | * The resource that is being accessed is locked. 124 | */ 125 | export const LOCKED = 423; 126 | 127 | /** 128 | * @deprecated 129 | * A deprecated response used by the Spring Framework when a method has failed. 130 | */ 131 | export const METHOD_FAILURE = 420; 132 | 133 | /** 134 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.5 135 | * 136 | * The request method is known by the server but has been disabled and cannot be used. For example, an API may forbid DELETE-ing a resource. The two mandatory methods, GET and HEAD, must never be disabled and should not return this error code. 137 | */ 138 | export const METHOD_NOT_ALLOWED = 405; 139 | 140 | /** 141 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.2 142 | * 143 | * This response code means that URI of requested resource has been changed. Probably, new URI would be given in the response. 144 | */ 145 | export const MOVED_PERMANENTLY = 301; 146 | 147 | /** 148 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.3 149 | * 150 | * This response code means that URI of requested resource has been changed temporarily. New changes in the URI might be made in the future. Therefore, this same URI should be used by the client in future requests. 151 | */ 152 | export const MOVED_TEMPORARILY = 302; 153 | 154 | /** 155 | * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.2 156 | * 157 | * A Multi-Status response conveys information about multiple resources in situations where multiple status codes might be appropriate. 158 | */ 159 | export const MULTI_STATUS = 207; 160 | 161 | /** 162 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.1 163 | * 164 | * The request has more than one possible responses. User-agent or user should choose one of them. There is no standardized way to choose one of the responses. 165 | */ 166 | export const MULTIPLE_CHOICES = 300; 167 | 168 | /** 169 | * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-6 170 | * 171 | * The 511 status code indicates that the client needs to authenticate to gain network access. 172 | */ 173 | export const NETWORK_AUTHENTICATION_REQUIRED = 511; 174 | 175 | /** 176 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.5 177 | * 178 | * There is no content to send for this request, but the headers may be useful. The user-agent may update its cached headers for this resource with the new ones. 179 | */ 180 | export const NO_CONTENT = 204; 181 | 182 | /** 183 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.4 184 | * This response code means returned meta-information set is not exact set as available from the origin server, but collected from a local or a third party copy. Except this condition, 200 OK response should be preferred instead of this response. 185 | */ 186 | export const NON_AUTHORITATIVE_INFORMATION = 203; 187 | 188 | /** 189 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.6 190 | * 191 | * This response is sent when the web server, after performing server-driven content negotiation, doesn't find any content following the criteria given by the user agent. 192 | */ 193 | export const NOT_ACCEPTABLE = 406; 194 | 195 | /** 196 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.4 197 | * 198 | * The server can not find requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 to hide the existence of a resource from an unauthorized client. This response code is probably the most famous one due to its frequent occurence on the web. 199 | */ 200 | export const NOT_FOUND = 404; 201 | 202 | /** 203 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.2 204 | * 205 | * The request method is not supported by the server and cannot be handled. The only methods that servers are required to support (and therefore that must not return this code) are GET and HEAD. 206 | */ 207 | export const NOT_IMPLEMENTED = 501; 208 | 209 | /** 210 | * Official Documentation @ https://tools.ietf.org/html/rfc7232#section-4.1 211 | * 212 | * This is used for caching purposes. It is telling to client that response has not been modified. So, client can continue to use same cached version of response. 213 | */ 214 | export const NOT_MODIFIED = 304; 215 | 216 | /** 217 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.1 218 | * 219 | * The request has succeeded. The meaning of a success varies depending on the HTTP method: 220 | * GET: The resource has been fetched and is transmitted in the message body. 221 | * HEAD: The entity headers are in the message body. 222 | * POST: The resource describing the result of the action is transmitted in the message body. 223 | * TRACE: The message body contains the request message as received by the server 224 | */ 225 | export const OK = 200; 226 | 227 | /** 228 | * Official Documentation @ https://tools.ietf.org/html/rfc7233#section-4.1 229 | * 230 | * This response code is used because of range header sent by the client to separate download into multiple streams. 231 | */ 232 | export const PARTIAL_CONTENT = 206; 233 | 234 | /** 235 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.2 236 | * 237 | * This response code is reserved for future use. Initial aim for creating this code was using it for digital payment systems however this is not used currently. 238 | */ 239 | export const PAYMENT_REQUIRED = 402; 240 | 241 | /** 242 | * Official Documentation @ https://tools.ietf.org/html/rfc7538#section-3 243 | * 244 | * This means that the resource is now permanently located at another URI, specified by the Location: HTTP Response header. This has the same semantics as the 301 Moved Permanently HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request. 245 | */ 246 | export const PERMANENT_REDIRECT = 308; 247 | 248 | /** 249 | * Official Documentation @ https://tools.ietf.org/html/rfc7232#section-4.2 250 | * 251 | * The client has indicated preconditions in its headers which the server does not meet. 252 | */ 253 | export const PRECONDITION_FAILED = 412; 254 | 255 | /** 256 | * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-3 257 | * 258 | * The origin server requires the request to be conditional. Intended to prevent the 'lost update' problem, where a client GETs a resource's state, modifies it, and PUTs it back to the server, when meanwhile a third party has modified the state on the server, leading to a conflict. 259 | */ 260 | export const PRECONDITION_REQUIRED = 428; 261 | 262 | /** 263 | * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.1 264 | * 265 | * This code indicates that the server has received and is processing the request, but no response is available yet. 266 | */ 267 | export const PROCESSING = 102; 268 | 269 | /** 270 | * Official Documentation @ https://tools.ietf.org/html/rfc7235#section-3.2 271 | * 272 | * This is similar to 401 but authentication is needed to be done by a proxy. 273 | */ 274 | export const PROXY_AUTHENTICATION_REQUIRED = 407; 275 | 276 | /** 277 | * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-5 278 | * 279 | * The server is unwilling to process the request because its header fields are too large. The request MAY be resubmitted after reducing the size of the request header fields. 280 | */ 281 | export const REQUEST_HEADER_FIELDS_TOO_LARGE = 431; 282 | 283 | /** 284 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.7 285 | * 286 | * This response is sent on an idle connection by some servers, even without any previous request by the client. It means that the server would like to shut down this unused connection. This response is used much more since some browsers, like Chrome, Firefox 27+, or IE9, use HTTP pre-connection mechanisms to speed up surfing. Also note that some servers merely shut down the connection without sending this message. 287 | */ 288 | export const REQUEST_TIMEOUT = 408; 289 | 290 | /** 291 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.11 292 | * 293 | * Request entity is larger than limits defined by server; the server might close the connection or return an Retry-After header field. 294 | */ 295 | export const REQUEST_TOO_LONG = 413; 296 | 297 | /** 298 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.12 299 | * 300 | * The URI requested by the client is longer than the server is willing to interpret. 301 | */ 302 | export const REQUEST_URI_TOO_LONG = 414; 303 | 304 | /** 305 | * Official Documentation @ https://tools.ietf.org/html/rfc7233#section-4.4 306 | * 307 | * The range specified by the Range header field in the request can't be fulfilled; it's possible that the range is outside the size of the target URI's data. 308 | */ 309 | export const REQUESTED_RANGE_NOT_SATISFIABLE = 416; 310 | 311 | /** 312 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.3.6 313 | * 314 | * This response code is sent after accomplishing request to tell user agent reset document view which sent this request. 315 | */ 316 | export const RESET_CONTENT = 205; 317 | 318 | /** 319 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.4 320 | * 321 | * Server sent this response to directing client to get requested resource to another URI with an GET request. 322 | */ 323 | export const SEE_OTHER = 303; 324 | 325 | /** 326 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.6.4 327 | * 328 | * The server is not ready to handle the request. Common causes are a server that is down for maintenance or that is overloaded. Note that together with this response, a user-friendly page explaining the problem should be sent. This responses should be used for temporary conditions and the Retry-After: HTTP header should, if possible, contain the estimated time before the recovery of the service. The webmaster must also take care about the caching-related headers that are sent along with this response, as these temporary condition responses should usually not be cached. 329 | */ 330 | export const SERVICE_UNAVAILABLE = 503; 331 | 332 | /** 333 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.2.2 334 | * 335 | * This code is sent in response to an Upgrade request header by the client, and indicates the protocol the server is switching too. 336 | */ 337 | export const SWITCHING_PROTOCOLS = 101; 338 | 339 | /** 340 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.7 341 | * 342 | * Server sent this response to directing client to get requested resource to another URI with same method that used prior request. This has the same semantic than the 302 Found HTTP response code, with the exception that the user agent must not change the HTTP method used: if a POST was used in the first request, a POST must be used in the second request. 343 | */ 344 | export const TEMPORARY_REDIRECT = 307; 345 | 346 | /** 347 | * Official Documentation @ https://tools.ietf.org/html/rfc6585#section-4 348 | * 349 | * The user has sent too many requests in a given amount of time ("rate limiting"). 350 | */ 351 | export const TOO_MANY_REQUESTS = 429; 352 | 353 | /** 354 | * Official Documentation @ https://tools.ietf.org/html/rfc7235#section-3.1 355 | * 356 | * Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. 357 | */ 358 | export const UNAUTHORIZED = 401; 359 | 360 | /** 361 | * Official Documentation @ https://tools.ietf.org/html/rfc2518#section-10.3 362 | * 363 | * The request was well-formed but was unable to be followed due to semantic errors. 364 | */ 365 | export const UNPROCESSABLE_ENTITY = 422; 366 | 367 | /** 368 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.5.13 369 | * 370 | * The media format of the requested data is not supported by the server, so the server is rejecting the request. 371 | */ 372 | export const UNSUPPORTED_MEDIA_TYPE = 415; 373 | 374 | /** 375 | * @deprecated 376 | * Official Documentation @ https://tools.ietf.org/html/rfc7231#section-6.4.6 377 | * 378 | * Was defined in a previous version of the HTTP specification to indicate that a requested response must be accessed by a proxy. It has been deprecated due to security concerns regarding in-band configuration of a proxy. 379 | */ 380 | export const USE_PROXY = 305; 381 | 382 | /** 383 | * Array with all the supported status codes. 384 | */ 385 | export const ALL: number[] = [ 386 | ACCEPTED, 387 | BAD_GATEWAY, 388 | BAD_REQUEST, 389 | CONFLICT, 390 | CONTINUE, 391 | CREATED, 392 | EXPECTATION_FAILED, 393 | FAILED_DEPENDENCY, 394 | FORBIDDEN, 395 | GATEWAY_TIMEOUT, 396 | GONE, 397 | HTTP_VERSION_NOT_SUPPORTED, 398 | IM_A_TEAPOT, 399 | INSUFFICIENT_SPACE_ON_RESOURCE, 400 | INSUFFICIENT_STORAGE, 401 | INTERNAL_SERVER_ERROR, 402 | LENGTH_REQUIRED, 403 | LOCKED, 404 | METHOD_FAILURE, 405 | METHOD_NOT_ALLOWED, 406 | MOVED_PERMANENTLY, 407 | MOVED_TEMPORARILY, 408 | MULTI_STATUS, 409 | MULTIPLE_CHOICES, 410 | NETWORK_AUTHENTICATION_REQUIRED, 411 | NO_CONTENT, 412 | NON_AUTHORITATIVE_INFORMATION, 413 | NOT_ACCEPTABLE, 414 | NOT_FOUND, 415 | NOT_IMPLEMENTED, 416 | NOT_MODIFIED, 417 | OK, 418 | PARTIAL_CONTENT, 419 | PAYMENT_REQUIRED, 420 | PERMANENT_REDIRECT, 421 | PRECONDITION_FAILED, 422 | PRECONDITION_REQUIRED, 423 | PROCESSING, 424 | PROXY_AUTHENTICATION_REQUIRED, 425 | REQUEST_HEADER_FIELDS_TOO_LARGE, 426 | REQUEST_TIMEOUT, 427 | REQUEST_TOO_LONG, 428 | REQUEST_URI_TOO_LONG, 429 | REQUESTED_RANGE_NOT_SATISFIABLE, 430 | RESET_CONTENT, 431 | SEE_OTHER, 432 | SERVICE_UNAVAILABLE, 433 | SWITCHING_PROTOCOLS, 434 | TEMPORARY_REDIRECT, 435 | TOO_MANY_REQUESTS, 436 | UNAUTHORIZED, 437 | UNPROCESSABLE_ENTITY, 438 | UNSUPPORTED_MEDIA_TYPE, 439 | USE_PROXY, 440 | ]; 441 | -------------------------------------------------------------------------------- /maps.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Filippo Rossi. All rights reserved. MIT license. 2 | 3 | import * as codes from "./codes.ts"; 4 | 5 | /** 6 | * Mapping all status codes to status phrases. 7 | */ 8 | export const STATUS_MESSAGES: { [key: number]: string } = { 9 | [codes.ACCEPTED]: "ACCEPTED", 10 | [codes.BAD_GATEWAY]: "BAD_GATEWAY", 11 | [codes.BAD_REQUEST]: "BAD_REQUEST", 12 | [codes.CONFLICT]: "CONFLICT", 13 | [codes.CONTINUE]: "CONTINUE", 14 | [codes.CREATED]: "CREATED", 15 | [codes.EXPECTATION_FAILED]: "EXPECTATION_FAILED", 16 | [codes.FAILED_DEPENDENCY]: "FAILED_DEPENDENCY", 17 | [codes.FORBIDDEN]: "FORBIDDEN", 18 | [codes.GATEWAY_TIMEOUT]: "GATEWAY_TIMEOUT", 19 | [codes.GONE]: "GONE", 20 | [codes.HTTP_VERSION_NOT_SUPPORTED]: "HTTP_VERSION_NOT_SUPPORTED", 21 | [codes.IM_A_TEAPOT]: "IM_A_TEAPOT", 22 | [codes.INSUFFICIENT_SPACE_ON_RESOURCE]: "INSUFFICIENT_SPACE_ON_RESOURCE", 23 | [codes.INSUFFICIENT_STORAGE]: "INSUFFICIENT_STORAGE", 24 | [codes.INTERNAL_SERVER_ERROR]: "INTERNAL_SERVER_ERROR", 25 | [codes.LENGTH_REQUIRED]: "LENGTH_REQUIRED", 26 | [codes.LOCKED]: "LOCKED", 27 | [codes.METHOD_FAILURE]: "METHOD_FAILURE", 28 | [codes.METHOD_NOT_ALLOWED]: "METHOD_NOT_ALLOWED", 29 | [codes.MOVED_PERMANENTLY]: "MOVED_PERMANENTLY", 30 | [codes.MOVED_TEMPORARILY]: "MOVED_TEMPORARILY", 31 | [codes.MULTI_STATUS]: "MULTI_STATUS", 32 | [codes.MULTIPLE_CHOICES]: "MULTIPLE_CHOICES", 33 | [codes.NETWORK_AUTHENTICATION_REQUIRED]: "NETWORK_AUTHENTICATION_REQUIRED", 34 | [codes.NO_CONTENT]: "NO_CONTENT", 35 | [codes.NON_AUTHORITATIVE_INFORMATION]: "NON_AUTHORITATIVE_INFORMATION", 36 | [codes.NOT_ACCEPTABLE]: "NOT_ACCEPTABLE", 37 | [codes.NOT_FOUND]: "NOT_FOUND", 38 | [codes.NOT_IMPLEMENTED]: "NOT_IMPLEMENTED", 39 | [codes.NOT_MODIFIED]: "NOT_MODIFIED", 40 | [codes.OK]: "OK", 41 | [codes.PARTIAL_CONTENT]: "PARTIAL_CONTENT", 42 | [codes.PAYMENT_REQUIRED]: "PAYMENT_REQUIRED", 43 | [codes.PERMANENT_REDIRECT]: "PERMANENT_REDIRECT", 44 | [codes.PRECONDITION_FAILED]: "PRECONDITION_FAILED", 45 | [codes.PRECONDITION_REQUIRED]: "PRECONDITION_REQUIRED", 46 | [codes.PROCESSING]: "PROCESSING", 47 | [codes.PROXY_AUTHENTICATION_REQUIRED]: "PROXY_AUTHENTICATION_REQUIRED", 48 | [codes.REQUEST_HEADER_FIELDS_TOO_LARGE]: "REQUEST_HEADER_FIELDS_TOO_LARGE", 49 | [codes.REQUEST_TIMEOUT]: "REQUEST_TIMEOUT", 50 | [codes.REQUEST_TOO_LONG]: "REQUEST_TOO_LONG", 51 | [codes.REQUEST_URI_TOO_LONG]: "REQUEST_URI_TOO_LONG", 52 | [codes.REQUESTED_RANGE_NOT_SATISFIABLE]: "REQUESTED_RANGE_NOT_SATISFIABLE", 53 | [codes.RESET_CONTENT]: "RESET_CONTENT", 54 | [codes.SEE_OTHER]: "SEE_OTHER", 55 | [codes.SERVICE_UNAVAILABLE]: "SERVICE_UNAVAILABLE", 56 | [codes.SWITCHING_PROTOCOLS]: "SWITCHING_PROTOCOLS", 57 | [codes.TEMPORARY_REDIRECT]: "TEMPORARY_REDIRECT", 58 | [codes.TOO_MANY_REQUESTS]: "TOO_MANY_REQUESTS", 59 | [codes.UNAUTHORIZED]: "UNAUTHORIZED", 60 | [codes.UNPROCESSABLE_ENTITY]: "UNPROCESSABLE_ENTITY", 61 | [codes.UNSUPPORTED_MEDIA_TYPE]: "UNSUPPORTED_MEDIA_TYPE", 62 | [codes.USE_PROXY]: "USE_PROXY", 63 | }; 64 | 65 | /** 66 | * Mapping all status phrases to status codes. 67 | */ 68 | export const STATUS_CODES: { [key: string]: number } = { 69 | "ACCEPTED": codes.ACCEPTED, 70 | "BAD_GATEWAY": codes.BAD_GATEWAY, 71 | "BAD_REQUEST": codes.BAD_REQUEST, 72 | "CONFLICT": codes.CONFLICT, 73 | "CONTINUE": codes.CONTINUE, 74 | "CREATED": codes.CREATED, 75 | "EXPECTATION_FAILED": codes.EXPECTATION_FAILED, 76 | "FAILED_DEPENDENCY": codes.FAILED_DEPENDENCY, 77 | "FORBIDDEN": codes.FORBIDDEN, 78 | "GATEWAY_TIMEOUT": codes.GATEWAY_TIMEOUT, 79 | "GONE": codes.GONE, 80 | "HTTP_VERSION_NOT_SUPPORTED": codes.HTTP_VERSION_NOT_SUPPORTED, 81 | "IM_A_TEAPOT": codes.IM_A_TEAPOT, 82 | "INSUFFICIENT_SPACE_ON_RESOURCE": codes.INSUFFICIENT_SPACE_ON_RESOURCE, 83 | "INSUFFICIENT_STORAGE": codes.INSUFFICIENT_STORAGE, 84 | "INTERNAL_SERVER_ERROR": codes.INTERNAL_SERVER_ERROR, 85 | "LENGTH_REQUIRED": codes.LENGTH_REQUIRED, 86 | "LOCKED": codes.LOCKED, 87 | "METHOD_FAILURE": codes.METHOD_FAILURE, 88 | "METHOD_NOT_ALLOWED": codes.METHOD_NOT_ALLOWED, 89 | "MOVED_PERMANENTLY": codes.MOVED_PERMANENTLY, 90 | "MOVED_TEMPORARILY": codes.MOVED_TEMPORARILY, 91 | "MULTI_STATUS": codes.MULTI_STATUS, 92 | "MULTIPLE_CHOICES": codes.MULTIPLE_CHOICES, 93 | "NETWORK_AUTHENTICATION_REQUIRED": codes.NETWORK_AUTHENTICATION_REQUIRED, 94 | "NO_CONTENT": codes.NO_CONTENT, 95 | "NON_AUTHORITATIVE_INFORMATION": codes.NON_AUTHORITATIVE_INFORMATION, 96 | "NOT_ACCEPTABLE": codes.NOT_ACCEPTABLE, 97 | "NOT_FOUND": codes.NOT_FOUND, 98 | "NOT_IMPLEMENTED": codes.NOT_IMPLEMENTED, 99 | "NOT_MODIFIED": codes.NOT_MODIFIED, 100 | "OK": codes.OK, 101 | "PARTIAL_CONTENT": codes.PARTIAL_CONTENT, 102 | "PAYMENT_REQUIRED": codes.PAYMENT_REQUIRED, 103 | "PERMANENT_REDIRECT": codes.PERMANENT_REDIRECT, 104 | "PRECONDITION_FAILED": codes.PRECONDITION_FAILED, 105 | "PRECONDITION_REQUIRED": codes.PRECONDITION_REQUIRED, 106 | "PROCESSING": codes.PROCESSING, 107 | "PROXY_AUTHENTICATION_REQUIRED": codes.PROXY_AUTHENTICATION_REQUIRED, 108 | "REQUEST_HEADER_FIELDS_TOO_LARGE": codes.REQUEST_HEADER_FIELDS_TOO_LARGE, 109 | "REQUEST_TIMEOUT": codes.REQUEST_TIMEOUT, 110 | "REQUEST_TOO_LONG": codes.REQUEST_TOO_LONG, 111 | "REQUEST_URI_TOO_LONG": codes.REQUEST_URI_TOO_LONG, 112 | "REQUESTED_RANGE_NOT_SATISFIABLE": codes.REQUESTED_RANGE_NOT_SATISFIABLE, 113 | "RESET_CONTENT": codes.RESET_CONTENT, 114 | "SEE_OTHER": codes.SEE_OTHER, 115 | "SERVICE_UNAVAILABLE": codes.SERVICE_UNAVAILABLE, 116 | "SWITCHING_PROTOCOLS": codes.SWITCHING_PROTOCOLS, 117 | "TEMPORARY_REDIRECT": codes.TEMPORARY_REDIRECT, 118 | "TOO_MANY_REQUESTS": codes.TOO_MANY_REQUESTS, 119 | "UNAUTHORIZED": codes.UNAUTHORIZED, 120 | "UNPROCESSABLE_ENTITY": codes.UNPROCESSABLE_ENTITY, 121 | "UNSUPPORTED_MEDIA_TYPE": codes.UNSUPPORTED_MEDIA_TYPE, 122 | "USE_PROXY": codes.USE_PROXY, 123 | }; 124 | -------------------------------------------------------------------------------- /mod.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Filippo Rossi. All rights reserved. MIT license. 2 | 3 | export type { Status } from "./status.ts"; 4 | export { status } from "./status.ts"; 5 | export * from "./codes.ts"; 6 | -------------------------------------------------------------------------------- /status.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Filippo Rossi. All rights reserved. MIT license. 2 | 3 | import * as maps from "./maps.ts"; 4 | import * as codes from "./codes.ts"; 5 | 6 | /** 7 | * Types. 8 | * @private 9 | */ 10 | export type Status = 11 | | string 12 | | number; 13 | 14 | /** 15 | * Module exports. 16 | * @public 17 | */ 18 | 19 | /** 20 | * Status code array 21 | */ 22 | status.codes = codes.ALL; 23 | 24 | /** 25 | * Status code to message map 26 | */ 27 | status.message = maps.STATUS_MESSAGES; 28 | 29 | /** 30 | * Status message (UPPER_CASE) to code map 31 | */ 32 | status.code = maps.STATUS_CODES; 33 | 34 | /** 35 | * Status codes for redirects. 36 | */ 37 | status.redirect = { 38 | [codes.MULTIPLE_CHOICES]: true, 39 | [codes.MOVED_PERMANENTLY]: true, 40 | [codes.MOVED_TEMPORARILY]: true, 41 | [codes.SEE_OTHER]: true, 42 | [codes.USE_PROXY]: true, 43 | [codes.TEMPORARY_REDIRECT]: true, 44 | [codes.PERMANENT_REDIRECT]: true, 45 | }; 46 | 47 | /** 48 | * Status codes for empty bodies. 49 | */ 50 | status.empty = { 51 | [codes.NO_CONTENT]: true, 52 | [codes.RESET_CONTENT]: true, 53 | [codes.NOT_MODIFIED]: true, 54 | }; 55 | 56 | /** 57 | * Status codes for when you should retry the request. 58 | */ 59 | status.retry = { 60 | [codes.BAD_GATEWAY]: true, 61 | [codes.SERVICE_UNAVAILABLE]: true, 62 | [codes.GATEWAY_TIMEOUT]: true, 63 | }; 64 | 65 | /** 66 | * Get the status code. But pretty printed. 67 | * 68 | * Given a number, this will throw if it is not a known status 69 | * code, otherwise the code will be returned. Given a string, 70 | * the string will be parsed for a number and return the code 71 | * if valid, otherwise will lookup the code assuming this is 72 | * the status message. 73 | * 74 | * Status codes remain the same as status(arg) 75 | * Status messages are pretty printed: 76 | * 'INTERNAL_SERVER_ERROR' -> 'Internal Server Error' 77 | * 78 | * @param {string|number} code 79 | * @returns {string|number} 80 | * @public 81 | */ 82 | status.pretty = function pretty(code: Status): Status { 83 | const res = status(code); 84 | if (typeof res === "string") { 85 | return res.replace(/_/g, " ") 86 | .toLowerCase() 87 | .split(" ") 88 | .map((s) => s.charAt(0).toUpperCase() + s.substring(1)) 89 | .join(" "); 90 | } 91 | return res; 92 | }; 93 | 94 | /** 95 | * Get the status code. 96 | * 97 | * Given a number, this will throw if it is not a known status 98 | * code, otherwise the code will be returned. Given a string, 99 | * the string will be parsed for a number and return the code 100 | * if valid, otherwise will lookup the code assuming this is 101 | * the status message. 102 | * 103 | * @param {string|number} code 104 | * @returns {string|number} 105 | * @public 106 | */ 107 | export function status(code: Status): Status { 108 | if (typeof code === "number") { 109 | if (!status.message[code]) throw new Error("invalid status code: " + code); 110 | return status.message[code]; 111 | } 112 | 113 | if (typeof code !== "string") { 114 | throw new TypeError("code must be a number or string"); 115 | } 116 | 117 | var n = parseInt(code, 10); 118 | if (!isNaN(n)) { 119 | if (!status.message[n]) throw new Error("invalid status code: " + n); 120 | return status.message[n]; 121 | } 122 | 123 | // remove prettyness 'inTerNal SeRvEr ErrRor' => 'INTERNAL_SERVER_ERROR' 124 | code = code.trim().replace(/ /g, "_").toUpperCase(); 125 | n = status.code[code]; 126 | if (!n) throw new Error('invalid status message: "' + code + '"'); 127 | return n; 128 | } 129 | -------------------------------------------------------------------------------- /status_test.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2020 Filippo Rossi. All rights reserved. MIT license. 2 | 3 | import { 4 | assert, 5 | assertStrictEquals as assertStrictEq, 6 | assertThrows, 7 | } from "https://deno.land/std/testing/asserts.ts"; 8 | 9 | import { MOVED_PERMANENTLY, status } from "./mod.ts"; 10 | 11 | Deno.test({ 12 | name: "status:arguments:number", 13 | fn(): void { 14 | assertStrictEq(status(200), "OK"); 15 | assertStrictEq(status(404), "NOT_FOUND"); 16 | assertStrictEq(status(500), "INTERNAL_SERVER_ERROR"); 17 | 18 | assertThrows(status.bind(null, 0), undefined, "invalid status code"); 19 | assertThrows(status.bind(null, 1000), undefined, "invalid status code"); 20 | assertThrows(status.bind(null, 299), undefined, "invalid status code"); 21 | assertThrows(status.bind(null, 310), undefined, "invalid status code"); 22 | 23 | assertThrows(status.bind(null, 306), undefined, "invalid status code"); 24 | }, 25 | }); 26 | 27 | Deno.test({ 28 | name: "status:arguments:string", 29 | fn(): void { 30 | assertStrictEq(status("200"), "OK"); 31 | assertStrictEq(status("404"), "NOT_FOUND"); 32 | assertStrictEq(status("500"), "INTERNAL_SERVER_ERROR"); 33 | 34 | assert(status("OK")); 35 | assert(status("internal server error")); 36 | assert(status("NoT FoUnd")); 37 | 38 | assertThrows( 39 | status.bind(null, "too many bugs"), 40 | undefined, 41 | "invalid status message", 42 | ); 43 | assertThrows(status.bind(null, "299"), undefined, "invalid status code"); 44 | }, 45 | }); 46 | 47 | Deno.test({ 48 | name: "status:pretty", 49 | fn(): void { 50 | assertStrictEq(status.pretty("200"), "Ok"); 51 | assertStrictEq(status.pretty("404"), "Not Found"); 52 | assertStrictEq(status.pretty("500"), "Internal Server Error"); 53 | 54 | assert(status.pretty("OK")); 55 | assert(status.pretty("internal server error")); 56 | assert(status.pretty("NoT FoUnd")); 57 | 58 | assertThrows( 59 | status.pretty.bind(null, "too many bugs"), 60 | undefined, 61 | "invalid status message", 62 | ); 63 | assertThrows( 64 | status.pretty.bind(null, "299"), 65 | undefined, 66 | "invalid status code", 67 | ); 68 | 69 | assertStrictEq(status(status.pretty("500")), 500); 70 | }, 71 | }); 72 | 73 | Deno.test({ 74 | name: "status:globals", 75 | fn(): void { 76 | MOVED_PERMANENTLY; 77 | }, 78 | }); 79 | --------------------------------------------------------------------------------