File tree Expand file tree Collapse file tree 2 files changed +25
-10
lines changed Expand file tree Collapse file tree 2 files changed +25
-10
lines changed Original file line number Diff line number Diff line change 1+
2+ export default class HTTPResponse {
3+ constructor ( response ) {
4+ this . status = response . statusCode ;
5+ this . headers = response . headers ;
6+ this . buffer = response . body ;
7+ this . cookies = response . headers [ "set-cookie" ] ;
8+ }
9+
10+ get text ( ) {
11+ return this . buffer . toString ( 'utf-8' ) ;
12+ }
13+ get data ( ) {
14+ if ( ! this . _data ) {
15+ try {
16+ this . _data = JSON . parse ( this . text ) ;
17+ } catch ( e ) { }
18+ }
19+ return this . _data ;
20+ }
21+ }
Original file line number Diff line number Diff line change 11var request = require ( "request" ) ,
2- Parse = require ( 'parse/node' ) . Parse ;
2+ Parse = require ( 'parse/node' ) . Parse ,
3+ HTTPResponse = require ( './HTTPResponse' ) . HTTPResponse ;
34
45var encodeBody = function ( options = { } ) {
56 let body = options . body ;
@@ -54,15 +55,8 @@ module.exports = function(options) {
5455 }
5556 return promise . reject ( error ) ;
5657 }
57- var httpResponse = { } ;
58- httpResponse . status = response . statusCode ;
59- httpResponse . headers = response . headers ;
60- httpResponse . buffer = response . body ;
61- httpResponse . cookies = response . headers [ "set-cookie" ] ;
62- httpResponse . text = response . body . toString ( 'utf-8' ) ;
63- try {
64- httpResponse . data = JSON . parse ( httpResponse . text ) ;
65- } catch ( e ) { }
58+ let httpResponse = new HTTPResponse ( response ) ;
59+
6660 // Consider <200 && >= 400 as errors
6761 if ( httpResponse . status < 200 || httpResponse . status >= 400 ) {
6862 if ( callbacks . error ) {
You can’t perform that action at this time.
0 commit comments