@gravity-ui/axios-wrapper

中文文档
1.5.1发布于 2024/09/18MITTypeScriptESM + CJS零依赖近 30 天下载 3.7万

Generic helper class for wrapping axios calls

原始使用文档
文档目录9 节
  1. Install
  2. HTTP API
  3. Constructor parameters
  4. Basic methods
  5. Headers
  6. CSRF
  7. Concurrent requests
  8. Collecting requests
  9. Usage

Axios Wrapper

This library provides a convenient wrapper around Axios adding automatic cancelling of concurrent requests to its features.

Install

npm install --save-dev @gravity-ui/axios-wrapper

HTTP API

Constructor parameters

config [optional]

The configuration of an axios instance.

collector [optional]

The configuration of requests collector is an object:

{
    "collectErrors": 10,
    "collectRequests": 10
}

Basic methods

Wrapper provides http-methods get, head, put, post, delete.

Methods get and head have the signature (url, params, options); put, post, while the delete method has (url, data, params, options) signature.

The params argument stands for query string parameters while options is a request settings.

Currently 4 request settings are supported:

  • concurrentId (string): optional request id
  • collectRequest (bool): optional flag, telling if the request should be logger (default true)
  • requestConfig (object): optional config with the custom request parameters
  • headers (object): optional object with custom request headers.
  • timeout (number): optional request timeout
  • onDownloadProgress (function): optional callback for processing file download progress

Headers

The setDefaultHeader({name (string), value (string), methods (array)}) method allows to add a default request header.

Arguments name and value are required, optional argument methods specified all methods which get those default headers (by default all methods will get those headers).

CSRF

The setCSRFToken method allows specifying CSRF-token, which will be added to all put, post and delete requests.

Concurrent requests

Sometimes it is better to cancel the request in flight if its results are no longer needed. To make this happen, one should pass to request's options the concurrentId id. When the next request with the same concurrentId occurs the previous request with that id will be cancelled.

One cancel a request manually as well by invoking the cancelRequest(concurrentId) method.

Collecting requests

It is possible to set up requests collection into the local storage using the collector option. It stores all requests and errors separately. The following apiInstance will keep 10 last requests (both successful and not) and 10 last erroneous requests.

const apiInstance = new API({
    collector: {
        collectErrors: 10,
        collectRequests: 10
    }
});

To obtain saved requests one have to invoke the getCollectedRequests method which returns the object {errors: [...], requests: [...]}.

Usage

The suggested usage is to subclass the base AxiosWrapper class:

export class API extends AxiosWrapper {
    getProjects() {
        return this.get('/projects');
    }
    getSensors({project, selectors}) {
        return this.get(`/projects/${project}/sensors`, {selectors, pageSize: 200});
    }
    getNames({project, selectors}) {
        return this.get(`/projects/${project}/sensors/names`, {selectors});
    }
    getLabels({project, names, selectors}) {
        return this.get(`/projects/${project}/sensors/labels`, {names, selectors});
    }
}

When the baseURL parameter is passed into axios config, all requested pathnames will be appended to it.

const apiInstance = new API({
    config: {
        baseURL: '/api/v2'
    }
});

版本与发布记录

@gravity-ui/axios-wrapper 共发布 5 个版本,首次发布于 2022/11/29;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.1latest2024/09/182 年前
1.5.02024/08/222 年前
1.4.12023/04/133 年前
1.4.02023/04/073 年前
1.3.02022/11/293 年前

关于 @gravity-ui/axios-wrapper 的常见问题

@gravity-ui/axios-wrapper 是什么?

Generic helper class for wrapping axios calls

如何安装 @gravity-ui/axios-wrapper?

在项目目录运行 npm install @gravity-ui/axios-wrapper,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:axios(^1.3.4)、lodash(^4.17.11)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gravity-ui/axios-wrapper 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gravity-ui/axios-wrapper 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gravity-ui/axios-wrapper 可以商用吗?

@gravity-ui/axios-wrapper 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gravity-ui/axios-wrapper 的下载量有多少?

根据 npm 官方下载统计,@gravity-ui/axios-wrapper 在 2026/10/17 – 2026/11/15 这 30 天内被下载 36,603 次,日均约 1,220 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gravity-ui/axios-wrapper 还在维护吗?

最新版本 1.5.1 发布于 2024/09/18(2 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/11/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。