Lithe
Welcome Getting started Routing Build Signals and binding Web component Templates View transitions Fetcher Multiple languages github-circle-black-transparent GitHub github-circle-black-transparent Example app

Fetcher

Fetch wrapper that adds instance config and interceptors

Fetcher

import { Fetcher } from '@thewebformula/lithe';

// Create instance with base config
const instance = new Fetcher(
  baseUrl: 'www.site.com',
  { Authorization: 'Bearer token' },
  credentials: 'include'
);


// Example JWT refresh token interceptor
instance.addResponseInterceptor(
  // callback
  async () => {
    try {
      const token = await getAccessToken(); // get new token
      instance.headers.set('Authorization', `Bearer ${token}`); // set header on instance (all requests)
      return true; // return true to retry the request
    } catch (e) {
      console.log(e);
      return false; // return false to end request
    }
  },

  // filter: allows you to control if the callback get called
  response => {
    return response.status === 401;
  }
);


// Make request
const response = await instance.fetch('/path');
          
menu