项目作者: maxali

项目描述 :
Fetch API with middleware
高级语言: TypeScript
项目地址: git://github.com/maxali/fej.git
创建时间: 2019-06-17T21:52:24Z
项目社区:https://github.com/maxali/fej

开源协议:

下载


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

  1. npm install fej

Usage

See following usage examples

Fej.setInit

Set some static headers

  1. import Fej from "fej";
  2. Fej.setInit({
  3. headers: {
  4. "Accept": "application/json",
  5. "Content-Type": "application/json"
  6. }
  7. });

Fej.addAsyncMiddleware

Updating fetch properties asynchronously

  1. import Fej from "fej";
  2. Fej.addAsyncMiddleware(async init => {
  3. // get access token
  4. const token = await authService.acquireTokenSilent();
  5. // update Authorization header with new access token
  6. return Promise.resolve({
  7. headers: { Authorization: "Bearer " + token.accessToken }
  8. });
  9. });

Fej.addMiddleware

  1. import Fej from "fej";
  2. Fej.addMiddleware(init => {
  3. // Get current time
  4. const currentDateTime = new Date().toISOString()
  5. // update Authorization header with new access token
  6. return {
  7. headers: { "Z-CURRENTTIME": currentDateTime }
  8. };
  9. });