Skip to content

Commit cea4298

Browse files
authored
fix: properly expose getProxyForUrl (introduced in #1482) (#1486)
* chore: use stricter typescript config * fix: properly expose getProxyForUrl (in #1482)
1 parent 6b19f7f commit cea4298

File tree

6 files changed

+36
-48
lines changed

6 files changed

+36
-48
lines changed

jest.config.cjs

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/CredentialsClient.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ export class CredentialsClient {
2020
private readonly requestHandler?: NodeHttpHandler;
2121

2222
constructor(props: CredentialsClientProps) {
23-
this.region = props.region;
23+
if (props.region !== undefined) {
24+
this.region = props.region;
25+
}
2426
if (props.proxyServer) {
2527
info('Configuring proxy handler for STS client');
26-
const getProxyForUrl = new ProxyResolver({
28+
const proxyOptions: { httpProxy: string; httpsProxy: string; noProxy?: string } = {
2729
httpProxy: props.proxyServer,
2830
httpsProxy: props.proxyServer,
29-
noProxy: props.noProxy,
30-
}).getProxyForUrl;
31+
};
32+
if (props.noProxy !== undefined) {
33+
proxyOptions.noProxy = props.noProxy;
34+
}
35+
const getProxyForUrl = new ProxyResolver(proxyOptions).getProxyForUrl;
3136
const handler = new ProxyAgent({ getProxyForUrl });
3237
this.requestHandler = new NodeHttpHandler({
3338
httpsAgent: handler,
@@ -38,11 +43,14 @@ export class CredentialsClient {
3843

3944
public get stsClient(): STSClient {
4045
if (!this._stsClient) {
41-
this._stsClient = new STSClient({
42-
region: this.region,
43-
customUserAgent: USER_AGENT,
44-
requestHandler: this.requestHandler ? this.requestHandler : undefined,
45-
});
46+
const config = { customUserAgent: USER_AGENT } as {
47+
customUserAgent: string;
48+
region?: string;
49+
requestHandler?: NodeHttpHandler;
50+
};
51+
if (this.region !== undefined) config.region = this.region;
52+
if (this.requestHandler !== undefined) config.requestHandler = this.requestHandler;
53+
this._stsClient = new STSClient(config);
4654
}
4755
return this._stsClient;
4856
}
@@ -88,9 +96,9 @@ export class CredentialsClient {
8896
}
8997

9098
private async loadCredentials() {
91-
const client = new STSClient({
92-
requestHandler: this.requestHandler ? this.requestHandler : undefined,
93-
});
99+
const config = {} as { requestHandler?: NodeHttpHandler };
100+
if (this.requestHandler !== undefined) config.requestHandler = this.requestHandler;
101+
const client = new STSClient(config);
94102
return client.config.credentials();
95103
}
96104
}

src/ProxyResolver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export class ProxyResolver {
1818
this.options = options;
1919
}
2020

21-
getProxyForUrl(url: string, _req: http.ClientRequest): string {
21+
// This method matches the interface expected by 'proxy-agent'. It is an arrow function to bind 'this'.
22+
public readonly getProxyForUrl = (url: string, _req: http.ClientRequest): string => {
2223
return this.getProxyForUrlOptions(url, this.options);
23-
}
24+
};
2425

2526
private getProxyForUrlOptions(url: string | URL, options?: ProxyOptions): string {
2627
let parsedUrl: URL;

src/helpers.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,14 @@ export async function getCallerIdentity(client: STSClient): Promise<{ Account: s
115115
if (!identity.Account || !identity.Arn) {
116116
throw new Error('Could not get Account ID or ARN from STS. Did you set credentials?');
117117
}
118-
return { Account: identity.Account, Arn: identity.Arn, UserId: identity.UserId };
118+
const result: { Account: string; Arn: string; UserId?: string } = {
119+
Account: identity.Account,
120+
Arn: identity.Arn,
121+
};
122+
if (identity.UserId !== undefined) {
123+
result.UserId = identity.UserId;
124+
}
125+
return result;
119126
}
120127

121128
// Obtains account ID from STS Client and sets it as output

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ export async function run() {
110110
exportRegion(region, outputEnvCredentials);
111111

112112
// Instantiate credentials client
113-
const credentialsClient = new CredentialsClient({ region, proxyServer, noProxy });
113+
const clientProps: { region: string; proxyServer?: string; noProxy?: string } = { region };
114+
if (proxyServer) clientProps.proxyServer = proxyServer;
115+
if (noProxy) clientProps.noProxy = noProxy;
116+
const credentialsClient = new CredentialsClient(clientProps);
114117
let sourceAccountId: string;
115118
let webIdentityToken: string;
116119

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"allowUnreachableCode": false,
44
"allowUnusedLabels": false,
55
"strict": true,
6-
"exactOptionalPropertyTypes": false,
6+
"exactOptionalPropertyTypes": true,
77
"noFallthroughCasesInSwitch": true,
88
"noImplicitOverride": true,
99
"noImplicitReturns": true,

0 commit comments

Comments
 (0)