0.0.6 • Published 2 years ago

@fetch-suit/fetch-interceptor v0.0.6

Weekly downloads
-
License
UNLICENSE
Repository
github
Last release
2 years ago

Fetch Interceptor

How To Use

First install @fetch-suit/fetch-interceptor via npm or yarn:

npm i @fetch-suit/fetch-interceptor

Implement an interception, the interceptor looks like this:

async function interceptor(request: Request, fetch: Fetch) {
  // before request
  const response = await fetch(request);
  // after response
  return response;
}

Use the applyInterceptors method to apply the interceptors. the following code, fetch1 will apply timestamp interceptor, and fetch2 both apply timestamp interceptor and bearer token header interceptor.

import { applyInterceptors } from '@fetch-suit/fetch-interceptor';

const fetch1 = applyInterceptors(
  window.fetch,
  // add timestamp
  async (request: Request, fetch: Fetch) => {
    const url = new URL(request.url);
    url.searchParams.append('_t', Date.now().toString());
    return await fetch(new Request(url.href, request));
  },
);

const fetch2 = applyInterceptors(
  fetch1,
  // add bearer token header
  async (request: Request, fetch: Fetch) => {
    request.headers.append('Authorization', `Bearer xxx.xxx.xxx`);
    return await fetch(request);
  },
);
0.0.5

2 years ago

0.0.4

2 years ago

0.0.6

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago