Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@onion-interceptor/pipes

Package Overview
Dependencies
Maintainers
0
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@onion-interceptor/pipes

onion-interceptor pipes

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@onion-interceptor/pipes

@onion-interceptor/pipes 是一个操作管道工具,它允许您以一种更简洁的方式编写请求拦截器。

安装

使用 npm 或 yarn 或 pnpm 安装 @onion-interceptor/pipes :

npm install @onion-interceptor/pipes

或者

yarn add @onion-interceptor/pipes

或者

pnpm add @onion-interceptor/pipes

使用方法

  • catchError

捕获错误

import { catchError } from "@onion-interceptor/pipes";
import type { Next, Context } from "onion-interceptor";

export async function errorInterceptor(ctx: Context, next: Next) {
  console.log("errorInterceptor start", ctx);
  await next(
    catchError((err) => {
      console.log(error);
      return Promise.reject(error);
    })
  );
  console.log("errorInterceptor end", ctx);
}
  • finalize

finalize 是一个操作管道,用于在请求或响应完成时执行一些操作(不管成功或失败)。

import { finalize } from '@onion-interceptor/pipes'
import type { Next, Context } from 'onion-interceptor'

export async function loadingInterceptor(ctx: Context, next: Next) {
  console.log('loadingInterceptor start', ctx)
  await next(finally(() => console.log('loadingInterceptor end', ctx)))
}
  • tap

tap 是一个操作管道,可接受三个回调函数,后两个选填 分别为 成功后的回调,错误回调,无论成功还是失败都会执行的回调。

import { tap } from "@onion-interceptor/pipes";

export async function errorInterceptor(ctx: Context, next: Next) {
  console.log("errorInterceptor start", ctx);
  await next(
    tap(
      (_ctx) => console.log("find error in res", _ctx.res),
      (error) => {
        console.log(error);
        return Promise.reject(error);
      },
      () => console.log("errorInterceptor end", ctx)
    )
  );
}
  • delay

延迟拦截器

import type { Next, Context } from 'onion-interceptor'
import { delay,finalize } from '@onion-interceptor/pipes'

export async function loadingInterceptor(ctx: Context, next: Next) {
  console.log('loadingInterceptor start', ctx)
  // 延迟 1 秒才执行
  await next(delay(1000),finally(() => console.log('loadingInterceptor end', ctx)))
}

贡献

如果你有任何疑问或建议,欢迎提交 issue 或 PR。

许可

本项目使用 MIT 许可证。

Keywords

FAQs

Package last updated on 06 Nov 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc