@@ -59,7 +59,7 @@ export default class Agent {
59
59
* @param {string } uri The URI to request
60
60
* @param {string } [auth] Authorization token to use
61
61
* @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
62
- * @param {string| object } [query] Key/VAlue pairs of query params or a correctly formatted string
62
+ * @param {object } [query] Key/Value pairs of query params
63
63
* @param {object } [context] The invocation context, describing the tool and project
64
64
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
65
65
*/
@@ -72,7 +72,7 @@ export default class Agent {
72
72
* @param {string } uri The URI to request
73
73
* @param {string } [auth] Authorization token to use
74
74
* @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
75
- * @param {string| object } [query] Key/VAlue pairs of query params or a correctly formatted string
75
+ * @param {object } [query] Key/Value pairs of query params
76
76
* @param {object } [context] The invocation context, describing the tool and project
77
77
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
78
78
*/
@@ -85,7 +85,7 @@ export default class Agent {
85
85
* @param {string } uri The URI to request
86
86
* @param {string } [auth] Authorization token to use
87
87
* @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
88
- * @param {string| object } [query] Key/VAlue pairs of query params or a correctly formatted string
88
+ * @param {object } [data] Payload to send in the request body in JSON format
89
89
* @param {object } [context] The invocation context, describing the tool and project
90
90
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
91
91
*/
@@ -98,7 +98,7 @@ export default class Agent {
98
98
* @param {string } uri The URI to request
99
99
* @param {string } [auth] Authorization token to use
100
100
* @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
101
- * @param {string| object } [query] Key/VAlue pairs of query params or a correctly formatted string
101
+ * @param {object } [data] Payload to send in the request body in JSON format
102
102
* @param {object } [context] The invocation context, describing the tool and project
103
103
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
104
104
*/
@@ -111,7 +111,7 @@ export default class Agent {
111
111
* @param {string } uri The URI to request
112
112
* @param {string } [auth] Authorization token to use
113
113
* @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
114
- * @param {string| object } [query] Key/VAlue pairs of query params or a correctly formatted string
114
+ * @param {object } [data] Payload to send in the request body in JSON format
115
115
* @param {object } [context] The invocation context, describing the tool and project
116
116
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
117
117
*/
@@ -125,9 +125,9 @@ export default class Agent {
125
125
* @param {String } config.uri The URI to request
126
126
* @param {String } config.method The method used to request the URI, should be in uppercase.
127
127
* @param {Object } config.headers Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
128
- * @param {String } config.data Arbitrary data to send as the body.
128
+ * @param {Object } config.data Arbitrary data to send as the body.
129
129
* @param {Object } config.auth Authorization
130
- * @param {String|Object } config.query Query parameters
130
+ * @param {String } config.query Query parameters
131
131
* @param {Object } config.form Form fields
132
132
* @param {Object } config.files array of file names and file content
133
133
* @param {Object } config.context the invocation context, describing the tool and project.
@@ -154,6 +154,8 @@ export default class Agent {
154
154
/**
155
155
* Promises to send the request and retreive the response.
156
156
* @param {[string, object] } requestParams First argument is the URI to request, the second one are the options.
157
+ * @param {boolean } isBuffer Indicate if the response body should be returned as a Buffer (Node) / ArrayBuffer (browser) instead of JSON
158
+ * @param {Fetch } [makerequest] The fetch function to use. Override for testing.
157
159
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
158
160
* @private
159
161
*/
@@ -216,35 +218,25 @@ export default class Agent {
216
218
actualUri = `${ this . baseUrl } ${ uri } ` ;
217
219
}
218
220
if ( query ) {
219
- let queryParams ;
220
- if ( typeof query === 'string' ) {
221
- queryParams = query ;
222
- } else {
223
- queryParams = qs . stringify ( query ) ;
224
- }
221
+ const queryParams = qs . stringify ( query ) ;
225
222
const hasParams = actualUri . includes ( '?' ) ;
226
223
actualUri = `${ actualUri } ${ hasParams ? '&' : '?' } ${ queryParams } ` ;
227
224
}
228
225
229
226
let body ;
230
- let contentType = { 'Content-Type' : 'application/x-www-form-urlencoded' } ;
227
+ let contentTypeHeader ;
231
228
if ( files ) {
232
- contentType = { } ; // Needed to allow fetch create it's own
229
+ contentTypeHeader = { } ; // Needed to allow fetch create its own
233
230
body = this . _getFromData ( files , form ) ;
234
231
} else if ( form ) {
232
+ contentTypeHeader = { 'Content-Type' : 'application/x-www-form-urlencoded' } ;
235
233
body = qs . stringify ( form ) ;
236
234
} else if ( data ) {
237
- if ( typeof data === 'object' ) {
238
- contentType = { 'Content-Type' : 'application/json' } ;
239
- body = qs . stringify ( data ) ;
240
- }
241
- if ( typeof data === 'string' ) {
242
- contentType = { 'Content-Type' : 'application/x-www-form-urlencoded' } ;
243
- body = data ;
244
- }
235
+ contentTypeHeader = { 'Content-Type' : 'application/json' } ;
236
+ body = JSON . stringify ( data ) ;
245
237
}
246
238
const finalHeaders = Object . assign ( { } ,
247
- contentType ,
239
+ contentTypeHeader ,
248
240
this . _getAuthorizationHeader ( auth ) ,
249
241
this . _getContextHeaders ( context ) ,
250
242
headers
0 commit comments