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

unready-fetch

v0.3.0

Published

Mock fetch API call simulation before real API are ready.

Readme


unready-fetch

A lightweight library for mocking API calls in JavaScript and TypeScript. Good for simulating asynchronous fetch requests before the real API is available.


📖 Overview

unready-fetch is a utility designed to simulate API calls using the fetch API interface during development. It allows you to:

  • Mock API responses.
  • Control HTTP status codes.
  • Simulate network delays.

The library is ideal for prototyping and testing while backend services are under development. Once the real API is available, you can seamlessly switch to fetch.


🌟 Features

  • Mock API Responses: Simulate both success and error responses.
  • Customizable Delays: Set a timeout to mimic network latency.
  • Flexible Mocking: Define custom status codes, error messages, and data payloads.
  • Abort Support: Automatically reject the request if the AbortSignal is triggered.
  • TypeScript Support: Built with TypeScript for type safety and better autocompletion.

📦 Installation

Install the package via npm, yarn, or pnpm:

npm install unready-fetch
yarn add unready-fetch
pnpm i unready-fetch

🚀 Usage

Basic Example

import { unreadyFetch } from "unready-fetch";

const mockData = {
  success: {
    message: "Data fetched successfully!",
    items: [{ id: 1, name: "Sample Item" }],
  },
  status: 200,
};

async function fetchData() {
  const response = await unreadyFetch(mockData)(
    "https://api.example.com/data",
    {
      method: "GET",
    }
  );

  if (response.ok) {
    const data = await response.json();
    console.log("Success:", data);
  } else {
    const error = await response.json();
    console.error("Error:", error);
  }
}

fetchData();

API Contract

unreadyFetch(mock?, timeout?)

  • mock (MockData): Optional mock data for simulating the API response. Defaults to a pre-defined mock success or error response.
  • timeout (number): Time in milliseconds to delay the response. Default is 1000ms.

Returns: A function that simulates a fetch request, accepting input and init as arguments.

MockFetch Function

  • input (RequestInfo | URL): The URL or Request object to fetch.
  • init (RequestInit): Optional configuration object for the request (e.g., method, headers, body).

Returns: A Promise resolving to a MockResponse object.


🔍 Interfaces

MockData

export interface MockData {
  success?: any; // Mocked successful response data
  error?: any; // Mocked error response data
  status?: number; // HTTP status code (e.g., 200, 404, 500)
}

MockResponse

export interface MockResponse {
  url: string; // Requested URL
  ok: boolean; // Whether the response is successful
  status: number; // HTTP status code
  json(): Promise<any>; // Returns the response body as JSON
  text(): Promise<string>; // Returns the response body as a string
}

📄 Full Example with Error Handling

import { unreadyFetch } from "unready-fetch";

const errorMock = {
  error: {
    message: "Something went wrong!",
  },
  status: 404,
};

async function fetchWithError() {
  try {
    const response = await unreadyFetch(errorMock, 800)(
      "https://api.example.com/error",
      {
        method: "GET",
      }
    );

    if (!response.ok) {
      const error = await response.json();
      console.error("API Error:", error);
    }
  } catch (err) {
    console.error("Request Aborted:", err);
  }
}

fetchWithError();

⚙️ How It Works

The unreadyFetch function simulates a fetch request by returning a function that behaves like fetch. It:

  • Uses the provided mock data to determine whether the response is a success or error.
  • Delays the response by the specified timeout to simulate network latency.
  • Supports AbortController to allow early termination of the request.

📝 Notes

  • Use this library during development and testing only.
  • When the API is ready, replace unreadyFetch with the native fetch API.

Let me know if you need further refinements!