Socket
Socket
Sign inDemoInstall

@prequest/error-retry

Package Overview
Dependencies
2
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @prequest/error-retry

错误重试中间件


Version published
Weekly downloads
7
decreased by-12.5%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

@prequest/error-retry

错误重试中间件

安装

npm install @prequest/error-retry

使用

创建请求实例

import { create } from '@prequest/xhr'
import errorRetryMiddleware from '@prequest/error-retry'

const prequest = create()

创建中间件

import { prequest } from '@prequest/xhr'
import errorRetryMiddleware from '@prequest/error-retry'

const middleware = errorRetryMiddleware({
  // 错误重试次数
  retryCount: 2,

  // opt 为 Request 类型,通过该函数,你可以控制那些接口需要错误重试
  retryControl(opt, e) {
    const { method, path } = opt

    // 如果是具体某个错误,则不进行重试
    if (e.message === 'internal error') return false

    // api 路径不进行错误重试
    if (path === '/api') return false

    // 只有 get 方法才进行错误重试
    return method === 'GET'
  },
})

prequest.use(middleware)

单一控制

注册错误重试中间件后,每一个请求也可以单独配置错误请求次数

prequest('/api', { errorRetry: 1 })

配置项

Option NameTypeDefaultRequiredMeaning
retryCountnumber1false错误重试次数
retryControl(opt: RequestOpt, e: Error) => boolean(opt: RequestOpt) => opt.method === 'GET'false重试策略,默认 get 请求会进行错误重试

Keywords

FAQs

Last updated on 10 Jul 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc