-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
143 lines (115 loc) · 3.54 KB
/
data.js
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
/**
* @remix-run/server-runtime v1.5.1
*
* Copyright (c) Remix Software Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var responses = require('./responses.js');
/**
* An object of arbitrary for route loaders and actions provided by the
* server's `getLoadContext()` function.
*/
async function callRouteAction({
loadContext,
match,
request
}) {
let action = match.route.module.action;
if (!action) {
let response = new Response(null, {
status: 405
});
response.headers.set("X-Remix-Catch", "yes");
return response;
}
let result;
try {
result = await action({
request: stripDataParam(stripIndexParam(request)),
context: loadContext,
params: match.params
});
} catch (error) {
if (!responses.isResponse(error)) {
throw error;
}
if (!responses.isRedirectResponse(error)) {
error.headers.set("X-Remix-Catch", "yes");
}
result = error;
}
if (result === undefined) {
throw new Error(`You defined an action for route "${match.route.id}" but didn't return ` + `anything from your \`action\` function. Please return a value or \`null\`.`);
}
return responses.isResponse(result) ? result : responses.json(result);
}
async function callRouteLoader({
loadContext,
match,
request
}) {
let loader = match.route.module.loader;
if (!loader) {
throw new Error(`You made a ${request.method} request to ${request.url} but did not provide ` + `a default component or \`loader\` for route "${match.route.id}", ` + `so there is no way to handle the request.`);
}
let result;
try {
result = await loader({
request: stripDataParam(stripIndexParam(request)),
context: loadContext,
params: match.params
});
} catch (error) {
if (!responses.isResponse(error)) {
throw error;
}
if (!responses.isRedirectResponse(error)) {
error.headers.set("X-Remix-Catch", "yes");
}
result = error;
}
if (result === undefined) {
throw new Error(`You defined a loader for route "${match.route.id}" but didn't return ` + `anything from your \`loader\` function. Please return a value or \`null\`.`);
}
return responses.isResponse(result) ? result : responses.json(result);
}
function stripIndexParam(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);
}
return new Request(url.href, request);
}
function stripDataParam(request) {
let url = new URL(request.url);
url.searchParams.delete("_data");
return new Request(url.href, request);
}
function extractData(response) {
let contentType = response.headers.get("Content-Type");
if (contentType && /\bapplication\/json\b/.test(contentType)) {
return response.json();
} // What other data types do we need to handle here? What other kinds of
// responses are people going to be returning from their loaders?
// - application/x-www-form-urlencoded ?
// - multipart/form-data ?
// - binary (audio/video) ?
return response.text();
}
exports.callRouteAction = callRouteAction;
exports.callRouteLoader = callRouteLoader;
exports.extractData = extractData;