File tree Expand file tree Collapse file tree 2 files changed +24
-9
lines changed Expand file tree Collapse file tree 2 files changed +24
-9
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 1
1
var request = require ( "request" ) ,
2
2
querystring = require ( 'querystring' ) ,
3
3
Parse = require ( 'parse/node' ) . Parse ;
4
+ HTTPResponse = require ( './HTTPResponse' ) . HTTPResponse ;
4
5
5
6
var encodeBody = function ( options = { } ) {
6
7
let body = options . body ;
@@ -62,15 +63,8 @@ module.exports = function(options) {
62
63
}
63
64
return promise . reject ( error ) ;
64
65
}
65
- var httpResponse = { } ;
66
- httpResponse . status = response . statusCode ;
67
- httpResponse . headers = response . headers ;
68
- httpResponse . buffer = response . body ;
69
- httpResponse . cookies = response . headers [ "set-cookie" ] ;
70
- httpResponse . text = response . body . toString ( 'utf-8' ) ;
71
- try {
72
- httpResponse . data = JSON . parse ( httpResponse . text ) ;
73
- } catch ( e ) { }
66
+ let httpResponse = new HTTPResponse ( response ) ;
67
+
74
68
// Consider <200 && >= 400 as errors
75
69
if ( httpResponse . status < 200 || httpResponse . status >= 400 ) {
76
70
if ( callbacks . error ) {
You can’t perform that action at this time.
0 commit comments