# anti-replay-ajax-util

> 基于umi-request, 在ajax请求层面, 实现请求防重.

Latest version **0.1.3** (published 2021-03-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install anti-replay-ajax-util
pnpm add anti-replay-ajax-util
yarn add anti-replay-ajax-util
bun add anti-replay-ajax-util
```

## Health

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

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

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2021-03-10 |
| First published | 2021-03-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 98.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | 潘志勇 |
| Maintainers | sd4015700 |

## Links

- npm: https://www.npmjs.com/package/anti-replay-ajax-util
- Repository: https://gitee.com/free_pan/anti-replay-ajax-util
- Homepage: https://gitee.com/free_pan/anti-replay-ajax-util.git
- Issues: https://gitee.com/free_pan/anti-replay-ajax-util/issues
- npm.io page: https://npm.io/package/anti-replay-ajax-util

## Dependencies (1)

- [umi-request](https://npm.io/package/umi-request.md) ^1.3.5

## Recent versions

- 0.1.3 (latest) — 2021-03-10
- 0.1.2 — 2021-03-05
- 0.1.1 — 2021-03-03
- 0.1.0 — 2021-03-03

## README

# anti-replay-ajax-util

## 基本介绍

基于umi-request, 在ajax请求层面, 实现请求防重.

支持的两种防重模式: `丢弃旧请求`, `丢弃新请求`

## 使用

```
yarn add anti-replay-ajax-util
```

```tsx
import {AjaxUtil, WebApiData} from 'anti-replay-ajax-util'

// 防重: 丢弃新请求
AjaxUtil.post({
    url:'web api url'
},AjaxUtil.AntiReplayModel.DISCARD_NEW)

// 防重: 丢弃旧请求
AjaxUtil.post({
    url:'web api url'
},AjaxUtil.AntiReplayModel.DISCARD_OLD)

// 无防重: 普通请求
AjaxUtil.post({
    url:'web api url'
})
```

### 全局拦截器

```typescript
import {AjaxUtil, WebApiData,umiRequest} from 'anti-replay-ajax-util'

// request拦截器, 改变url 或 options.
umiRequest.interceptors.request.use((url, options) => {
  return (
    {
      url: `${url}&interceptors=yes`,
      options: { ...options, interceptors: true },
    }
  );
});

// 和上一个相同
umiRequest.interceptors.request.use((url, options) => {
  return (
    {
      url: `${url}&interceptors=yes`,
      options: { ...options, interceptors: true },
    }
  );
}, { global: true });


// response拦截器, 处理response
umiRequest.interceptors.response.use((response, options) => {
  const contentType = response.headers.get('Content-Type');
  return response;
});

// 提前对响应做异常处理
umiRequest.interceptors.response.use((response) => {
  const codeMaps = {
    502: '网关错误。',
    503: '服务不可用，服务器暂时过载或维护。',
    504: '网关超时。',
  };
  console.error(codeMaps[response.status]);
  return response;
});

// 克隆响应对象做解析处理
umiRequest.interceptors.response.use(async (response) => {
  const data = await response.clone().json();
  if(data && data.NOT_LOGIN) {
    location.href = '登录url';
  }
  return response;
})

AjaxUtil.post({
    url:'web api url',
    umiRequest:umiRequest
},AjaxUtil.AntiReplayModel.DISCARD_NEW)
```

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