@metatypes/http-statuses
Version:
Well-Documented HTTP Status Code Enums
100 lines (70 loc) • 2.17 kB
Markdown
> Complete, well-documented HTTP Status Code definitions for TypeScript Projects.
```typescript
import { Statuses } from '@metatypes/http-statuses'
async function fetchUsers() {
const response = await fetch(/* ... */)
switch (response.status) {
case Statuses.HTTP_200_OK:
return response.data
case Statuses.HTTP_404_NOT_FOUND:
alert('the user could not be found')
break
default:
throw new Error('Unknown API Error')
}
}
```
For full-stack teams, it might be advantageous to use the same naming scheme on the front- and backend. So, you can also use framework-flavored status codes.
If you’d like to add another framework, feel free to submit a pull request or create an issue.
This is pretty much the same as the default `Statuses` enum, but status codes that aren’t supported by DRF are removed.
```typescript
import { DRFStatuses } from '@metatypes/http-statuses'
async function fetchUsers() {
const response = await fetch(/* ... */)
switch (response.status) {
case DRFStatuses.HTTP_200_OK:
return response.data
case DRFStatuses.HTTP_404_NOT_FOUND:
alert('the user could not be found')
break
default:
throw new Error('Unknown API Error')
}
}
```
```typescript
import { NodeJsStatuses } from '@metatypes/http-statuses'
async function fetchUsers() {
const response = await fetch(/* ... */)
switch (response.status) {
case NodeJsStatuses.OK:
return response.data
case NodeJsStatuses.NotFound:
alert('the user could not be found')
break
default:
throw new Error('Unknown API Error')
}
}
```
```typescript
import { SpringStatuses } from '@metatypes/http-statuses'
async function fetchUsers() {
const response = await fetch(/* ... */)
switch (response.status) {
case SpringStatuses.OK:
return response.data
case SpringStatuses.NotFound:
alert('the user could not be found')
break
default:
throw new Error('Unknown API Error')
}
}
```