-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(createAlgoliaInsightsPlugin): automatically load Insights when n…
…ot passed (#1106)
- Loading branch information
1 parent
8144cf3
commit e9da97f
Showing
8 changed files
with
270 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 30 additions & 1 deletion
31
packages/autocomplete-plugin-algolia-insights/src/types/InsightsClient.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,30 @@ | ||
export type InsightsClient = any; | ||
import type { | ||
InsightsMethodMap, | ||
InsightsClient as _InsightsClient, | ||
} from 'search-insights'; | ||
|
||
export type { | ||
Init as InsightsInit, | ||
AddAlgoliaAgent as InsightsAddAlgoliaAgent, | ||
SetUserToken as InsightsSetUserToken, | ||
GetUserToken as InsightsGetUserToken, | ||
OnUserTokenChange as InsightsOnUserTokenChange, | ||
} from 'search-insights'; | ||
|
||
export type InsightsClientMethod = keyof InsightsMethodMap; | ||
|
||
export type InsightsClientPayload = { | ||
eventName: string; | ||
queryID: string; | ||
index: string; | ||
objectIDs: string[]; | ||
positions?: number[]; | ||
}; | ||
|
||
type QueueItemMap = Record<string, unknown>; | ||
|
||
type QueueItem = QueueItemMap[keyof QueueItemMap]; | ||
|
||
export type InsightsClient = _InsightsClient & { | ||
queue?: QueueItem[]; | ||
}; |
31 changes: 31 additions & 0 deletions
31
packages/autocomplete-shared/src/__tests__/safelyRunOnBrowser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { safelyRunOnBrowser } from '../safelyRunOnBrowser'; | ||
|
||
describe('safelyRunOnBrowser', () => { | ||
const originalWindow = (global as any).window; | ||
|
||
afterEach(() => { | ||
(global as any).window = originalWindow; | ||
}); | ||
|
||
test('runs callback on browsers', () => { | ||
const callback = jest.fn(() => ({ env: 'client' })); | ||
|
||
const result = safelyRunOnBrowser(callback); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
expect(callback).toHaveBeenCalledWith({ window }); | ||
expect(result).toEqual({ env: 'client' }); | ||
}); | ||
|
||
test('does not run callback on servers', () => { | ||
// @ts-expect-error | ||
delete global.window; | ||
|
||
const callback = jest.fn(() => ({ env: 'client' })); | ||
|
||
const result = safelyRunOnBrowser(callback); | ||
|
||
expect(callback).toHaveBeenCalledTimes(0); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
type BrowserCallback<TReturn> = (params: { window: typeof window }) => TReturn; | ||
|
||
/** | ||
* Safely runs code meant for browser environments only. | ||
*/ | ||
export function safelyRunOnBrowser<TReturn>( | ||
callback: BrowserCallback<TReturn> | ||
): TReturn | undefined { | ||
if (typeof window !== 'undefined') { | ||
return callback({ window }); | ||
} | ||
|
||
return undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters