-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Documentation based on Vitepress (#45)
* chore: init docs * docs: add logo & Adjust docs config * docs: document migration and some modifications * docs: move some files * docs: update docs index page details * docs: adjust indev page hero theme style
- Loading branch information
1 parent
4db858b
commit f7c380c
Showing
23 changed files
with
1,909 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,3 +175,6 @@ dist_ssg | |
|
||
# Android | ||
*.keystore | ||
|
||
# Vitepress | ||
cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { defineConfig } from 'vitepress'; | ||
|
||
export default defineConfig({ | ||
title: 'Uni Network', | ||
description: '为 uni-app 打造的基于 Promise 的 HTTP 客户端。', | ||
head: [ | ||
['link', { rel: 'icon', type: 'image/png', href: '/logo.png' }], | ||
['meta', { name: 'og:type', content: 'website' }], | ||
['meta', { name: 'og:locale', content: 'zh-cn' }], | ||
['meta', { name: 'og:site_name', content: 'Uni Network' }], | ||
[ | ||
'meta', | ||
{ | ||
name: 'og:image', | ||
content: | ||
'https://github.com/uni-helper/website/raw/main/.github/assets/uni-helper-banner.png', | ||
}, | ||
], | ||
], | ||
themeConfig: { | ||
logo: { src: '/logo.png', width: 24, height: 24 }, | ||
nav: [ | ||
{ text: '指南', link: '/guide/introduction' }, | ||
{ | ||
text: '更新日志', | ||
link: 'https://github.com/uni-helper/uni-network/tree/main/CHANGELOG.md', | ||
}, | ||
], | ||
|
||
sidebar: [ | ||
{ | ||
text: '指南', | ||
items: [ | ||
{ text: '介绍', link: '/guide/introduction' }, | ||
{ text: '安装', link: '/guide/quick-start' }, | ||
{ text: '示例', link: '/guide/example' }, | ||
], | ||
}, | ||
{ | ||
text: 'Un Api', | ||
items: [ | ||
{ text: 'API', link: '/guide/api-intro' }, | ||
{ text: 'Un 实例', link: '/guide/instance' }, | ||
{ text: '请求配置', link: '/guide/req-config' }, | ||
{ text: '响应结构', link: '/guide/res-schema' }, | ||
{ text: '默认配置', link: '/guide/config-defaults' }, | ||
], | ||
}, | ||
{ | ||
text: '进阶', | ||
items: [ | ||
{ text: '拦截器', link: '/advanced/interceptors' }, | ||
{ text: '错误处理', link: '/advanced/handling-errors' }, | ||
{ text: '取消请求', link: '/advanced/cancellation' }, | ||
{ text: 'TS 支持', link: '/advanced/ts-support' }, | ||
{ text: '高级功能', link: '/advanced/enhancements' }, | ||
{ text: '组合式函数', link: '/advanced/composition-api' }, | ||
], | ||
}, | ||
{ | ||
text: '其它', | ||
items: [ | ||
{ text: '构建', link: '/other/build' }, | ||
{ text: '为什么不是...?', link: '/other/why-not' }, | ||
{ text: '致谢', link: '/other/thank' }, | ||
], | ||
}, | ||
], | ||
|
||
socialLinks: [{ icon: 'github', link: 'https://github.com/uni-helper/uni-network' }], | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# 取消请求 | ||
|
||
## AbortController | ||
|
||
支持使用 [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) 取消请求。要使用 AbortController,请使用 [这个 polyfill](https://github.com/mysticatea/abort-controller)。 | ||
|
||
::: code-group | ||
|
||
```sh [npm] | ||
npm add -D abort-controller@^3.0.0 | ||
``` | ||
|
||
```sh [pnpm] | ||
pnpm add -D abort-controller@^3.0.0 | ||
``` | ||
|
||
```sh [yarn] | ||
yarn add -D abort-controller@^3.0.0 | ||
``` | ||
|
||
::: | ||
|
||
```ts | ||
import AbortController from 'abort-controller/dist/abort-controller'; | ||
// ❌ 错误做法 1 | ||
// import AbortController from 'abort-controller'; | ||
// ❌ 错误做法 2 | ||
// import 'abort-controller/polyfill'; | ||
|
||
const controller = new AbortController(); | ||
|
||
un.get('/foo/bar', { | ||
signal: controller.signal, | ||
}).then(function (response) { | ||
//... | ||
}); | ||
// 取消请求 | ||
controller.abort(); | ||
``` | ||
|
||
## CancelToken | ||
|
||
你也可以使用 `CancelToken`。 | ||
|
||
```ts | ||
const CancelToken = un.CancelToken; | ||
const source = CancelToken.source(); | ||
|
||
un.get('/user/12345', { | ||
cancelToken: source.token, | ||
}).catch(function (error) { | ||
if (un.isUnCancel(error)) { | ||
console.log('Request canceled', error.message); | ||
} else { | ||
// 处理错误 | ||
} | ||
}); | ||
|
||
un.post( | ||
'/user/12345', | ||
{ | ||
name: 'new name', | ||
}, | ||
{ | ||
cancelToken: source.token, | ||
}, | ||
); | ||
// 取消请求(信息是可选的) | ||
source.cancel('Operation canceled by the user.'); | ||
``` | ||
|
||
你也可以通过向 `CancelToken` 构造函数传递一个执行函数来创建一个 `CancelToken` 实例。 | ||
|
||
```ts | ||
const CancelToken = un.CancelToken; | ||
let cancel; | ||
|
||
un.get('/user/12345', { | ||
cancelToken: new CancelToken(function executor(c) { | ||
cancel = c; | ||
}), | ||
}); | ||
|
||
// 取消请求 | ||
cancel(); | ||
``` | ||
|
||
> 注意:你可以用同一个 `CancelToken` / `AbortController` 取消几个请求。 | ||
> 如果在发起请求的时候已经取消请求,那么该请求就会被立即取消,不会真正发起请求。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# 组合式函数 | ||
|
||
如果你还不了解组合式函数,请先阅读 [组合式 API 常见问答](https://cn.vuejs.org/guide/extras/composition-api-faq.html) 和 [组合式函数](https://cn.vuejs.org/guide/reusability/composables.html)。 | ||
|
||
我们使用 [vue-demi](https://github.com/vueuse/vue-demi) 来同时支持 `vue2` 和 `vue3`。请先阅读它的使用说明。 | ||
|
||
你需要从 `@uni-helper/uni-network/composables` 中导入组合式函数。 | ||
|
||
```typescript | ||
import { useUn } from '@uni-helper/uni-network/composables'; | ||
``` | ||
|
||
`useUn` 的用法和 [useAxios](https://vueuse.org/integrations/useaxios/) 几乎完全一致。这里不再赘述。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 高级功能 | ||
|
||
对于缓存、去重、重试的高级功能,建议结合 [@tanstack/query](https://tanstack.com/query/)、[swrv](https://docs-swrv.netlify.app/)、[vue-request](https://www.attojs.com/)、[alova](https://alova.js.org/zh-CN/) 等库使用。 | ||
|
||
如果你不希望引入过多的库导致占用体积过多,你也可以参考以下内容以实现部分高级功能。 | ||
|
||
## 缓存 | ||
|
||
请参考 [Axios 如何缓存请求数据](https://juejin.cn/post/6974902702400602148)。 | ||
|
||
## 去重 | ||
|
||
请参考 [Axios 如何取消重复请求](https://juejin.cn/post/6955610207036801031) 和 [Axios 如何取消重复请求?取消重复请求方法有哪几种?](https://apifox.com/apiskills/axios-repeated-request/)。 | ||
|
||
## 重试 | ||
|
||
请参考 [Axios 如何实现请求重试?](https://juejin.cn/post/6973812686584807432)。 | ||
|
||
## 响应失败不抛出错误 | ||
|
||
::: warning ⚠️ 注意: | ||
这可能不是一个好主意。请慎重考虑。 | ||
::: | ||
|
||
在某些情况下,你可能不希望响应失败抛出错误,这时候可以使用响应拦截器来处理。 | ||
|
||
```typescript | ||
// 添加响应拦截器 | ||
un.interceptors.response.use( | ||
(response) => response, | ||
// 直接返回错误,不再需要使用 catch 来捕获 | ||
// 需要注意返回值可能是 UnError 类型 | ||
(error) => error, | ||
); | ||
``` | ||
|
||
## 无感刷新登录态 | ||
|
||
在某些情况下,你可能希望无感刷新登录态,避免当前登录态过期后用户手动登录。 | ||
|
||
如果你有一个可以使用过期登录态换取新鲜登录态的接口,请参考 [uni-ajax - FAQ - 无感刷新 Token](https://uniajax.ponjs.com/guide/question#%E6%97%A0%E6%84%9F%E5%88%B7%E6%96%B0-token)。 | ||
该部分代码实现略经修改也适用于使用双登录态的认证系统。 | ||
|
||
::: warning ⚠️ 注意: | ||
这类接口并非十分安全。请慎重考虑。 | ||
::: | ||
|
||
如果你正在使用一个使用双登录态的认证系统,请参考 [项目中前端如何实现无感刷新 token!](https://juejin.cn/post/7254572706536734781) 和 [基于 Axios 封装一个完美的双 token 无感刷新](https://juejin.cn/post/7271139265442021391)。 | ||
|
||
## 全局请求加载 | ||
|
||
请参考 [uni-ajax - FAQ - 配置全局请求加载](https://uniajax.ponjs.com/guide/question#%E9%85%8D%E7%BD%AE%E5%85%A8%E5%B1%80%E8%AF%B7%E6%B1%82%E5%8A%A0%E8%BD%BD)。 | ||
|
||
::: warning ⚠️ 注意: | ||
这类做法不适用于局部加载展示。 | ||
::: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 错误处理 | ||
|
||
`uni-network` 默认把每一个返回的状态代码不在 `2xx` 范围内的响应视为错误。 | ||
|
||
```ts | ||
un.get('/user/12345').catch((error) => { | ||
if (error.response) { | ||
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围 | ||
console.log(error.response.data); | ||
console.log(error.response.status); | ||
console.log(error.response.headers); | ||
} else if (error.task) { | ||
// 请求已经成功发起,但没有收到响应 | ||
// `error.task` 是 task 实例 | ||
console.log(error.task); | ||
} else { | ||
// 发送请求时出了点问题 | ||
console.log('Error', error.message); | ||
} | ||
console.log(error.config); | ||
}); | ||
``` | ||
|
||
使用 `validateStatus` 配置选项,可以自定义抛出错误的 HTTP code。 | ||
|
||
```ts | ||
un.get('/user/12345', { | ||
validateStatus: (status) => { | ||
return status < 500; // 处理状态码小于 500 的情况 | ||
}, | ||
}); | ||
``` | ||
|
||
如果你追求语义化,可以使用导出的和挂载的状态码、[statuses](https://github.com/jshttp/statuses)、[http-status-codes](https://github.com/prettymuchbryce/http-status-codes) 或 [node-http-status](https://github.com/adaltas/node-http-status)。 | ||
|
||
```ts | ||
import { HttpStatusCode } from '@uni-helper/uni-network'; | ||
|
||
un.get('/user/12345', { | ||
validateStatus: (status) => { | ||
return status < HttpStatusCode.InternalServerError; // 处理状态码小于 500 的情况 | ||
// return status < un.HttpStatusCode.InternalServerError; // 也可以使用挂载在 un 上的状态码 | ||
}, | ||
}); | ||
``` | ||
|
||
使用 `toJSON` 可以获取更多关于 HTTP 错误的信息。 | ||
|
||
```ts | ||
un.get('/user/12345').catch((error) => { | ||
console.log(error.toJSON()); | ||
}); | ||
``` | ||
|
||
如果需要针对 `UnError` 和非 `UnError` 做处理,可以使用导出的 `isUnError` 方法判断。 | ||
|
||
```ts | ||
import { isUnError } from '@uni-helper/uni-network'; | ||
|
||
un.get('/user/12345').catch((error) => { | ||
if (isUnError(error)) { | ||
/* ... */ | ||
} else { | ||
/* ... */ | ||
} | ||
}); | ||
``` |
Oops, something went wrong.