Skip to content

Commit 148b677

Browse files
authoredJul 9, 2024··
fix(algoliasearch): correctly retrieve headers for v5 (#1263)
1 parent 0ae0c5c commit 148b677

12 files changed

+144
-44
lines changed
 

‎examples/react-17/src/Highlight.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type HighlightHitParams<THit> = {
1111
*
1212
* You can use the array syntax to reference nested attributes.
1313
*/
14-
attribute: keyof THit | (string | number)[];
14+
attribute: keyof THit | Array<string | number>;
1515
/**
1616
* The tag name to use for highlighted parts.
1717
*

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@typescript-eslint/eslint-plugin": "2.34.0",
4646
"@typescript-eslint/parser": "2.34.0",
4747
"algoliasearch": "4.16.0",
48+
"algoliasearch-v5": "npm:algoliasearch@5.0.0-beta.8",
4849
"autoprefixer": "10.4.14",
4950
"babel-eslint": "10.1.0",
5051
"babel-loader": "8.2.3",
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export type ParseAlgoliaHitParams<TItem> = {
22
hit: TItem;
3-
attribute: keyof TItem | (string | number)[];
3+
attribute: keyof TItem | Array<string | number>;
44
};

‎packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitHighlight.test.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,19 @@ describe('parseAlgoliaHitHighlight', () => {
175175
objectID: '1',
176176
titles: ['Hello', 'world'],
177177
_highlightResult: {
178-
titles: [{
179-
value: 'Hello',
180-
matchLevel: 'none',
181-
matchedWords: [],
182-
}, {
183-
value: '__aa-highlight__world__/aa-highlight__',
184-
matchLevel: 'full',
185-
matchedWords: ['world'],
186-
fullyHighlighted: true,
187-
}]
178+
titles: [
179+
{
180+
value: 'Hello',
181+
matchLevel: 'none',
182+
matchedWords: [],
183+
},
184+
{
185+
value: '__aa-highlight__world__/aa-highlight__',
186+
matchLevel: 'full',
187+
matchedWords: ['world'],
188+
fullyHighlighted: true,
189+
},
190+
],
188191
},
189192
},
190193
})

‎packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitReverseHighlight.test.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,19 @@ describe('parseAlgoliaHitReverseHighlight', () => {
172172
objectID: '1',
173173
titles: ['Hello', 'world'],
174174
_highlightResult: {
175-
titles: [{
176-
value: 'Hello',
177-
matchLevel: 'none',
178-
matchedWords: [],
179-
}, {
180-
value: '__aa-highlight__world__/aa-highlight__',
181-
matchLevel: 'full',
182-
matchedWords: ['world'],
183-
fullyHighlighted: true,
184-
}]
175+
titles: [
176+
{
177+
value: 'Hello',
178+
matchLevel: 'none',
179+
matchedWords: [],
180+
},
181+
{
182+
value: '__aa-highlight__world__/aa-highlight__',
183+
matchLevel: 'full',
184+
matchedWords: ['world'],
185+
fullyHighlighted: true,
186+
},
187+
],
185188
},
186189
},
187190
})

‎packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitReverseSnippet.test.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ describe('parseAlgoliaHitReverseSnippet', () => {
163163
objectID: '1',
164164
titles: ['Hello', 'world'],
165165
_snippetResult: {
166-
titles: [{
167-
value: 'Hello',
168-
matchLevel: 'none',
169-
}, {
170-
value: '__aa-highlight__world__/aa-highlight__',
171-
matchLevel: 'full',
172-
}]
166+
titles: [
167+
{
168+
value: 'Hello',
169+
matchLevel: 'none',
170+
},
171+
{
172+
value: '__aa-highlight__world__/aa-highlight__',
173+
matchLevel: 'full',
174+
},
175+
],
173176
},
174177
},
175178
})

‎packages/autocomplete-preset-algolia/src/highlight/__tests__/parseAlgoliaHitSnippet.test.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,16 @@ describe('parseAlgoliaHitSnippet', () => {
163163
objectID: '1',
164164
titles: ['Hello', 'world'],
165165
_snippetResult: {
166-
titles: [{
167-
value: 'Hello',
168-
matchLevel: 'none',
169-
}, {
170-
value: '__aa-highlight__world__/aa-highlight__',
171-
matchLevel: 'full',
172-
}]
166+
titles: [
167+
{
168+
value: 'Hello',
169+
matchLevel: 'none',
170+
},
171+
{
172+
value: '__aa-highlight__world__/aa-highlight__',
173+
matchLevel: 'full',
174+
},
175+
],
173176
},
174177
},
175178
})

‎packages/autocomplete-preset-algolia/src/utils/__tests__/getAppIdAndApiKey.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
import algoliasearchV4 from 'algoliasearch';
2+
import { liteClient as algoliasearchV5 } from 'algoliasearch-v5/lite';
23

34
import { getAppIdAndApiKey } from '../getAppIdAndApiKey';
45

56
const APP_ID = 'myAppId';
67
const API_KEY = 'myApiKey';
78

89
describe('getAppIdAndApiKey', () => {
9-
it('gets appId and apiKey from searchClient', () => {
10+
it('gets appId and apiKey from searchClient@v4', () => {
1011
const searchClient = algoliasearchV4(APP_ID, API_KEY);
1112
const { appId, apiKey } = getAppIdAndApiKey(searchClient);
1213
expect(appId).toEqual(APP_ID);
1314
expect(apiKey).toEqual(API_KEY);
1415
});
1516

17+
it('gets appId and apiKey from searchClient@v5', () => {
18+
const searchClient = algoliasearchV5(APP_ID, API_KEY);
19+
const { appId, apiKey } = getAppIdAndApiKey(searchClient);
20+
expect(appId).toEqual(APP_ID);
21+
expect(apiKey).toEqual(API_KEY);
22+
});
23+
1624
it('gets undefined appId and apiKey from broken search client', () => {
1725
const searchClient = {
1826
search: algoliasearchV4(APP_ID, API_KEY).search,
1927
};
20-
// @ts-expect-error
2128
const { appId, apiKey } = getAppIdAndApiKey(searchClient);
2229
expect(appId).toEqual(undefined);
2330
expect(apiKey).toEqual(undefined);

‎packages/autocomplete-preset-algolia/src/utils/getAppIdAndApiKey.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
import type { SearchClient } from '../types';
2-
3-
export function getAppIdAndApiKey(searchClient: SearchClient): {
1+
// typed as any, since it accepts the _real_ js clients, not the interface we otherwise expect
2+
export function getAppIdAndApiKey(searchClient: any): {
43
appId: string;
54
apiKey: string;
65
} {
7-
const { headers = {}, queryParameters = {} } = searchClient.transporter || {};
6+
const transporter = searchClient.transporter || {};
7+
const headers = transporter.headers || transporter.baseHeaders || {};
8+
const queryParameters =
9+
transporter.queryParameters || transporter.baseQueryParameters || {};
810
const APP_ID = 'x-algolia-application-id';
911
const API_KEY = 'x-algolia-api-key';
1012
const appId = headers[APP_ID] || queryParameters[APP_ID];
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function getAttributeValueByPath<TRecord>(
22
record: TRecord,
3-
path: (string | number)[]
3+
path: Array<string | number>
44
): any {
55
return path.reduce((current, key) => current && current[key], record);
66
}

‎packages/autocomplete-shared/src/js/HighlightHitParams.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type HighlightHitParams<THit> = {
88
*
99
* You can use the array syntax to reference nested attributes.
1010
*/
11-
attribute: keyof THit | (string | number)[];
11+
attribute: keyof THit | Array<string | number>;
1212
/**
1313
* The tag name to use for highlighted parts.
1414
*

‎yarn.lock

+78
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
dependencies:
3636
"@algolia/cache-common" "4.16.0"
3737

38+
"@algolia/client-abtesting@5.0.0-beta.8":
39+
version "5.0.0-beta.8"
40+
resolved "https://registry.yarnpkg.com/@algolia/client-abtesting/-/client-abtesting-5.0.0-beta.8.tgz#07950e042e19abe473b6659660dbbaf3d5ae5750"
41+
integrity sha512-SX9/v/1CinWhn8UyPS4zoiCuqLepAoaPiwiJvMal1q8huQzHPYgaQRGu5SwXf/jzUnXnWLiWakyZZ0WkGHj6yA==
42+
dependencies:
43+
"@algolia/client-common" "5.0.0-beta.9"
44+
"@algolia/requester-browser-xhr" "5.0.0-beta.9"
45+
"@algolia/requester-node-http" "5.0.0-beta.9"
46+
3847
"@algolia/client-account@4.16.0":
3948
version "4.16.0"
4049
resolved "https://registry.yarnpkg.com/@algolia/client-account/-/client-account-4.16.0.tgz#3164e28df1b6428a60f43b8a97ea669d6bbbce9e"
@@ -54,6 +63,15 @@
5463
"@algolia/requester-common" "4.16.0"
5564
"@algolia/transporter" "4.16.0"
5665

66+
"@algolia/client-analytics@5.0.0-beta.8":
67+
version "5.0.0-beta.8"
68+
resolved "https://registry.yarnpkg.com/@algolia/client-analytics/-/client-analytics-5.0.0-beta.8.tgz#86d110bed99ca28359bd4e70eaeff568cb6462bb"
69+
integrity sha512-SzMg3FeF7do/+plUawHRw2Lr3/KebKF8rCkswdR907kUa/aMVCFzsginL3xc/k8bIxx6wU8fk5iKLeK1+Ykxnw==
70+
dependencies:
71+
"@algolia/client-common" "5.0.0-beta.9"
72+
"@algolia/requester-browser-xhr" "5.0.0-beta.9"
73+
"@algolia/requester-node-http" "5.0.0-beta.9"
74+
5775
"@algolia/client-common@4.16.0":
5876
version "4.16.0"
5977
resolved "https://registry.yarnpkg.com/@algolia/client-common/-/client-common-4.16.0.tgz#670f4b2286755090a6389c55d79402716e4556ac"
@@ -62,6 +80,11 @@
6280
"@algolia/requester-common" "4.16.0"
6381
"@algolia/transporter" "4.16.0"
6482

83+
"@algolia/client-common@5.0.0-beta.9":
84+
version "5.0.0-beta.9"
85+
resolved "https://registry.yarnpkg.com/@algolia/client-common/-/client-common-5.0.0-beta.9.tgz#f659c0fca45663a09f7bcf5eb36318ef5ab39b40"
86+
integrity sha512-TWf6l4/pWMk8CgV+8A4zTe49XaygaCZ6ir8bwf4WnpDgAxx8Y5X/I6e7vPzl7sljQEEB9q1+qlkss1nxNeRJPw==
87+
6588
"@algolia/client-personalization@4.16.0":
6689
version "4.16.0"
6790
resolved "https://registry.yarnpkg.com/@algolia/client-personalization/-/client-personalization-4.16.0.tgz#927728c069b42f39fc83ce61a1fdcc79cd38b842"
@@ -71,6 +94,15 @@
7194
"@algolia/requester-common" "4.16.0"
7295
"@algolia/transporter" "4.16.0"
7396

97+
"@algolia/client-personalization@5.0.0-beta.8":
98+
version "5.0.0-beta.8"
99+
resolved "https://registry.yarnpkg.com/@algolia/client-personalization/-/client-personalization-5.0.0-beta.8.tgz#18b624c9e268da3c36c5416e593e778cdaca338c"
100+
integrity sha512-8mGxWE3TA3+2ekc+rM38JAwFfpwPyWyPxjjOe33afiFkmDliLrsGDAyoCasu7w34JhpuLeR1wfgo58g9s9+OLw==
101+
dependencies:
102+
"@algolia/client-common" "5.0.0-beta.9"
103+
"@algolia/requester-browser-xhr" "5.0.0-beta.9"
104+
"@algolia/requester-node-http" "5.0.0-beta.9"
105+
74106
"@algolia/client-search@4.16.0":
75107
version "4.16.0"
76108
resolved "https://registry.yarnpkg.com/@algolia/client-search/-/client-search-4.16.0.tgz#19a76bc6cfa495aa9011b32dd85305baa2579589"
@@ -80,6 +112,15 @@
80112
"@algolia/requester-common" "4.16.0"
81113
"@algolia/transporter" "4.16.0"
82114

115+
"@algolia/client-search@5.0.0-beta.8":
116+
version "5.0.0-beta.8"
117+
resolved "https://registry.yarnpkg.com/@algolia/client-search/-/client-search-5.0.0-beta.8.tgz#5abeb90de119ee868aa6a2d898c9d98cf8292258"
118+
integrity sha512-Ub7CEFLDvCF13tq6imE7sEKnxmo177k4euXjaKGUMlznUf9qLWT5g6HuvBH4Nrxwaotima3cRnIw3zkq4JZORw==
119+
dependencies:
120+
"@algolia/client-common" "5.0.0-beta.9"
121+
"@algolia/requester-browser-xhr" "5.0.0-beta.9"
122+
"@algolia/requester-node-http" "5.0.0-beta.9"
123+
83124
"@algolia/events@^4.0.1":
84125
version "4.0.1"
85126
resolved "https://registry.yarnpkg.com/@algolia/events/-/events-4.0.1.tgz#fd39e7477e7bc703d7f893b556f676c032af3950"
@@ -97,13 +138,29 @@
97138
dependencies:
98139
"@algolia/logger-common" "4.16.0"
99140

141+
"@algolia/recommend@5.0.0-beta.8":
142+
version "5.0.0-beta.8"
143+
resolved "https://registry.yarnpkg.com/@algolia/recommend/-/recommend-5.0.0-beta.8.tgz#eff81537479a3390a33ddbffcbf82ae194160019"
144+
integrity sha512-B7tuqYF2YlPmRH5jfDbbC3V1sq8X3a7oadwzAOPGGC1qwi2tO8BoBLM2iIwJRAAesw4NRVeBVDDmdsdiStHeEQ==
145+
dependencies:
146+
"@algolia/client-common" "5.0.0-beta.9"
147+
"@algolia/requester-browser-xhr" "5.0.0-beta.9"
148+
"@algolia/requester-node-http" "5.0.0-beta.9"
149+
100150
"@algolia/requester-browser-xhr@4.16.0":
101151
version "4.16.0"
102152
resolved "https://registry.yarnpkg.com/@algolia/requester-browser-xhr/-/requester-browser-xhr-4.16.0.tgz#742090e53301889ae01581eedcc5bfc2c6dd7660"
103153
integrity sha512-gK+kvs6LHl/PaOJfDuwjkopNbG1djzFLsVBklGBsSU6h6VjFkxIpo6Qq80IK14p9cplYZfhfaL12va6Q9p3KVQ==
104154
dependencies:
105155
"@algolia/requester-common" "4.16.0"
106156

157+
"@algolia/requester-browser-xhr@5.0.0-beta.9":
158+
version "5.0.0-beta.9"
159+
resolved "https://registry.yarnpkg.com/@algolia/requester-browser-xhr/-/requester-browser-xhr-5.0.0-beta.9.tgz#7b43b57cfabe9328e54fb4a96dd10026d27329f1"
160+
integrity sha512-zw/ZmZv/CLjLqBUfukk5kR8N/xF/TbUt6xLdXF1LkaEJDj7BWU1RqMMDeSU5SEcehPMqNumfzFjpaz8D0rZ/jw==
161+
dependencies:
162+
"@algolia/client-common" "5.0.0-beta.9"
163+
107164
"@algolia/requester-common@4.16.0":
108165
version "4.16.0"
109166
resolved "https://registry.yarnpkg.com/@algolia/requester-common/-/requester-common-4.16.0.tgz#a24eb9fc9062f76c2e06599c678542e78c8bce1c"
@@ -116,6 +173,13 @@
116173
dependencies:
117174
"@algolia/requester-common" "4.16.0"
118175

176+
"@algolia/requester-node-http@5.0.0-beta.9":
177+
version "5.0.0-beta.9"
178+
resolved "https://registry.yarnpkg.com/@algolia/requester-node-http/-/requester-node-http-5.0.0-beta.9.tgz#afc04a658c1b624e03e378ea076020a51e737637"
179+
integrity sha512-/32llTTdZ0zMNwn8I8FA12Pw4wZW93AvoHU/8Si2UR8S+FyL8dAkwgmQAvRZinNXD/Bo45Mpjwgydhx9UNZNBA==
180+
dependencies:
181+
"@algolia/client-common" "5.0.0-beta.9"
182+
119183
"@algolia/transporter@4.16.0":
120184
version "4.16.0"
121185
resolved "https://registry.yarnpkg.com/@algolia/transporter/-/transporter-4.16.0.tgz#5e6ca73c8a72d91b7dd75b00b5e45e8538bad8b6"
@@ -5150,6 +5214,20 @@ algoliasearch-helper@^3.11.3:
51505214
dependencies:
51515215
"@algolia/events" "^4.0.1"
51525216

5217+
"algoliasearch-v5@npm:algoliasearch@5.0.0-beta.8":
5218+
version "5.0.0-beta.8"
5219+
resolved "https://registry.yarnpkg.com/algoliasearch/-/algoliasearch-5.0.0-beta.8.tgz#df9ae7351f4d16ef20f04c7ed90203bfb8bdc9df"
5220+
integrity sha512-HeHubssxYRD9OEBySLXM8DdsrDthdyVSogncyQkxO3U+EYfg94Fxe7Kn9ooDm4sX88HS+lOaIV3I+JSwpvoy6A==
5221+
dependencies:
5222+
"@algolia/client-abtesting" "5.0.0-beta.8"
5223+
"@algolia/client-analytics" "5.0.0-beta.8"
5224+
"@algolia/client-common" "5.0.0-beta.9"
5225+
"@algolia/client-personalization" "5.0.0-beta.8"
5226+
"@algolia/client-search" "5.0.0-beta.8"
5227+
"@algolia/recommend" "5.0.0-beta.8"
5228+
"@algolia/requester-browser-xhr" "5.0.0-beta.9"
5229+
"@algolia/requester-node-http" "5.0.0-beta.9"
5230+
51535231
algoliasearch@4.16.0:
51545232
version "4.16.0"
51555233
resolved "https://registry.yarnpkg.com/algoliasearch/-/algoliasearch-4.16.0.tgz#2807f1aee8a574fa8480f39a09b16e407d4cc1a6"

0 commit comments

Comments
 (0)
Please sign in to comment.