0.0.3 • Published 5 months ago

clear-fetch v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

A clear fetch library

  • Example
import useUserStore from "@/stores/auth/user";
import Create from "clear-fetch";

const TOKEN_BLACK_LIST = ["/login"];

const instance = Create({
  baseUrl: process.env.NEXT_PUBLIC_BASE_URL as string,
  timeout: 3000,
});

instance.useReqInterceptor(
  (config: any) => {
    const isAddToken = !TOKEN_BLACK_LIST.includes(config.url as string);
    if (isAddToken) {
      const { token } = useUserStore.getState();
      (config.headers as any)["Authorization"] = `Bearer ${token}`;
    }
    return config;
  },
  (error: any) => {
    return Promise.reject(error);
  },
);

instance.useResInterceptor(
  (response: any) => {
    // 没有授权直接跳转login
    return response;
  },
  (error: any) => {
    return Promise.reject(error);
  },
);

export default instance.request;
0.0.3

5 months ago

0.0.2

11 months ago

0.0.1

11 months ago