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

@truefit/http-utils

v2.0.4

Published

a handy wrapper around axios

Downloads

448

Readme

@truefit/http-utils

This library is a lightweight wrapper around axios. It provides a set of utility functions to enable making http requests easily.

This library is now written in Typescript and it exports its own types, as well as some common ones from axios for convenience.

Install

npm install @truefit/http-utils

or

yarn add @truefit/http-utils

Use

This library is generally meant to be used in client apps (such as website or mobile apps). It makes the assumption of there being a single base api for the app (an assumption that axios clearly can't make). The goal of this decision is to provide syntactic sugar to the developer and readability to code.

It is possible, and sometimes useful, to use this library in node apps as well, but when doing so you need to be mindful of the singleton nature of the base configuration.

API

As mentioned above, this library makes the assumption that you are building an app around a central api. Given this approach, you need to provide the shared configuration settings on the boot of your app (for example, in a react app, you would typically do it in your index file).

Once this configuration is setup, you can make http calls from any file in the app using this base configuration.

Configure

| Field | Type | Description | | ----------------- | -------------------------------------------------------------- | ------------------------------------------- | | baseConfig | AxiosRequestConfig | shared configuration for all requests | | transformConfig | (config: AxiosRequestConfig, url?: string): AxiosRequestConfig | hook called on creation of each request | | baseHeaders | any | shared headers to be sent with each request | | transformHeaders | (headers: any, url?: string): any | hook called on creation of each request | | configureInstance | (AxiosInstance, url?: string): void | hook called on creation of each request |

Examples

At it's simplest, you just need to supply the baseURL to be used.

import {configureHttp} from '@truefit/http-utils';

configureHttp({
  baseConfig: {
    baseURL: 'https://a.domain.com/',
  },
});

That said, apps are rarely this simple. Often you will need to provide a piece of data or authentication token that can only be known at runtime. To account for these situations, the configuration properties (full list above), provide hooks that are invoked on the creation of each request.

import {configureHttp, AxiosRequestConfig} from '@truefit/http-utils';

configureHttp({
  baseConfig: {
    baseURL: 'https://a.domain.com/',
  },
  transformConfig: (baseConfig: AxiosRequestConfig) => {
    return {
      ...baseConfig
      timeout: 1000,
    };
  },
});

Individual Calls

Get

| Parameter | Type | Description | | --------- | ------------------------ | ---------------------------------------------------------- | | url | string | the relative url for the request | | configure | ConfigureInstanceRequest | hook to provide the configuration for this particular call |

import {get} from '@truefit/http-utils';

const response = await get('/users');

const response = await get('/users', () => ({
  headers: {'X-Custom-Header': 'foobar'},
}));

Post

| Parameter | Type | Description | | --------- | ------------------------ | ---------------------------------------------------------- | | url | string | the relative url for the request | | data | any (required) | the data to use in the body of the request | | configure | ConfigureInstanceRequest | hook to provide the configuration for this particular call |

import {post} from '@truefit/http-utils';

const response = await post('/users', {name: 'Jim Bob'});

Patch

| Parameter | Type | Description | | --------- | ------------------------ | ---------------------------------------------------------- | | url | string | the relative url for the request | | data | any (required) | the data to use in the body of the request | | configure | ConfigureInstanceRequest | hook to provide the configuration for this particular call |

import {patch} from '@truefit/http-utils';

const response = await patch('/users', {id: 1, name: 'Jim Bob'});

Put

| Parameter | Type | Description | | --------- | ------------------------ | ---------------------------------------------------------- | | url | string | the relative url for the request | | data | any (required) | the data to use in the body of the request | | configure | ConfigureInstanceRequest | hook to provide the configuration for this particular call |

import {put} from '@truefit/http-utils';

const response = await put('/users', {name: 'Jim Bob'});

Delete

| Parameter | Type | Description | | --------- | ------------------------ | ---------------------------------------------------------- | | url | string | the relative url for the request | | configure | ConfigureInstanceRequest | hook to provide the configuration for this particular call |

import {del} from '@truefit/http-utils';

const response = await del('/users/1');

Request

Allows you to create a request, but requires you to specify the method manually. This is used to make the lesser used calls (such as options or link).

| Parameter | Type | Description | | --------- | ------------------------ | ---------------------------------------------------------- | | url | string | the relative url for the request | | configure | ConfigureInstanceRequest | hook to provide the configuration for this particular call |

import {request} from '@truefit/http-utils';

const response = await get('/users', () => ({
  method: 'options',
}));

createAxiosInstance

Exposes the raw shared axios creation method - will return an AxiosInstance.

| Parameter | Type | Description | | --------- | ------ | -------------------------------- | | url | string | the relative url for the request |

import {createAxiosInstance} from '@truefit/http-utils';

const instance = createAxiosInstance('/users');

Misc

You can pass a fully qualified url to any of the functions to bypass the baseURL provided to the base configuration.

const response = await get('https://a.domain.com/');