-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from kitbagjs/router-push-types
- Loading branch information
Showing
11 changed files
with
105 additions
and
74 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
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
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
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,38 @@ | ||
import { Resolved, Route, RouteComponent, RouterPush, RouterPushOptions, Routes } from '@/types' | ||
import { flattenParentMatches } from '@/utilities/flattenParentMatches' | ||
import { RouterNavigation } from '@/utilities/routerNavigation' | ||
import { assembleUrl } from '@/utilities/urlAssembly' | ||
|
||
type AnyRoutes = [{ name: string, path: string, component: RouteComponent }] | ||
|
||
type RouterPushContext = { | ||
navigation: RouterNavigation, | ||
resolved: Resolved<Route>[], | ||
} | ||
|
||
export function createRouterPush<const TRoutes extends Routes>({ navigation, resolved }: RouterPushContext): RouterPush<TRoutes> { | ||
|
||
const push: RouterPush<AnyRoutes> = (urlOrRouteConfig, options?: RouterPushOptions) => { | ||
if (typeof urlOrRouteConfig === 'object') { | ||
const { route, params, ...options } = urlOrRouteConfig | ||
const match = resolved.find((resolvedRoute) => flattenParentMatches(resolvedRoute) === route) | ||
|
||
if (!match) { | ||
throw `No route found: "${String(route)}"` | ||
} | ||
|
||
const url = assembleUrl(match, params) | ||
|
||
return push(url, options) | ||
} | ||
|
||
if (typeof urlOrRouteConfig === 'string') { | ||
return navigation.update(urlOrRouteConfig, options) | ||
} | ||
|
||
const exhaustive: never = urlOrRouteConfig | ||
throw new Error(`Unhandled router push overload: ${JSON.stringify(exhaustive)}`) | ||
} | ||
|
||
return push as any | ||
} |
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