# @prequest/interceptor

> 拦截器中间件。

Latest version **1.5.2** (published 2022-07-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @prequest/interceptor
pnpm add @prequest/interceptor
yarn add @prequest/interceptor
bun add @prequest/interceptor
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.2 |
| Published | 2022-07-10 |
| First published | 2021-05-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 1 |
| Unpacked size | 14.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 89 |
| Author | xdoer |
| Maintainers | thomas_han |
| Keywords | http, request, XMLHttpRequest, fetch, https, middleware, interceptor |

## Links

- npm: https://www.npmjs.com/package/@prequest/interceptor
- Repository: https://github.com/xdoer/PreQuest
- Homepage: https://pre-quest.vercel.app
- Issues: https://github.com/xdoer/PreQuest/issues
- npm.io page: https://npm.io/package/@prequest/interceptor

## Dependencies (1)

- [@prequest/types](https://npm.io/package/@prequest/types.md) ^1.5.2

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.5.2 (latest) — 2022-07-10
- 1.5.1 — 2022-07-03
- 1.5.0 — 2022-06-18
- 1.4.1 — 2022-05-02
- 1.4.0 — 2022-05-02
- 1.3.10 — 2022-04-18
- 1.3.9 — 2022-04-18
- 1.3.8 — 2022-04-17
- 1.3.7 — 2022-04-17
- 1.3.6 — 2022-04-03
- 1.3.5 — 2022-03-09
- 1.3.3 — 2022-02-27
- 1.3.3-alpha.0 — 2022-02-26
- 1.3.2 — 2022-02-09
- 1.2.6 — 2021-12-18
- … 38 more at https://npm.io/package/@prequest/interceptor/versions

## README

# @prequest/interceptor

拦截器中间件。

中间件模型可以更好的完成修改请求和拦截响应的需求，但为了方便习惯使用 axios 的用户，提供此中间件。

## 安装

```bash
npm install @prequest/interceptor
```

## 使用

```ts
import { prequest } from '@prequest/xhr'
import interceptorMiddleware from '@prequest/interceptor'

const interceptor = new InterceptorMiddleware()

// 修改请求参数
interceptor.request.use(
  request => {
    if (!request.path) throw new Error('can not find path')

    request.path = '/prefix' + request.path

    // 注意要将 request 参数返回
    return request
  },
  error => {
    console.log('modify request path fail', error)
    throw error
  }
)

// 修改响应
interceptor.response.use(
  response => {
    if (response.statusCode === 500) throw new Error('internal error')

    response.data = JSON.parse(response.data)

    // 注意要将 response 返回
    return response
  },
  error => {
    // 如果是某个错误
    if (error.message === 'internal error') {
      // 要进行什么操作

      return
    }

    // 否则将错误继续抛出
    throw error
  }
)

prequest.use(interceptor.run)
```

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