forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routers.ts
76 lines (67 loc) · 2.38 KB
/
routers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { useRouter as useAppRouter } from 'next/navigation'
import { useRouter as usePagesRouter } from 'next/router'
import type { PoC_Routes, Route as NextRoute } from 'next'
import { type Route, createUrl } from './utils'
export namespace AppDir {
export function useRouter() {
const { push, replace, prefetch, ...router } = useAppRouter()
return {
...router,
push: <Path extends PoC_Routes.AllRoutes>(
hrefTemplate: Path,
options: Route.Data<Path> & Parameters<typeof push>[1]
) => push(createUrl(hrefTemplate, options), options),
replace: <Path extends PoC_Routes.AllRoutes>(
hrefTemplate: Path,
options: Route.Data<Path> & Parameters<typeof replace>[1]
) => replace(createUrl(hrefTemplate, options), options),
prefetch: <Path extends PoC_Routes.AllRoutes>(
hrefTemplate: Path,
options: Route.Data<Path>
) => prefetch(createUrl(hrefTemplate, options)),
} as const
}
}
export namespace PagesDir {
type RouteData<Path extends PoC_Routes.AllRoutes> = Route.Data<Path> & {
path: Path
}
export function useRouter() {
const { push, replace, prefetch, ...router } = usePagesRouter()
return {
...router,
push: <
Path extends PoC_Routes.AllRoutes,
As extends PoC_Routes.AllRoutes
>(
url: RouteData<Path> /* | Parameters<typeof push>[0] */,
as?: RouteData<As> /* | Parameters<typeof push>[1] */,
options?: Parameters<typeof push>[2]
) =>
push(
// 'path' in url ? createUrl(url.path, url) : url,
createUrl(url.path, url),
as === undefined ? undefined : createUrl(as.path, as),
options
),
replace: <
Path extends PoC_Routes.AllRoutes,
As extends PoC_Routes.AllRoutes
>(
url: RouteData<Path> /* | Parameters<typeof replace>[0] */,
as?: RouteData<As> /* | Parameters<typeof replace>[1] */,
options?: Parameters<typeof replace>[2]
) =>
replace(
// 'path' in url ? createUrl(url.path, url) : url,
createUrl(url.path, url),
as === undefined ? undefined : createUrl(as.path, as),
options
),
prefetch: <Path extends PoC_Routes.AllRoutes>(
hrefTemplate: Path,
options: Route.Data<Path>
) => prefetch(createUrl(hrefTemplate, options)),
} as const
}
}