Skip to content

Commit f1b38ec

Browse files
Merge pull request #7 from opentable/request
Switch to request
2 parents 61a2d52 + 9904d47 commit f1b38ec

File tree

3 files changed

+36
-50
lines changed

3 files changed

+36
-50
lines changed

index.js

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,37 @@
1-
const fetch = require('isomorphic-fetch');
1+
const http = require('http');
2+
const request = require('request');
23
const _ = require('lodash');
34

4-
let client;
5+
let settings;
56

6-
const createQuery = (query, variables) => {
7-
const body = {
8-
query,
9-
variables,
10-
operationName: null,
11-
};
12-
13-
return JSON.stringify(body);
14-
};
15-
16-
const checkResponseStatus = (response) => {
17-
if (response.ok) {
18-
return response;
19-
}
20-
21-
const error = new Error(response.statusText);
22-
error.response = response;
23-
throw error;
24-
};
25-
26-
const parseResponse = response => response.json();
27-
28-
module.exports.register = (opts, dependencies, next) => { // eslint-disable-line consistent-return
7+
module.exports.register = (opts, dependencies, next) => {
298
if (!opts.serverUrl) {
309
return next(new Error('The serverUrl parameter is invalid'));
3110
}
3211

33-
client = (options, headers, timeout) => fetch(opts.serverUrl, {
34-
method: 'POST',
35-
timeout,
36-
headers: _.extend(
37-
{
38-
'Content-Type': 'application/json',
39-
'User-Agent': 'oc',
40-
}, headers),
41-
42-
body: createQuery(options.query, options.variables),
43-
}).then(checkResponseStatus)
44-
.then(parseResponse);
45-
46-
next();
12+
settings = opts;
13+
return next();
4714
};
4815

4916
module.exports.execute = () => ({
50-
query: (options, headers, timeout) => client(options, headers, timeout),
17+
query: (options, headers, timeout) => new Promise((resolve, reject) => request({
18+
body: {
19+
query: options.query,
20+
variables: options.variables,
21+
operationName: null,
22+
},
23+
headers: _.extend({ 'User-Agent': 'oc' }, headers),
24+
json: true,
25+
method: 'POST',
26+
timeout,
27+
url: settings.serverUrl,
28+
}, (err, result, body) => {
29+
if (err) {
30+
return reject(new Error(err));
31+
} else if (result.statusCode !== 200) {
32+
return reject(new Error(http.STATUS_CODES[result.statusCode]));
33+
}
34+
35+
return resolve(body);
36+
})),
5137
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"test": "npm run lint && mocha tests"
1515
},
1616
"dependencies": {
17-
"isomorphic-fetch": "^2.2.1",
18-
"lodash": "^4.17.4"
17+
"lodash": "^4.17.4",
18+
"request": "^2.81.0"
1919
},
2020
"devDependencies": {
2121
"chai": "^3.5.0",

tests/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
66

77
describe('when calling register with an valid batchInterval', () => {
88
const plugin = injectr('../index.js', {
9-
'fetch': (url, options) => { }
9+
request: sinon.stub().yields(null, {}, 'ok')
1010
});
1111

1212
let error;
@@ -24,7 +24,7 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
2424

2525
describe('when calling register with no serverUrl', () => {
2626
const plugin = injectr('../index.js', {
27-
'fetch': (url, options) => { }
27+
request: sinon.stub().yields(null, {}, 'ok')
2828
});
2929
let error;
3030
beforeEach((done) => {
@@ -41,7 +41,7 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
4141

4242
describe('when calling with the correct options', () => {
4343
const plugin = injectr('../index.js', {
44-
'fetch': (url, options) => { }
44+
request: sinon.stub().yields(null, {}, 'ok')
4545
});
4646
const next = sinon.spy();
4747

@@ -59,7 +59,7 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
5959

6060
describe('when calling execute', () => {
6161
const plugin = injectr('../index.js', {
62-
'fetch': (url, options) => { }
62+
request: sinon.stub().yields(null, {}, 'ok')
6363
});
6464
let client;
6565
beforeEach((done) => {
@@ -78,7 +78,7 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
7878
describe('when calling query and endpoint fails', () => {
7979
let client;
8080
const plugin = injectr('../index.js', {
81-
'isomorphic-fetch': sinon.stub().returns(Promise.resolve({ ok: false, statusText: 'Failure' }))
81+
request: sinon.stub().yields(null, { statusCode: 500}, {})
8282
});
8383

8484
beforeEach((done) => {
@@ -92,15 +92,15 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
9292
it('should return a failure message', (done) => {
9393
client.query({ query: {}, variables: { test: 1 } }, { 'accept-language': 'en-US' })
9494
.catch(error => {
95-
expect(error.message).to.equal('Failure')
95+
expect(error.message).to.equal('Internal Server Error')
9696
}).then(done, done)
9797
});
9898
});
9999

100100
describe('when calling query successfully', () => {
101101
let client;
102102
const plugin = injectr('../index.js', {
103-
'isomorphic-fetch': sinon.stub().returns(Promise.resolve({ ok: true, json: () => Promise.resolve('PASSED') }))
103+
request: sinon.stub().yields(null, { statusCode: 200}, { someJson: true })
104104
});
105105

106106
beforeEach((done) => {
@@ -114,7 +114,7 @@ describe('OpenTable OC registry :: plugins :: graphql-plugin ', () => {
114114
it('should return a failture message', (done) => {
115115
client.query({ query: {}, variables: { test: 1 } }, { 'accept-language': 'en-US' })
116116
.then(res => {
117-
expect(res).to.equal('PASSED')
117+
expect(res).to.eql({ someJson: true })
118118
}).then(done, done)
119119
});
120120
});

0 commit comments

Comments
 (0)