Skip to content

Commit 148b677

Browse files
authored
fix(algoliasearch): correctly retrieve headers for v5 (#1263)
1 parent 0ae0c5c commit 148b677

File tree

12 files changed

+144
-44
lines changed

12 files changed

+144
-44
lines changed

examples/react-17/src/Highlight.tsx

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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:[email protected]",
4849
"autoprefixer": "10.4.14",
4950
"babel-eslint": "10.1.0",
5051
"babel-loader": "8.2.3",
Lines changed: 1 addition & 1 deletion
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

Lines changed: 13 additions & 10 deletions
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

Lines changed: 13 additions & 10 deletions
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

Lines changed: 10 additions & 7 deletions
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

Lines changed: 10 additions & 7 deletions
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

Lines changed: 9 additions & 2 deletions
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

Lines changed: 6 additions & 4 deletions
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];
Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)