Skip to content

Commit 4aeecae

Browse files
committed
chore(commons): update Powertools UA middleware detection (#1762)
* chore(commons): fix double ua detection * chore(commons): fix unit test
1 parent 44da5be commit 4aeecae

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

packages/commons/src/awsSdkUtils.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,25 @@ const customUserAgentMiddleware = (feature: string) => {
5151
};
5252
};
5353

54+
/**
55+
* @internal
56+
* Checks if the middleware stack already has the Powertools UA middleware
57+
*/
58+
const hasPowertools = (middlewareStack: string[]): boolean => {
59+
let found = false;
60+
for (const middleware of middlewareStack) {
61+
if (middleware.includes('addPowertoolsToUserAgent')) {
62+
found = true;
63+
}
64+
}
65+
66+
return found;
67+
};
68+
5469
const addUserAgentMiddleware = (client: unknown, feature: string): void => {
5570
try {
5671
if (isSdkClient(client)) {
57-
if (
58-
client.middlewareStack
59-
.identify()
60-
.includes('addPowertoolsToUserAgent: POWERTOOLS,USER_AGENT')
61-
) {
72+
if (hasPowertools(client.middlewareStack.identify())) {
6273
return;
6374
}
6475
client.middlewareStack.addRelativeTo(
@@ -70,8 +81,8 @@ const addUserAgentMiddleware = (client: unknown, feature: string): void => {
7081
`The client provided does not match the expected interface`
7182
);
7283
}
73-
} catch (e) {
74-
console.warn('Failed to add user agent middleware', e);
84+
} catch (error) {
85+
console.warn('Failed to add user agent middleware', error);
7586
}
7687
};
7788

packages/commons/tests/unit/awsSdkUtils.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ describe('Helpers: awsSdk', () => {
3333
// Prepare
3434
const client = {
3535
middlewareStack: {
36-
identify: () => 'addPowertoolsToUserAgent: POWERTOOLS,USER_AGENT',
36+
identify: () => [
37+
'addPowertoolsToUserAgent: after getUserAgentMiddleware',
38+
],
3739
addRelativeTo: jest.fn(),
3840
},
3941
send: jest.fn(),

0 commit comments

Comments
 (0)