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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@enzotrr/nextjs-fetcher

v1.0.10

Published

Lightweight, type-safe HTTP client for React and Next.js applications

Downloads

31

Readme

License: MIT

NextJS Fetcher

A lightweight, type-safe HTTP client for React and Next.js applications that provides a simple wrapper around the Fetch API.

Features

  • 🔄 Simple wrapper around the native Fetch API
  • 🌐 Base URL configuration for all requests
  • 🔒 Default and custom headers support
  • 🔧 Header interceptors for authentication and other use cases
  • 📦 Type-safe responses with TypeScript generics
  • ⚠️ Automatic error handling for non-OK responses
  • 🧩 Support for all common HTTP methods (GET, POST, PUT, DELETE, PATCH)

Installation

# npm
npm install @enzotrr/nextjs-fetcher

# yarn
yarn add @enzotrr/nextjs-fetcher

# pnpm
pnpm add @enzotrr/nextjs-fetcher

Usage

Basic Setup

import { NextJsFetcher } from '@enzotrr/nextjs-fetcher';

// Create a new instance with optional configuration
const fetcher = new NextJsFetcher({
  baseURL: 'https://api.example.com',
  headers: {
    'Content-Type': 'application/json',
  },
});

// Use it in your components or API handlers
async function fetchData() {
  try {
    const data = await fetcher.get('/users');
    console.log(data);
  } catch (error) {
    console.error('Failed to fetch data:', error);
  }
}

With Type Safety

interface User {
  id: number;
  name: string;
  email: string;
}

// Get a typed response
const user = await fetcher.get<User>('/users/1');
console.log(user.data.data.name); // TypeScript knows the shape of 'user'

// Post with a typed request body and response
const newUser = await fetcher.post<User>('/users', {
  name: 'John Doe',
  email: '[email protected]',
});

Using Header Interceptors

// Add an interceptor to modify headers before each request
// Useful for adding authentication tokens
fetcher.interceptor(async (headers) => {
  const token = await getAuthToken(); // Your token retrieval logic
  return {
    ...headers,
    'Authorization': `Bearer ${token}`,
  };
});

// Now all requests will include the Authorization header
const data = await fetcher.get('/protected-resource');

API Reference

Constructor

new NextJsFetcher(options?: NextJsFetcherOptions)

Options

  • baseURL?: string - Base URL for all requests
  • headers?: HeadersInit - Default headers for all requests

Methods

HTTP Methods

type PartiallyData = { status: number; statusText: string; headers: Headers; options: RequestInit; };

  • get<T = unknown>(url: string, options?: RequestInit): Promise<PartiallyData & {data: T | null}>
  • post<T = unknown>(url: string, body?: any, options?: RequestInit): Promise<PartiallyData & {data: T | null}>
  • put<T = unknown>(url: string, body?: any, options?: RequestInit): Promise<PartiallyData & {data: T | null}>
  • delete<T = unknown>(url: string, options?: RequestInit): Promise<PartiallyData & {data: T | null}>
  • patch<T = unknown>(url: string, body?: any, options?: RequestInit): Promise<PartiallyData & {data: T | null}>

Advanced Methods

  • request<T = unknown>(url: string, options: RequestInit): Promise<PartiallyData & {data: T | null}> - Make a custom request
  • interceptor(fn: HeadersInterceptor) - Set a header interceptor function

Types

type HeadersInterceptor = (headers: HeadersInit) => HeadersInit | Promise<HeadersInit>;

interface NextJsFetcherOptions {
  baseURL?: string;
  headers?: HeadersInit;
}

Error Handling

The fetcher automatically throws an error for non-OK responses (status outside the 200-299 range). The error includes the status code and response body:

try {
  const data = await fetcher.get('/some-endpoint');
  // Process successful response
} catch (error) {
  // Error message format: "HTTP error! Status: 404, Body: Not Found"
  console.error(error);
}

📄 License

This project is licensed under the MIT License.
You can freely use it for personal or commercial purposes.
See the LICENSE file for more details.