npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

nuxt-advanced-fetch

v1.1.2

Published

Enhances Nuxt 3 $fetch with lifecycle handlers, dynamic management, and custom fetch instances for modular API requests.

Readme

API Plugin for Nuxt 3 🚀

This plugin enhances the $fetch instance in Nuxt 3 by introducing powerful handler mechanisms and customizable fetch instances. Whether you need global request/response handlers or isolated configurations for specific use cases, this plugin has you covered.


Key Features ✨

  1. Multiple Handlers Support:

    • Add multiple handlers for different fetch lifecycle stages: onRequest, onRequestError, onResponse, onResponseError.
    • Handlers can be added and removed dynamically.
  2. Instance Creation:

    • Easily create new fetch instances with their own set of handlers.
    • Each instance can have isolated behavior and configurations.
  3. Enhanced Debugging:

    • Centralize and manage request/response handlers for better control and observability.

Installation 📦

  1. Add the plugin to your project:

    npm install nuxt-advanced-fetch
    
    pnpm add nuxt-advanced-fetch
    
    yarn add nuxt-advanced-fetch
  2. Register the plugin in your Nuxt application:

    // nuxt.config.ts
    export default defineNuxtConfig({
      modules: ['nuxt-advanced-fetch']
    })

Usage 🛠️

Accessing the Enhanced API

The enhanced $fetch instance is available in useNuxtApp():



const { $api } = useNuxtApp()


const data = await $api('/api/resource', {
  method: 'GET',
  onRequest(context) {
    console.log('Request started:', context)
  },
  onResponse(context) {
    console.log('Response received:', context)
  }
})

Adding Handlers

You can add handlers globally or per-instance:

$api.addHandler('onRequest', (context) => {
  console.log('Global onRequest handler:', context)
})

$api.addHandler('onResponseError', (error) => {
  console.error('Global response error:', error)
})

Removing Handlers

const handler = (context) => console.log('Removing this handler', context)
$api.addHandler('onRequest', handler)
$api.removeHandler('onRequest', handler)

Creating Custom Instances

Each custom instance has its own set of handlers:

const customApi = $api.create({ baseURL: 'https://custom-api.com' })

customApi.addHandler('onResponse', (context) => {
  context.options.headers.append('Authorization', `Bearer ${token}`)
})

await customApi('/custom-endpoint')

API Reference 📖

Methods

$api(url: string, options?: IFetchOptions): Promise<any>

  • Enhanced fetch method with lifecycle handlers.

addHandler(type: keyof IApiHandlers, handler: (context: IApiHandlerTypes[K]) => void): void

  • Adds a new handler for the specified lifecycle stage.

removeHandler(type: keyof IApiHandlers, handler: (context: IApiHandlerTypes[K]) => void): void

  • Removes an existing handler for the specified lifecycle stage.

create(options?: IFetchOptions): IApiPlugin

  • Creates a new fetch instance with isolated handlers and configurations.

Why Use This Plugin? 🤔

Problem 1: Lack of Multiple Handlers

Native $fetch in Nuxt lacks the ability to manage multiple handlers for a single lifecycle stage. This plugin resolves that by allowing you to:

  • Register multiple handlers for onRequest, onRequestError, onResponse, and onResponseError.
  • Dynamically add or remove handlers as needed.

Problem 2: Limited Customization for Instances

Creating multiple fetch instances with isolated configurations is not straightforward. With this plugin:

  • You can create multiple instances, each with its own handlers and configurations.
  • This is especially useful for modular or microservice-based applications.

Contributing 🤝

Contributions are welcome! Please submit an issue or a pull request on GitHub if you have suggestions or improvements.


License 📜

This plugin is licensed under the MIT License. See the LICENSE file for details.