diff --git a/README.md b/README.md index 8b054ca..aa2740f 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,18 @@ const client = addProxyToClient(new S3Client({})); // `client` now has HTTP proxy config at 'http://127.0.0.1' ``` +or + +```ts +import { S3Client } from '@aws-sdk/client-s3'; +import { addProxyToClient } from 'aws-sdk-v3-proxy'; + +const client = addProxyToClient(new S3Client({}), { + httpProxy: 'http://127.0.0.1', +}); +// `client` now has HTTP proxy config at 'http://127.0.0.1' +``` + ### HTTPS Proxy ```ts @@ -48,6 +60,18 @@ const client = addProxyToClient(new S3Client({})); // `client` now has HTTPS proxy config at 'https://127.0.0.1' ``` +or + +```ts +import { S3Client } from '@aws-sdk/client-s3'; +import { addProxyToClient } from 'aws-sdk-v3-proxy'; + +const client = addProxyToClient(new S3Client({}), { + httpsProxy: 'https://127.0.0.1', +}); +// `client` now has HTTPS proxy config at 'https://127.0.0.1' +``` + ### No Proxy with exception disabled ```ts @@ -112,6 +136,18 @@ Default: `false` Toggles additional logging for debugging. +##### httpProxy + +Type: `string` + +The URL for the HTTP proxy server. +If not specified, the value of `process.env.http_proxy` or `process.env.HTTP_RPOXY` will be used. + +##### httpsProxy + +The URL for the HTTPS proxy server. +If not specified, the value of `process.env.https_proxy` or `process.env.HTTPS_RPOXY` will be used. + ##### agentOptions Type: `HttpsProxyAgentOptions` @@ -126,17 +162,17 @@ See [CONTRIBUTING](CONTRIBUTING.md#security-issue-notifications) for more inform This project is licensed under the Apache-2.0 License. -[build-img]:https://github.com/awslabs/aws-sdk-v3-js-proxy/actions/workflows/release.yml/badge.svg -[build-url]:https://github.com/awslabs/aws-sdk-v3-js-proxy/actions/workflows/release.yml -[downloads-img]:https://img.shields.io/npm/dt/aws-sdk-v3-proxy -[downloads-url]:https://www.npmtrends.com/aws-sdk-v3-proxy -[npm-img]:https://img.shields.io/npm/v/aws-sdk-v3-proxy -[npm-url]:https://www.npmjs.com/package/aws-sdk-v3-proxy -[issues-img]:https://img.shields.io/github/issues/awslabs/aws-sdk-v3-js-proxy -[issues-url]:https://github.com/awslabs/aws-sdk-v3-js-proxy/issues -[codecov-img]:https://codecov.io/gh/awslabs/aws-sdk-v3-js-proxy/branch/main/graph/badge.svg -[codecov-url]:https://codecov.io/gh/awslabs/aws-sdk-v3-js-proxy -[semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg -[semantic-release-url]:https://github.com/semantic-release/semantic-release -[commitizen-img]:https://img.shields.io/badge/commitizen-friendly-brightgreen.svg -[commitizen-url]:http://commitizen.github.io/cz-cli/ +[build-img]: https://github.com/awslabs/aws-sdk-v3-js-proxy/actions/workflows/release.yml/badge.svg +[build-url]: https://github.com/awslabs/aws-sdk-v3-js-proxy/actions/workflows/release.yml +[downloads-img]: https://img.shields.io/npm/dt/aws-sdk-v3-proxy +[downloads-url]: https://www.npmtrends.com/aws-sdk-v3-proxy +[npm-img]: https://img.shields.io/npm/v/aws-sdk-v3-proxy +[npm-url]: https://www.npmjs.com/package/aws-sdk-v3-proxy +[issues-img]: https://img.shields.io/github/issues/awslabs/aws-sdk-v3-js-proxy +[issues-url]: https://github.com/awslabs/aws-sdk-v3-js-proxy/issues +[codecov-img]: https://codecov.io/gh/awslabs/aws-sdk-v3-js-proxy/branch/main/graph/badge.svg +[codecov-url]: https://codecov.io/gh/awslabs/aws-sdk-v3-js-proxy +[semantic-release-img]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg +[semantic-release-url]: https://github.com/semantic-release/semantic-release +[commitizen-img]: https://img.shields.io/badge/commitizen-friendly-brightgreen.svg +[commitizen-url]: http://commitizen.github.io/cz-cli/ diff --git a/src/add-proxy.ts b/src/add-proxy.ts index 6e0a6cb..b7071e2 100644 --- a/src/add-proxy.ts +++ b/src/add-proxy.ts @@ -15,11 +15,11 @@ export const addProxyToClient = ( httpsOnly = false, throwOnNoProxy = true, agentOptions = {}, + httpProxy = getHttpProxy(), + httpsProxy = getHttpsProxy(), ...opts }: AddProxyOptions = {} ): T => { - const httpProxy = getHttpProxy(); - const httpsProxy = getHttpsProxy(); const httpAgent = httpProxy ? new HttpsProxyAgent({ proxy: httpProxy, ...agentOptions }) : undefined; diff --git a/src/types.ts b/src/types.ts index b6d49de..94cb946 100644 --- a/src/types.ts +++ b/src/types.ts @@ -37,6 +37,16 @@ export interface AddProxyOptions * @default false */ httpsOnly?: boolean; + /** + * The URL for the HTTP proxy server. + * If not specified, the value of `process.env.http_proxy` or `process.env.HTTP_RPOXY` will be used. + */ + httpProxy?: string; + /** + * The URL for the HTTPS proxy server. + * If not specified, the value of `process.env.https_proxy` or `process.env.HTTPS_RPOXY` will be used. + */ + httpsProxy?: string; /** * Options to be provided to the proxy agent. This can be used for modifyi */ diff --git a/test/add-proxy.spec.ts b/test/add-proxy.spec.ts index df53939..9ee67aa 100644 --- a/test/add-proxy.spec.ts +++ b/test/add-proxy.spec.ts @@ -186,5 +186,21 @@ describe('add-proxy', () => { expect.objectContaining(opts) ); }); + + it('should attach an httpAgent AND httpsAgent when both proxies are set by options', () => { + const opts: AddProxyOptions = { + httpProxy: 'http://localhost', + httpsProxy: 'https://localhost', + }; + + addProxyToClient(client, opts); + + expect(proxyAgentSpy).toHaveBeenNthCalledWith(1, { + proxy: 'http://localhost', + }); + expect(proxyAgentSpy).toHaveBeenNthCalledWith(2, { + proxy: 'https://localhost', + }); + }); }); });