Skip to content

Latest commit

 

History

History
63 lines (46 loc) · 1.2 KB

README.md

File metadata and controls

63 lines (46 loc) · 1.2 KB

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 }
    };
  });