@@ -32,7 +32,7 @@ import packageJson from '../package.json';
3232
3333/**
3434 * 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
3636 * the request config and whether is on browser or node
3737 */
3838
@@ -57,11 +57,12 @@ export default class Agent {
5757
5858 /**
5959 * 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
6566 * @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
6667 */
6768 get ( { uri, auth, headers, query, context } ) {
@@ -70,11 +71,12 @@ export default class Agent {
7071
7172 /**
7273 * 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
7880 * @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
7981 */
8082 head ( { uri, auth, headers, query, context } ) {
@@ -83,11 +85,12 @@ export default class Agent {
8385
8486 /**
8587 * 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
9194 * @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
9295 */
9396 post ( { uri, headers, data, auth, context } ) {
@@ -96,11 +99,12 @@ export default class Agent {
9699
97100 /**
98101 * 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
104108 * @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
105109 */
106110 put ( { uri, auth, headers, data, context } ) {
@@ -109,11 +113,12 @@ export default class Agent {
109113
110114 /**
111115 * 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
117122 * @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
118123 */
119124 delete ( { uri, auth, headers, data, context } ) {
@@ -122,17 +127,17 @@ export default class Agent {
122127
123128 /**
124129 *
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
136141 * @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
137142 */
138143 request ( {
@@ -156,7 +161,7 @@ export default class Agent {
156161 * Promises to send the request and retreive the response.
157162 * @param {[string, object] } requestParams First argument is the URI to request, the second one are the options.
158163 * @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.
160165 * @returns {Promise<RequestResponse, RequestError> } A promise that resolves with either the requested data or an error object
161166 * @private
162167 */
@@ -213,6 +218,11 @@ export default class Agent {
213218 } ) ;
214219 }
215220
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+ */
216226 _buildRequest ( { uri, method, headers, data, auth, query, form, files, context } ) {
217227 let actualUri = uri ;
218228 if ( this . baseUrl && uri [ 0 ] === '/' ) {
@@ -228,6 +238,7 @@ export default class Agent {
228238 let body ;
229239 let contentTypeHeader ;
230240 if ( files ) {
241+ // @ts -ignore
231242 contentTypeHeader = { } ; // Needed to allow fetch create its own
232243 body = this . _getFromData ( files , form ) ;
233244 } else if ( form ) {
@@ -352,7 +363,7 @@ export default class Agent {
352363
353364 /**
354365 * Adds an authorization header.
355- * @param {string } auth The authorization bearer token.
366+ * @param {string|object } auth The authorization bearer token.
356367 * @returns {object } The original request.
357368 */
358369 _getAuthorizationHeader ( auth ) {
0 commit comments