Constants enumerating the HTTP status codes with utility functions for TypeScript and JavaScript applications
npx @tessl/cli install tessl/npm-http-status-codes@2.3.00
# HTTP Status Codes
1
2
HTTP Status Codes is a zero-dependency TypeScript library providing comprehensive HTTP status code constants and utility functions. It offers enumerated constants for all standard HTTP status codes defined in RFC1945 (HTTP/1.0), RFC2616 (HTTP/1.1), RFC2518 (WebDAV), RFC6585 (Additional HTTP Status Codes), and RFC7538 (Permanent Redirect).
3
4
## Package Information
5
6
- **Package Name**: http-status-codes
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install http-status-codes`
10
11
## Core Imports
12
13
```typescript
14
import {
15
StatusCodes,
16
ReasonPhrases,
17
getReasonPhrase,
18
getStatusCode,
19
} from "http-status-codes";
20
```
21
22
For CommonJS:
23
24
```javascript
25
const {
26
StatusCodes,
27
ReasonPhrases,
28
getReasonPhrase,
29
getStatusCode,
30
} = require("http-status-codes");
31
```
32
33
## Basic Usage
34
35
```typescript
36
import {
37
StatusCodes,
38
ReasonPhrases,
39
getReasonPhrase,
40
getStatusCode,
41
} from "http-status-codes";
42
43
// Using enum constants
44
response.status(StatusCodes.OK).send(ReasonPhrases.OK);
45
46
// Using utility functions
47
response
48
.status(StatusCodes.INTERNAL_SERVER_ERROR)
49
.send({
50
error: getReasonPhrase(StatusCodes.INTERNAL_SERVER_ERROR)
51
});
52
53
// Reverse lookup from reason phrase to status code
54
const statusCode = getStatusCode("Internal Server Error");
55
response.status(statusCode).send({ error: "Internal Server Error" });
56
```
57
58
## Architecture
59
60
The library is organized around several key components:
61
62
- **StatusCodes Enum**: Numeric constants for all HTTP status codes with descriptive names
63
- **ReasonPhrases Enum**: String constants for corresponding human-readable reason phrases
64
- **Utility Functions**: Bidirectional conversion between status codes and reason phrases
65
- **Legacy Constants**: Individual constants for v1 backwards compatibility (deprecated)
66
- **Internal Lookups**: Efficient Record-based mappings for fast conversions
67
68
## Capabilities
69
70
### Status Code Constants
71
72
Enumerated constants for all standard HTTP status codes with descriptive names and RFC documentation.
73
74
```typescript { .api }
75
enum StatusCodes {
76
// 1xx Informational
77
CONTINUE = 100,
78
SWITCHING_PROTOCOLS = 101,
79
PROCESSING = 102,
80
EARLY_HINTS = 103,
81
// 2xx Success
82
OK = 200,
83
CREATED = 201,
84
ACCEPTED = 202,
85
// ... (59 total status codes)
86
}
87
```
88
89
[Status Code Constants](./status-codes.md)
90
91
### Reason Phrase Constants
92
93
Enumerated string constants for human-readable HTTP reason phrases corresponding to status codes.
94
95
```typescript { .api }
96
enum ReasonPhrases {
97
// 1xx Informational
98
CONTINUE = "Continue",
99
SWITCHING_PROTOCOLS = "Switching Protocols",
100
PROCESSING = "Processing",
101
EARLY_HINTS = "Early Hints",
102
// 2xx Success
103
OK = "OK",
104
CREATED = "Created",
105
ACCEPTED = "Accepted",
106
// ... (59 total reason phrases)
107
}
108
```
109
110
[Reason Phrase Constants](./reason-phrases.md)
111
112
### Utility Functions
113
114
Bidirectional conversion functions between status codes and reason phrases with error handling.
115
116
```typescript { .api }
117
function getReasonPhrase(statusCode: number | string): string;
118
function getStatusCode(reasonPhrase: string): number;
119
function getStatusText(statusCode: number | string): string; // @deprecated
120
```
121
122
[Utility Functions](./utility-functions.md)
123
124
### Legacy Support
125
126
Backwards compatibility with v1 API through individual status code constants (deprecated).
127
128
```typescript { .api }
129
// Individual constants (deprecated - use StatusCodes enum instead)
130
const ACCEPTED = 202;
131
const BAD_REQUEST = 400;
132
const INTERNAL_SERVER_ERROR = 500;
133
// ... (54 total legacy constants)
134
```
135
136
[Legacy Constants](./legacy-constants.md)