0.1.2 • Published 4 years ago

@tolujimoh/request-multiple-urls v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Request Multiple Urls

This package fetches the content of multiple urls and returns them in an array.

NPM version

Installation

yarn add @tolujimoh/request-multiple-urls

Usage

requestMultipleUrls(url [, urlsOptions ] [, urlIndexOptions])

Where
  • urls is an Array of urls to be fetch
  • urlsOptions is the options that is applied to all provided urls
  • urlIndexOptions is an Object of options with index of targeted url (index in urls) as keys i.e the url that the options should be applied to
on success
  • returns urlContent as an array of Object or string

  • Response Object

    • httpStatus is the HTTP Status Code
    • body is the content of url
    • urlIndex is the index of url in urls
    • url is the url string
on failure
  • throws a TypeError, RangeError, or Error Error

  • Error Object

    • message is the error message
    • urlIndex is the index of url in urls
    • url is the url string
    • httpStatus is the HTTP Status Code (Optional)

Options

  • retries number
    • is the number of times a failed request should at most be retried, (default is 0)
  • retryInterval number
    • is the interval is ms between retires, (default is 1000ms)
  • responseType "json" | "text",
    • is the type the response that should be returned ("json" returns an Object, "text" returns a string)
Note

This module uses fetch to make http/https request. As such, all options from the fetch api is also available. Here is a link to the fetch documentation Link

Examples

const requestMultipleUrls = require('@tolujimoh/request-multiple-urls');

const urls = [
'https://ft-tech-test-example.s3-eu-west-1.amazonaws.com/ftse-fsi.json',
'https://ft-tech-test-example.s3-eu-west-1.amazonaws.com/gbp-hkd.json',
'https://ft-tech-test-example.s3-eu-west-1.amazonaws.com/gbp-usd.json'
];

requestMultipleUrls(urls, 
    {
        retries: 5, 
        retryInterval: 500,
        responseType: "json"
    }, {
        1: {
            retries: 3 
        }
    })
    .then(urlContent => {
        // on Success
    })
    .catch(error => {
        // on Failure
    });
with fetch API options
const requestMultipleUrls = require('@tolujimoh/request-multiple-urls');

const urls = [
'https://ft-tech-test-example.s3-eu-west-1.amazonaws.com/ftse-fsi.json',
'https://ft-tech-test-example.s3-eu-west-1.amazonaws.com/gbp-hkd.json',
'https://ft-tech-test-example.s3-eu-west-1.amazonaws.com/gbp-usd.json'
];

requestMultipleUrls(urls, 
    {
        retries: 3,
        // fetch api options
        headers: {
            "Authorization": "Bearer {token}"
        }
    })
    .then(urlContent => {
        // on Success
    })
    .catch(error => {
        // on Failure
    });

Dependencies

  • es6-promise
  • isomorphic-fetch

Development

Run tests

Navigate to project directory

$ yarn
$ yarn test

It has a 91.18% test coverage

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago