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

@jaseeey/request-manager

v2.0.1

Published

A library to prevent multiple API requests when called simultaneously

Downloads

23

Readme

RequestManager

RequestManager is a TypeScript-based library designed to manage and regulate HTTP requests efficiently. It ensures that multiple requests to the same URL with the same method are not made concurrently, thus preventing duplicate requests. When a request to a specific URL and method is in progress, subsequent requests to the same URL and method will return the same promise.

Background and Scope

The RequestManager library was specifically written to address a particular issue encountered in my projects. It was not designed or intended to serve as a comprehensive request management solution. Instead, its primary aim is to tackle the specific challenges I faced, offering a targeted approach to prevent duplicate requests and manage HTTP calls more efficiently.

Given its focused nature, there are a set of known limitations that have been accepted, as these constraints do not affect the scenarios for which the library was developed. While the library provides a solution to the identified issues, its functionality may not cover all use cases or requirements that might be expected from a full-fledged request manager.

Users are encouraged to understand these known limitations and consider how they align with their specific needs before integrating the library into their projects.

Should you wish to extend the behaviour, or modify it for your own purposes, then you are free to do so.

Features

  • Prevents duplicate simultaneous requests to the same URL and method.
  • Supports TypeScript for improved type safety and developer experience.
  • Allows for optional onSuccess and onError callbacks to handle responses.
  • Generates both CommonJS (CJS) and ECMAScript Module (ESM) distributions for broad compatibility.

Installation

To install RequestManager, use the following npm command:

npm install @jaseeey/request-manager

Usage

Here's a basic example of how to use the RequestManager library:

With Callbacks

import { RequestManager } from '@jaseeey/request-manager';

const client = axios.create();
const url = 'https://example.com';
const method = 'GET';

RequestManager.call(client, method, url, null, null, res => {
    // Handle your response here, any processing will only be run once.
}, err => {
    // Handle your error here
});

Without Callbacks

import { RequestManager } from '@jaseeey/request-manager';

const client = axios.create();
const url = 'https://example.com';
const method = 'GET';

try {
    const response = await RequestManager.call(client, method, url);
    // Handle your response here, any processing will be run multiple times upon multiple requests.
}
catch (err) {
    // Handle your error here
}

API Reference

call(client, method, url, data, config, onSuccess, onError)

  • client: The HTTP client instance used for making requests.
  • method: The HTTP method (e.g., 'GET', 'POST').
  • url: The URL to which the request is sent.
  • data (optional): The data to be sent as the request body.
  • config (optional): The configuration options for the request.
  • onSuccess (optional): A callback function that is called when the request is successful.
  • onError (optional): A callback function that is called when the request fails.

Known Limitations

Unified Callback Execution

The RequestManager executes only the onSuccess or onError callbacks of the first request in the case of identical requests made concurrently. This design choice optimises network and computational resources but may limit individual response handling flexibility. Though, you could simply bypass the use of the callbacks and handle each response independently.

State and Error Handling

Given that only the first set of callbacks are executed, managing state updates or performing granular error handling based on different parts of the application's needs could be challenging. This approach assumes a uniform handling strategy for success and error responses.

Request Differentiation

The system identifies duplicate requests based solely on URL and method, potentially overlooking requests that differ in headers, query parameters, or POST bodies. Applications requiring differentiation based on these factors may need to extend the key generation logic within the RequestManager.

Lifecycle Management

The centralised management of requests might complicate the handling of component lifecycles, such as cancelling requests when components unmount, especially in scenarios where multiple components depend on the outcome of a single request.

Singleton Design

The RequestManager is implemented as a singleton, which inherently restricts the creation of multiple instances of the class. This design choice aligns with the library's goal to centrally manage and de-duplicate HTTP requests across the application. While this meets the requirements for which the library was developed, it may pose a limitation for scenarios where multiple, isolated request managers are needed. Users should consider this design aspect when planning to integrate the library into their applications, especially if there's a potential need for managing requests in isolated contexts.

Contributing

Contributions to the RequestManager library are welcome. If you have suggestions or improvements, feel free to fork the repository and submit a pull request.

License

RequestManager is released under the MIT License.