@waldojeffers/rx-request

request + RxJs + JSONStream

Usage no npm install needed!

<script type="module">
  import waldojeffersRxRequest from 'https://cdn.skypack.dev/@waldojeffers/rx-request';
</script>

README

rx-request

Dependency Status

request + RxJS + JSONStream

Description

A small node.js module that allows you to manipulate the result of an HTTP request as an observable sequence.

Installation

With npm :

npm install @waldojeffers/rx-request

Then, in a node.js file :

const RxRequest = require('@waldojeffers/rx-request');

Usage

RxRequest(url | options, [path = null])

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).

RxRequest.HTTP_METHOD(url | options, [path = null])

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

Without JSON path

The following request :

RxRequest.get('https://swapi.co/api/starships/').subscribe(...)

will give you the entire JSON response as one element in the observable sequence :

{
  "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 :

RxRequest.get('https://swapi.co/api/starships/').flatMap(data => data.results).subscribe(...)

With a JSON path

The following request :

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 :

{
  "name": "Death Star"
}
{
  "name": "Millennium Falcon"
}