From f1c0f348be84d1d1da3dd7c0b0e8b57d906e2bcf Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Wed, 5 Apr 2023 15:24:27 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E4=B8=8A=E4=BC=A0=E5=92=8C=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E4=BD=BF=E7=94=A8=E4=BB=8B=E7=BB=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/.vitepress/config.ts | 3 +++ docs/api/is-axios-error.md | 21 +++++++++++++++++++++ docs/config/download.md | 27 +++++++++++++++++++++++++++ docs/config/upload.md | 25 +++++++++++++++++++++++++ src/adapter.ts | 25 ++++++++++--------------- 5 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 docs/api/is-axios-error.md create mode 100644 docs/config/download.md create mode 100644 docs/config/upload.md diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 28d9231..4487a8e 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -32,6 +32,8 @@ export default defineConfig({ { text: 'transformRequest', link: '/config/transform-request' }, { text: 'transformResponse', link: '/config/transform-response' }, { text: 'errorHandler', link: '/config/error-handler' }, + { text: 'upload', link: '/config/upload' }, + { text: 'download', link: '/config/download' }, { text: 'adapter', link: '/config/adapter' }, ], }, @@ -41,6 +43,7 @@ export default defineConfig({ { text: 'interceptors', link: '/api/interceptors' }, { text: 'CancelToken', link: '/api/cancel-token' }, { text: 'isCancel', link: '/api/is-cancel' }, + { text: 'isAxiosError', link: '/api/is-axios-error' }, { text: 'getUri', link: '/api/get-uri' }, { text: 'create', link: '/api/create' }, { text: 'Axios', link: '/api/axios' }, diff --git a/docs/api/is-axios-error.md b/docs/api/is-axios-error.md new file mode 100644 index 0000000..8f2d859 --- /dev/null +++ b/docs/api/is-axios-error.md @@ -0,0 +1,21 @@ +# isAxiosError + +## `axios.isAxiosError` 是否是 AxiosError + +可以判断当前错误是否来自请求响应,而不是语法错误或者用户主动抛出的错误 + +```typescript +axios('/user').catch((error) => { + if (axios.isAxiosError(error)) { + // 错误是否来自原生接口的 fail 回调 + // 如果错误来自对状态码的判断,这种情况是没有这个属性的 + error.isFail; + // 请求配置 + error.config; + // 请求任务 + error.request; + // 响应体 + error.response; + } +}); +``` diff --git a/docs/config/download.md b/docs/config/download.md new file mode 100644 index 0000000..a630cd3 --- /dev/null +++ b/docs/config/download.md @@ -0,0 +1,27 @@ +# download + +## 发送下载请求 + +可以从服务端下载文件本地,只有 get 请求才生效 + +```typescript +axios + .get( + '/file', + { + // 指定文件下载后存储的路径 (本地路径),选填 + filePath: '', + // 指定文件下载后存储的名称,选填 + fileName: '', + }, + { + download: true, + }, + ) + .then((response) => { + // 用户文件路径 (本地路径)。传入 filePath 时会返回,跟传入的 filePath 一致 + response.data.filePath; + // 用户文件路径 (本地临时路径)。 + response.data.tempFilePath; + }); +``` diff --git a/docs/config/upload.md b/docs/config/upload.md new file mode 100644 index 0000000..9df4d51 --- /dev/null +++ b/docs/config/upload.md @@ -0,0 +1,25 @@ +# upload + +## 发送上传请求 + +可以上传文件到服务端,只有 post 请求才生效 + +```typescript +axios.post( + '/file', + { + // 文件名称,必填 + fileName: 'image.png', + // 文件路径,必填 + filePath: '/file/image.png', + // 文件类型,选填 + fileType: 'image' | 'video' | 'audio'; + // 可以传入更多自定义字段,这些自定义字段最终会以 formData 的形式发送给服务端 (前提是平台支持) + custom1: 'name', + custom2: 'id' + }, + { + upload: true, + }, +); +``` diff --git a/src/adapter.ts b/src/adapter.ts index 7b01f0d..c6b95cc 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -274,22 +274,17 @@ export function createAdapter(platform: AxiosPlatform): AxiosAdapter { } function injectDownloadData(response: AnyObject): void { - response.data = response.data || {}; + response.data = { + filePath: response.filePath, + tempFilePath: + response.tempFilePath || + // response.apFilePath 为支付宝小程序基础库小于 2.7.23 的特有属性。 + response.apFilePath, + }; - if (response.tempFilePath) { - response.data.tempFilePath = response.tempFilePath; - delete response.tempFilePath; - } - - if (response.apFilePath) { - response.data.tempFilePath = response.apFilePath; - delete response.apFilePath; - } - - if (response.filePath) { - response.data.filePath = response.filePath; - delete response.filePath; - } + if (response.tempFilePath) delete response.tempFilePath; + if (response.apFilePath) delete response.apFilePath; + if (response.filePath) delete response.filePath; } return adapter;