Tuesday, April 30, 2024
HomeJavaScriptrekwest - npm

rekwest – npm


The sturdy request library that humanity deserves 🌐

This bundle supplies extremely doubtless purposeful and easy-to-use abstraction atop of
native http(s).request
and http2.request.

Summary

  • Fetch-alike
  • Cool-beans 🫐 config choices (with defaults)
  • Automated HTTP/2 assist (ALPN negotiation)
  • Automated or opt-in physique parse (with non-UTF-8 charset decoding)
  • Automated and simplistic Cookies therapy (with built-in jar)
  • Automated decompression (with opt-in physique compression)
  • Constructed-in streamable File & FormData interfaces
  • Help redirects & retries with fine-grained tune-ups
  • Help all legit request physique varieties (embody blobs & streams)
  • Help each CJS and ESM module methods
  • Absolutely promise-able and pipe-able
  • Zero dependencies

Conditions

Set up

npm set up rekwest --save

Utilization

import rekwest, { constants } from 'rekwest';

const {
  HTTP2_HEADER_AUTHORIZATION,
  HTTP2_HEADER_CONTENT_ENCODING,
  HTTP2_METHOD_POST,
  HTTP_STATUS_OK,
} = constants;

const url = 'https://somewhe.re/considerably/endpoint';

const res = await rekwest(url, {
  physique: { celestial: 'payload' },
  headers: {
    [HTTP2_HEADER_AUTHORIZATION]: 'Bearer [token]',
    [HTTP2_HEADER_CONTENT_ENCODING]: 'br',  // allows: physique compression
    /** [HTTP2_HEADER_CONTENT_TYPE]
     * is undue for
     * Array/Blob/File/FormData/Object/URLSearchParams physique varieties
     * and shall be set routinely, with an choice to override it right here
     */
  },
  technique: HTTP2_METHOD_POST,
});

console.assert(res.statusCode === HTTP_STATUS_OK);
console.data(res.headers);
console.log(res.physique);

import rekwest, {
  constants,
  Blob,
  File,
  FormData,
} from 'rekwest';
import { Readable } from 'node:stream';

const {
  HTTP2_HEADER_AUTHORIZATION,
  HTTP2_HEADER_CONTENT_ENCODING,
  HTTP2_METHOD_POST,
  HTTP_STATUS_OK,
} = constants;

const blob = new Blob(['bits']);
const file = new File(['bits'], 'file.dab');
const readable = Readable.from('bits');

const fd = new FormData({
  aux: Date.now(),  // both [[key, value]], or kv sequenceable
});

fd.append('celestial', 'payload');
fd.append('blob', blob, 'blob.dab');
fd.append('file', file);
fd.append('readable', readable, 'readable.dab');

const url = 'https://somewhe.re/considerably/endpoint';

const res = await rekwest(url, {
  physique: fd,
  headers: {
    [HTTP2_HEADER_AUTHORIZATION]: 'Bearer [token]',
    [HTTP2_HEADER_CONTENT_ENCODING]: 'br',  // allows: physique compression
  },
  technique: HTTP2_METHOD_POST,
});

console.assert(res.statusCode === HTTP_STATUS_OK);
console.data(res.headers);
console.log(res.physique);

API

rekwest(url[, options])

  • url URL The URL to ship the request to
  • choices {Object}
    Extends https.RequestOptions
    together with
    additional http2.ClientSessionOptions
    & http2.ClientSessionRequestOptions
    and tls.ConnectionOptions
    for HTTP/2 attunes

    • physique ArrayBuffer The physique to ship with the request
    • cookies Object Default: true The cookies so as to add to
      the request
    • digest {boolean} Default: true Controls whether or not to learn the response stream or just add a mixin
    • observe {quantity} Default: 20 The variety of redirects to observe
    • h2 {boolean} Default: false Forces the usage of HTTP/2 protocol
    • headers {Object} The headers so as to add to the request
    • maxRetryAfter {quantity} The higher restrict of retry-after header. If unset, it would use timeout worth
    • parse {boolean} Default: true Controls whether or not to parse response physique or just return a buffer
    • redirect guide Default: observe Controls the redirect flows
    • retry {Object} Represents the retry choices

      • makes an attempt {quantity} Default: 0 The variety of retry makes an attempt
      • backoffStrategy {string} Default: interval * Math.log(Math.random() * (Math.E * Math.E - Math.E) + Math.E)
        The backoff technique algorithm that will increase logarithmically. To fixate set worth to interval * 1
      • interval {quantity} Default: 1e3 The preliminary retry interval
      • retryAfter {boolean} Default: true Controls retry-after header receptiveness
      • statusCodes {quantity[]} Default: [429, 503] The listing of standing codes to retry on
    • thenable {boolean} Default: false Controls the promise resolutions
    • timeout {quantity} Default: 3e5 The variety of milliseconds a request can take earlier than termination
    • trimTrailingSlashes {boolean} Default: false Controls whether or not to trim trailing slashes within the URL earlier than
      proceed with the request
  • Returns: Promise that resolves to
    prolonged http.IncomingMessage
    or http2.ClientHttp2Stream which is respectively
    readable and duplex streams

    • if degist: true & parse: true
      • physique Array The physique based mostly on its content material sort
    • if degist: false
      • arrayBuffer {AsyncFunction} Reads the response and returns ArrayBuffer
      • blob {AsyncFunction} Reads the response and returns Blob
      • physique {AsyncFunction} Reads the response and returns Buffer if parse: false
      • json {AsyncFunction} Reads the response and returns Object
      • textual content {AsyncFunction} Reads the response and returns String
    • bodyUsed {boolean} Signifies whether or not the response have been learn or not
    • cookies Cookies The cookies despatched and acquired with the response
    • headers {Object} The headers acquired with the response
    • httpVersion {string} Signifies protocol model negotiated with the server
    • okay {boolean} Signifies if the response was profitable (statusCode: 200-299)
    • redirected {boolean} Signifies if the response is the results of a redirect
    • statusCode {quantity} Signifies the standing code of the response
    • trailers Object The trailer headers acquired with the response

rekwest.defaults

The thing to meet with default choices


rekwest.stream(url[, options])

The strategy with restricted performance to make use of with streams and/or pipes

  • No automata
  • No redirects
  • Move h2: true in choices to make use of HTTP/2 protocol
    • Or use ackn({ url: URL }) technique prematurely to probe the obtainable protocols

For extra particulars, please examine exams (protection: >97%) within the repository

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments