-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtx.ts
128 lines (110 loc) · 2.89 KB
/
rtx.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
121
122
123
124
125
126
127
128
import type {
ErrorHandle as IErrorHandle,
Handle as IHandle,
Method as IMethod,
Route as IRoute,
} from "@fartlabs/rt";
import { createRouter, Router as CRouter } from "@fartlabs/rt";
/**
* ComponentsInterface is the interface for the components.
*/
export type ComponentsInterface = Record<
Capitalize<Lowercase<IMethod>>,
(props: RouteProps) => CRouter<unknown>
>;
/**
* RouteProps are the props for a route component.
*/
export interface RouteProps {
pattern: string;
handle: IHandle;
}
/**
* RouterProps are the props for the router component.
*/
export interface RouterProps {
children?: unknown[];
default?: IHandle;
error?: IErrorHandle;
}
/**
* Router is the router component.
*/
export function Router(props: RouterProps): CRouter<unknown> {
const router = createRouter();
((props.children) as CRouter<unknown>[])
?.forEach((child) => {
if (child instanceof CRouter) {
router.use(child);
return;
}
throw new Error("Invalid child of Router");
});
if (props.default) {
router.default(props.default);
}
if (props.error) {
router.error(props.error);
}
return router;
}
/**
* Route is the route component.
*/
export function Route(props: IRoute): CRouter<unknown> {
return createRouter().with(props);
}
/**
* Connect is the route component for a CONNECT route.
*/
export function Connect(props: RouteProps): CRouter<unknown> {
return createRouter().connect(props.pattern, props.handle);
}
/**
* Delete is the route component for a DELETE route.
*/
export function Delete(props: RouteProps): CRouter<unknown> {
return createRouter().delete(props.pattern, props.handle);
}
/**
* Get is the route component for a GET route.
*/
export function Get(props: RouteProps): CRouter<unknown> {
return createRouter().get(props.pattern, props.handle);
}
/**
* Head is the route component for a HEAD route.
*/
export function Head(props: RouteProps): CRouter<unknown> {
return createRouter().head(props.pattern, props.handle);
}
/**
* Options is the route component for a OPTIONS route.
*/
export function Options(props: RouteProps): CRouter<unknown> {
return createRouter().options(props.pattern, props.handle);
}
/**
* Patch is the route component for a PATCH route.
*/
export function Patch(props: RouteProps): CRouter<unknown> {
return createRouter().patch(props.pattern, props.handle);
}
/**
* Post is the route component for a POST route.
*/
export function Post(props: RouteProps): CRouter<unknown> {
return createRouter().post(props.pattern, props.handle);
}
/**
* Put is the route component for a PUT route.
*/
export function Put(props: RouteProps): CRouter<unknown> {
return createRouter().put(props.pattern, props.handle);
}
/**
* Trace is the route component for a TRACE route.
*/
export function Trace(props: RouteProps): CRouter<unknown> {
return createRouter().trace(props.pattern, props.handle);
}