0.1.4 • Published 3 months ago

di-fetch v0.1.4

Weekly downloads
-
License
ISC
Repository
-
Last release
3 months ago
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);
0.1.2

3 months ago

0.1.1

3 months ago

0.1.4

3 months ago

0.1.3

3 months ago

0.1.0

5 months ago

0.0.5

6 months ago

0.0.4

7 months ago

0.0.3-alpha.1

8 months ago

0.0.3-alpha.0

8 months ago

0.0.1-alpha.2

8 months ago