File tree Expand file tree Collapse file tree 5 files changed +44
-6
lines changed
packages/instantsearch.js/src Expand file tree Collapse file tree 5 files changed +44
-6
lines changed Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change 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+ / A l g o l i a f o r J a v a S c r i p t \( 5 \. \d + \. \d + (?: - [ ^ ) ] + ) ? \) /
11+ ) ;
12+ } ) ;
13+
14+ it ( 'should return the user agent for v4 clients' , ( ) => {
15+ expect ( getAlgoliaAgent ( algoliasearchV4 ( 'appId' , 'apiKey' ) ) ) . toMatch (
16+ / A l g o l i a f o r J a v a S c r i p t \( 4 \. \d + \. \d + (?: - [ ^ ) ] + ) ? \) /
17+ ) ;
18+ } ) ;
19+
20+ it ( 'should return the user agent for v3 clients' , ( ) => {
21+ expect ( getAlgoliaAgent ( algoliasearchV3 ( 'appId' , 'apiKey' ) ) ) . toMatch (
22+ / A l g o l i a f o r J a v a S c r i p t \( 3 \. \d + \. \d + (?: - [ ^ ) ] + ) ? \) /
23+ ) ;
24+ } ) ;
25+ } ) ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ export * from './escapeFacetValue';
1818export * from './find' ;
1919export * from './findIndex' ;
2020export * from './geo-search' ;
21+ export * from './getAlgoliaAgent' ;
2122export * from './getAppIdAndApiKey' ;
2223export * from './getContainerNode' ;
2324export * from './getHighlightedParts' ;
Original file line number Diff line number Diff line change 1- import { createInitArgs , safelyRunOnBrowser } from '../lib/utils' ;
1+ import {
2+ createInitArgs ,
3+ getAlgoliaAgent ,
4+ safelyRunOnBrowser ,
5+ } from '../lib/utils' ;
26
37import 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 ( ) ,
You can’t perform that action at this time.
0 commit comments