🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

xfetch

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xfetch

A light-weight module that brings window.fetch to Node.js (with HTTP_PROXY support).

3.1.1
Source
npm
Version published
Weekly downloads
4
-91.3%
Maintainers
1
Weekly downloads
 
Created
Source

xfetch

Travis build status Coveralls NPM version Canonical Code Style

A light-weight module that brings window.fetch to Node.js (with HTTP_PROXY support).

API

type HeadersConfigurationType = {
  [key: string]: string | number
};

type RawHeadersType = {|
  [key: string]: Array<string>
|};

type HeadersType = {|
  +raw: () => RawHeadersType,
  +get: (name: string) => string
|};

type IsResponseRedirectType = (Response: ResponseType) => boolean;

type IsResponseValidType = (response: ResponseType) => boolean | Promise<boolean>;

type HttpMethodType = 'get' | 'post' | 'delete' | 'post' | 'trace';

/**
 * @see https://github.com/tim-kos/node-retry#retrytimeoutsoptions
 */
type RetryConfigurationType = {
  factor?: number,
  maxTimeout?: number,
  minTimeout?: number,
  randomize?: boolean,
  retries?: number
};

type ResponseType = {|
  +headers: HeadersType,
  +json: () => Promise<Object>,
  +status: number,
  +text: () => Promise<string>
|} | string;

/**
 * @property isResponseValid Used to validate response. Refer to [Validate response](#validate-response).
 * @property retry Used to retry requests that produce response that does not pass validation. Refer to [Retry request](#retry-request) and [Validating response](#validating-response).
 * @property jar An instance of `tough-cookie` [`CookieJar`](https://github.com/salesforce/tough-cookie#cookiejar). Used to collect & set cookies.
 */
type UserConfigurationType = {
  +body?: string,
  +compress?: boolean,
  +headers?: HeadersConfigurationType,
  +isResponseRedirect?: IsResponseRedirectType,
  +isResponseValid?: IsResponseValidType,
  +jar?: CookieJar,
  +method?: HttpMethodType,
  +retry?: RetryConfigurationType,
  +responseType?: 'full' | 'text'
};

type fetch = (url: string, configuration?: ConfigurationType) => Promise<ResponseType>;

Behaviour

HTTP proxy

Uses HTTP_PROXY (http://host:port) environment variable value to configure HTTP proxy.

Note: You must also configure NODE_TLS_REJECT_UNAUTHORIZED=0. This is a lazy workaround to enable the proxy to work with TLS.

Throws an error if response code is non-2xx or 3xx

Throws UnexpectedResponseCodeError error if response code is non-2xx or 3xx.

This behaviour can be overrode using isResponseValid configuration.

Cookbook

Retry request

Requests that result in non-2xx response will be retried.

retry option is used to configure request retry strategy.

The retry configuration shape matches configuration of the retry module.

Validate response

Define a custom validator function to force use the request retry strategy.

A custom validator is configured using isResponseValid configuration, e.g.

import xfetch, {
  UnexpectedResponseError
};

const isResponseValid = async (response) => {
  const body = await response.text();

  if (body.includes('rate error')) {
    throw new UnexpectedResponseError(response);
  }

  return true;
}

await xfetch('http://gajus.com', {
  isResponseValid
});

A custom validator must return a boolean flag indicating whether the response is valid. A custom validator can throw an error that extends UnexpectedResponseError error.

xfetch default validator can be imported and used to extend a custom validator, e.g.

import xfetch, {
  UnexpectedResponseError,
  isResponseValid as defaultIsResponseValid
};

const isResponseValid = async (response) => {
  const responseIsValid = await defaultIsResponseValid(response);

  if (!responseIsValid) {
    return responseIsValid;
  }

  const body = await response.text();

  if (body.includes('rate error')) {
    throw new UnexpectedResponseError(response);
  }

  return true;
}

await xfetch('http://gajus.com', {
  isResponseValid
});

Make cookies persist between requests

jar parameter can be passed an instance of tough-cookie CookieJar to collect cookies and use them for the following requests.

import xfetch, {
  CookieJar
};

const jar = new CookieJar();

await xfetch('http://gajus.com/this-url-sets-cookies', {
  jar
});

await xfetch('http://gajus.com/this-url-requires-cookies-to-be-present', {
  jar
});

Note:

xfetch exports CookieJar class that can be used to construct an instance of tough-cookie CookieJar.

Keywords

promise

FAQs

Package last updated on 17 Mar 2017

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