@haensl/http

1.1.1 • Public • Published

@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

    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);
    
    // ...

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

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

Package Sidebar

Install

npm i @haensl/http

Weekly Downloads

34

Version

1.1.1

License

MIT

Unpacked Size

6.28 kB

Total Files

4

Last publish

Collaborators

  • haensl