@cdellacqua/http-status
TypeScript icon, indicating that this package has built-in type declarations

1.0.6 • Public • Published

Enums for HTTP Status Codes and Messages

NPM Package

npm install @cdellacqua/http-status

Documentation

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);
	}
});

Readme

Keywords

Package Sidebar

Install

npm i @cdellacqua/http-status

Weekly Downloads

2

Version

1.0.6

License

MIT

Unpacked Size

22.9 kB

Total Files

8

Last publish

Collaborators

  • cdellacqua