request + RxJs + JSONStream
npm install @waldojeffers/rx-requestbash
npm install @waldojeffers/rx-request
`
Then, in a node.js file :
`js
const RxRequest = require('@waldojeffers/rx-request');
`
Usage
$3
where :
* url | options (String | Object) : string URL, or an options object to pass to request
* path (String) : a JSON path that will be passed to JSONStream.parse. Each element matched by this path will be emitted as a single element in the resulting observable sequence.
HTTP error handling : By default, if a request returns a statusCode which is not in the 2xx Success range (ie [200, 299]), the observable sequence will emit an error. Thus, you can make sure every request in your observable sequence has succeeded. Caution : if a single request fails, the entire observable sequence might fail as well if you use RxJs operators which do not handle errors (eg concatMap, flatMap).
$3
where :
* HTTP_METHOD : any HTTP method supported by request.
This is just a shorthand method (provided by request). All parameters are the same as above.
Examples
$3
The following request :
`js
RxRequest.get('https://swapi.co/api/starships/').subscribe(...)
`
will give you the entire JSON response as one element in the observable sequence :
`json
{
"count": 37,
"next": "http://swapi.co/api/starships/?page=2",
"previous": null,
"results": [
{
"name": "Death Star"
},
{
"name": "Millennium Falcon"
}
]
}
`
If you want to perform modifications on each starship in your observable sequence, you will need to use RxJs's flatMap or concatMap operator as follows :
`js
RxRequest.get('https://swapi.co/api/starships/').flatMap(data => data.results).subscribe(...)
`
$3
The following request :
`js
RxRequest.get('https://swapi.co/api/starships/', 'results.*').subscribe(...)
`
will emit each starship as an element of the observable sequence, avoiding you the extra flattening operation :
`json
{
"name": "Death Star"
}
{
"name": "Millennium Falcon"
}
``