Skip to content

Commit

Permalink
feat: add reviver option
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Feb 4, 2019
1 parent 1d7521b commit 7a24210
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions httpie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface HttpieOptions {
[name: string]: string
},
redirect: boolean,
reviver: (key: string, value: any) => any,
}

export interface HttpieResponse<T = any> extends IncomingMessage {
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ Whether or not redirect responses should be followed automatically.

> **Note:** This may only happen with a 3xx status _and_ if the response had a [`Location`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Location) header.
#### opts.reviver
Type: `Function`<br>
Default: `undefined`

An optional function that's passed directly to [`JSON.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Parameters), allowing you transform aspects of the response data before the `httpie` request resolves.

> **Note:** This will _only_ run if `httpie` detects that JSON is contained in the response!
### get(url, opts={})
> Alias for [`send('GET', url, opts)`](#sendmethod-url-opts).
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function send(method, uri, opts={}) {
r.on('end', () => {
let type = r.headers['content-type'];
if (type && out && type.includes('application/json')) {
out = JSON.parse(out);
out = JSON.parse(out, opts.reviver);
}
r.data = out;
if (r.statusCode >= 400) {
Expand Down

0 comments on commit 7a24210

Please sign in to comment.