npm

di-fetch
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published
import { FetchEngine, fetch, serializeDataPlugin, mockPlugin } from "di-fetch";

const fetchEngine = new FetchEngine({
  baseUrl: configKit.apiBaseUrl,
  headers: {
    "Content-Type": "application/json",
    "x-app-id": appid,
    "x-app-version": configKit.config.currentVersion ?? "",
  },
  client: taro.request,
});

fetchEngine.setHeaders({
  Authorization: `Bearer ${token}`,
});

function mockApi({ interceptor, instance }) {
  interceptor("willFetch", (request) => {
    if (request.url.includes("")) {
      instance.response = {};
      instance.complete();
    } else {
      instance.next();
    }
  });
}
function expiredToken({ interceptor }) {
  interceptor("didFetch", (response) => {
    // token 过期
    if (response.code === 300) {
      goPage("index", { method: "relaunch" });
    } else {
      return response;
    }
  });
}

fetchEngine.use(mockPlugin([{
    endpoint: //,
    method: 'GET',
    response: {}
  }]));
fetchEngine.use(expiredToken);
fetchEngine.use(serializeDataPlugin);

Readme

Keywords

none

Package Sidebar

Install

npm i di-fetch

Weekly Downloads

0

Version

0.1.4

License

ISC

Unpacked Size

19.7 kB

Total Files

20

Last publish

Collaborators

  • redchili