-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
routeModules.ts
267 lines (244 loc) · 7.82 KB
/
routeModules.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import type {
ActionFunction as RRActionFunction,
ActionFunctionArgs as RRActionFunctionArgs,
AgnosticRouteMatch,
LoaderFunction as RRLoaderFunction,
LoaderFunctionArgs as RRLoaderFunctionArgs,
Location,
Params,
} from "@remix-run/router";
import type { AppData, AppLoadContext } from "./data";
import type { LinkDescriptor } from "./links";
import type { SerializeFrom } from "./serialize";
export interface RouteModules<RouteModule> {
[routeId: string]: RouteModule;
}
/**
* @deprecated Use `LoaderFunctionArgs`/`ActionFunctionArgs` instead
*/
export type DataFunctionArgs = RRActionFunctionArgs<AppLoadContext> &
RRLoaderFunctionArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
// RR also doesn't export DataFunctionArgs, so we extend the two interfaces here
// even tough they're identical under the hood
context: AppLoadContext;
};
/**
* A function that handles data mutations for a route on the server
*/
export type ActionFunction = (
args: ActionFunctionArgs
) => ReturnType<RRActionFunction>;
/**
* Arguments passed to a route `action` function
*/
export type ActionFunctionArgs = RRActionFunctionArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
context: AppLoadContext;
};
/**
* A function that handles data mutations for a route on the client
* @private Public API is exported from @remix-run/react
*/
type ClientActionFunction = (
args: ClientActionFunctionArgs
) => ReturnType<RRActionFunction>;
/**
* Arguments passed to a route `clientAction` function
* @private Public API is exported from @remix-run/react
*/
export type ClientActionFunctionArgs = RRActionFunctionArgs<undefined> & {
serverAction: <T = AppData>() => Promise<SerializeFrom<T>>;
};
/**
* A function that loads data for a route on the server
*/
export type LoaderFunction = (
args: LoaderFunctionArgs
) => ReturnType<RRLoaderFunction>;
/**
* Arguments passed to a route `loader` function
*/
export type LoaderFunctionArgs = RRLoaderFunctionArgs<AppLoadContext> & {
// Context is always provided in Remix, and typed for module augmentation support.
context: AppLoadContext;
};
/**
* A function that loads data for a route on the client
* @private Public API is exported from @remix-run/react
*/
type ClientLoaderFunction = ((
args: ClientLoaderFunctionArgs
) => ReturnType<RRLoaderFunction>) & {
hydrate?: boolean;
};
/**
* Arguments passed to a route `clientLoader` function
* @private Public API is exported from @remix-run/react
*/
export type ClientLoaderFunctionArgs = RRLoaderFunctionArgs<undefined> & {
serverLoader: <T = AppData>() => Promise<SerializeFrom<T>>;
};
export type HeadersArgs = {
loaderHeaders: Headers;
parentHeaders: Headers;
actionHeaders: Headers;
errorHeaders: Headers | undefined;
};
/**
* A function that returns HTTP headers to be used for a route. These headers
* will be merged with (and take precedence over) headers from parent routes.
*/
export interface HeadersFunction {
(args: HeadersArgs): Headers | HeadersInit;
}
/**
* A function that defines `<link>` tags to be inserted into the `<head>` of
* the document on route transitions.
*/
export interface LinksFunction {
(): LinkDescriptor[];
}
/**
* A function that returns an array of data objects to use for rendering
* metadata HTML tags in a route. These tags are not rendered on descendant
* routes in the route hierarchy. In other words, they will only be rendered on
* the route in which they are exported.
*
* @param Loader - The type of the current route's loader function
* @param MatchLoaders - Mapping from a parent route's filepath to its loader
* function type
*
* Note that parent route filepaths are relative to the `app/` directory.
*
* For example, if this meta function is for `/sales/customers/$customerId`:
*
* ```ts
* // app/root.tsx
* const loader = () => {
* return json({ hello: "world" } as const)
* }
* export type Loader = typeof loader
*
* // app/routes/sales.tsx
* const loader = () => {
* return json({ salesCount: 1074 })
* }
* export type Loader = typeof loader
*
* // app/routes/sales/customers.tsx
* const loader = () => {
* return json({ customerCount: 74 })
* }
* export type Loader = typeof loader
*
* // app/routes/sales/customers/$customersId.tsx
* import type { Loader as RootLoader } from "../../../root"
* import type { Loader as SalesLoader } from "../../sales"
* import type { Loader as CustomersLoader } from "../../sales/customers"
*
* const loader = () => {
* return json({ name: "Customer name" })
* }
*
* const meta: MetaFunction<typeof loader, {
* "root": RootLoader,
* "routes/sales": SalesLoader,
* "routes/sales/customers": CustomersLoader,
* }> = ({ data, matches }) => {
* const { name } = data
* // ^? string
* const { customerCount } = matches.find((match) => match.id === "routes/sales/customers").data
* // ^? number
* const { salesCount } = matches.find((match) => match.id === "routes/sales").data
* // ^? number
* const { hello } = matches.find((match) => match.id === "root").data
* // ^? "world"
* }
* ```
*/
export interface ServerRuntimeMetaFunction<
Loader extends LoaderFunction | unknown = unknown,
ParentsLoaders extends Record<string, LoaderFunction | unknown> = Record<
string,
unknown
>
> {
(
args: ServerRuntimeMetaArgs<Loader, ParentsLoaders>
): ServerRuntimeMetaDescriptor[];
}
interface ServerRuntimeMetaMatch<
RouteId extends string = string,
Loader extends LoaderFunction | unknown = unknown
> {
id: RouteId;
pathname: AgnosticRouteMatch["pathname"];
data: Loader extends LoaderFunction ? SerializeFrom<Loader> : unknown;
handle?: RouteHandle;
params: AgnosticRouteMatch["params"];
meta: ServerRuntimeMetaDescriptor[];
error?: unknown;
}
type ServerRuntimeMetaMatches<
MatchLoaders extends Record<string, LoaderFunction | unknown> = Record<
string,
unknown
>
> = Array<
{
[K in keyof MatchLoaders]: ServerRuntimeMetaMatch<
Exclude<K, number | symbol>,
MatchLoaders[K]
>;
}[keyof MatchLoaders]
>;
export interface ServerRuntimeMetaArgs<
Loader extends LoaderFunction | unknown = unknown,
MatchLoaders extends Record<string, LoaderFunction | unknown> = Record<
string,
unknown
>
> {
data:
| (Loader extends LoaderFunction ? SerializeFrom<Loader> : AppData)
| undefined;
params: Params;
location: Location;
matches: ServerRuntimeMetaMatches<MatchLoaders>;
error?: unknown;
}
export type ServerRuntimeMetaDescriptor =
| { charSet: "utf-8" }
| { title: string }
| { name: string; content: string }
| { property: string; content: string }
| { httpEquiv: string; content: string }
| { "script:ld+json": LdJsonObject }
| { tagName: "meta" | "link"; [name: string]: string }
| { [name: string]: unknown };
type LdJsonObject = { [Key in string]: LdJsonValue } & {
[Key in string]?: LdJsonValue | undefined;
};
type LdJsonArray = LdJsonValue[] | readonly LdJsonValue[];
type LdJsonPrimitive = string | number | boolean | null;
type LdJsonValue = LdJsonPrimitive | LdJsonObject | LdJsonArray;
/**
* An arbitrary object that is associated with a route.
*/
export type RouteHandle = unknown;
export interface EntryRouteModule {
clientAction?: ClientActionFunction;
clientLoader?: ClientLoaderFunction;
ErrorBoundary?: any; // Weakly typed because server-runtime is not React-aware
HydrateFallback?: any; // Weakly typed because server-runtime is not React-aware
default: any; // Weakly typed because server-runtime is not React-aware
handle?: RouteHandle;
links?: LinksFunction;
meta?: ServerRuntimeMetaFunction;
}
export interface ServerRouteModule extends EntryRouteModule {
action?: ActionFunction;
headers?: HeadersFunction | { [name: string]: string };
loader?: LoaderFunction;
}