Minimalist API for HTTP requests from Node.js or browsers.
Using yarn
:
yarn add @twuni/http-request
Using npm
:
npm install @twuni/http-request
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 } }
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
});