foca-axios
TypeScript icon, indicating that this package has built-in type declarations

4.1.1 • Public • Published

foca-axios

axios++,支持 缓存、截流、重试,响应结果去掉烦人的.data

License GitHub Workflow Status (branch) Codecov npm npm dependency version

特性

  • 合并相同的并发请求
  • 失败重试
  • 缓存响应成功的数据
  • 优化 axios 返回值

安装

pnpm add foca-axios

使用

// File: http.ts
import { axios } from 'foca-axios';

export const http = axios;

自定义配置

import { axios } from 'foca-axios';

export const http = axios.create({
  retry: false,
});

使用原始数据

// [{id: 1, ...}]
await axios.get('/users');

// { data: [{id: 1, ...}], status: 200, ... }
await axios.get('/users').toRaw();

请求遇到授权限制

retry配置中提供了resolveUnauthorized函数

当检测到401 unauthorized状态码,如果该函数返回true,则忽略 allowedMethodsallowedHttpStatus 的判断并继续重试。

注意:函数内的请求即使出错也不会进行重试

const http = axios.create({
  retry: {
    async resolveUnauthorized(config) {
      const result = await axios.post('/refresh/token', {...});
      // config是即将重试的请求配置
      config.headers.Authorization = `Bearer ${result.token}`;
      // 代表已经解决授权问题,继续重试
      return true;
    },
  },
});

Readme

Keywords

none

Package Sidebar

Install

npm i foca-axios

Weekly Downloads

121

Version

4.1.1

License

MIT

Unpacked Size

108 kB

Total Files

11

Last publish

Collaborators

  • geekact