Skip to content

maxali/fej

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fej

Fetch API with middleware

fej exposes simple middleware API to manipulate request properties.

You can override middleware and initial data with each request: fej("/api/users", { headers: {"Accept": "application/xml"} })

Install

  npm install fej

Usage

See following usage examples

Fej.setInit

Set some static headers

  import Fej from "fej";

  Fej.setInit({
    headers: {
      "Accept": "application/json",
      "Content-Type": "application/json"
    }
  });

Fej.addAsyncMiddleware

Updating fetch properties asynchronously

  import Fej from "fej";

  Fej.addAsyncMiddleware(async init => {

    // get access token
    const token = await authService.acquireTokenSilent();

    // update Authorization header with new access token
    return Promise.resolve({
      headers: { Authorization: "Bearer " + token.accessToken }
    });
  });

Fej.addMiddleware

  import Fej from "fej";

  Fej.addMiddleware(init => {

    // Get current time
    const currentDateTime = new Date().toISOString()

    // update Authorization header with new access token
    return {
      headers: { "Z-CURRENTTIME": currentDateTime }
    };
  });