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

@i.un/nuxt-api

v1.0.20

Published

[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href] [![License][license-src]][license-href] [![Nuxt][nuxt-src]][nuxt-href]

Readme

@i.un/nuxt-api

npm version npm downloads License Nuxt

A robust, production-ready API client module for Nuxt 4. Designed for enterprise-grade authentication handling, SSR compatibility, and seamless token refreshing.

Features

  • 🔐 Smart Token Refresh:
    • Automatic 401 interception and token refreshing.
    • Request Queueing: Prevents multiple refresh requests; pending requests retry automatically with the new token.
    • SSR Support: Perfectly handles cookie passing between SSR server and backend API.
    • Cross-Domain & Subdomain Support: Auto-detects and fixes cookie domains for seamless cross-subdomain authentication.
  • 🛡 Security First:
    • Respects HttpOnly cookies.
    • Does not overwrite backend security flags unless necessary.
  • 🛠 Developer Friendly:
    • Full TypeScript support with UseApiOptions.
    • Configurable Debug Logging.
    • Return full response or just data.

Installation

# npm
npm install @i.un/nuxt-api

# pnpm
pnpm add @i.un/nuxt-api

# yarn
yarn add @i.un/nuxt-api

Configuration

Add the module to your nuxt.config.ts:

export default defineNuxtConfig({
  modules: ['@i.un/nuxt-api'],

  iunApi: {
    // Backend API URL
    apiBase: 'https://api.example.com',
    // Optional: Different URL for SSR (e.g. internal docker network)
    apiBaseServer: 'http://backend-service:8080',

    // Authentication
    cookieName: 'access_token', // Default

    // Endpoints
    endpoints: {
      // Set to false to disable token refresh
      // Or provide a custom refresh endpoint path (e.g. '/auth/refresh')
      refresh: false, // default
    },
    // Redirects
    redirects: {
      // Set to false to disable redirect
      // Or provide a custom redirect path (e.g. '/auth/login')
      login: false, // default
    },

    // Response Parsing default:
    responseConfig: {
      codeKey: 'code',
      dataKey: 'data',
      messageKey: 'message',
      successCode: 0,
      unauthorizedCode: 401,
    },

    // Debugging
    debug: process.env.NODE_ENV === 'development', // Enable detailed logs
  },
});

Usage

1. Basic Requests (useApi)

useApi provides a wrapper around Nuxt's $fetch with auto-auth and error handling.

<script setup lang="ts">
const api = useApi();

// GET request (returns dataKey directly by default)
const users = await api.get<User[]>('/users');

// POST request
await api.post('/posts', { title: 'Hello' });

// Custom Options (TypeScript supported)
const profile = await api.get('/profile', {}, {
  headers: { 'X-Custom': 'value' },
  timeout: 5000
});
</script>

2. Get Full Response

Sometimes you need the status code or message, not just the data.

const res = await api.post('/submit', formData, {
  returnFullResponse: true,
});

if (res.code === 0) {
  console.log(res.message); // "Success"
  console.log(res.data); // { ... }
}

3. Manual Token Management (useTokenRefresh)

Usually handled automatically, but exposed if needed.

const { refreshToken } = useTokenRefresh();

const doRefresh = async () => {
  const newToken = await refreshToken();
  if (newToken) {
    console.log('Token refreshed manually');
  }
};

License

MIT