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

@dmitryrechkin/request-sender-core

v1.0.3

Published

**Request Sender Core is a TypeScript library that provides a unified and extensible interface for sending HTTP requests.** This library is designed to be the foundation for implementing request-sending adapters, allowing you to easily integrate with vari

Downloads

297

Readme

Request Sender Core

Request Sender Core is a TypeScript library that provides a unified and extensible interface for sending HTTP requests. This library is designed to be the foundation for implementing request-sending adapters, allowing you to easily integrate with various APIs or services while keeping your codebase clean and consistent.

Installation

Install the package using pnpm:

pnpm add @dmitryrechkin/request-sender-core

Features

  • Unified Interface: A single interface (RequestSenderInterface) to handle HTTP requests, making it easy to swap out implementations without changing the consumer code.
  • Factory Pattern: Use the RequestSenderFactoryInterface to create instances of request senders, allowing for dynamic instantiation based on specific requirements.
  • Extensibility: Easily extend the library using the Adapter Pattern to create specialized request senders for different services.
  • Error Handling: Built-in error handling for network failures, returning custom responses to ensure consistent behavior.

Usage

Sending a Basic HTTP Request

import { HttpRequestSender } from "@dmitryrechkin/request-sender-core";

const requestSender = new HttpRequestSender();
const response = await requestSender.send('https://example.com/api', {
    method: 'GET',
    headers: { 'Content-Type': 'application/json' },
});

console.log('Response status:', response.status);

Using the Factory Pattern

The factory pattern allows you to create instances of RequestSenderInterface dynamically, which can be useful in scenarios where you need different implementations based on runtime conditions.

Creating a Factory

First, implement the RequestSenderFactoryInterface:

import { RequestSenderFactoryInterface } from "@dmitryrechkin/request-sender-core";
import { HttpRequestSender } from "@dmitryrechkin/request-sender-core";

export class HttpRequestSenderFactory implements RequestSenderFactoryInterface {
    public create(...args: unknown[]): HttpRequestSender {
        // Create and return a new HttpRequestSender instance
        return new HttpRequestSender();
    }
}

Using the Factory to Create a Request Sender

import { HttpRequestSenderFactory } from './HttpRequestSenderFactory';

const factory = new HttpRequestSenderFactory();
const requestSender = factory.create(); // Create a new HttpRequestSender instance

const response = await requestSender.send('https://example.com/api', {
    method: 'POST',
    body: JSON.stringify({ key: 'value' }),
});

console.log('Response status:', response.status);

Extending with Custom Adapters

To create a specialized adapter for a specific service, implement the RequestSenderInterface:

import { RequestSenderInterface } from "@dmitryrechkin/request-sender-core";

export class CustomServiceRequestSender implements RequestSenderInterface {
    public async send(url: string, options: RequestInit = {}): Promise<Response> {
        // Custom implementation for sending requests to your specific service
        return fetch(url, {
            ...options,
            // Add custom logic here
        });
    }
}

Using a Custom Adapter with a Factory

You can also create a factory for your custom adapter:

import { RequestSenderFactoryInterface } from "@dmitryrechkin/request-sender-core";
import { CustomServiceRequestSender } from './CustomServiceRequestSender';

export class CustomServiceRequestSenderFactory implements RequestSenderFactoryInterface {
    public create(...args: unknown[]): CustomServiceRequestSender {
        return new CustomServiceRequestSender();
    }
}

This pattern allows you to abstract away the creation logic and use the same factory interface for different implementations, providing flexibility and modularity in your codebase.

When to Use

Request Sender Core is ideal for projects that require a flexible and maintainable approach to handling HTTP requests, such as:

  • API Integrations: Build integrations with various APIs using a consistent interface.
  • Service Adapters: Create custom adapters for different services, ensuring your codebase remains clean and modular.
  • Error-Resilient Applications: Leverage built-in error handling to manage network failures and ensure consistent responses.
  • Dynamic Request Handling: Use factories to dynamically create instances of request senders based on runtime conditions.

Installation & Setup

Install the package using pnpm:

pnpm add @dmitryrechkin/request-sender-core

Ensure your project is set up to handle TypeScript and supports ES modules, as this library is built with modern JavaScript standards.

Rationale

Extensibility with the Adapter and Factory Patterns

The Request Sender Core library is built with extensibility in mind, using both the Adapter and Factory Patterns. These patterns allow you to:

  • Decouple Implementation from Usage: The consumer code only interacts with interfaces, not specific implementations. This makes it easier to switch out implementations as needed.
  • Promote Reusability: Create adapters and factories that can be reused across different projects or modules, saving time and effort.
  • Simplify Testing: With consistent interfaces, testing becomes easier since you can mock or replace implementations without modifying the tests themselves.
  • Dynamic Instantiation: The factory pattern allows for flexible and dynamic creation of request sender instances, tailored to the specific needs of your application.

Contributing

Contributions are welcome! Feel free to fork this project and submit pull requests. Before submitting, please ensure your code passes all linting and unit tests.

You can run unit tests using:

pnpm test