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

@sachin-acharya-projects/resource-client

v1.0.1

Published

A flexible, minimal, and extendable TypeScript REST API client for any HTTP backend, compatible with Axios, Fetch, and similar clients.

Downloads

5

Readme

Resource Client

A flexible, minimal, and extendable TypeScript REST API client for any HTTP backend. Compatible with Axios, Fetch, and similar HTTP clients.

Features

  • Supports all standard REST operations: GET, POST, PUT, PATCH, DELETE
  • Works seamlessly with Axios, Fetch, or any client following the Axios request signature
  • Global default headers with per-request overrides and extend options
  • Configurable trailing slash behavior
  • Written in TypeScript with types included
  • Minimal boilerplate for easy integration

Installation

npm install resource-client
# or
yarn add resource-client

Usage

import ResourceClient from 'resource-client';
import axios from 'axios';

const api = new ResourceClient('users', {
    client: axios,
    baseURL: 'https://api.example.com',
    defaultHeaders: {
        'Content-Type': 'application/json',
        Authorization: 'Bearer YOUR_TOKEN',
    },
    extendHeaders: true,
    trailingSlash: true,
});

// List users
const users = await api.list({ params: { page: 1 } });

// Get a user by ID
const user = await api.get('123');

// Create a user
await api.store({ name: 'John Doe' });

// Update a user
await api.update('123', { name: 'Jane Doe' });

// Delete a user
await api.destroy('123');

1. Minimal fetch adapter (to fit ResourceClient’s expected interface)

// fetchAdapter.ts
export async function fetchAdapter(config: {
    url: string;
    method: string;
    headers?: Record<string, string>;
    params?: Record<string, any>;
    data?: any;
}) {
    // Build query string from params
    const queryString = config.params ? '?' + new URLSearchParams(config.params).toString() : '';

    const response = await fetch(config.url + queryString, {
        method: config.method.toUpperCase(),
        headers: config.headers,
        body: ['GET', 'HEAD'].includes(config.method.toUpperCase())
            ? undefined
            : JSON.stringify(config.data),
    });

    if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
    }

    // Assuming JSON response
    return response.json();
}

2. Using ResourceClient with fetchAdapter

import ResourceClient from 'resource-client';
import { fetchAdapter } from './fetchAdapter';

const api = new ResourceClient('posts', {
    client: fetchAdapter,
    baseURL: 'https://api.example.com',
    defaultHeaders: {
        'Content-Type': 'application/json',
    },
    extendHeaders: true,
    trailingSlash: true,
});

async function run() {
    // List posts (with optional query)
    const posts = await api.list({ params: { page: 1, limit: 10 } });
    console.log(posts);

    // Get a post by ID
    const post = await api.get('42');
    console.log(post);

    // Create a new post
    const newPost = await api.store({
        title: 'Hello world',
        body: 'This is a test.',
    });
    console.log(newPost);

    // Update a post fully
    const updatedPost = await api.update('42', {
        title: 'Updated title',
        body: 'Updated body',
    });
    console.log(updatedPost);

    // Patch a post partially
    const patchedPost = await api.patch('42', {
        title: 'Partially updated title',
    });
    console.log(patchedPost);

    // Delete a post
    await api.destroy('42');
    console.log('Deleted post 42');
}

run().catch(console.error);

Notes:

  • fetchAdapter converts params into query string automatically.
  • It serializes the body as JSON for non-GET/HEAD requests.
  • Throws an error on non-2xx responses.
  • You can customize fetchAdapter if your API expects different formats (e.g., form data).

API

| Method | Description | Params | | ---------------------------- | ---------------------------------- | ---------------------------------------- | | list(query?, options?) | Fetch a list of resources | Query params and request options | | get(id, options?) | Fetch a single resource by ID | ID and request options | | store(data, options?) | Create a new resource | Payload and request options | | update(id, data, options?) | Replace an existing resource by ID | ID, payload, and request options | | patch(id, data, options?) | Partially update a resource by ID | ID, partial payload, and request options | | destroy(id, options?) | Delete a resource by ID | ID and request options |

Configuration Options

| Option | Type | Default | Description | | ---------------- | ----------- | ------- | -------------------------------------------------------- | | client | HTTP client | — | HTTP client instance (e.g., axios) | | baseURL | string | — | Base URL for all requests | | defaultHeaders | Record | {} | Headers to include in every request | | extendHeaders | boolean | true | Whether to merge per-request headers with defaultHeaders | | trailingSlash | boolean | true | Whether URLs should end with a slash |

License

MIT © Sachin Acharya