@tolujimoh/request-multiple-urls
v0.1.2
Published
This package fetches the content of multiple urls and returns them in an array.
Downloads
3
Readme
Request Multiple Urls
This package fetches the content of multiple urls and returns them in an array.
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
ofoptions
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
orstring
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
, orError
ErrorError 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 astring
)
- is the type the response that should be returned ("json" returns an
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