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 🙏

© 2024 – Pkg Stats / Ryan Hefner

tailorfetch

v1.6.0

Published

TailorFetch is a lightweight Node.js library for making HTTP requests with customizable options and response transformations.

Downloads

12

Readme

Node.js Package

TailorFetch

TailorFetch is a lightweight Node.js library for making HTTP requests with customizable options and response transformations.

Warning WIP! Documentation might be outdated or incomplete

Installation

npm install tailorfetch 

Features

  • Simplified HTTP GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS requests
  • Flexible options for headers, query parameters, timeouts, and more
  • Ability to transform response data using custom transformers
  • Ability to intercept request with custom intercept logic
  • Easy-to-use interface for handling common use cases
  • Enables use of Redis or built-in caching

Usage

Making GET Request

import TailorFetch from 'tailorfetch';

await TailorFetch.GET('https://dummyjson.com/products');

Making POST Request

import TailorFetch from 'tailorfetch';

const url = 'https://dummyjson.com/products/add';
const options = {
    body: JSON.stringify({
      title: 'BMW Pencil'
    }),
};

await TailorFetch.POST(url, options);

Supported Methods:

  • GET - Retrieves data or information from a specified resource.
  • POST - Submits data to be processed to a specified resource.
  • PUT - Updates a specified resource or creates it if it doesn't exist.
  • PATCH - Applies partial modifications to a resource.
  • DELETE - Deletes a specified resource.
  • HEAD - Requests headers from a specified resource without the actual data.
  • OPTIONS - Requests information about the communication options for a resource.

Options:

  • headers - An object containing request headers.
  • queryParams - An object containing query parameters for the URL.
  • timeout - The request timeout in milliseconds.
  • json - Set to true to parse the response as JSON.
  • body - Data to be sent to remote server
  • transformResponse - A custom response transformer.
  • requestInterceptor - A custom request interceptor
  • requestMode
  • requestCache
  • requestCredentials
  • onProgress - Callback function for progress reporting
  • cache:
    • expiresIn - How long should cache be valid for (milliseconds)
    • redisClient - Redis client instance
  • retry:
    • maxRetries - Maximum number of times to attempt to make an HTTP request
    • retryDelay - Number of milliseconds to wait between attempts

Cache

Built-in cache

Warning Built-in cache might be unstable and might return live results at all times

By default, when cache option is specified with only expiry time and no redis client option has been specified then internal cache will be used.

Redis Cache

You can use Redis cache with GET requests by supplying Redis client to request cache options as follows.

import TailorFetch, { TailorResponse } from 'tailorfetch';
import {createClient} from 'redis';

// Setup redis
const client = createClient();
client.on('error', (error) => console.log(error));
client.connect();

// Request options
const options = {
   cache: {
      expiresIn: 600000,
      redisClient: client
   }
};

await TailorFetch.GET('https://dummyjson.com/products/1', options);

Custom Request Interceptor

You can define a custom request transformer by extending the BaseRequestInterceptor class and overriding the intercept method. Modify headers, add authentication, or enhance the request before it's sent.

Class based interceptor:

import {BaseRequestInterceptor} from 'tailorfetch';

class MyInterceptor implements BaseRequestInterceptor {
   intercept(requestOptions: RequestInit) {
      // Custom intercept logic
      
      return requestOptions;
   }
}

const options = {
   requestInterceptor: new MyInterceptor(),
};

await TailorFetch.GET(url, options);

Function based interceptor:

import {IRequestOptions} from 'tailorfetch';

const options = {
   requestInterceptor: (requestOptions: RequestInit): RequestInit => {
       
       // Custom request interceptor logic
       return requestOptions;
   },
};

await TailorFetch.GET(url, options);

Custom Response Transformer

You can define a custom response transformer by extending the BaseTransform class and overriding the transform method.

Class based transformer:

import TailorFetch, {BaseTransform, IRequestOptions} from 'tailorfetch';

class MyTransformer implements BaseTransform {
   transform(responseData: any, requestOptions: IRequestOptions) {
      // Custom transformation logic
      return transformedData;
   }
}

const options = {
   transformResponse: new MyTransformer,
};

await TailorFetch.GET(url, options);

Function based transformer:

import TailorFetch, {BaseTransform, IRequestOptions} from 'tailorfetch';

const options = {
   transformResponse: (responseData: any, requestOptions: IRequestOptions): any => {
      // Custom transformation logic
      return transformedData;
   }
}

await TailorFetch.GET(url, options);

Helper Functions

TailorResponse

Following helper functions are available on TailorResponse returned when request is made

  • json() - Returns parsed JSON data, only if parseJSON request option is false
  • successful() - Returns boolean whether request was successful
  • failed() - Returns boolean whether request has failed
  • clientError() - Returns boolean whether an error was client error
  • serverError() - Returns boolean whether an error was server error