Skip to content

Commit 388001d

Browse files
committed
fix(resource): configure the node http client depending on the protocol
Closes #35
1 parent 9fc86ec commit 388001d

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ See the code in the [example folder](https://github.com/usabilla/api-js-node/tre
4242

4343
The client can be configured during instantiation with the following options:
4444

45-
- `protocol` (default: `https`) - The protocol to use when making requests
45+
- `protocol` (default: `https`) - The protocol to use when making requests, this will also configure the type of node http client either `http` or `https`
4646
- `host` (default: `data.usabilla.com`) - The host to use when making requests
4747
- `port` (default: `null`) - The port to use when making requests
4848
- `iterator` (default: `true`) - Whether to iterate until all results are retrieved

src/resources/resource.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const https = require('https');
2+
const http = require('http');
23
const packageJson = require('../../package.json');
34

45
/**
@@ -12,6 +13,11 @@ class Resource {
1213
this.config = config;
1314
this.str = '';
1415
this.queryParams = {};
16+
this.httpClient = this.getHttpClient(this.config.protocol);
17+
}
18+
19+
getHttpClient(protocol) {
20+
return protocol === 'http' ? http : https;
1521
}
1622

1723
handleOnData(newJsonString) {
@@ -87,7 +93,7 @@ class Resource {
8793
};
8894

8995
return new Promise((resolve, reject) => {
90-
https
96+
this.httpClient
9197
.get(requestOptions, this.handleGetResponse.bind(this, resolve, reject))
9298
.on('error', this.handleOnError.bind(this, reject));
9399
});

test/resources/resource.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ describe('Resource', function() {
2626
);
2727
});
2828

29+
describe('getHttpClient', function() {
30+
it('returns http when protocol is http', function() {
31+
expect(this.resource.getHttpClient('http')).toBeDefined();
32+
});
33+
});
34+
2935
describe('handleOnData', function() {
3036
it('concatenates new json string to existing json string', function() {
3137
this.resource.str = 'foo';

0 commit comments

Comments
 (0)