@desicode/fetch

19.6.2 • Public • Published

fetch

A micro library that abstracts away HTTP5 Request methods into a singleton

Install

npm install @desicode/fetch

Usage

import http from '@desicode/fetch';

http.get('http(s?)://URL');

Advanced usage

Intercepting

In single page applications, authentication tokens are typically stored and managed on the Client. Request Interceptor allows to inject these token(s) as custom headers. Can also be used to centrally modify the HTTP request before sending it to the server.

Response Interceptors allow to inspect and optionally modify the response object from the server. This function's intended use is to check auth status codes and perform redirection in the SPA accordingly

const HTTP_UNAUTHORIZED_CODE = 401;

http.responseInterceptor = async (response) => {
  //~ console.log('Intercepting response', response);
  if (response.statusCode === HTTP_UNAUTHORIZED_CODE) {
    route(`/login`);
  }
};

http.requestInterceptor = () => {
  return new Headers({
    Authorization: AppStore.get('token').token
  });
};

Note

This library DOES NOT support ES5. This is only designed for use with modern browsers.

Readme

Keywords

Package Sidebar

Install

npm i @desicode/fetch

Weekly Downloads

1

Version

19.6.2

License

WTFPL

Unpacked Size

4.61 kB

Total Files

4

Last publish

Collaborators

  • desikoder