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

Commit e1e139d

Browse files
rkretzschmarRené Kretzschmar
authored andcommitted
Add http.Agent option
1 parent b9f83f2 commit e1e139d

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const pathPrefix = '/api/v10'
1212
const host = `${domain}:${port}`
1313
const origin = `${scheme}://${host}`
1414
const accessToken = 'testAccessToken'
15+
const agent = { key: 'value' }
1516

1617
const getClient = function(opts = {}) {
1718
return new NetlifyAPI(opts.accessToken, Object.assign({ scheme, host, pathPrefix }, opts))
@@ -23,6 +24,7 @@ test('Default options', async t => {
2324
t.is(client.host, 'api.netlify.com')
2425
t.is(client.pathPrefix, '/api/v1')
2526
t.is(client.accessToken, null)
27+
t.is(client.agent, undefined)
2628
t.deepEqual(client.globalParams, {})
2729
t.deepEqual(client.defaultHeaders, {
2830
'User-agent': 'netlify/js-client',
@@ -543,5 +545,32 @@ test('Gives up retrying on API rate limiting after a timeout', async t => {
543545
t.false(scope.isDone())
544546
})
545547

548+
test('Can set (proxy) agent', async t => {
549+
const client = getClient({ accessToken, agent })
550+
t.is(client.agent, agent)
551+
})
552+
553+
test('(Proxy) agent is passed as request option', async t => {
554+
const account_id = '15'
555+
const scope = nock(origin)
556+
.get(`${pathPrefix}/accounts/${account_id}`)
557+
.reply(200)
558+
559+
const client = getClient({ accessToken, agent })
560+
await client.getAccount({ account_id })
561+
t.is(scope.interceptors[0].req.options.agent, agent)
562+
})
563+
564+
test('(Proxy) agent is not passed as request option if not set', async t => {
565+
const account_id = '15'
566+
const scope = nock(origin)
567+
.get(`${pathPrefix}/accounts/${account_id}`)
568+
.reply(200)
569+
570+
const client = getClient({ accessToken })
571+
await client.getAccount({ account_id })
572+
t.falsy(scope.interceptors[0].req.options.agent)
573+
})
574+
546575
const TEST_RATE_LIMIT_DELAY = 5e3
547576
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)