@prexjs/statuses 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +97 -2
- package/lib/index.cjs +7 -0
- package/lib/index.d.cts +27 -0
- package/lib/index.d.mts +27 -0
- package/lib/index.mjs +7 -0
- package/package.json +64 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Prexjs (https://prexjs.com)
|
|
4
|
+
Copyright (c) 2026 Sorabh (https://Sorabh.org)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,98 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @prexjs/statuses
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**HTTP status code and reason phrase utility with category checks for redirects, empty bodies, and retries.**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@prexjs/statuses)
|
|
6
|
+
[](https://www.npmjs.com/package/@prexjs/statuses)
|
|
7
|
+
[](./LICENSE)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
|
|
10
|
+
`@prexjs/statuses` provides lookup between HTTP status codes (e.g. `404`) and standard status messages (e.g. `'Not Found'`), alongside convenient dictionaries for status categorization (`redirect`, `empty`, `retry`).
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- Bi-directional lookup between HTTP status codes and phrases.
|
|
15
|
+
- Categorization maps: `statuses.redirect`, `statuses.empty`, `statuses.retry`.
|
|
16
|
+
- Complete list of standard codes: `statuses.codes`.
|
|
17
|
+
- Dual ES Module (`.mjs`) and CommonJS (`.cjs`) support with TypeScript definitions.
|
|
18
|
+
- Zero runtime dependencies.
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Install using your preferred package manager:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
# npm
|
|
26
|
+
npm install @prexjs/statuses
|
|
27
|
+
|
|
28
|
+
# pnpm
|
|
29
|
+
pnpm add @prexjs/statuses
|
|
30
|
+
|
|
31
|
+
# Yarn
|
|
32
|
+
yarn add @prexjs/statuses
|
|
33
|
+
|
|
34
|
+
# Bun
|
|
35
|
+
bun add @prexjs/statuses
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick start
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import statuses from '@prexjs/statuses';
|
|
42
|
+
|
|
43
|
+
// Status code to message
|
|
44
|
+
console.log(statuses(200)); // 'OK'
|
|
45
|
+
console.log(statuses(404)); // 'Not Found'
|
|
46
|
+
|
|
47
|
+
// Message to status code
|
|
48
|
+
console.log(statuses('not found')); // 404
|
|
49
|
+
console.log(statuses('bad request')); // 400
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Both default and named exports are supported:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import statuses, {
|
|
56
|
+
message,
|
|
57
|
+
code,
|
|
58
|
+
codes,
|
|
59
|
+
redirect,
|
|
60
|
+
empty,
|
|
61
|
+
retry,
|
|
62
|
+
} from '@prexjs/statuses';
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Examples
|
|
66
|
+
|
|
67
|
+
### Checking status categories
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import statuses from '@prexjs/statuses';
|
|
71
|
+
|
|
72
|
+
// Check if a status requires an empty body (204, 205, 304)
|
|
73
|
+
if (statuses.empty[res.statusCode]) {
|
|
74
|
+
res.end();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Check if a status is a redirect (300, 301, 302, 303, 305, 307, 308)
|
|
78
|
+
if (statuses.redirect[res.statusCode]) {
|
|
79
|
+
console.log('Redirecting...');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Check if a status indicates a retryable server error (502, 503, 504)
|
|
83
|
+
if (statuses.retry[res.statusCode]) {
|
|
84
|
+
// schedule retry
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Compatibility
|
|
89
|
+
|
|
90
|
+
`@prexjs/statuses` runs anywhere ECMAScript executes (Node.js, Deno, Bun, browser).
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
This project is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.
|
|
95
|
+
|
|
96
|
+
## Related projects
|
|
97
|
+
|
|
98
|
+
- [prex.js](https://github.com/prexjs/prex.js) — prex.js monorepo and ecosystem.
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @prexjs/statuses
|
|
3
|
+
* Copyright (c) 2026 Prexjs (https://prexjs.com)
|
|
4
|
+
* Copyright (c) 2026 Sorabh (https://Sorabh.org)
|
|
5
|
+
* MIT Licensed
|
|
6
|
+
*/
|
|
7
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e={100:"Continue",101:"Switching Protocols",102:"Processing",103:"Early Hints",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",208:"Already Reported",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a Teapot",421:"Misdirected Request",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",425:"Too Early",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",508:"Loop Detected",509:"Bandwidth Limit Exceeded",510:"Not Extended",511:"Network Authentication Required"};const t=function(e){const t={};return Object.keys(e).forEach(o=>{const r=Number(o);t[e[r].toLowerCase()]=r}),t}(e),o=function(e){return Object.keys(e).map(e=>Number(e))}(e),r={300:!0,301:!0,302:!0,303:!0,305:!0,307:!0,308:!0},n={204:!0,205:!0,304:!0},a={502:!0,503:!0,504:!0};function i(t){if(!Object.prototype.hasOwnProperty.call(e,t))throw new Error("invalid status code: "+t);return e[t]}const s=Object.assign(function(e){if("number"==typeof e)return i(e);if("string"!=typeof e)throw new TypeError("code must be a number or string");const o=parseInt(e,10);return isNaN(o)?function(e){const o=e.toLowerCase();if(!Object.prototype.hasOwnProperty.call(t,o))throw new Error('invalid status message: "'+e+'"');return t[o]}(e):i(o)},{message:e,code:t,codes:o,redirect:r,empty:n,retry:a});s.default=s,s.statuses=s,exports.code=t,exports.codes=o,exports.default=s,exports.empty=n,exports.message=e,exports.redirect=r,exports.retry=a,exports.statuses=s;
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare const codes: Record<number, string>;
|
|
2
|
+
declare const codeMap: Record<string, number>;
|
|
3
|
+
declare const codeList: number[];
|
|
4
|
+
declare const redirect: Record<number, boolean>;
|
|
5
|
+
declare const empty: Record<number, boolean>;
|
|
6
|
+
declare const retry: Record<number, boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* Get the status code.
|
|
9
|
+
*
|
|
10
|
+
* Given a number, this will throw if it is not a known status
|
|
11
|
+
* code, otherwise the message will be returned. Given a string,
|
|
12
|
+
* the string will be parsed for a number and return the message
|
|
13
|
+
* if valid, otherwise will lookup the code assuming this is
|
|
14
|
+
* the status message.
|
|
15
|
+
*/
|
|
16
|
+
declare function status(code: number): string;
|
|
17
|
+
declare function status(code: string): string | number;
|
|
18
|
+
declare const statuses: typeof status & {
|
|
19
|
+
message: Record<number, string>;
|
|
20
|
+
code: Record<string, number>;
|
|
21
|
+
codes: number[];
|
|
22
|
+
redirect: Record<number, boolean>;
|
|
23
|
+
empty: Record<number, boolean>;
|
|
24
|
+
retry: Record<number, boolean>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { codeMap as code, codeList as codes, statuses as default, empty, codes as message, redirect, retry, statuses };
|
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare const codes: Record<number, string>;
|
|
2
|
+
declare const codeMap: Record<string, number>;
|
|
3
|
+
declare const codeList: number[];
|
|
4
|
+
declare const redirect: Record<number, boolean>;
|
|
5
|
+
declare const empty: Record<number, boolean>;
|
|
6
|
+
declare const retry: Record<number, boolean>;
|
|
7
|
+
/**
|
|
8
|
+
* Get the status code.
|
|
9
|
+
*
|
|
10
|
+
* Given a number, this will throw if it is not a known status
|
|
11
|
+
* code, otherwise the message will be returned. Given a string,
|
|
12
|
+
* the string will be parsed for a number and return the message
|
|
13
|
+
* if valid, otherwise will lookup the code assuming this is
|
|
14
|
+
* the status message.
|
|
15
|
+
*/
|
|
16
|
+
declare function status(code: number): string;
|
|
17
|
+
declare function status(code: string): string | number;
|
|
18
|
+
declare const statuses: typeof status & {
|
|
19
|
+
message: Record<number, string>;
|
|
20
|
+
code: Record<string, number>;
|
|
21
|
+
codes: number[];
|
|
22
|
+
redirect: Record<number, boolean>;
|
|
23
|
+
empty: Record<number, boolean>;
|
|
24
|
+
retry: Record<number, boolean>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export { codeMap as code, codeList as codes, statuses as default, empty, codes as message, redirect, retry, statuses };
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @prexjs/statuses
|
|
3
|
+
* Copyright (c) 2026 Prexjs (https://prexjs.com)
|
|
4
|
+
* Copyright (c) 2026 Sorabh (https://Sorabh.org)
|
|
5
|
+
* MIT Licensed
|
|
6
|
+
*/
|
|
7
|
+
const e={100:"Continue",101:"Switching Protocols",102:"Processing",103:"Early Hints",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",208:"Already Reported",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a Teapot",421:"Misdirected Request",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",425:"Too Early",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",508:"Loop Detected",509:"Bandwidth Limit Exceeded",510:"Not Extended",511:"Network Authentication Required"};const t=function(e){const t={};return Object.keys(e).forEach(o=>{const r=Number(o);t[e[r].toLowerCase()]=r}),t}(e),o=function(e){return Object.keys(e).map(e=>Number(e))}(e),r={300:!0,301:!0,302:!0,303:!0,305:!0,307:!0,308:!0},n={204:!0,205:!0,304:!0},a={502:!0,503:!0,504:!0};function i(t){if(!Object.prototype.hasOwnProperty.call(e,t))throw new Error("invalid status code: "+t);return e[t]}const s=Object.assign(function(e){if("number"==typeof e)return i(e);if("string"!=typeof e)throw new TypeError("code must be a number or string");const o=parseInt(e,10);return isNaN(o)?function(e){const o=e.toLowerCase();if(!Object.prototype.hasOwnProperty.call(t,o))throw new Error('invalid status message: "'+e+'"');return t[o]}(e):i(o)},{message:e,code:t,codes:o,redirect:r,empty:n,retry:a});s.default=s,s.statuses=s;export{t as code,o as codes,s as default,n as empty,e as message,r as redirect,a as retry,s as statuses};
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prexjs/statuses",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "HTTP status code and reason phrase utility with category checks for redirects, empty bodies, and retries.",
|
|
5
|
+
"authors": [
|
|
6
|
+
{
|
|
7
|
+
"name": "Prexjs",
|
|
8
|
+
"email": "hello@prexjs.com",
|
|
9
|
+
"url": "https://prexjs.com"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"name": "Sorabh",
|
|
13
|
+
"email": "hello@sorabh.org",
|
|
14
|
+
"url": "https://sorabh.org"
|
|
15
|
+
}
|
|
16
|
+
],
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"files": [
|
|
21
|
+
"lib",
|
|
22
|
+
"README.md",
|
|
23
|
+
"LICENSE"
|
|
24
|
+
],
|
|
25
|
+
"main": "./lib/index.cjs",
|
|
26
|
+
"module": "./lib/index.mjs",
|
|
27
|
+
"types": "./lib/index.d.cts",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./lib/index.d.mts",
|
|
32
|
+
"default": "./lib/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./lib/index.d.cts",
|
|
36
|
+
"default": "./lib/index.cjs"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"./package.json": "./package.json"
|
|
40
|
+
},
|
|
41
|
+
"repository": {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/prexjs/prex.js.git",
|
|
44
|
+
"directory": "packages/statuses"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/prexjs/prex.js/tree/main/packages/statuses#readme",
|
|
47
|
+
"bugs": {
|
|
48
|
+
"url": "https://github.com/prexjs/prex.js/issues"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"keywords": [
|
|
54
|
+
"prexjs",
|
|
55
|
+
"statuses",
|
|
56
|
+
"status",
|
|
57
|
+
"http",
|
|
58
|
+
"status-codes",
|
|
59
|
+
"status-message",
|
|
60
|
+
"404",
|
|
61
|
+
"200",
|
|
62
|
+
"500",
|
|
63
|
+
"redirect",
|
|
64
|
+
"typescript"
|
|
65
|
+
]
|
|
66
|
+
}
|