@@ -32,7 +32,7 @@ import packageJson from '../package.json';
32
32
33
33
/**
34
34
* The possible response from an API request
35
- * @typedef {JSONResponse|Buffer|arrayBuffer } RequestResponse The type is based on
35
+ * @typedef {JSONResponse|Buffer|ArrayBuffer } RequestResponse The type is based on
36
36
* the request config and whether is on browser or node
37
37
*/
38
38
@@ -57,11 +57,12 @@ export default class Agent {
57
57
58
58
/**
59
59
* Make a GET request
60
- * @param {string } uri The URI to request
61
- * @param {string } [auth] Authorization token to use
62
- * @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
63
- * @param {object } [query] Key/Value pairs of query params
64
- * @param {object } [context] The invocation context, describing the tool and project
60
+ * @param {object } params Configurations to customize the request
61
+ * @param {string } params.uri The URI to request
62
+ * @param {string|object } [params.auth] Authorization token to use
63
+ * @param {object } [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
64
+ * @param {string|object } [params.query] Key/Value pairs of query params or a correctly formatted string
65
+ * @param {object } [params.context] The invocation context, describing the tool and project
65
66
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
66
67
*/
67
68
get ( { uri, auth, headers, query, context } ) {
@@ -70,11 +71,12 @@ export default class Agent {
70
71
71
72
/**
72
73
* Make a HEAD request
73
- * @param {string } uri The URI to request
74
- * @param {string } [auth] Authorization token to use
75
- * @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
76
- * @param {object } [query] Key/Value pairs of query params
77
- * @param {object } [context] The invocation context, describing the tool and project
74
+ * @param {object } params Configurations to customize the request
75
+ * @param {string } params.uri The URI to request
76
+ * @param {string|object } [params.auth] Authorization token to use
77
+ * @param {object } [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
78
+ * @param {string|object } [params.query] Key/Value pairs of query params or a correctly formatted string
79
+ * @param {object } [params.context] The invocation context, describing the tool and project
78
80
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
79
81
*/
80
82
head ( { uri, auth, headers, query, context } ) {
@@ -83,11 +85,12 @@ export default class Agent {
83
85
84
86
/**
85
87
* Make a POST request
86
- * @param {string } uri The URI to request
87
- * @param {string } [auth] Authorization token to use
88
- * @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
89
- * @param {object } [data] Payload to send in the request body in JSON format
90
- * @param {object } [context] The invocation context, describing the tool and project
88
+ * @param {object } params Configurations to customize the request
89
+ * @param {string } params.uri The URI to request
90
+ * @param {string|object } [params.auth] Authorization token to use
91
+ * @param {object } [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
92
+ * @param {object } [params.data] Key/Value pairs of query params or a correctly formatted string
93
+ * @param {object } [params.context] The invocation context, describing the tool and project
91
94
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
92
95
*/
93
96
post ( { uri, headers, data, auth, context } ) {
@@ -96,11 +99,12 @@ export default class Agent {
96
99
97
100
/**
98
101
* Make a PUT request
99
- * @param {string } uri The URI to request
100
- * @param {string } [auth] Authorization token to use
101
- * @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
102
- * @param {object } [data] Payload to send in the request body in JSON format
103
- * @param {object } [context] The invocation context, describing the tool and project
102
+ * @param {object } params Configurations to customize the request
103
+ * @param {string } params.uri The URI to request
104
+ * @param {string|object } [params.auth] Authorization token to use
105
+ * @param {object } [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
106
+ * @param {object } [params.data] Key/VAlue pairs of query params or a correctly formatted string
107
+ * @param {object } [params.context] The invocation context, describing the tool and project
104
108
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
105
109
*/
106
110
put ( { uri, auth, headers, data, context } ) {
@@ -109,11 +113,12 @@ export default class Agent {
109
113
110
114
/**
111
115
* Make a DELETE request
112
- * @param {string } uri The URI to request
113
- * @param {string } [auth] Authorization token to use
114
- * @param {object } [headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
115
- * @param {object } [data] Payload to send in the request body in JSON format
116
- * @param {object } [context] The invocation context, describing the tool and project
116
+ * @param {object } params Configurations to customize the request
117
+ * @param {string } params.uri The URI to request
118
+ * @param {string|object } [params.auth] Authorization token to use
119
+ * @param {object } [params.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
120
+ * @param {object } [params.data] Key/Value pairs of query params or a correctly formatted string
121
+ * @param {object } [params.context] The invocation context, describing the tool and project
117
122
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
118
123
*/
119
124
delete ( { uri, auth, headers, data, context } ) {
@@ -122,17 +127,17 @@ export default class Agent {
122
127
123
128
/**
124
129
*
125
- * @param {Object } config An obj with all the possible request configurations
126
- * @param {String } config.uri The URI to request
127
- * @param {String } config.method The method used to request the URI, should be in uppercase.
128
- * @param {Object } config.headers Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
129
- * @param {Object } config.data Arbitrary data to send as the body.
130
- * @param {Object } config.auth Authorization
131
- * @param {String } config.query Query parameters
132
- * @param {Object } config.form Form fields
133
- * @param {Object } config.files array of file names and file content
134
- * @param {Object } config.context the invocation context, describing the tool and project.
135
- * @param {boolean } config.isBuffer Indicate if the response should be treated as Buffer instead of JSON
130
+ * @param {object } config An obj with all the possible request configurations
131
+ * @param {string } config.uri The URI to request
132
+ * @param {string } config.method The method used to request the URI, should be in uppercase.
133
+ * @param {object } [ config.headers] Key/Value pairs like `{ 'X-FOO': 'foo', X-BAR: 'bar' }` to send as headers.
134
+ * @param {object } [ config.data] Arbitrary data to send as the body.
135
+ * @param {string|object } [ config.auth] Authorization
136
+ * @param {string|object } [ config.query] Query parameters
137
+ * @param {object } [ config.form] Form fields
138
+ * @param {object } [ config.files] Array of file names and file content
139
+ * @param {object } [ config.context] The invocation context, describing the tool and project.
140
+ * @param {boolean } [ config.isBuffer=false] Indicate if the response should be treated as Buffer instead of JSON
136
141
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
137
142
*/
138
143
request ( {
@@ -156,7 +161,7 @@ export default class Agent {
156
161
* Promises to send the request and retreive the response.
157
162
* @param {[string, object] } requestParams First argument is the URI to request, the second one are the options.
158
163
* @param {boolean } isBuffer Indicate if the response body should be returned as a Buffer (Node) / ArrayBuffer (browser) instead of JSON
159
- * @param {Fetch } [makerequest] The fetch function to use. Override for testing.
164
+ * @param {function } [makerequest=fetch ] The fetch function to use. Override for testing.
160
165
* @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
161
166
* @private
162
167
*/
@@ -213,6 +218,11 @@ export default class Agent {
213
218
} ) ;
214
219
}
215
220
221
+ /**
222
+ * Generate the params in a format valid for 'fetch'
223
+ * @returns {[string, object] } The uri to make the request too, and extra configs
224
+ * @private
225
+ */
216
226
_buildRequest ( { uri, method, headers, data, auth, query, form, files, context } ) {
217
227
let actualUri = uri ;
218
228
if ( this . baseUrl && uri [ 0 ] === '/' ) {
@@ -228,6 +238,7 @@ export default class Agent {
228
238
let body ;
229
239
let contentTypeHeader ;
230
240
if ( files ) {
241
+ // @ts -ignore
231
242
contentTypeHeader = { } ; // Needed to allow fetch create its own
232
243
body = this . _getFromData ( files , form ) ;
233
244
} else if ( form ) {
@@ -352,7 +363,7 @@ export default class Agent {
352
363
353
364
/**
354
365
* Adds an authorization header.
355
- * @param {string } auth The authorization bearer token.
366
+ * @param {string|object } auth The authorization bearer token.
356
367
* @returns {object } The original request.
357
368
*/
358
369
_getAuthorizationHeader ( auth ) {
0 commit comments