19.6.2 • Published 6 years ago

@desicode/fetch v19.6.2

Weekly downloads
-
License
WTFPL
Repository
-
Last release
6 years ago

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.

19.6.2

6 years ago

19.5.18

6 years ago