Warning
n-fetch has been deprecated as of 2024-02-08. It will reach end-of-life on 2024-07-01 at which point no further security patches will be applied. The library will continue to work in currently-supported versions of Node.js but it should not be used in new projects.
This package is a wrapper for node-fetch and provides an interface for fetching resources server side.
Additional features:
- If the resouce returns JSON, then the response will be sent as JSON automatically
- Errors are logged to the console using Reliability Kit logger
- Node version defined by
engines.node
inpackage.json
. Run commandnvm use
to switch your local Node version to the one specified in.nvmrc
.
git clone git@github.com:Financial-Times/n-fetch.git
cd n-fetch
make install
In order to run the tests locally you'll need to run:
make test
npm install --save @financial-times/n-fetch
const fetch = require('@financial-times/n-fetch');
fetch('https://api.fastly.com/public-ip-list', {
method: 'GET',
headers: {}, // request headers. format is the identical to that accepted by the Headers constructor (see below)
body: null, // request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream
redirect: 'follow', // set to `manual` to extract redirect headers, `error` to reject redirect
signal: null, // pass an instance of AbortSignal to optionally abort requests
follow: 20, // maximum redirect count. 0 to not follow redirect
timeout: 0, // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies). Signal is recommended instead.
compress: true, // support gzip/deflate content encoding. false to disable
size: 0, // maximum response body size in bytes. 0 to disable
agent: null // http(s).Agent instance or function that returns an instance (see below)
}).then((data) => {
console.log(data);
}).catch((error) => {
console.log(error);
});
This library exports a single default method with the following arguments and returns a promise.
input
- A string representing the URL for fetchinginit
- an object with the request options