# share-fetch

> 并发请求控制库

Latest version **1.0.4** (published 2021-06-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install share-fetch
pnpm add share-fetch
yarn add share-fetch
bun add share-fetch
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2021-06-03 |
| First published | 2020-04-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | 疯狂紫萧 |
| Maintainers | cuo9958 |
| Keywords | fetch |

## Links

- npm: https://www.npmjs.com/package/share-fetch
- Repository: https://github.com/cuo9958/share-fetch
- Homepage: https://github.com/cuo9958/share-fetch#readme
- Issues: https://github.com/cuo9958/share-fetch/issues
- npm.io page: https://npm.io/package/share-fetch

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2021-06-03
- 1.0.3 — 2020-04-24
- 1.0.2 — 2020-04-23
- 1.0.1 — 2020-04-17
- 1.0.0 — 2020-04-17

## README

# share-fetch 并发请求库

> 在现代前端开发环境下，多个小组会负责多个模块的开发。
> 假如同时发起同样的请求，会导致同一个接口多次调用，非常浪费系统的资源。
> 该方法库做的事情就是将接口合并在一个请求中，返回结果之后

## 使用方式

```javascript
const req2 = new ShareFetch();
req2.start("/demo/test.json", { method: "GET", cache: "default" })
    .then((data) => {
        console.log("data1", data);
    })
    .catch((err) => {
        console.log("收到错误", err);
    });
req2.start("/demo/test.json", { method: "GET", cache: "default" })
    .then((data) => {
        console.log("data2", data);
    })
    .catch((err) => {
        console.log("收到错误2", err);
    });
```

## api

### 初始化

初始化的时候允许自定义请求方法，方便使用其他的请求库。

```javascript
function fetchData() {
    //其他自定义的请求方法
}
new ShareFetch(fetchData);
```

### 替换默认的请求方法

```javascript
function fetchData() {
    //其他自定义的请求方法
}
const req = new ShareFetch();
req.setFetch(fetchData);
```

### 开始请求

```javascript
const req = new ShareFetch();
await req.start("/test.json");

await req.start("/test.json", {});
```

### 请求参数

默认使用 fetch 方法，参数类型:`RequestInit`

1. `method?: string;`请求方法:'GET','POST','PUT','DELETE','HEAD'
2. `body?: BodyInit | null;`POST 的参数，`Blob | BufferSource | FormData | URLSearchParams | ReadableStream<Uint8Array> | string;`
3. `headers?: HeadersInit;`头信息

---
_Source: https://npm.io/package/share-fetch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
