You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

simple-http-request-builder

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-http-request-builder

A simple HTTP request interface written in TypeScript

2.1.0
latest
Source
npmnpm
Version published
Weekly downloads
250
-16.94%
Maintainers
0
Weekly downloads
 
Created
Source

Simple HTTP Request Builder

This library provides:

  • A very basic representation of an HTTP request that is independent of any HTTP client
  • A fluent API to create an HTTP request and make an HTTP call (though to execute the call, an HTTP client must be provided)

Installation

Using NPM:

npm install simple-http-request-builder

Using Yarn:

yarn add simple-http-request-builder

Usage

An HTTP client is something that takes an HTTP request as a parameter and returns something. It can be a Promise, an Object or whatever.

Usage example using an HTTP client:

const response: Promise<Response> = new HttpRequest<Promise<Response>>(
  httpClient,
  'http://localhost/api',
  HttpMethod.GET,
  '/session',
)
.queryParams([['username', 'test'], ['password', 'azerty']])
.execute();

The HttpRequest creation process should generally be made using a dedicated class. Examples can be found:

In a browser environment, the base URL can be built using the window.location object: ${window.location.protocol}//${window.location.host}/api

HTTP Client sample using fetch

This sample is from Simple HTTP REST Client library.

const httpClient = (httpRequest: HttpRequest<unknown>): Promise<Response> => {
  const timeoutHandle = setTimeout(
    () => httpRequest.optionValues.timeoutAbortController.abort(),
    httpRequest.optionValues.timeoutInMillis,
  );
  return fetch(
    httpRequest.buildUrl(),
    {
      headers: httpRequest.headersValue,
      method: httpRequest.method,
      body: httpRequest.bodyValue,
      credentials: httpRequest.credentials,
      signal: httpRequest.optionValues.timeoutAbortController.signal,
    },
  )
  .finally(() => clearTimeout(timeoutHandle));
};

Timeout

By default, the request global timeout is 20 seconds (connect + wait + read). This can be configured using the request options method:

const response: Promise<Response> = new HttpRequest<...>(...)
  .queryParams(...)
  // 60 seconds timeout
  .options({ timeoutInMillis: 60000 })
  .execute();

Request cancellation

To cancel the request manually, it is also possible to specify the AbortController as an option:

const abortController = new AbortController();
const response: Promise<Response> = new HttpRequest<...>(...)
  .queryParams(...)
  // 60 seconds timeout
  .options({ timeoutAbortController: abortController })
  .execute();

// then later, the promise can be manually stopped if it hasn't been resolved
abortController.abort(); // the promise is now cancelled

Keywords

http

FAQs

Package last updated on 16 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts