Home > Web Front-end > JS Tutorial > Introduction to @rxliuli/vista: A unified request interceptor library for both Fetch and XHR with middleware support.

Introduction to @rxliuli/vista: A unified request interceptor library for both Fetch and XHR with middleware support.

Patricia Arquette
Release: 2025-01-07 00:19:40
Original
667 people have browsed it

Introduction to @rxliuli/vista: A unified request interceptor library for both Fetch and XHR with middleware support.
Introduction to @rxliuli/vista: A unified request interceptor library for both Fetch and XHR with middleware support.

@rxliuli/vista is a powerful homogeneous request interceptor library that supports unified interception of Fetch/XHR requests. It allows you to intervene at different stages of the request lifecycle, enabling various functions such as request monitoring, modification, and mocking.

Characteristics

  • ? Supports both Fetch and XHR request interception
  • ? Use middleware pattern, flexible and easy to expand
  • ? Support interventions before and after requests
  • ? Modifiable request and response data
  • ? Zero dependency, compact size
  • ? Supports browser environment only

Installation

npm install @rxliuli/vista
# Or
yarn add @rxliuli/vista
# Or
pnpm add @rxliuli/vista
Copy after login

Basic Usage

import { Vista } from '@rxliuli/vista'

new Vista()
  .use(async (c, next) => {
    console.log('Request started:', c.req.url)
    await next()
  })
  .use(async (c, next) => {
    await next()
    console.log('Response data:', await c.res.clone().text())
  })
  .intercept()
Copy after login

Advanced Use Cases

Add global request headers

new Vista()
  .use(async (c, next) => {
    c.req.headers.set('Authorization', 'Bearer token')
    await next()
  })
  .intercept()
Copy after login

Request Result Cache

const cache = new Map()

new Vista()
  .use(async (c, next) => {
    const key = c.req.url
    if (cache.has(key)) {
      c.res = cache.get(key).clone()
      return
    }
    await next()
    cache.set(key, c.res.clone())
  })
  .intercept()
Copy after login

Request failed, please retry

new Vista()
  .use(async (c, next) => {
    const maxRetries = 3
    let retries = 0

    while (retries < maxRetries) {
      try {
        await next()
        break
      } catch (err) {
        retries++
        if (retries === maxRetries) throw err
      }
    }
  })
  .intercept()
Copy after login

Dynamic modify response

new Vista()
  .use(async (c, next) => {
    await next()
    if (c.req.url === 'https://example.com/example') {
      const json = await c.res.json()
      json.id = 2
      c.res = new Response(JSON.stringify(json), c.res)
    }
  })
  .intercept()
Copy after login

API Reference

Vista Class

Main interceptor class, providing the following methods:

  • use(middleware): Add middleware
  • intercept(): Start intercepting requests
  • destroy(): Stop intercepting requests

Middleware Context

The middleware function receives two parameters:

  • context: Contains request and response information
    • req: Request object res: Response object
    • type: Request type, fetch or xhr
  • next: Call the function of the next middleware or original request

FAQ

  1. How to stop interception?
   const vista = new Vista()
   vista.intercept()
   // When not needed
   vista.destroy()
Copy after login
  1. Does it support asynchronous operations?
    Yes, the middleware supports async/await syntax.

  2. Does it support intercepting requests in Node.js?

No, it only supports intercepting requests in the browser.

Thanks

  • xhook: A library that implements xhr interception, helpful for the implementation of some features.
  • hono: An excellent web server framework that provides a lot of inspiration in its API.

Contribution Guidelines

Welcome to submit Issues and Pull Requests!

License

MIT License

Try it and tell me your experience, welcome any error or feature feedback.

The above is the detailed content of Introduction to @rxliuli/vista: A unified request interceptor library for both Fetch and XHR with middleware support.. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template