Skip to content

Commit 34bfd94

Browse files
committed
Add initial version
0 parents  commit 34bfd94

File tree

9 files changed

+6527
-0
lines changed

9 files changed

+6527
-0
lines changed

.github/workflows/main.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: CI
2+
on: [push]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
7+
steps:
8+
- name: Begin CI...
9+
uses: actions/checkout@v2
10+
11+
- name: Use Node 12
12+
uses: actions/setup-node@v1
13+
with:
14+
node-version: 12.x
15+
16+
- name: Use cached node_modules
17+
uses: actions/cache@v1
18+
with:
19+
path: node_modules
20+
key: nodeModules-${{ hashFiles('**/yarn.lock') }}
21+
restore-keys: |
22+
nodeModules-
23+
24+
- name: Install dependencies
25+
run: yarn install --frozen-lockfile
26+
env:
27+
CI: true
28+
29+
- name: Lint
30+
run: yarn lint
31+
env:
32+
CI: true
33+
34+
- name: Test
35+
run: yarn test --ci --coverage --maxWorkers=2
36+
env:
37+
CI: true
38+
39+
- name: Build
40+
run: yarn build
41+
env:
42+
CI: true

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.log
2+
.DS_Store
3+
node_modules
4+
dist
5+
.idea

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Flávio Carvalho
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

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# simple-http-status
2+
3+
## Why
4+
5+
Heavily inspired by [Django Rest Framework](https://www.django-rest-framework.org/api-guide/status-codes)
6+
7+
Using bare status codes in your responses isn't recommended. This package includes a set of named constants that you can use to make your code more obvious and readable.
8+
9+
## Getting started
10+
11+
```js
12+
yarn add simple-http-status
13+
```
14+
15+
## Usage
16+
17+
```ts
18+
import { Status } from 'simple-http-status';
19+
20+
app.get('/', (req, res, next) => {
21+
res.status(Status.HTTP_200_OK).send('Hello world');
22+
});
23+
```
24+
25+
### Informational - 1xx
26+
27+
This class of status code indicates a provisional response.
28+
29+
```js
30+
HTTP_100_CONTINUE;
31+
HTTP_101_SWITCHING_PROTOCOLS;
32+
```
33+
34+
### Successful - 2xx
35+
36+
This class of status code indicates that the client's request was successfully received, understood, and accepted.
37+
38+
```js
39+
HTTP_200_OK;
40+
HTTP_201_CREATED;
41+
HTTP_202_ACCEPTED;
42+
HTTP_203_NON_AUTHORITATIVE_INFORMATION;
43+
HTTP_204_NO_CONTENT;
44+
HTTP_205_RESET_CONTENT;
45+
HTTP_206_PARTIAL_CONTENT;
46+
HTTP_207_MULTI_STATUS;
47+
HTTP_208_ALREADY_REPORTED;
48+
HTTP_226_IM_USED;
49+
```
50+
51+
### Redirection - 3xx
52+
53+
This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request.
54+
55+
```js
56+
HTTP_300_MULTIPLE_CHOICES;
57+
HTTP_301_MOVED_PERMANENTLY;
58+
HTTP_302_FOUND;
59+
HTTP_303_SEE_OTHER;
60+
HTTP_304_NOT_MODIFIED;
61+
HTTP_305_USE_PROXY;
62+
HTTP_306_RESERVED;
63+
HTTP_307_TEMPORARY_REDIRECT;
64+
HTTP_308_PERMANENT_REDIRECT;
65+
```
66+
67+
### Client Error - 4xx
68+
69+
The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.
70+
71+
```js
72+
HTTP_400_BAD_REQUEST;
73+
HTTP_401_UNAUTHORIZED;
74+
HTTP_402_PAYMENT_REQUIRED;
75+
HTTP_403_FORBIDDEN;
76+
HTTP_404_NOT_FOUND;
77+
HTTP_405_METHOD_NOT_ALLOWED;
78+
HTTP_406_NOT_ACCEPTABLE;
79+
HTTP_407_PROXY_AUTHENTICATION_REQUIRED;
80+
HTTP_408_REQUEST_TIMEOUT;
81+
HTTP_409_CONFLICT;
82+
HTTP_410_GONE;
83+
HTTP_411_LENGTH_REQUIRED;
84+
HTTP_412_PRECONDITION_FAILED;
85+
HTTP_413_REQUEST_ENTITY_TOO_LARGE;
86+
HTTP_414_REQUEST_URI_TOO_LONG;
87+
HTTP_415_UNSUPPORTED_MEDIA_TYPE;
88+
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE;
89+
HTTP_417_EXPECTATION_FAILED;
90+
HTTP_422_UNPROCESSABLE_ENTITY;
91+
HTTP_423_LOCKED;
92+
HTTP_424_FAILED_DEPENDENCY;
93+
HTTP_426_UPGRADE_REQUIRED;
94+
HTTP_428_PRECONDITION_REQUIRED;
95+
HTTP_429_TOO_MANY_REQUESTS;
96+
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE;
97+
HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS;
98+
```
99+
100+
### Server Error - 5xx
101+
102+
Response status codes beginning with the digit "5" indicate cases in which the server is aware that it has erred or is incapable of performing the request. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.
103+
104+
```js
105+
HTTP_500_INTERNAL_SERVER_ERROR;
106+
HTTP_501_NOT_IMPLEMENTED;
107+
HTTP_502_BAD_GATEWAY;
108+
HTTP_503_SERVICE_UNAVAILABLE;
109+
HTTP_504_GATEWAY_TIMEOUT;
110+
HTTP_505_HTTP_VERSION_NOT_SUPPORTED;
111+
HTTP_506_VARIANT_ALSO_NEGOTIATES;
112+
HTTP_507_INSUFFICIENT_STORAGE;
113+
HTTP_508_LOOP_DETECTED;
114+
HTTP_509_BANDWIDTH_LIMIT_EXCEEDED;
115+
HTTP_510_NOT_EXTENDED;
116+
HTTP_511_NETWORK_AUTHENTICATION_REQUIRED;
117+
```
118+
119+
### Helper functions
120+
121+
The following helper functions are available for identifying the category of the response code.
122+
123+
```js
124+
import {
125+
isInformational,
126+
isSuccess,
127+
isRedirect,
128+
isClientError,
129+
isServerError,
130+
} from 'simple-http-status';
131+
132+
isInformational(); // 1xx
133+
isSuccess(); // 2xx
134+
isRedirect(); // 3xx
135+
isClientError(); // 4xx
136+
isServerError(); // 5xx
137+
```

package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "simple-http-status",
3+
"version": "1.0.0",
4+
"author": "Flávio Carvalho",
5+
"module": "dist/simple-http-status.esm.js",
6+
"license": "MIT",
7+
"main": "dist/index.js",
8+
"typings": "dist/index.d.ts",
9+
"files": [
10+
"dist",
11+
"src"
12+
],
13+
"engines": {
14+
"node": ">=10"
15+
},
16+
"scripts": {
17+
"start": "tsdx watch",
18+
"build": "tsdx build",
19+
"test": "tsdx test",
20+
"lint": "tsdx lint",
21+
"prepare": "tsdx build"
22+
},
23+
"peerDependencies": {},
24+
"husky": {
25+
"hooks": {
26+
"pre-commit": "tsdx lint"
27+
}
28+
},
29+
"prettier": {
30+
"printWidth": 80,
31+
"semi": true,
32+
"singleQuote": true,
33+
"trailingComma": "es5"
34+
},
35+
"devDependencies": {
36+
"husky": "^4.2.5",
37+
"tsdx": "^0.13.2",
38+
"tslib": "^2.0.0",
39+
"typescript": "^3.9.7"
40+
}
41+
}

src/index.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export enum Status {
2+
// Informational
3+
HTTP_100_CONTINUE = 100,
4+
HTTP_101_SWITCHING_PROTOCOLS = 101,
5+
6+
// Successful
7+
HTTP_200_OK = 200,
8+
HTTP_201_CREATED = 201,
9+
HTTP_202_ACCEPTED = 202,
10+
HTTP_203_NON_AUTHORITATIVE_INFORMATION = 203,
11+
HTTP_204_NO_CONTENT = 204,
12+
HTTP_205_RESET_CONTENT = 205,
13+
HTTP_206_PARTIAL_CONTENT = 206,
14+
HTTP_207_MULTI_STATUS = 207,
15+
HTTP_208_ALREADY_REPORTED = 208,
16+
HTTP_226_IM_USED = 226,
17+
18+
// Redirection
19+
HTTP_300_MULTIPLE_CHOICES = 300,
20+
HTTP_301_MOVED_PERMANENTLY = 301,
21+
HTTP_302_FOUND = 302,
22+
HTTP_303_SEE_OTHER = 303,
23+
HTTP_304_NOT_MODIFIED = 304,
24+
HTTP_305_USE_PROXY = 305,
25+
HTTP_306_RESERVED = 306,
26+
HTTP_307_TEMPORARY_REDIRECT = 307,
27+
HTTP_308_PERMANENT_REDIRECT = 308,
28+
29+
// Client Error
30+
HTTP_400_BAD_REQUEST = 400,
31+
HTTP_401_UNAUTHORIZED = 401,
32+
HTTP_402_PAYMENT_REQUIRED = 402,
33+
HTTP_403_FORBIDDEN = 403,
34+
HTTP_404_NOT_FOUND = 404,
35+
HTTP_405_METHOD_NOT_ALLOWED = 405,
36+
HTTP_406_NOT_ACCEPTABLE = 406,
37+
HTTP_407_PROXY_AUTHENTICATION_REQUIRED = 407,
38+
HTTP_408_REQUEST_TIMEOUT = 408,
39+
HTTP_409_CONFLICT = 409,
40+
HTTP_410_GONE = 410,
41+
HTTP_411_LENGTH_REQUIRED = 411,
42+
HTTP_412_PRECONDITION_FAILED = 412,
43+
HTTP_413_REQUEST_ENTITY_TOO_LARGE = 413,
44+
HTTP_414_REQUEST_URI_TOO_LONG = 414,
45+
HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415,
46+
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
47+
HTTP_417_EXPECTATION_FAILED = 417,
48+
HTTP_422_UNPROCESSABLE_ENTITY = 422,
49+
HTTP_423_LOCKED = 423,
50+
HTTP_424_FAILED_DEPENDENCY = 424,
51+
HTTP_426_UPGRADE_REQUIRED = 426,
52+
HTTP_428_PRECONDITION_REQUIRED = 428,
53+
HTTP_429_TOO_MANY_REQUESTS = 429,
54+
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
55+
HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
56+
57+
// Server Error
58+
HTTP_500_INTERNAL_SERVER_ERROR = 500,
59+
HTTP_501_NOT_IMPLEMENTED = 501,
60+
HTTP_502_BAD_GATEWAY = 502,
61+
HTTP_503_SERVICE_UNAVAILABLE = 503,
62+
HTTP_504_GATEWAY_TIMEOUT = 504,
63+
HTTP_505_HTTP_VERSION_NOT_SUPPORTED = 505,
64+
HTTP_506_VARIANT_ALSO_NEGOTIATES = 506,
65+
HTTP_507_INSUFFICIENT_STORAGE = 507,
66+
HTTP_508_LOOP_DETECTED = 508,
67+
HTTP_509_BANDWIDTH_LIMIT_EXCEEDED = 509,
68+
HTTP_510_NOT_EXTENDED = 510,
69+
HTTP_511_NETWORK_AUTHENTICATION_REQUIRED = 511,
70+
}
71+
72+
export const isInformational = (status: number) =>
73+
status >= 100 && status < 200;
74+
75+
export const isSuccess = (status: number) => status >= 200 && status < 300;
76+
77+
export const isRedirect = (status: number) => status >= 300 && status < 400;
78+
79+
export const isClientError = (status: number) => status >= 400 && status < 500;
80+
81+
export const isServerError = (status: number) => status >= 500 && status < 600;

0 commit comments

Comments
 (0)