Skip to content

Commit 9046ec1

Browse files
committed
feat(proxy): add httpProxy and httpsProxy in the options of the addProxyToClient function
1 parent 0a58b76 commit 9046ec1

File tree

4 files changed

+78
-16
lines changed

4 files changed

+78
-16
lines changed

README.md

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ const client = addProxyToClient(new S3Client({}));
3737
// `client` now has HTTP proxy config at 'http://127.0.0.1'
3838
```
3939

40+
or
41+
42+
```ts
43+
import { S3Client } from '@aws-sdk/client-s3';
44+
import { addProxyToClient } from 'aws-sdk-v3-proxy';
45+
46+
const client = addProxyToClient(new S3Client({}), {
47+
httpProxy: 'http://127.0.0.1',
48+
});
49+
// `client` now has HTTP proxy config at 'http://127.0.0.1'
50+
```
51+
4052
### HTTPS Proxy
4153

4254
```ts
@@ -48,6 +60,18 @@ const client = addProxyToClient(new S3Client({}));
4860
// `client` now has HTTPS proxy config at 'https://127.0.0.1'
4961
```
5062

63+
or
64+
65+
```ts
66+
import { S3Client } from '@aws-sdk/client-s3';
67+
import { addProxyToClient } from 'aws-sdk-v3-proxy';
68+
69+
const client = addProxyToClient(new S3Client({}), {
70+
httpsProxy: 'https://127.0.0.1',
71+
});
72+
// `client` now has HTTPS proxy config at 'https://127.0.0.1'
73+
```
74+
5175
### No Proxy with exception disabled
5276

5377
```ts
@@ -112,6 +136,18 @@ Default: `false`
112136

113137
Toggles additional logging for debugging.
114138

139+
##### httpProxy
140+
141+
Type: `string`
142+
143+
The URL for the HTTP proxy server.
144+
If not specified, the value of `process.env.http_proxy` or `process.env.HTTP_RPOXY` will be used.
145+
146+
##### httpsProxy
147+
148+
The URL for the HTTPS proxy server.
149+
If not specified, the value of `process.env.https_proxy` or `process.env.HTTPS_RPOXY` will be used.
150+
115151
##### agentOptions
116152

117153
Type: `HttpsProxyAgentOptions`
@@ -126,17 +162,17 @@ See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more inform
126162

127163
This project is licensed under the Apache-2.0 License.
128164

129-
[build-img]:https://github.com/awslabs/aws-sdk-v3-js-proxy/actions/workflows/release.yml/badge.svg
130-
[build-url]:https://github.com/awslabs/aws-sdk-v3-js-proxy/actions/workflows/release.yml
131-
[downloads-img]:https://img.shields.io/npm/dt/aws-sdk-v3-proxy
132-
[downloads-url]:https://www.npmtrends.com/aws-sdk-v3-proxy
133-
[npm-img]:https://img.shields.io/npm/v/aws-sdk-v3-proxy
134-
[npm-url]:https://www.npmjs.com/package/aws-sdk-v3-proxy
135-
[issues-img]:https://img.shields.io/github/issues/awslabs/aws-sdk-v3-js-proxy
136-
[issues-url]:https://github.com/awslabs/aws-sdk-v3-js-proxy/issues
137-
[codecov-img]:https://codecov.io/gh/awslabs/aws-sdk-v3-js-proxy/branch/main/graph/badge.svg
138-
[codecov-url]:https://codecov.io/gh/awslabs/aws-sdk-v3-js-proxy
139-
[semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
140-
[semantic-release-url]:https://github.com/semantic-release/semantic-release
141-
[commitizen-img]:https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
142-
[commitizen-url]:http://commitizen.github.io/cz-cli/
165+
[build-img]: https://github.com/awslabs/aws-sdk-v3-js-proxy/actions/workflows/release.yml/badge.svg
166+
[build-url]: https://github.com/awslabs/aws-sdk-v3-js-proxy/actions/workflows/release.yml
167+
[downloads-img]: https://img.shields.io/npm/dt/aws-sdk-v3-proxy
168+
[downloads-url]: https://www.npmtrends.com/aws-sdk-v3-proxy
169+
[npm-img]: https://img.shields.io/npm/v/aws-sdk-v3-proxy
170+
[npm-url]: https://www.npmjs.com/package/aws-sdk-v3-proxy
171+
[issues-img]: https://img.shields.io/github/issues/awslabs/aws-sdk-v3-js-proxy
172+
[issues-url]: https://github.com/awslabs/aws-sdk-v3-js-proxy/issues
173+
[codecov-img]: https://codecov.io/gh/awslabs/aws-sdk-v3-js-proxy/branch/main/graph/badge.svg
174+
[codecov-url]: https://codecov.io/gh/awslabs/aws-sdk-v3-js-proxy
175+
[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
176+
[semantic-release-url]: https://github.com/semantic-release/semantic-release
177+
[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
178+
[commitizen-url]: http://commitizen.github.io/cz-cli/

src/add-proxy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ export const addProxyToClient = <T>(
1515
httpsOnly = false,
1616
throwOnNoProxy = true,
1717
agentOptions = {},
18+
httpProxy = getHttpProxy(),
19+
httpsProxy = getHttpsProxy(),
1820
...opts
1921
}: AddProxyOptions = {}
2022
): T => {
21-
const httpProxy = getHttpProxy();
22-
const httpsProxy = getHttpsProxy();
2323
const httpAgent = httpProxy
2424
? new HttpsProxyAgent({ proxy: httpProxy, ...agentOptions })
2525
: undefined;

src/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ export interface AddProxyOptions
3737
* @default false
3838
*/
3939
httpsOnly?: boolean;
40+
/**
41+
* The URL for the HTTP proxy server.
42+
* If not specified, the value of `process.env.http_proxy` or `process.env.HTTP_RPOXY` will be used.
43+
*/
44+
httpProxy?: string;
45+
/**
46+
* The URL for the HTTPS proxy server.
47+
* If not specified, the value of `process.env.https_proxy` or `process.env.HTTPS_RPOXY` will be used.
48+
*/
49+
httpsProxy?: string;
4050
/**
4151
* Options to be provided to the proxy agent. This can be used for modifyi
4252
*/

test/add-proxy.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,21 @@ describe('add-proxy', () => {
186186
expect.objectContaining(opts)
187187
);
188188
});
189+
190+
it('should attach an httpAgent AND httpsAgent when both proxies are set by options', () => {
191+
const opts: AddProxyOptions = {
192+
httpProxy: 'http://localhost',
193+
httpsProxy: 'https://localhost',
194+
};
195+
196+
addProxyToClient(client, opts);
197+
198+
expect(proxyAgentSpy).toHaveBeenNthCalledWith(1, {
199+
proxy: 'http://localhost',
200+
});
201+
expect(proxyAgentSpy).toHaveBeenNthCalledWith(2, {
202+
proxy: 'https://localhost',
203+
});
204+
});
189205
});
190206
});

0 commit comments

Comments
 (0)