Skip to content

Commit 6d811bd

Browse files
authored
feat(chat): annotate with algoliaAgent (#6812)
[FX-3592]
1 parent b5d7f91 commit 6d811bd

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

packages/instantsearch.js/src/connectors/chat/connectChat.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
checkRendering,
99
createDocumentationMessageGenerator,
1010
createSendEventForHits,
11+
getAlgoliaAgent,
1112
getAppIdAndApiKey,
1213
noop,
1314
warning,
@@ -191,6 +192,7 @@ export default (function connectChat<TWidgetParams extends UnknownWidgetParams>(
191192
headers: {
192193
'x-algolia-application-id': appId,
193194
'x-algolia-api-Key': apiKey,
195+
'x-algolia-agent': getAlgoliaAgent(instantSearchInstance.client),
194196
},
195197
});
196198
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import algoliasearchV3 from 'algoliasearch-v3';
2+
import algoliasearchV4 from 'algoliasearch-v4';
3+
import { algoliasearch as algoliasearchV5 } from 'algoliasearch-v5';
4+
5+
import { getAlgoliaAgent } from '../getAlgoliaAgent';
6+
7+
describe('getAlgoliaAgent', () => {
8+
it('should return the user agent for v5 clients', () => {
9+
expect(getAlgoliaAgent(algoliasearchV5('appId', 'apiKey'))).toMatch(
10+
/Algolia for JavaScript \(5\.\d+\.\d+(?:-[^)]+)?\)/
11+
);
12+
});
13+
14+
it('should return the user agent for v4 clients', () => {
15+
expect(getAlgoliaAgent(algoliasearchV4('appId', 'apiKey'))).toMatch(
16+
/Algolia for JavaScript \(4\.\d+\.\d+(?:-[^)]+)?\)/
17+
);
18+
});
19+
20+
it('should return the user agent for v3 clients', () => {
21+
expect(getAlgoliaAgent(algoliasearchV3('appId', 'apiKey'))).toMatch(
22+
/Algolia for JavaScript \(3\.\d+\.\d+(?:-[^)]+)?\)/
23+
);
24+
});
25+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type v4 = { transporter?: { userAgent: { value: string } } };
2+
type v3 = { _ua: string };
3+
type AnySearchClient = v4 & v3;
4+
5+
export function getAlgoliaAgent(client: unknown): string {
6+
const clientTyped = client as AnySearchClient;
7+
return clientTyped.transporter && clientTyped.transporter.userAgent
8+
? clientTyped.transporter.userAgent.value
9+
: clientTyped._ua;
10+
}

packages/instantsearch.js/src/lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export * from './escapeFacetValue';
1818
export * from './find';
1919
export * from './findIndex';
2020
export * from './geo-search';
21+
export * from './getAlgoliaAgent';
2122
export * from './getAppIdAndApiKey';
2223
export * from './getContainerNode';
2324
export * from './getHighlightedParts';

packages/instantsearch.js/src/middlewares/createMetadataMiddleware.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { createInitArgs, safelyRunOnBrowser } from '../lib/utils';
1+
import {
2+
createInitArgs,
3+
getAlgoliaAgent,
4+
safelyRunOnBrowser,
5+
} from '../lib/utils';
26

37
import type {
48
InstantSearch,
@@ -103,11 +107,7 @@ export function createMetadataMiddleware({
103107
subscribe() {
104108
// using setTimeout here to delay extraction until widgets have been added in a tick (e.g. Vue)
105109
setTimeout(() => {
106-
const client = instantSearchInstance.client as any;
107-
payload.ua =
108-
client.transporter && client.transporter.userAgent
109-
? client.transporter.userAgent.value
110-
: client._ua;
110+
payload.ua = getAlgoliaAgent(instantSearchInstance.client);
111111

112112
extractWidgetPayload(
113113
instantSearchInstance.mainIndex.getWidgets(),

0 commit comments

Comments
 (0)