1.1.1 • Published 2 months ago

@haensl/http v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@haensl/http

Simple JSON support module bundling common HTTP headers, status codes and methods.

NPM

npm version CircleCI

Installation

Via npm

$ npm install -S @haensl/http

Via yarn

$ yarn add @haensl/http

Usage

  1. Install @haensl/http

  2. Use http status codes, methods and headers in your projects:

ESM, i.e. `import`

```javascript
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`

```javascript
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);

// ...
```

Synopsis

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",
    // ...
  }
}

JSON

Attention: This JSON object is not exhaustive(, yet)! If you need a status code, header or method added, please create a feature request

Changelog