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

@iam4x/request

v1.4.0

Published

A lightweight, TypeScript-first HTTP request utility with built-in retry logic, query string handling, and automatic undefined value filtering

Readme

@iam4x/request

A lightweight, TypeScript-first HTTP request utility with built-in retry logic, query string handling, and automatic undefined value filtering.

Features

  • 🚀 Simple API - Clean, intuitive interface for making HTTP requests
  • 🔄 Built-in Retry Logic - Automatic retry mechanism for failed requests
  • 🔗 Query String Utilities - Parse and stringify query parameters with support for arrays and nested objects
  • 🧹 Automatic Cleanup - Filters out undefined values from request bodies and params
  • 📦 TypeScript First - Full TypeScript support with comprehensive type definitions
  • Zero Dependencies - Uses native fetch API, no external HTTP libraries required
  • 🎯 Tree Shakeable - Import only what you need

Installation

npm install @iam4x/request
# or
yarn add @iam4x/request
# or
pnpm add @iam4x/request
# or
bun add @iam4x/request

Quick Start

import { request } from '@iam4x/request';

// Simple GET request
const data = await request<{ id: number; name: string }>({
  url: 'https://api.example.com/users',
});

// GET request with query parameters
const users = await request({
  url: 'https://api.example.com/users',
  params: {
    page: 1,
    limit: 10,
    status: 'active',
  },
});

// POST request with body
const newUser = await request({
  url: 'https://api.example.com/users',
  method: 'POST',
  body: {
    name: 'John Doe',
    email: '[email protected]',
  },
});

// Request with retry logic
const result = await request({
  url: 'https://api.example.com/data',
  retries: 3, // Will retry up to 3 times on failure
});

API Reference

request<T>(req: Request): Promise<T>

Makes an HTTP request with the specified options.

Parameters

  • req.url (string, required) - The URL to make the request to
  • req.method (string, optional) - HTTP method (GET, POST, PUT, DELETE). Defaults to GET
  • req.headers (Record<string, string>, optional) - Custom headers to include in the request
  • req.params (RequestParams, optional) - Query string parameters (automatically converted to query string)
  • req.body (RequestParams, optional) - Request body (automatically JSON stringified)
  • req.retries (number, optional) - Number of retry attempts on failure. Defaults to 0

Returns

Promise<T> - The parsed JSON response

Example

const response = await request<ApiResponse>({
  url: 'https://api.example.com/users',
  method: 'POST',
  headers: {
    Authorization: 'Bearer token123',
  },
  params: {
    include: ['profile', 'settings'],
  },
  body: {
    name: 'John Doe',
    email: '[email protected]',
  },
  retries: 3,
});

Advanced Usage

Custom Headers

const data = await request({
  url: 'https://api.example.com/protected',
  headers: {
    Authorization: 'Bearer your-token',
    'X-Custom-Header': 'value',
  },
});

Complex Query Parameters

const data = await request({
  url: 'https://api.example.com/search',
  params: {
    q: 'search term',
    filters: {
      category: 'electronics',
      price: { min: 100, max: 500 },
    },
    tags: ['new', 'featured'],
  },
});

Retry Logic

The retry mechanism will automatically retry failed requests:

// Retry up to 3 times on failure
const data = await request({
  url: 'https://api.example.com/unstable-endpoint',
  retries: 3,
});

Type Safety

The request function is fully typed. Specify your response type for full type safety:

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

const user = await request<User>({
  url: 'https://api.example.com/users/1',
});
// user is typed as User

Error Handling

The request function throws a RequestError for non-2xx HTTP responses:

import { request, RequestError } from '@iam4x/request';

try {
  const data = await request({
    url: 'https://api.example.com/users/999',
  });
} catch (error) {
  if (error instanceof RequestError) {
    console.error(`Request failed: ${error.status} ${error.statusText}`);
    console.error('Response data:', error.response);
  }
}

RequestError Class

  • message (string) - Error message
  • status (number) - HTTP status code
  • statusText (string) - HTTP status text
  • response (unknown) - Parsed response body (JSON or text)

RequestParams Type

The RequestParams type supports:

  • string
  • number
  • boolean
  • string[]
  • number[]
  • Record<string, any> (nested objects)
  • Array<Record<string, any>> (arrays of objects)

Behavior

  • Undefined Values: Automatically filtered out from params and body
  • Content-Type: Automatically set to application/json for requests with a body
  • Query String Encoding: Special characters are automatically URL-encoded
  • Array Parameters: Arrays in query params are serialized as repeated keys (?tags=js&tags=ts)
  • Nested Objects: Nested objects in query params use bracket notation (?user[name]=John)

Exports

The package exports:

  • request - Main request function
  • RequestError - Error class for failed requests
  • Request - Type for request options
  • RequestParams - Type for request parameters/body

Requirements

  • TypeScript 5.8.3 or higher (peer dependency)
  • A JavaScript runtime that supports the fetch API (Node.js 18+, Bun, or modern browsers)

License

MIT

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

Author

@iam4x