Skip to content

Commit

Permalink
Allow for specifying an agent in the query, to enable support using a…
Browse files Browse the repository at this point in the history
…n HTTPS proxy
  • Loading branch information
Aaron Mefford committed Jan 30, 2018
1 parent 9c59b96 commit 2b99813
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Optional callback function where the first parameter is an error and the second
* [API](#api)
* [Promises](#promises)
* [Callbacks](#callbacks)
* [Proxy Server](#proxy-server)
* [Examples](#examples)
* [API Methods](#api-methods)
* [autoComplete](#autocomplete)
Expand Down Expand Up @@ -98,6 +99,27 @@ googleTrends.interestOverTime({keyword: 'Women\'s march'}, function(err, results
})
```

### Proxy Server
A proxy server can be used by specifying an http agent as part of the query.

```js
const HttpsProxyAgent = require('https-proxy-agent');

let proxyAgent = new HttpsProxyAgent('http://proxy-host:8888/');

let query = {
keyword: 'Women\'s march',
agent: proxyAgent
};

googleTrends.interestOverTime(query)
.then(function(results){
console.log('These proxied results are incredible', results);
})
.catch(function(err){
console.error('Oh no there was an error, double check your proxy settings', err);
});
```

### Multiple Keywords
Compare multiple keywords with any of the api methods by supplying an `array` instead of a single `string`
Expand Down
4 changes: 3 additions & 1 deletion src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import https from 'https';
import querystring from 'querystring';

export default function request({method, host, path, qs}) {
export default function request({method, host, path, qs, agent}) {
const options = {
host,
method,
path: `${path}?${querystring.stringify(qs)}`,
};

if (agent) options.agent = agent;

return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let chunk = '';
Expand Down
2 changes: 2 additions & 0 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ export function getResults(request) {
},
};

if (obj.agent) options.agent = obj.agent;

const { path, resolution, _id } = map[searchType];

return request(options)
Expand Down

0 comments on commit 2b99813

Please sign in to comment.