Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,42 @@ describe('createSearchInsightsApi', () => {
});
});

test('allows arbitrary additional data to be sent', () => {
const insightsClient = jest.fn();
const insightsApi = createSearchInsightsApi(insightsClient);

insightsApi.convertedObjectIDsAfterSearch({
// Regular properties
eventName: 'Items Added to cart',
index: 'index1',
items: getAlgoliaItems(1),
queryID: 'queryID',
// Extra additional properties
eventSubtype: 'purchase',
objectData: [
{ discount: 0, price: 100, quantity: 1, queryID: 'queryID' },
],
value: 100,
currency: 'USD',
});

expect(insightsClient).toHaveBeenCalledWith(
'convertedObjectIDsAfterSearch',
{
eventName: 'Items Added to cart',
index: 'index1',
objectIDs: ['0'],
queryID: 'queryID',
eventSubtype: 'purchase',
objectData: [
{ discount: 0, price: 100, quantity: 1, queryID: 'queryID' },
],
value: 100,
currency: 'USD',
}
);
});

test('viewedObjectIDs() splits large payloads into multiple chunks', () => {
const insightsClient = jest.fn();
const insightsApi = createSearchInsightsApi(insightsClient);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ConvertedObjectIDsParams,
InsightsClient,
InsightsClientMethod,
WithArbitraryParams,
InsightsParamsWithItems,
ViewedFiltersParams,
ViewedObjectIDsParams,
Expand Down Expand Up @@ -83,13 +84,17 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*/
clickedObjectIDsAfterSearch(
...params: Array<
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
WithArbitraryParams<
InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
>
>
) {
if (params.length > 0) {
sendToInsights(
'clickedObjectIDsAfterSearch',
mapToInsightsParamsApi(params),
mapToInsightsParamsApi<
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way mapToInsightsParamsApi is typed is what's causing the typing issues I was encountering. When I inline the implementation of mapToInsightsParamsApi directly, the issue disappears.

I'm not sure what's going on exactly in how TypeScript interprets it all, but when unioning a strict record with an arbitrary record, the generic type TInsightsParamsType loses all the mandatory properties and the return type only contains objectIDs.

I'm doubtful we can fix this at the mapToInsightsParamsApi level, so the pragmatic option seems to cast.

Lmk if you have a working option that works better.

InsightsParamsWithItems<ClickedObjectIDsAfterSearchParams>
>(params),
params[0].items
);
}
Expand All @@ -100,12 +105,16 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-object-ids/
*/
clickedObjectIDs(
...params: Array<InsightsParamsWithItems<ClickedObjectIDsParams>>
...params: Array<
WithArbitraryParams<InsightsParamsWithItems<ClickedObjectIDsParams>>
>
) {
if (params.length > 0) {
sendToInsights(
'clickedObjectIDs',
mapToInsightsParamsApi(params),
mapToInsightsParamsApi<
InsightsParamsWithItems<ClickedObjectIDsParams>
>(params),
params[0].items
);
}
Expand All @@ -115,7 +124,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*
* @link https://www.algolia.com/doc/api-reference/api-methods/clicked-filters/
*/
clickedFilters(...params: ClickedFiltersParams[]) {
clickedFilters(
...params: Array<WithArbitraryParams<ClickedFiltersParams>>
) {
if (params.length > 0) {
searchInsights('clickedFilters', ...params);
}
Expand All @@ -127,13 +138,17 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*/
convertedObjectIDsAfterSearch(
...params: Array<
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
WithArbitraryParams<
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
>
>
) {
if (params.length > 0) {
sendToInsights(
'convertedObjectIDsAfterSearch',
mapToInsightsParamsApi(params),
mapToInsightsParamsApi<
InsightsParamsWithItems<ConvertedObjectIDsAfterSearchParams>
>(params),
params[0].items
);
}
Expand All @@ -144,12 +159,16 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-object-ids/
*/
convertedObjectIDs(
...params: Array<InsightsParamsWithItems<ConvertedObjectIDsParams>>
...params: Array<
WithArbitraryParams<InsightsParamsWithItems<ConvertedObjectIDsParams>>
>
) {
if (params.length > 0) {
sendToInsights(
'convertedObjectIDs',
mapToInsightsParamsApi(params),
mapToInsightsParamsApi<
InsightsParamsWithItems<ConvertedObjectIDsParams>
>(params),
params[0].items
);
}
Expand All @@ -159,7 +178,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*
* @link https://www.algolia.com/doc/api-reference/api-methods/converted-filters/
*/
convertedFilters(...params: ConvertedFiltersParams[]) {
convertedFilters(
...params: Array<WithArbitraryParams<ConvertedFiltersParams>>
) {
if (params.length > 0) {
searchInsights('convertedFilters', ...params);
}
Expand All @@ -170,7 +191,9 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-object-ids/
*/
viewedObjectIDs(
...params: Array<InsightsParamsWithItems<ViewedObjectIDsParams>>
...params: Array<
WithArbitraryParams<InsightsParamsWithItems<ViewedObjectIDsParams>>
>
) {
if (params.length > 0) {
params
Expand Down Expand Up @@ -202,7 +225,7 @@ export function createSearchInsightsApi(searchInsights: InsightsClient) {
*
* @link https://www.algolia.com/doc/api-reference/api-methods/viewed-filters/
*/
viewedFilters(...params: ViewedFiltersParams[]) {
viewedFilters(...params: Array<WithArbitraryParams<ViewedFiltersParams>>) {
if (params.length > 0) {
searchInsights('viewedFilters', ...params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export type AutocompleteInsightsApi = ReturnType<
typeof createSearchInsightsApi
>;

export type WithArbitraryParams<TParams extends Record<string, unknown>> =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's technically an all-purpose utility type, but since it's only being used here for now, it seemed like an unnecessary hassle to move it to shared.

Record<string, unknown> & TParams;

export type InsightsParamsWithItems<TParams extends { objectIDs: string[] }> =
Omit<TParams, 'objectIDs'> & {
items: AlgoliaInsightsHit[];
Expand Down