@cdellacqua/http-status
v1.0.6
Published
HTTP Status TypeScript enums
Downloads
5
Readme
Enums for HTTP Status Codes and Messages
npm install @cdellacqua/http-status
Highlights
import {HttpStatus, HttpStatusMessage} from '@cdellacqua/http-status';
console.log(HttpStatus.OK); // 200
console.log(HttpStatusMessage[301]); // "Moved Permanently"
console.log(HttpStatusMessage[HttpStatus.MovedPermanently]); // "Moved Permanently"
// It can be used to create expressive dispatch tables:
const dispatchTable = {
[HttpStatus.OK]: async (response) => alert('here we go: ' + (await response.text())),
[HttpStatus.InternalServerError]: () => alert('oops!'),
[HttpStatus.BadRequest]: () => alert('mistakes were made...'),
};
fetch('/some-api.json').then((response) => {
if (response.status in dispatchTable) {
dispatchTable[response.status](response);
} else {
alert('unhandled status! ' + response.status);
}
});