-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathdata.ts
145 lines (129 loc) · 3.42 KB
/
data.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
import {
redirect,
json,
isDeferredData,
isResponse,
isRedirectStatusCode,
} from "./responses";
import type {
ActionFunction,
DataFunctionArgs,
LoaderFunction,
} from "./routeModules";
/**
* An object of unknown type for route loaders and actions provided by the
* server's `getLoadContext()` function. This is defined as an empty interface
* specifically so apps can leverage declaration merging to augment this type
* globally: https://www.typescriptlang.org/docs/handbook/declaration-merging.html
*/
export interface AppLoadContext {
[key: string]: unknown;
}
/**
* Data for a route that was returned from a `loader()`.
*/
export type AppData = unknown;
export async function callRouteActionRR({
loadContext,
action,
params,
request,
routeId,
}: {
request: Request;
action: ActionFunction;
params: DataFunctionArgs["params"];
loadContext: AppLoadContext;
routeId: string;
}) {
let result = await action({
request: stripDataParam(stripIndexParam(request)),
context: loadContext,
params,
});
if (result === undefined) {
throw new Error(
`You defined an action for route "${routeId}" but didn't return ` +
`anything from your \`action\` function. Please return a value or \`null\`.`
);
}
return isResponse(result) ? result : json(result);
}
export async function callRouteLoaderRR({
loadContext,
loader,
params,
request,
routeId,
}: {
request: Request;
loader: LoaderFunction;
params: DataFunctionArgs["params"];
loadContext: AppLoadContext;
routeId: string;
}) {
let result = await loader({
request: stripDataParam(stripIndexParam(request)),
context: loadContext,
params,
});
if (result === undefined) {
throw new Error(
`You defined a loader for route "${routeId}" but didn't return ` +
`anything from your \`loader\` function. Please return a value or \`null\`.`
);
}
if (isDeferredData(result)) {
if (result.init && isRedirectStatusCode(result.init.status || 200)) {
return redirect(
new Headers(result.init.headers).get("Location")!,
result.init
);
}
return result;
}
return isResponse(result) ? result : json(result);
}
// TODO: Document these search params better
// and stop stripping these in V2. These break
// support for running in a SW and also expose
// valuable info to data funcs that is being asked
// for such as "is this a data request?".
function stripIndexParam(request: Request) {
let url = new URL(request.url);
let indexValues = url.searchParams.getAll("index");
url.searchParams.delete("index");
let indexValuesToKeep = [];
for (let indexValue of indexValues) {
if (indexValue) {
indexValuesToKeep.push(indexValue);
}
}
for (let toKeep of indexValuesToKeep) {
url.searchParams.append("index", toKeep);
}
let init: RequestInit = {
method: request.method,
body: request.body,
headers: request.headers,
signal: request.signal,
};
if (init.body) {
(init as { duplex: "half" }).duplex = "half";
}
return new Request(url.href, init);
}
function stripDataParam(request: Request) {
let url = new URL(request.url);
url.searchParams.delete("_data");
let init: RequestInit = {
method: request.method,
body: request.body,
headers: request.headers,
signal: request.signal,
};
if (init.body) {
(init as { duplex: "half" }).duplex = "half";
}
return new Request(url.href, init);
}