From ae6719ae0f26aab602b2670902c06b2729f776c2 Mon Sep 17 00:00:00 2001 From: Aaron Mefford Date: Tue, 30 Jan 2018 19:09:28 +0000 Subject: [PATCH] Allow for specifying an agent in the query, to enable support using an HTTPS proxy --- README.md | 22 ++++++++++++++++++++++ src/request.js | 4 +++- src/utilities.js | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 060c092..0e467af 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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` diff --git a/src/request.js b/src/request.js index dbc44bf..75a4205 100644 --- a/src/request.js +++ b/src/request.js @@ -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 = ''; diff --git a/src/utilities.js b/src/utilities.js index 9cf028d..94b5ac2 100644 --- a/src/utilities.js +++ b/src/utilities.js @@ -170,6 +170,8 @@ export function getResults(request) { }, }; + if (obj.agent) options.agent = obj.agent; + const { path, resolution, _id } = map[searchType]; return request(options)