Skip to content

Commit 2ec68ae

Browse files
committed
Only accept objects as data and query
1 parent 8147157 commit 2ec68ae

File tree

2 files changed

+20
-42
lines changed

2 files changed

+20
-42
lines changed

src/Agent.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class Agent {
5959
* @param {string} uri The URI to request
6060
* @param {string} [auth] Authorization token to use
6161
* @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
6363
* @param {object} [context] The invocation context, describing the tool and project
6464
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
6565
*/
@@ -72,7 +72,7 @@ export default class Agent {
7272
* @param {string} uri The URI to request
7373
* @param {string} [auth] Authorization token to use
7474
* @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
7676
* @param {object} [context] The invocation context, describing the tool and project
7777
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
7878
*/
@@ -85,7 +85,7 @@ export default class Agent {
8585
* @param {string} uri The URI to request
8686
* @param {string} [auth] Authorization token to use
8787
* @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
8989
* @param {object} [context] The invocation context, describing the tool and project
9090
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
9191
*/
@@ -98,7 +98,7 @@ export default class Agent {
9898
* @param {string} uri The URI to request
9999
* @param {string} [auth] Authorization token to use
100100
* @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
102102
* @param {object} [context] The invocation context, describing the tool and project
103103
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
104104
*/
@@ -111,7 +111,7 @@ export default class Agent {
111111
* @param {string} uri The URI to request
112112
* @param {string} [auth] Authorization token to use
113113
* @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
115115
* @param {object} [context] The invocation context, describing the tool and project
116116
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
117117
*/
@@ -125,9 +125,9 @@ export default class Agent {
125125
* @param {String} config.uri The URI to request
126126
* @param {String} config.method The method used to request the URI, should be in uppercase.
127127
* @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.
129129
* @param {Object} config.auth Authorization
130-
* @param {String|Object} config.query Query parameters
130+
* @param {String} config.query Query parameters
131131
* @param {Object} config.form Form fields
132132
* @param {Object} config.files array of file names and file content
133133
* @param {Object} config.context the invocation context, describing the tool and project.
@@ -154,6 +154,8 @@ export default class Agent {
154154
/**
155155
* Promises to send the request and retreive the response.
156156
* @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.
157159
* @returns {Promise<RequestResponse, RequestError>} A promise that resolves with either the requested data or an error object
158160
* @private
159161
*/
@@ -216,35 +218,25 @@ export default class Agent {
216218
actualUri = `${this.baseUrl}${uri}`;
217219
}
218220
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);
225222
const hasParams = actualUri.includes('?');
226223
actualUri = `${actualUri}${hasParams ? '&' : '?'}${queryParams}`;
227224
}
228225

229226
let body;
230-
let contentType = { 'Content-Type': 'application/x-www-form-urlencoded' };
227+
let contentTypeHeader;
231228
if (files){
232-
contentType = {}; // Needed to allow fetch create it's own
229+
contentTypeHeader = {}; // Needed to allow fetch create its own
233230
body = this._getFromData(files, form);
234231
} else if (form){
232+
contentTypeHeader = { 'Content-Type': 'application/x-www-form-urlencoded' };
235233
body = qs.stringify(form);
236234
} 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);
245237
}
246238
const finalHeaders = Object.assign({},
247-
contentType,
239+
contentTypeHeader,
248240
this._getAuthorizationHeader(auth),
249241
this._getContextHeaders(context),
250242
headers

test/Agent.spec.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -285,36 +285,22 @@ describe('Agent', () => {
285285
expect(opts.headers).to.have.property('Authorization', `Bearer ${auth}`);
286286
});
287287

288-
// it('should invoke query with the given query', () => {
289-
it('adds new query params with the given query string', () => {
290-
const query = 'foo=1&bar=2';
291-
const [uri] = agent._buildRequest({ uri: '/uri', method: 'get', query });
292-
expect(uri).to.equal(`abc/uri?${query}`);
293-
});
294-
295288
it('adds new query params with the given query object', () => {
296289
const query = { foo: 1, bar: 2 };
297290
const [uri] = agent._buildRequest({ uri: '/uri', method: 'get', query });
298291
expect(uri).to.equal('abc/uri?foo=1&bar=2');
299292
});
300293

301294
it('adds query params without colliding with existing ones', () => {
302-
const query = 'foo=1&bar=2';
295+
const query = { foo: 1, bar: 2 };
303296
const [uri] = agent._buildRequest({ uri: '/uri?test=true', method: 'get', query });
304-
expect(uri).to.equal(`abc/uri?test=true&${query}`);
297+
expect(uri).to.equal('abc/uri?test=true&foo=1&bar=2');
305298
});
306299

307-
it('adds the provided data as a requets body', () => {
308-
const [, opts] = agent._buildRequest({ uri: 'uri', method: 'get', data: 'a=abcd' });
309-
expect(opts.body).to.eql('a=abcd');
310-
expect(opts.headers).to.have.property('Content-Type', 'application/x-www-form-urlencoded');
311-
});
312-
313-
it('adds the provided data as a requets body', () => {
300+
it('adds the provided data as a JSON request body', () => {
314301
const [, opts] = agent._buildRequest({ uri: 'uri', method: 'get', data: { a: 'abcd' } });
315-
expect(opts.body).to.eql('a=abcd');
302+
expect(opts.body).to.eql('{"a":"abcd"}');
316303
expect(opts.headers).to.have.property('Content-Type', 'application/json');
317-
318304
});
319305

320306
it('should setup form send when form data is given', () => {

0 commit comments

Comments
 (0)