Skip to content

Commit 68fc758

Browse files
authored
chore(commons): update Powertools UA middleware detection (#1762)
* chore(commons): fix double ua detection * chore(commons): fix unit test
1 parent 665fff4 commit 68fc758

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

Diff for: packages/commons/src/awsSdk/userAgentMiddleware.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,25 @@ const customUserAgentMiddleware = (feature: string) => {
3030
};
3131
};
3232

33+
/**
34+
* @internal
35+
* Checks if the middleware stack already has the Powertools UA middleware
36+
*/
37+
const hasPowertools = (middlewareStack: string[]): boolean => {
38+
let found = false;
39+
for (const middleware of middlewareStack) {
40+
if (middleware.includes('addPowertoolsToUserAgent')) {
41+
found = true;
42+
}
43+
}
44+
45+
return found;
46+
};
47+
3348
const addUserAgentMiddleware = (client: unknown, feature: string): void => {
3449
try {
3550
if (isSdkClient(client)) {
36-
if (
37-
client.middlewareStack
38-
.identify()
39-
.includes('addPowertoolsToUserAgent: POWERTOOLS,USER_AGENT')
40-
) {
51+
if (hasPowertools(client.middlewareStack.identify())) {
4152
return;
4253
}
4354
client.middlewareStack.addRelativeTo(
@@ -49,8 +60,8 @@ const addUserAgentMiddleware = (client: unknown, feature: string): void => {
4960
`The client provided does not match the expected interface`
5061
);
5162
}
52-
} catch (e) {
53-
console.warn('Failed to add user agent middleware', e);
63+
} catch (error) {
64+
console.warn('Failed to add user agent middleware', error);
5465
}
5566
};
5667

Diff for: packages/commons/tests/unit/awsSdk.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ describe('Helpers: awsSdk', () => {
3030
// Prepare
3131
const client = {
3232
middlewareStack: {
33-
identify: () => 'addPowertoolsToUserAgent: POWERTOOLS,USER_AGENT',
33+
identify: () => [
34+
'addPowertoolsToUserAgent: after getUserAgentMiddleware',
35+
],
3436
addRelativeTo: jest.fn(),
3537
},
3638
send: jest.fn(),

0 commit comments

Comments
 (0)