From 81c934c0ac3af47fa37afc59f5b5f324d3e87bb2 Mon Sep 17 00:00:00 2001 From: Haroen Viaene Date: Mon, 27 Feb 2023 15:06:45 +0100 Subject: [PATCH] feat: identify router --- packages/instantsearch.js/src/lib/routers/history.ts | 1 + .../instantsearch.js/src/lib/stateMappings/simple.ts | 2 ++ .../src/lib/stateMappings/singleIndex.ts | 1 + .../src/middlewares/createRouterMiddleware.ts | 4 +++- packages/instantsearch.js/src/types/router.ts | 10 ++++++++++ .../src/index.ts | 1 + 6 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/instantsearch.js/src/lib/routers/history.ts b/packages/instantsearch.js/src/lib/routers/history.ts index d09e40e1dc6..8c75416797b 100644 --- a/packages/instantsearch.js/src/lib/routers/history.ts +++ b/packages/instantsearch.js/src/lib/routers/history.ts @@ -36,6 +36,7 @@ const setWindowTitle = (title?: string): void => { }; class BrowserHistory implements Router { + public $$type = 'ais.browser'; /** * Transforms a UI state into a title for the page. */ diff --git a/packages/instantsearch.js/src/lib/stateMappings/simple.ts b/packages/instantsearch.js/src/lib/stateMappings/simple.ts index 75094abc2d8..11e2f405b60 100644 --- a/packages/instantsearch.js/src/lib/stateMappings/simple.ts +++ b/packages/instantsearch.js/src/lib/stateMappings/simple.ts @@ -14,6 +14,8 @@ export default function simpleStateMapping< TUiState extends UiState = UiState >(): StateMapping { return { + $$type: 'ais.simple', + stateToRoute(uiState) { return Object.keys(uiState).reduce( (state, indexId) => ({ diff --git a/packages/instantsearch.js/src/lib/stateMappings/singleIndex.ts b/packages/instantsearch.js/src/lib/stateMappings/singleIndex.ts index 3ed1c9f46d0..0ad943877fb 100644 --- a/packages/instantsearch.js/src/lib/stateMappings/singleIndex.ts +++ b/packages/instantsearch.js/src/lib/stateMappings/singleIndex.ts @@ -13,6 +13,7 @@ export default function singleIndexStateMapping< indexName: keyof TUiState ): StateMapping { return { + $$type: 'ais.singleIndex', stateToRoute(uiState) { return getIndexStateWithoutConfigure(uiState[indexName] || {}); }, diff --git a/packages/instantsearch.js/src/middlewares/createRouterMiddleware.ts b/packages/instantsearch.js/src/middlewares/createRouterMiddleware.ts index 2080751e21e..b96f10c2404 100644 --- a/packages/instantsearch.js/src/middlewares/createRouterMiddleware.ts +++ b/packages/instantsearch.js/src/middlewares/createRouterMiddleware.ts @@ -68,7 +68,9 @@ export const createRouterMiddleware = < const initialUiState = instantSearchInstance._initialUiState; return { - $$type: 'ais.router', + $$type: `ais.router({router:${ + router.$$type || '__unknown__' + }, stateMapping:${stateMapping.$$type || '__unknown__'}})`, $$internal, onStateChange({ uiState }) { const routeState = stateMapping.stateToRoute(uiState); diff --git a/packages/instantsearch.js/src/types/router.ts b/packages/instantsearch.js/src/types/router.ts index 5786f1a44e7..b1fd6e5f310 100644 --- a/packages/instantsearch.js/src/types/router.ts +++ b/packages/instantsearch.js/src/types/router.ts @@ -35,6 +35,11 @@ export type Router = { * Called when InstantSearch is disposed. Used to remove subscriptions. */ dispose(): void; + + /** + * Identifier for this router. Used to differentiate between routers. + */ + $$type?: string; }; /** @@ -57,4 +62,9 @@ export type StateMapping = { * The format is the output of `stateToRoute`. */ routeToState(routeState: TRouteState): TUiState; + + /** + * Identifier for this stateMapping. Used to differentiate between stateMappings. + */ + $$type?: string; }; diff --git a/packages/react-instantsearch-hooks-router-nextjs/src/index.ts b/packages/react-instantsearch-hooks-router-nextjs/src/index.ts index 93bc85311e3..283ffabff66 100644 --- a/packages/react-instantsearch-hooks-router-nextjs/src/index.ts +++ b/packages/react-instantsearch-hooks-router-nextjs/src/index.ts @@ -165,6 +165,7 @@ export function createInstantSearchRouterNext( ...routerOptions, }); router._isNextRouter = true; + router.$$type = 'ais.nextjs'; return router; }