Skip to content

Minimal unified API for HTTP requests in Node.js and browsers.

License

Notifications You must be signed in to change notification settings

twuni/http-request.js

Repository files navigation

@twuni/http-request

CircleCI npm version npm downloads dependencies devDependencies license

Minimalist API for HTTP requests from Node.js or browsers.

Installing

Using yarn:

yarn add @twuni/http-request

Using npm:

npm install @twuni/http-request

Usage

First, import the module:

// Using ES6 modules:
import { http } from '@twuni/http-request';

// ...or, if you are using CommonJS modules:
const { http } = require('@twuni/http-request');

Then, you can use the http() function like this:

const response = await http('https://example.com/hello.json');
// response: { body: ArrayBuffer, headers: {}, status: { code: number, text: string } }

Options

Additional options for the request may be configured via an optional second argument. All available options are described below along with their default values:

await http(url, {
  // The request body, in any format allowed by the Fetch spec (https://fetch.spec.whatwg.org/#bodyinit)
  body: undefined,

  // Key-value pairs for the request headers to be sent. Forbidden headers are excluded (https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name)
  headers: {},

  // If true, the browser will send credentials along with this request (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials)
  includeCredentials: false,

  // The HTTP method to use for this request (https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
  method: 'GET',

  // How long, in milliseconds, to wait for a response before considering the request to have timed out.
  timeout: 300000
});

About

Minimal unified API for HTTP requests in Node.js and browsers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published