Skip to content

Commit

Permalink
feat(mapJson): add support for passing in json stream mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 23, 2018
1 parent 9eb0095 commit 0600986
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -336,6 +336,16 @@ See also [`opts.proxy`](#opts-proxy)
Logger object to use for logging operation details. Must have the same methods
as `npmlog`.

##### <a name="opts-map-json"></a> `opts.map-json`

* Alias: `mapJson`, `mapJSON`
* Type: Function
* Default: undefined

When using `fetch.json.stream()` (NOT `fetch.json()`), this will be passed down
to [`JSONStream`](https://npm.im/JSONStream) as the second argument to
`JSONStream.parse`, and can be used to transform stream data before output.

##### <a name="opts-maxsockets"></a> `opts.maxsockets`

* Alias: `opts.max-sockets`
Expand Down
3 changes: 3 additions & 0 deletions config.js
Expand Up @@ -43,6 +43,9 @@ module.exports = figgyPudding({
'log': {
default: silentLog
},
'map-json': 'mapJson',
'mapJSON': 'mapJson',
'mapJson': {},
'max-sockets': 'maxsockets',
'maxsockets': {
default: 12
Expand Down
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -114,7 +114,8 @@ function fetchJSON (uri, opts) {

module.exports.json.stream = fetchJSONStream
function fetchJSONStream (uri, jsonPath, opts) {
const parser = JSONStream.parse(jsonPath)
opts = config(opts)
const parser = JSONStream.parse(jsonPath, opts.mapJson)
const pt = parser.pipe(new PassThrough({objectMode: true}))
parser.on('error', err => pt.emit('error', err))
regFetch(uri, opts).then(res => {
Expand Down
21 changes: 21 additions & 0 deletions test/index.js
Expand Up @@ -255,6 +255,27 @@ test('fetch.json.stream()', t => {
})
})

test('fetch.json.stream opts.mapJson', t => {
tnock(t, OPTS.registry).get('/hello').reply(200, {
a: 1,
b: 2,
c: 3
})
return getStream.array(
fetch.json.stream('/hello', '*', OPTS.concat({
mapJson (value, [key]) {
return [key, value]
}
}))
).then(data => {
t.deepEqual(data, [
['a', 1],
['b', 2],
['c', 3]
], 'data mapped')
})
})

test('opts.ignoreBody', t => {
tnock(t, OPTS.registry)
.get('/hello')
Expand Down

0 comments on commit 0600986

Please sign in to comment.