Skip to content

Commit 3f8d670

Browse files
authored
Merge pull request amplitude#57 from acontreras89/ac-query-params
Let superagent handle request body
2 parents fdebc68 + f9b6893 commit 3f8d670

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

src/adapters/superagent.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
11
import superagent from 'superagent';
22
import * as httpMethods from '../constants/http-methods';
33

4-
export const createRequest = (url, method) => {
4+
const createRequest = (url, method, body) => {
55
switch (method) {
6+
case httpMethods.HEAD:
7+
return superagent.head(url, body);
68
case httpMethods.GET:
7-
return superagent.get(url);
9+
return superagent.get(url, body);
810
case httpMethods.POST:
9-
return superagent.post(url);
11+
return superagent.post(url, body);
1012
case httpMethods.PUT:
11-
return superagent.put(url);
13+
return superagent.put(url, body);
1214
case httpMethods.PATCH:
13-
return superagent.patch(url);
15+
return superagent.patch(url, body);
1416
case httpMethods.DELETE:
15-
return superagent.del(url);
17+
return superagent.del(url, body);
1618
default:
1719
throw new Error(`Unsupported HTTP method: ${method}`);
1820
}
1921
};
2022

2123
const superagentNetworkAdapter = (url, method, { body, headers, credentials } = {}) => {
22-
const request = createRequest(url, method);
23-
24-
if (body) {
25-
request.send(body);
26-
}
24+
const request = createRequest(url, method, body);
2725

2826
if (headers) {
2927
request.set(headers);

test/adapters/superagent.test.js

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
11
import { assert } from 'chai';
22
import * as HTTPMethods from '../../src/constants/http-methods';
3-
import superagentAdapter, { createRequest } from '../../src/adapters/superagent';
3+
import superagentAdapter from '../../src/adapters/superagent';
44

55
describe('superagent adapter', () => {
6-
it('must return an object with both execute and abort functions', () => {
6+
it('must return an object with both execute and abort functions, as well as the request instance', () => {
77
const adapter = superagentAdapter('http://localhost', HTTPMethods.GET);
88
assert.isFunction(adapter.execute);
99
assert.isFunction(adapter.abort);
10+
assert(adapter.instance);
1011
});
1112

12-
describe('createRequest', () => {
13-
it('must return a DELETE request when supplied a DELETE method', () => {
14-
const request = createRequest('http://localhost', HTTPMethods.DELETE);
15-
assert.equal(request.method, HTTPMethods.DELETE);
16-
});
17-
18-
it('must return a GET request when supplied a GET method', () => {
19-
const request = createRequest('http://localhost', HTTPMethods.GET);
20-
assert.equal(request.method, HTTPMethods.GET);
21-
});
22-
23-
it('must return a PATCH request when supplied a PATCH method', () => {
24-
const request = createRequest('http://localhost', HTTPMethods.PATCH);
25-
assert.equal(request.method, HTTPMethods.PATCH);
26-
});
27-
28-
it('must return a POST request when supplied a POST method', () => {
29-
const request = createRequest('http://localhost', HTTPMethods.POST);
30-
assert.equal(request.method, HTTPMethods.POST);
31-
});
32-
33-
it('must return a PUT request when supplied a PUT method', () => {
34-
const request = createRequest('http://localhost', HTTPMethods.PUT);
35-
assert.equal(request.method, HTTPMethods.PUT);
36-
});
37-
38-
it('must throw an error when supplied an invalid HTTP method', () => {
39-
const request = () => createRequest('http://localhost', 'abc');
40-
assert.throws(request, /Unsupported HTTP method/);
41-
});
13+
it('must return a HEAD request when supplied a HEAD method', () => {
14+
const { instance } = superagentAdapter('http://localhost', HTTPMethods.HEAD);
15+
assert.equal(instance.method, HTTPMethods.HEAD);
16+
});
17+
18+
it('must return a DELETE request when supplied a DELETE method', () => {
19+
const { instance } = superagentAdapter('http://localhost', HTTPMethods.DELETE);
20+
assert.equal(instance.method, HTTPMethods.DELETE);
21+
});
22+
23+
it('must return a GET request when supplied a GET method', () => {
24+
const { instance } = superagentAdapter('http://localhost', HTTPMethods.GET);
25+
assert.equal(instance.method, HTTPMethods.GET);
26+
});
27+
28+
it('must return a PATCH request when supplied a PATCH method', () => {
29+
const { instance } = superagentAdapter('http://localhost', HTTPMethods.PATCH);
30+
assert.equal(instance.method, HTTPMethods.PATCH);
31+
});
32+
33+
it('must return a POST request when supplied a POST method', () => {
34+
const { instance } = superagentAdapter('http://localhost', HTTPMethods.POST);
35+
assert.equal(instance.method, HTTPMethods.POST);
36+
});
37+
38+
it('must return a PUT request when supplied a PUT method', () => {
39+
const { instance } = superagentAdapter('http://localhost', HTTPMethods.PUT);
40+
assert.equal(instance.method, HTTPMethods.PUT);
41+
});
42+
43+
it('must throw an error when supplied an invalid HTTP method', () => {
44+
const invalid = () => superagentAdapter('http://localhost', 'abc');
45+
assert.throws(invalid, /Unsupported HTTP method/);
4246
});
4347
});

0 commit comments

Comments
 (0)