Skip to content

Commit

Permalink
release v1.0.0-beta.2
Browse files Browse the repository at this point in the history
  • Loading branch information
saqqdy committed Jul 9, 2023
1 parent c09c45a commit ff45b19
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 36 deletions.
114 changes: 83 additions & 31 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,36 +68,88 @@ const http = axiosSeries(instance, {
export default http
```

## Behavior
## 使用表现

## API Reference
serializer 的配置

### options
| 参数 | 描述 | 类型 | 可选值 | 是否必填 | 默认值 |
| -------- | ----------------------------------------------- | ------------------------------------------------------ | ---------- | -------- | ------ |
| unique | make request unique, clear all similar requests | `boolean` | true/false | false | false |
| orderly | resolve results orderly | `boolean` | true/false | false | true |
| onCancel | callback function for cancel requests | `(err: any, config: InternalAxiosRequestConfig): void` | - | false | null |

serializer options
### unique

| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ----------------------------------------------- | ------------------------------------------------------ | ---------- | -------- | ------- |
| unique | make request unique, clear all similar requests | `boolean` | true/false | false | false |
| orderly | resolve results orderly | `boolean` | true/false | false | true |
| onCancel | callback function for cancel requests | `(err: any, config: InternalAxiosRequestConfig): void` | - | false | null |
当向同一个接口(url 一致但 data 可以不一样)同时(或者间隔很短时间)发起多次请求时,可能用户只需要最后一次的请求结果,当 `unique` 设置为 `true` 时,前面的请求会被取消

> 神奇的是:当向同一个接口同时发起多次请求时,axiosSerios 并不是死板地等待上一个接口返回之后才开始请求。所有的请求都是同时发起的,所以 axiosSerios 对于**网页性能是没有损失的**
- 版本: `1.0.0`

- 示例:

1. 向 /test/api/1 (data 可以不一样)同时(或者间隔很短时间)发起 3 次请求
. 设置 `unique``true`

```ts
// 请求 1
http({
url: '/test/api/1',
data: { id: 1 }
})
// 请求 2
http({
url: '/test/api/1',
data: { id: 2 }
})

// 请求1会被取消
```

### orderly

当同时(或者间隔很短时间)向同一个接口(url 一致但 data 可以不一样)请求多次,由于网络原因无法保证先执行的请求先返回结果,这个 `orderly` 参数就是用来解决这个问题的。当`orderly`设置为 true 时,先请求的一定会先于后请求的先返回结果

- 版本: `1.0.0`

- 示例:

1. 向 /test/api/1 (data 可以不一样)同时(或者间隔很短时间)发起 3 次请求
. 设置 `orderly` 为 true

```ts
// 请求 1
http({
url: '/test/api/1',
data: { id: 1 }
})
// 请求 2
http({
url: '/test/api/1',
data: { id: 2 }
})

// 请求1一定会先于请求2返回结果
```

## API 参考

### axiosSeries

axios serializer wrapper function
axios serializer 包装器

- Since: `1.0.0`
- 版本: `1.0.0`

- Arguments:
- 参数:

| Parameters | Description | Type | Optional | Required | Default |
| ---------- | ----------------------- | ------------------- | ------------------- | -------- | ------- |
| instance | axios or axios instance | `AxiosInstance` | axios/axiosInstance | true | - |
| options | serializer options | `SerializerOptions` | - | false | - |
| 参数 | 描述 | 类型 | 可选值 | 是否必填 | 默认值 |
| -------- | ----------------------- | ------------------- | ------------------- | -------- | ------ |
| instance | axios or axios instance | `AxiosInstance` | axios/axiosInstance | true | - |
| options | serializer options | `SerializerOptions` | - | false | - |

- Returns: new axios instance with serializer
- 返回: new axios instance with serializer

- Example:
- 示例:

```ts
const http = axiosSeries(instance, {
Expand All @@ -106,7 +158,7 @@ const http = axiosSeries(instance, {
})
```

- Types:
- 类型:

```ts
function axiosWithSeries<T = any, R = AxiosResponse<T>, D = any>(
Expand All @@ -120,47 +172,47 @@ function axiosWithSeries<T = any, R = AxiosResponse<T>, D = any>(

### axiosSeries.clear

clear all series
清理所有的请求队列

- Since: `1.0.0`
- 版本: `1.0.0`

- Arguments: `none`
- 参数: `none`

- Returns: 'none'
- 返回: 'none'

- Example:
- 示例:

```ts
const http = axiosSeries(instance, {})
http.clear()
```

- Types:
- 类型:

```ts
type clear = () => void
```

### axiosSeries.series

all waiting series
所有的请求队列

- Since: `1.0.0`
- 版本: `1.0.0`

- Arguments: `none`
- 参数: `none`

- Returns: 'WaitingList'
- 返回: 'WaitingList'

- Example:
- 示例:

```ts
const http = axiosSeries(instance, {})
console.log(http.series) // []
```

- Types:
- 类型:

```ts
declare interface Series {
Expand Down
58 changes: 54 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ export default http

## Behavior

## API Reference

### options

serializer options

| Parameters | Description | Type | Optional | Required | Default |
Expand All @@ -82,6 +78,60 @@ serializer options
| orderly | resolve results orderly | `boolean` | true/false | false | true |
| onCancel | callback function for cancel requests | `(err: any, config: InternalAxiosRequestConfig): void` | - | false | null |

### unique

When multiple requests are made to the same interface (url is the same but data can be different) at the same time (or at short intervals), the user may only need the result of the last request, and when `unique` is set to `true`, the previous request will be cancelled.

> Here's the magic: when multiple requests are made to the same interface at the same time, axiosSerios does not wait rigidly for the previous interface to return before starting the request. All requests are made at the same time, so axiosSerios has **no loss in web performance**
- Since: `1.0.0`

- Example:

Make 2 requests to /test/api/1 (data can be different) at the same time (or at very short intervals). set `unique` to `true`

```ts
// request 1
http({
url: '/test/api/1',
data: { id: 1 }
})
// request 2
http({
url: '/test/api/1',
data: { id: 2 }
})

// request 1 will be cancelled
```

### orderly

When multiple requests are launched to the same interface (url is the same but data can be different) at the same time (or a short interval), the first request executed cannot be guaranteed to return the result first due to network reasons, and this `orderly` parameter is used to solve this problem. When `orderly` is set to true, the first request will definitely return the result before the second request.

- Since: `1.0.0`

- Example:

Make 2 requests to xxx (data can be different) at the same time (or at very short intervals). set `orderly` to `true`

```ts
// request 1
http({
url: '/test/api/1',
data: { id: 1 }
})
// request 2
http({
url: '/test/api/1',
data: { id: 2 }
})

// request 1 will definitely return the result before the request 2
```

## API Reference

### axiosSeries

axios serializer wrapper function
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "axios-series",
"description": "A serializer for axios",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.2",
"packageManager": "[email protected]",
"main": "dist/index.cjs.js",
"module": "dist/index.esm-bundler.js",
Expand Down

0 comments on commit ff45b19

Please sign in to comment.