Simple JSON support module bundling common HTTP headers, status codes and methods.
$ npm install -S @haensl/http
$ yarn add @haensl/http
-
Use http status codes, methods and headers in your projects:
ESM, i.e.
import
import { statusCodes} from '@haensl/http'; // ... const response = await fetch(url); if (response.statusCode < statusCodes.badRequest) { // do stuff when response status code < 400 } // ...
CJS, i.e.
require
const { headers, methods } = require('@haensl/http'); // ... const requestOptions = { headers: { [headers.contentType]: 'application/json' }, method: methods.post, body: JSON.stringify(data) }; const response = await fetch(url, requestOptions); // ...
The http
object is a simple JSON object that maps status codes, headers and strings:
{
"statusCodes": {
"ok": 200,
"created": 201,
"accepted": 202,
// ...
},
"headers": {
"accept": "Accept",
"accessControl": {
"allow": {
"origin": "Access-Control-Allow-Origin",
"methods": "Access-Control-Allow-Methods"
// ...
},
// ...
},
"authorization": "Authorization",
"contentType": "Content-Type",
// ...
},
"methods": {
"delete": "delete",
"get": "get",
// ...
}
}
Attention: This JSON object is not exhaustive(, yet)! If you need a status code, header or method added, please create a feature request