Skip to content

Commit

Permalink
feat: 支持具名导入工具函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zjxxxxxxxxx committed Apr 18, 2023
1 parent a374647 commit d714ed2
Show file tree
Hide file tree
Showing 23 changed files with 409 additions and 279 deletions.
29 changes: 16 additions & 13 deletions docs/pages/advanced/adapter.md
Expand Up @@ -124,7 +124,7 @@ axios.defaults.adapter = (config) => {
// 状态码
status: response.statusCode,

// 状态文本
// 状态文本,选填,不传默认 'OK'
statusText: 'OK',

// 响应头
Expand All @@ -139,16 +139,17 @@ axios.defaults.adapter = (config) => {
},
fail: (error) => {
config.fail({
// 状态码
// 状态码,选填,不传默认 400
status: 400,

// 状态文本
// 状态文本,选填,不传默认 'Fail Adapter'
statusText: 'Fail Adapter',

// 响应头
// 响应头,选填,不传默认 {}
headers: {},

// 响应数据
// 平台 api 错误 error 通常是一个包含 errMsg 属性的对象
data: error,
});
},
Expand All @@ -172,28 +173,29 @@ axios.defaults.adapter = (config) => {
// 状态码
status: response.statusCode,

// 状态文本
// 状态文本,选填,不传默认 'OK'
statusText: 'OK',

// 响应头
headers: response.header ?? {},
headers: response.header,

// 响应数据
data: response.data,
});
},
fail: (error) => {
config.fail({
// 状态码
// 状态码,选填,不传默认 400
status: 400,

// 状态文本
// 状态文本,选填,不传默认 'Fail Adapter'
statusText: 'Fail Adapter',

// 响应头
// 响应头,选填,不传默认 {}
headers: {},

// 响应数据
// 平台 api 错误 error 通常是一个包含 errMsg 属性的对象
data: error,
});
},
Expand All @@ -210,7 +212,7 @@ axios.defaults.adapter = (config) => {
// 状态码
status: response.statusCode,

// 状态文本
// 状态文本,选填,不传默认 'OK'
statusText: 'OK',

// 响应头
Expand All @@ -225,16 +227,17 @@ axios.defaults.adapter = (config) => {
},
fail: (error) => {
config.fail({
// 状态码
// 状态码,选填,不传默认 400
status: 400,

// 状态文本
// 状态文本,选填,不传默认 'Fail Adapter'
statusText: 'Fail Adapter',

// 响应头
// 响应头,选填,不传默认 {}
headers: {},

// 响应数据
// 平台 api 错误 error 通常是一个包含 errMsg 属性的对象
data: error,
});
},
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/basics/defaults.md
Expand Up @@ -18,7 +18,7 @@ title: 默认配置
{
// 适配器,在支持的平台中有值。
// 对于不支持平台而言,此值始终为 undefined,需要您手动适配。
adapter: getAdapterDefault(),
adapter: getDefaultAdapter(),

// 请求头
headers: {
Expand Down
68 changes: 60 additions & 8 deletions docs/pages/guide/quick-start.md
Expand Up @@ -24,7 +24,7 @@ $ pnpm install -D axios-miniprogram

原生小程序也可以直接[下载源码包](https://github.com/zjx0905/axios-miniprogram/releases),但是这样是失去类型提示和 `sourceMap` 定位功能。

在条件允许的情况下建议优先使用包管理工具安装的方式,而不是使用下载源码包的方式。
建议在条件允许的情况下优先使用包管理工具安装的方式,而不是使用下载源码包的方式。

## 引用

Expand All @@ -46,6 +46,65 @@ axios('test');

## 使用

### 引用

可以导入需要使用的功能。

```ts
import axios, {
// 取消令牌
CancelToken,

// 判断取消请求错误
isCancel,

// 原始 Axios 类
Axios,

// 判断请求响应错误
isAxiosError,

// 创建平台适配器
createAdapter,
} from 'axios-miniprogram';
```

axios 上也有同样的功能。

```ts
import axios from 'axios-miniprogram';

// axios 同样也有这些功能
const {
// 取消令牌
CancelToken,

// 判断取消请求错误
isCancel,

// 原始 Axios 类
Axios,

// 判断请求响应错误
isAxiosError,

// 创建平台适配器
createAdapter,
} = axios;
```

axios 上还有一些额外的功能。

```ts
const {
// 创建实例
create,

// 获取已处理的 URL
getUri,
} = axios;
```

### `axios(url, config?)`

可以通过把 `url``config` 传递给 `axios` 来发送请求。
Expand Down Expand Up @@ -119,10 +178,3 @@ axios({
- [axios.delete(url, params?, config?)](/method/DELETE)
- [axios.trace(url, config?)](/method/TRACE)
- [axios.connect(url, config?)](/method/CONNECT)

还提供了一系列工具方法。

- `axios.create(defaults?)` 创建新的 `axios` 实例
- `axios.createAdapter(platform)` 创建平台适配器
- `axios.isCancel(error)` 判断异常是否来自取消请求
- `axios.isAxiosError(error)` 判断异常是否来自请求响应
2 changes: 1 addition & 1 deletion rollup.config.ts
Expand Up @@ -26,7 +26,7 @@ function buildConfig(format: ModuleFormat | 'dts'): RollupOptions {
file: resolveOutput(format, isDts),
format: isDts ? 'es' : format,
name: pkg.name,
exports: 'default',
exports: 'named',
indent: false,
sourcemap: isDts ? false : sourceMap,
};
Expand Down
11 changes: 6 additions & 5 deletions scripts/docs.deploy.ts
Expand Up @@ -4,9 +4,6 @@ import { exec } from './utils';
main();

function main() {
exec('pnpm docs:build');

console.log('');
consola.info('Clean');
const exist = exec('git branch --list docs', {
stdio: 'pipe',
Expand All @@ -18,9 +15,13 @@ function main() {
}
console.log('');

consola.info('Create docs\n');
consola.info('Check build');
exec('pnpm docs:build');
console.log('');

consola.info('Git branch docs\n');
exec('git branch docs');

consola.info('Push docs\n');
consola.info('Git push docs\n');
exec('git push origin docs -f');
}
2 changes: 1 addition & 1 deletion src/adapter.ts
Expand Up @@ -170,7 +170,7 @@ export interface AxiosAdapter {
(config: AxiosAdapterRequestConfig): AxiosAdapterTask;
}

export function getAdapterDefault() {
export function getDefaultAdapter() {
const platform = revisePlatformApiNames(getPlatform());

function getPlatform() {
Expand Down
5 changes: 3 additions & 2 deletions src/axios.ts
Expand Up @@ -65,11 +65,12 @@ function createInstance(defaults: AxiosRequestConfig) {

const axios = createInstance(defaults) as AxiosStatic;

axios.Axios = Axios;
axios.CancelToken = CancelToken;
axios.create = function create(defaults) {
return createInstance(mergeConfig(axios.defaults, defaults));
};

axios.Axios = Axios;
axios.CancelToken = CancelToken;
axios.createAdapter = createAdapter;
axios.isCancel = isCancel;
axios.isAxiosError = isAxiosError;
Expand Down
2 changes: 0 additions & 2 deletions src/core/Axios.ts
Expand Up @@ -6,9 +6,7 @@ import {
AxiosAdapter,
AxiosAdapterRequestMethod,
AxiosAdapterTask,
AxiosAdapterResponse,
AxiosAdapterRequestConfig,
AxiosAdapterResponseError,
AxiosAdapterResponseData,
} from '../adapter';
import InterceptorManager, { Interceptor } from './InterceptorManager';
Expand Down

0 comments on commit d714ed2

Please sign in to comment.