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

@barisvarlik/api-client

v1.1.1

Published

HTTP Client Library with builder pattern and interception support.

Downloads

66

Readme

API Client

A flexible HTTP client library with builder pattern and interception support for JavaScript and TypeScript applications.

Installation

npm install api-client

Features

  • Builder pattern for easy configuration
  • Request and response interception
  • Timeout support
  • Type-safe responses (TypeScript)
  • URL path and query parameter handling

Usage

Basic Usage

import { createApiClient } from 'api-client';
// Create a client
const client = createApiClient()
.withBaseUrl('https://api.example.com')
.withDefaultHeader('Content-Type', 'application/json')
.build();
// Make a GET request
client.get('users')
.execute()
.then(data => console.log(data))
.catch(error => console.error(error));
// Make a POST request
client.post('users', { name: 'John', email: '[email protected]' })
.execute()
.then(data => console.log(data))
.catch(error => console.error(error));

TypeScript Usage

import { createApiClient } from 'api-client';

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

// Create a client
const client = createApiClient()
  .withBaseUrl('https://api.example.com')
  .withDefaultHeader('Content-Type', 'application/json')
  .build();

// Make a GET request with type safety
client.get<User[]>('users')
  .execute()
  .then(users => {
    // TypeScript knows that users is User[]
    users.forEach(user => console.log(user.name));
  });

// Make a POST request with type safety
client.post<User>('users', { name: 'John', email: '[email protected]' })
  .execute()
  .then(user => {
    // TypeScript knows that user is User
    console.log(user.id);
  });

Adding Request Headers

client.get('users')
  .withHeader('Authorization', 'Bearer token123')
  .execute();

Setting Timeout

client.get('users')
  .withTimeout(5000) // 5 seconds
  .execute();

Using Interceptors

// Request interceptor
const authInterceptor = {
  intercept: (request, url) => {
    request.headers = {
      ...request.headers,
      'Authorization': 'Bearer token123'
    };
    return request;
  }
};

// Response interceptor
const loggingInterceptor = {
  intercept: async (response) => {
    console.log(`Response status: ${response.status}`);
    return response;
  }
};

// Add interceptors to client
const client = createApiClient()
  .withBaseUrl('https://api.example.com')
  .withRequestInterceptor(authInterceptor)
  .withResponseInterceptor(loggingInterceptor)
  .build();

Important Note on Response Types

For JavaScript Users: This library currently only supports JSON responses by default. If your API returns non-JSON responses (like plain text), you will need to modify the Client.ts file to handle different content types.

For TypeScript Users: You can specify any return type using generics, but be aware that the underlying implementation expects JSON responses. If your API returns non-JSON data, you'll need to modify the client implementation.

API Reference

ApiClientBuilder

  • withBaseUrl(url: string): Set the base URL for all requests
  • withDefaultHeader(name: string, value: string): Add a default header
  • withDefaultHeaders(headers: HeadersInit): Add multiple default headers
  • withRequestInterceptor(interceptor: RequestInterceptor): Add a request interceptor
  • withResponseInterceptor(interceptor: ResponseInterceptor): Add a response interceptor
  • build(): Create the ApiClient instance

ApiClient

  • get<T>(path?: string, id?: string | number, params?: Record<string, any>): Create a GET request
  • post<T>(endpoint?: string, data: any): Create a POST request
  • put<T>(path?: string, id: string | number, data: any): Create a PUT request
  • delete<T>(path?: string, id: string | number): Create a DELETE request

RequestBuilder

  • withHeader(name: string, value: string): Add a header to the request
  • withTimeout(timeoutMs: number): Set a timeout for the request
  • withRequestInterceptor(interceptor: RequestInterceptor): Add a request interceptor
  • withResponseInterceptor(interceptor: ResponseInterceptor): Add a response interceptor
  • execute(): Execute the request and return a promise with the response

License

ISC