Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 81af34a

Browse files
authored
Merge pull request #100 from rkretzschmar/master
2 parents b9f83f2 + cff2a98 commit 81af34a

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Create a new instance of the Netlify API client with the provided `accessToken`.
5353
host: 'api.netlify.com',
5454
pathPrefix: '/api/v1',
5555
accessToken: '1234myAccessToken',
56+
agent: undefined, // e.g. HttpsProxyAgent
5657
globalParams: {} // parameters you want available for every request.
5758
// Global params are only sent of the OpenAPI spec specifies the provided params.
5859
}
@@ -192,6 +193,18 @@ Optional `opts` include:
192193
}
193194
```
194195

196+
## Proxy support
197+
198+
**Node.js only**: If this client is used behind a corporate proxy, you can pass an `HttpsProxyAgent` or any other `http.Agent` that can handle your situation as `agent` option:
199+
200+
```js
201+
const HttpsProxyAgent = require('https-proxy-agent')
202+
203+
const proxyUri = 'http(s)://[user:password@]proxyhost:port'
204+
const agent = new HttpsProxyAgent(proxyUri)
205+
const client = new NetlifyAPI('1234myAccessToken', { agent })
206+
```
207+
195208
## UMD Builds
196209

197210
A UMD build is provided for your convenience, however browser support is still experimental. Contributions to improve browser support are welcome.

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class NetlifyAPI {
3939
this.pathPrefix = opts.pathPrefix
4040
this.globalParams = opts.globalParams
4141
this.accessToken = opts.accessToken
42+
this.agent = opts.agent
4243
}
4344

4445
get accessToken() {

src/index.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const http = require('http')
2+
13
const test = require('ava')
24
const fromString = require('from2-string')
35
const { TextHTTPError, JSONHTTPError } = require('micro-api-client')
@@ -12,6 +14,13 @@ const pathPrefix = '/api/v10'
1214
const host = `${domain}:${port}`
1315
const origin = `${scheme}://${host}`
1416
const accessToken = 'testAccessToken'
17+
const agent = new http.Agent({
18+
keepAlive: true,
19+
keepAliveMsecs: 60000,
20+
maxSockets: 10,
21+
maxFreeSockets: 10,
22+
timeout: 60000
23+
})
1524

1625
const getClient = function(opts = {}) {
1726
return new NetlifyAPI(opts.accessToken, Object.assign({ scheme, host, pathPrefix }, opts))
@@ -23,6 +32,7 @@ test('Default options', async t => {
2332
t.is(client.host, 'api.netlify.com')
2433
t.is(client.pathPrefix, '/api/v1')
2534
t.is(client.accessToken, null)
35+
t.is(client.agent, undefined)
2636
t.deepEqual(client.globalParams, {})
2737
t.deepEqual(client.defaultHeaders, {
2838
'User-agent': 'netlify/js-client',
@@ -543,5 +553,32 @@ test('Gives up retrying on API rate limiting after a timeout', async t => {
543553
t.false(scope.isDone())
544554
})
545555

556+
test('Can set (proxy) agent', async t => {
557+
const client = getClient({ accessToken, agent })
558+
t.is(client.agent, agent)
559+
})
560+
561+
test('(Proxy) agent is passed as request option', async t => {
562+
const account_id = '15'
563+
const scope = nock(origin)
564+
.get(`${pathPrefix}/accounts/${account_id}`)
565+
.reply(200)
566+
567+
const client = getClient({ accessToken, agent })
568+
await client.getAccount({ account_id })
569+
t.is(scope.interceptors[0].req.options.agent, agent)
570+
})
571+
572+
test('(Proxy) agent is not passed as request option if not set', async t => {
573+
const account_id = '15'
574+
const scope = nock(origin)
575+
.get(`${pathPrefix}/accounts/${account_id}`)
576+
.reply(200)
577+
578+
const client = getClient({ accessToken })
579+
await client.getAccount({ account_id })
580+
t.falsy(scope.interceptors[0].req.options.agent)
581+
})
582+
546583
const TEST_RATE_LIMIT_DELAY = 5e3
547584
const SECS_TO_MSECS = 1e3

src/methods/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ const getOpts = function({ verb, parameters }, NetlifyApi, { body }, opts) {
4343
const optsA = addHttpMethod(verb, opts)
4444
const optsB = addDefaultHeaders(NetlifyApi, optsA)
4545
const optsC = addBody(body, parameters, optsB)
46-
return optsC
46+
const optsD = addAgent(NetlifyApi, optsC)
47+
return optsD
4748
}
4849

4950
// Add the HTTP method based on the OpenAPI definition
@@ -58,6 +59,15 @@ const addDefaultHeaders = function(NetlifyApi, opts) {
5859
})
5960
}
6061

62+
// Assign fetch agent (like for example HttpsProxyAgent) if there is one
63+
const addAgent = function(NetlifyApi, opts) {
64+
if (NetlifyApi.agent) {
65+
return Object.assign({}, opts, { agent: NetlifyApi.agent })
66+
} else {
67+
return opts
68+
}
69+
}
70+
6171
const makeRequestOrRetry = async function(url, opts) {
6272
for (let index = 0; index <= MAX_RETRY; index++) {
6373
const response = await makeRequest(url, opts)

0 commit comments

Comments
 (0)