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
npm install tailorfetch
- 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
import TailorFetch from 'tailorfetch';
await TailorFetch.GET('https://dummyjson.com/products');
import TailorFetch from 'tailorfetch';
const url = 'https://dummyjson.com/products/add';
const options = {
body: JSON.stringify({
title: 'BMW Pencil'
}),
};
await TailorFetch.POST(url, options);
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.
headers
- An object containing request headers.queryParams
- An object containing query parameters for the URL.timeout
- The request timeout in milliseconds.json
- Set totrue
to parse the response as JSON.body
- Data to be sent to remote servertransformResponse
- A custom response transformer.requestInterceptor
- A custom request interceptorrequestMode
requestCache
requestCredentials
onProgress
- Callback function for progress reportingcache
: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 requestretryDelay
- Number of milliseconds to wait between attempts
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.
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);
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);
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);
Following helper functions are available on TailorResponse
returned when request is made
json()
- Returns parsed JSON data, only ifparseJSON
request option is falsesuccessful()
- Returns boolean whether request was successfulfailed()
- Returns boolean whether request has failedclientError()
- Returns boolean whether an error was client errorserverError()
- Returns boolean whether an error was server error