1
+ var http_ = require ( 'http' ) ;
2
+ var https = require ( 'https' ) ;
1
3
const os = require ( 'os' ) ;
2
4
const pkg = require ( '../../package.json' ) ;
5
+ const qs = require ( 'qs' ) ;
3
6
const { TwilioCliError } = require ( '../services/error' ) ;
4
7
const { NETWORK_ERROR } = require ( '../services/messaging/help-messages' ) ;
5
8
6
- const NETWORK_ERROR_CODES = new Set ( [ 'ETIMEDOUT' , 'ESOCKETTIMEDOUT' ] ) ;
9
+ const NETWORK_ERROR_CODES = new Set ( [ 'ETIMEDOUT' , 'ESOCKETTIMEDOUT' , 'ECONNABORTED' ] ) ;
7
10
8
11
class CliRequestClient {
9
12
constructor ( commandName , logger , http ) {
10
13
this . commandName = commandName ;
11
14
this . logger = logger ;
12
- this . http = require ( 'util' ) . promisify ( http || require ( 'request' ) ) ;
15
+ this . http = http || require ( 'axios' ) ;
13
16
}
14
17
15
18
/**
@@ -58,20 +61,26 @@ class CliRequestClient {
58
61
59
62
const options = {
60
63
timeout : opts . timeout || 30000 ,
61
- followRedirect : opts . allowRedirects || false ,
64
+ maxRedirects : opts . allowRedirects ? 10 : 0 ,
62
65
url : opts . uri ,
63
66
method : opts . method ,
64
67
headers,
65
- forever : opts . forever !== false
68
+ httpAgent : opts . forever ? new http_ . Agent ( { keepAlive : true } ) : undefined ,
69
+ httpsAgent : opts . forever ? new https . Agent ( { keepAlive : true } ) : undefined ,
70
+ validateStatus : status => {
71
+ return status >= 100 && status < 600 ;
72
+ }
66
73
} ;
67
74
68
75
if ( opts . data ) {
69
- options . formData = opts . data ;
76
+ options . data = qs . stringify ( opts . data , { arrayFormat : 'repeat' } ) ;
70
77
}
71
78
72
79
if ( opts . params ) {
73
- options . qs = opts . params ;
74
- options . useQuerystring = true ;
80
+ options . params = opts . params ;
81
+ options . paramSerializer = params => {
82
+ return qs . stringify ( params , { arrayFormat : 'repeat' } ) ;
83
+ } ;
75
84
}
76
85
77
86
this . lastRequest = options ;
@@ -80,15 +89,19 @@ class CliRequestClient {
80
89
try {
81
90
const response = await this . http ( options ) ;
82
91
83
- this . logger . debug ( 'response.statusCode: ' + response . statusCode ) ;
92
+ this . logger . debug ( 'response.statusCode: ' + response . status ) ;
84
93
this . logger . debug ( 'response.headers: ' + JSON . stringify ( response . headers ) ) ;
85
94
86
- if ( response . statusCode < 200 || response . statusCode >= 300 ) {
87
- const parsed = JSON . parse ( response . body ) ;
95
+ if ( response . status < 200 || response . status >= 300 ) {
96
+ const parsed = response . data ;
88
97
throw new TwilioCliError ( `Error code ${ parsed . code } from Twilio: ${ parsed . message } . See ${ parsed . more_info } for more info.` , parsed . code ) ;
89
98
}
90
99
91
- return response ;
100
+ return {
101
+ body : response . data ,
102
+ statusCode : response . status ,
103
+ headers : response . headers
104
+ } ;
92
105
} catch ( error ) {
93
106
if ( NETWORK_ERROR_CODES . has ( error . code ) ) {
94
107
throw new TwilioCliError ( NETWORK_ERROR ) ;
@@ -102,14 +115,14 @@ class CliRequestClient {
102
115
this . logger . debug ( '-- BEGIN Twilio API Request --' ) ;
103
116
this . logger . debug ( options . method + ' ' + options . url ) ;
104
117
105
- if ( options . formData ) {
118
+ if ( options . data ) {
106
119
this . logger . debug ( 'Form data:' ) ;
107
- this . logger . debug ( options . formData ) ;
120
+ this . logger . debug ( options . data ) ;
108
121
}
109
122
110
- if ( options . qs && Object . keys ( options . qs ) . length > 0 ) {
123
+ if ( options . params && Object . keys ( options . params ) . length > 0 ) {
111
124
this . logger . debug ( 'Querystring:' ) ;
112
- this . logger . debug ( options . qs ) ;
125
+ this . logger . debug ( options . params ) ;
113
126
}
114
127
115
128
this . logger . debug ( 'User-Agent: ' + options . headers [ 'User-Agent' ] ) ;
0 commit comments