-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate-router.ts
120 lines (113 loc) · 4.42 KB
/
create-router.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
EnvironmentInjector,
inject,
Provider,
signal,
Type,
} from '@angular/core';
import type { RouterHistory } from '@tanstack/history';
import type {
AnyRoute,
CreateRouterFn,
RouterConstructorOptions,
TrailingSlashOption,
} from '@tanstack/router-core';
import { RouterCore } from '@tanstack/router-core';
declare module '@tanstack/router-core' {
export interface UpdatableRouteOptionsExtensions {
component: () => Type<any>;
providers?: Provider[];
}
export interface RouterOptionsExtensions {
/**
* The default `component` a route should use if no component is provided.
*
* @default Outlet
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#defaultcomponent-property)
*/
// defaultComponent?: RouteComponent
/**
* The default `errorComponent` a route should use if no error component is provided.
*
* @default ErrorComponent
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#defaulterrorcomponent-property)
* @link [Guide](https://tanstack.com/router/latest/docs/framework/solid/guide/data-loading#handling-errors-with-routeoptionserrorcomponent)
*/
// defaultErrorComponent?: ErrorRouteComponent
/**
* The default `pendingComponent` a route should use if no pending component is provided.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#defaultpendingcomponent-property)
* @link [Guide](https://tanstack.com/router/latest/docs/framework/solid/guide/data-loading#showing-a-pending-component)
*/
// defaultPendingComponent?: RouteComponent
/**
* The default `notFoundComponent` a route should use if no notFound component is provided.
*
* @default NotFound
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#defaultnotfoundcomponent-property)
* @link [Guide](https://tanstack.com/router/latest/docs/framework/solid/guide/not-found-errors#default-router-wide-not-found-handling)
*/
// defaultNotFoundComponent?: NotFoundRouteComponent
/**
* A component that will be used to wrap the entire router.
*
* This is useful for providing a context to the entire router.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#wrap-property)
*/
// Wrap?: (props: { children: any }) => Type<unknown>
/**
* A component that will be used to wrap the inner contents of the router.
*
* This is useful for providing a context to the inner contents of the router where you also need access to the router context and hooks.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/solid/api/router/RouterOptionsType#innerwrap-property)
*/
// InnerWrap?: (props: { children: any }) => Type<unknown>
/**
* The default `onCatch` handler for errors caught by the Router ErrorBoundary
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#defaultoncatch-property)
* @link [Guide](https://tanstack.com/router/latest/docs/framework/react/guide/data-loading#handling-errors-with-routeoptionsoncatch)
*/
defaultOnCatch?: (error: Error) => void;
}
}
export const createRouter: CreateRouterFn = (options) => {
return new NgRouter(options);
};
export class NgRouter<
in out TRouteTree extends AnyRoute,
in out TTrailingSlashOption extends TrailingSlashOption = 'never',
in out TDefaultStructuralSharingOption extends boolean = false,
in out TRouterHistory extends RouterHistory = RouterHistory,
in out TDehydrated extends Record<string, any> = Record<string, any>,
> extends RouterCore<
TRouteTree,
TTrailingSlashOption,
TDefaultStructuralSharingOption,
TRouterHistory,
TDehydrated
> {
readonly routerState = signal(this.state);
readonly injector = inject(EnvironmentInjector);
constructor(
options: RouterConstructorOptions<
TRouteTree,
TTrailingSlashOption,
TDefaultStructuralSharingOption,
TRouterHistory,
TDehydrated
>
) {
super(options);
void this.load({ sync: true });
this.__store.subscribe(() => {
this.routerState.set(this.state);
});
}
getRouteById(routeId: string) {
return this.routesById[routeId];
}
}