@uni-helper/axios-adapter 1.18.0 → 1.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,20 +6,32 @@
6
6
 
7
7
  [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/uni-helper/axios-adapter)
8
8
 
9
- ## 安装
9
+ ## 目录
10
10
 
11
- ```
12
- pnpm i @uni-helper/axios-adapter axios
13
- ```
11
+ - [简介](#简介)
12
+ - [平台兼容性](#平台兼容性)
13
+ - [安装](#安装)
14
+ - [快速开始](#快速开始)
15
+ - [使用](#使用)
16
+ - [创建自定义实例](#创建自定义实例)
17
+ - [与 useAxios 一起使用](#与-useaxios-一起使用)
18
+ - [上传和下载](#上传和下载)
19
+ - [小程序](#小程序)
20
+ - [API 参考](#api-参考)
21
+ - [版本策略](#版本策略)
22
+ - [参与贡献](#参与贡献)
23
+ - [许可证](#许可证)
14
24
 
15
- ## 使用
25
+ ## 简介
26
+
27
+ `@uni-helper/axios-adapter` 封装了 uni-app 的 `uni.request`、`uni.downloadFile`、`uni.uploadFile` 等接口,让开发者可以在 uni-app 的各类平台上继续使用熟悉的 axios 编程范式。适配器同时为 TypeScript 项目提供全局类型扩展,补充 `upload`、`download` 以及 uni-app 专有请求参数的类型定义。
16
28
 
17
- <details>
18
- <summary>点击查看平台兼容性</summary>
29
+ ## 平台兼容性
19
30
 
20
- | Vue2 | Vue3 |
21
- | ---- | ---- |
22
- | | √ |
31
+ | Vue 版本 | 支持情况 |
32
+ | -------- | -------- |
33
+ | Vue 2 | √ |
34
+ | Vue 3 | √ |
23
35
 
24
36
  | App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ 小程序 |
25
37
  | ---------------------------------------- | ------ | ---------- | ------------ | ---------- | ---------- | --------- |
@@ -33,9 +45,15 @@ pnpm i @uni-helper/axios-adapter axios
33
45
  | --------- | --------------- | ------------------- | ------------------ | ------ | --- | ---- | ------- | --------- |
34
46
  | √ | √ | √ | √ | √ | √ | √ | √ | √ |
35
47
 
36
- </details>
48
+ ## 安装
37
49
 
38
- ### 基本用法
50
+ ```bash
51
+ pnpm i @uni-helper/axios-adapter axios
52
+ ```
53
+
54
+ 适配器与 axios 的版本需要保持主版本号和次版本号一致。当前适配器版本为 `1.18.0`,推荐搭配 `axios@^1.18.0` 使用。
55
+
56
+ ## 快速开始
39
57
 
40
58
  ```ts
41
59
  import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'
@@ -44,7 +62,11 @@ import axios from 'axios'
44
62
  axios.defaults.adapter = createUniAppAxiosAdapter()
45
63
  ```
46
64
 
47
- 或者创建自定义实例
65
+ 配置完成后,即可像往常一样使用 `axios.get`、`axios.post` 等方法发起请求。
66
+
67
+ ## 使用
68
+
69
+ ### 创建自定义实例
48
70
 
49
71
  ```ts
50
72
  import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'
@@ -86,13 +108,12 @@ axios.request({
86
108
 
87
109
  ### 小程序
88
110
 
89
- 自 axios 1.4.0 开始,axios 内部已经处理了小程序的兼容性问题
111
+ 自 axios 1.4.0 开始,axios 已对小程序环境做了兼容处理。若仍需在小程序中使用 `FormData` 和 `Blob`,可安装对应的 polyfill 并启用构建插件:
90
112
 
91
- <details>
92
- <summary>我要使用 `FormData` `Blob` 对象</summary>
113
+ ```bash
114
+ pnpm add miniprogram-formdata miniprogram-blob
115
+ ```
93
116
 
94
- 小程序没有 `FormData` 和 `Blob` 对象,
95
- 使用 `pnpm add miniprogram-formdata miniprogram-blob` 来安装对应的 polyfill,然后使用插件
96
117
  ```ts
97
118
  // vite.config.js
98
119
  import uniAxiosAdapter from '@uni-helper/axios-adapter/vite'
@@ -104,12 +125,33 @@ export default {
104
125
  }
105
126
  ```
106
127
 
107
- 如果你使用的是 Vue CLI,改用 `@uni-helper/axios-adapter/webpack` 即可
128
+ 如果你使用的是 Vue CLI,改用 `@uni-helper/axios-adapter/webpack` 即可。
129
+
130
+ ## API 参考
131
+
132
+ ### `createUniAppAxiosAdapter(options?: UserOptions): AxiosAdapter`
133
+
134
+ 创建适配器实例,可直接赋值给 `axios.defaults.adapter` 或 `axios.create({ adapter })`。
135
+
136
+ - `options`:可选配置对象,当前版本支持扩展 uni-app 请求参数(参见 `globalExtensions.ts` 中的类型定义)。
137
+ - 返回值:标准的 `AxiosAdapter` 函数。
138
+
139
+ ### `axios.upload<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>`
140
+
141
+ 以 `uni.uploadFile` 发送上传请求,`data` 通常为 `FormData` 实例。
142
+
143
+ ### `axios.download<T>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>`
144
+
145
+ 以 `uni.downloadFile` 发起下载请求,返回的 `response.data` 默认为文件临时路径或 `Buffer`(取决于运行环境)。
146
+
147
+ ## 版本策略
148
+
149
+ 自 1.4.0 开始,请始终保持主版本号和次版本号与 axios 一致。例如当你安装了 1.5.1 版本时,你可以安装 axios 的 1.5.x 版本。这样做的目的是确保适配器始终支持最新的 axios 特性。
108
150
 
109
- </details>
151
+ ## 参与贡献
110
152
 
111
- ### 版本
153
+ 欢迎通过 Issue 或 Pull Request 参与改进本项目。开始前请阅读 [CONTRIBUTING.md](./CONTRIBUTING.md),了解本地开发、测试、提交规范与架构说明。
112
154
 
113
- 1.4.0 开始,请始终保持主版本号和次版本号与 axios 一致。例如当你安装了 1.5.1 版本时,你可以安装 axios 的 1.5.x 版本。
155
+ ## 许可证
114
156
 
115
- 这样做的目的是始终支持最新的 axios
157
+ [MIT](https://github.com/uni-helper/axios-adapter/blob/main/LICENSE)
package/dist/index.cjs CHANGED
@@ -70,7 +70,8 @@ function progressEventReducer(listener, isDownloadStream) {
70
70
  bytes: progressBytes,
71
71
  rate: rate || void 0,
72
72
  estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
73
- event: result
73
+ event: result,
74
+ lengthComputable: true
74
75
  };
75
76
  data[isDownloadStream ? "download" : "upload"] = true;
76
77
  listener(data);
@@ -137,7 +138,7 @@ var OnCanceled = class {
137
138
  if (this.config.cancelToken || this.config.signal) {
138
139
  this.onCanceled = (cancel) => {
139
140
  if (!task) return;
140
- reject(!cancel || cancel.type ? new axios.CanceledError(void 0, void 0, this.config, task) : cancel);
141
+ reject(!cancel || cancel.type ? new axios.CanceledError(void 0, this.config, task) : cancel);
141
142
  task.abort();
142
143
  task = null;
143
144
  };
package/dist/index.d.cts CHANGED
@@ -1,6 +1,9 @@
1
- import { t as UserOptions } from "./types-NHxcq8Qp.cjs";
2
1
  import { AxiosAdapter } from "axios";
3
2
 
3
+ //#region src/types.d.ts
4
+ interface Options {}
5
+ interface UserOptions extends Partial<Options> {}
6
+ //#endregion
4
7
  //#region src/globalExtensions.d.ts
5
8
  declare module 'axios' {
6
9
  interface AxiosRequestConfig extends Omit<UniApp.RequestOptions, 'success' | 'fail' | 'complete' | 'header'>, Omit<UniApp.UploadFileOption, 'success' | 'fail' | 'complete' | 'header' | 'formData'>, Omit<UniApp.DownloadFileOption, 'success' | 'fail' | 'complete' | 'header'>, Partial<Pick<UniApp.RequestTask, 'onHeadersReceived'>> {}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
- import { t as UserOptions } from "./types-NHxcq8Qp.js";
2
1
  import { AxiosAdapter } from "axios";
3
2
 
3
+ //#region src/types.d.ts
4
+ interface Options {}
5
+ interface UserOptions extends Partial<Options> {}
6
+ //#endregion
4
7
  //#region src/globalExtensions.d.ts
5
8
  declare module 'axios' {
6
9
  interface AxiosRequestConfig extends Omit<UniApp.RequestOptions, 'success' | 'fail' | 'complete' | 'header'>, Omit<UniApp.UploadFileOption, 'success' | 'fail' | 'complete' | 'header' | 'formData'>, Omit<UniApp.DownloadFileOption, 'success' | 'fail' | 'complete' | 'header'>, Partial<Pick<UniApp.RequestTask, 'onHeadersReceived'>> {}
package/dist/index.js CHANGED
@@ -64,7 +64,8 @@ function progressEventReducer(listener, isDownloadStream) {
64
64
  bytes: progressBytes,
65
65
  rate: rate || void 0,
66
66
  estimated: rate && total && inRange ? (total - loaded) / rate : void 0,
67
- event: result
67
+ event: result,
68
+ lengthComputable: true
68
69
  };
69
70
  data[isDownloadStream ? "download" : "upload"] = true;
70
71
  listener(data);
@@ -131,7 +132,7 @@ var OnCanceled = class {
131
132
  if (this.config.cancelToken || this.config.signal) {
132
133
  this.onCanceled = (cancel) => {
133
134
  if (!task) return;
134
- reject(!cancel || cancel.type ? new CanceledError(void 0, void 0, this.config, task) : cancel);
135
+ reject(!cancel || cancel.type ? new CanceledError(void 0, this.config, task) : cancel);
135
136
  task.abort();
136
137
  task = null;
137
138
  };
@@ -3,7 +3,7 @@ let node_process = require("node:process");
3
3
  node_process = require_rolldown_runtime.__toESM(node_process, 1);
4
4
  let local_pkg = require("local-pkg");
5
5
  //#region src/unplugin.ts
6
- const unplugin$1 = (0, require("unplugin").createUnplugin)((_options = {}) => {
6
+ const unplugin$1 = (0, require("unplugin").createUnplugin)(() => {
7
7
  const hasFormDataPolyfill = (0, local_pkg.isPackageExists)("miniprogram-formdata");
8
8
  const hasBlobPolyfill = (0, local_pkg.isPackageExists)("miniprogram-blob");
9
9
  return {
@@ -2,7 +2,7 @@ import process from "node:process";
2
2
  import { isPackageExists } from "local-pkg";
3
3
  import { createUnplugin } from "unplugin";
4
4
  //#region src/unplugin.ts
5
- const unplugin = createUnplugin((_options = {}) => {
5
+ const unplugin = createUnplugin(() => {
6
6
  const hasFormDataPolyfill = isPackageExists("miniprogram-formdata");
7
7
  const hasBlobPolyfill = isPackageExists("miniprogram-blob");
8
8
  return {
package/dist/vite.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/vite.ts
2
- var vite_default = require("./unplugin-Cgwh-Njz.cjs").unplugin.vite;
2
+ var vite_default = require("./unplugin-CbkCxx2I.cjs").unplugin.vite;
3
3
  //#endregion
4
4
  module.exports = vite_default;
package/dist/vite.d.cts CHANGED
@@ -1,5 +1,3 @@
1
- import { n as UserUnpluginOptions } from "./types-NHxcq8Qp.cjs";
2
-
3
1
  //#region src/vite.d.ts
4
- declare const _default: (options: UserUnpluginOptions) => import("vite").Plugin<any> | import("vite").Plugin<any>[];
2
+ declare const _default: (options?: unknown) => import("vite").Plugin<any> | import("vite").Plugin<any>[];
5
3
  export = _default;
package/dist/vite.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- import { n as UserUnpluginOptions } from "./types-NHxcq8Qp.js";
2
-
3
1
  //#region src/vite.d.ts
4
- declare const _default: (options: UserUnpluginOptions) => import("vite").Plugin<any> | import("vite").Plugin<any>[];
2
+ declare const _default: (options?: unknown) => import("vite").Plugin<any> | import("vite").Plugin<any>[];
5
3
  //#endregion
6
4
  export { _default as default };
package/dist/vite.js CHANGED
@@ -1,4 +1,4 @@
1
- import { t as unplugin } from "./unplugin-Br8JKdJO.js";
1
+ import { t as unplugin } from "./unplugin-DJxh_3Ax.js";
2
2
  //#region src/vite.ts
3
3
  var vite_default = unplugin.vite;
4
4
  //#endregion
package/dist/webpack.cjs CHANGED
@@ -1,4 +1,4 @@
1
1
  //#region src/webpack.ts
2
- var webpack_default = require("./unplugin-Cgwh-Njz.cjs").unplugin.webpack;
2
+ var webpack_default = require("./unplugin-CbkCxx2I.cjs").unplugin.webpack;
3
3
  //#endregion
4
4
  module.exports = webpack_default;
@@ -1,5 +1,3 @@
1
- import { n as UserUnpluginOptions } from "./types-NHxcq8Qp.cjs";
2
-
3
1
  //#region src/webpack.d.ts
4
- declare const _default: (options: UserUnpluginOptions) => WebpackPluginInstance;
2
+ declare const _default: (options?: unknown) => WebpackPluginInstance;
5
3
  export = _default;
package/dist/webpack.d.ts CHANGED
@@ -1,6 +1,4 @@
1
- import { n as UserUnpluginOptions } from "./types-NHxcq8Qp.js";
2
-
3
1
  //#region src/webpack.d.ts
4
- declare const _default: (options: UserUnpluginOptions) => WebpackPluginInstance;
2
+ declare const _default: (options?: unknown) => WebpackPluginInstance;
5
3
  //#endregion
6
4
  export { _default as default };
package/dist/webpack.js CHANGED
@@ -1,4 +1,4 @@
1
- import { t as unplugin } from "./unplugin-Br8JKdJO.js";
1
+ import { t as unplugin } from "./unplugin-DJxh_3Ax.js";
2
2
  //#region src/webpack.ts
3
3
  var webpack_default = unplugin.webpack;
4
4
  //#endregion
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@uni-helper/axios-adapter",
3
3
  "type": "module",
4
- "version": "1.18.0",
4
+ "version": "1.18.1",
5
5
  "description": "The Axios adapter for uniapp",
6
6
  "author": {
7
7
  "name": "kejunmao",
8
8
  "email": "kejun1997@gmail.com"
9
9
  },
10
10
  "license": "MIT",
11
+ "funding": "https://afdian.com/a/kejun",
11
12
  "homepage": "https://github.com/uni-helper/axios-adapter",
12
13
  "repository": {
13
14
  "type": "git",
@@ -70,12 +71,12 @@
70
71
  "@antfu/eslint-config": "^9.0.0",
71
72
  "@dcloudio/types": "^3.4.31",
72
73
  "@types/node": "^24.13.2",
73
- "axios": "^1.18.0",
74
+ "axios": "^1.18.1",
74
75
  "bumpp": "^11.1.0",
75
76
  "eslint": "^10.5.0",
76
77
  "msw": "^2.14.6",
77
- "tsdown": "^0.22.2",
78
- "typescript": "^5.9.3",
78
+ "tsdown": "^0.22.3",
79
+ "typescript": "^6.0.3",
79
80
  "vite": "^5.4.21",
80
81
  "vitest": "^3.2.6"
81
82
  },
@@ -1,7 +0,0 @@
1
- //#region src/types.d.ts
2
- interface Options {}
3
- interface UserOptions extends Partial<Options> {}
4
- interface UnpluginOptions {}
5
- interface UserUnpluginOptions extends Partial<UnpluginOptions> {}
6
- //#endregion
7
- export { UserUnpluginOptions as n, UserOptions as t };
@@ -1,7 +0,0 @@
1
- //#region src/types.d.ts
2
- interface Options {}
3
- interface UserOptions extends Partial<Options> {}
4
- interface UnpluginOptions {}
5
- interface UserUnpluginOptions extends Partial<UnpluginOptions> {}
6
- //#endregion
7
- export { UserUnpluginOptions as n, UserOptions as t };