@modern-js/plugin-data-loader 2.5.0 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/{create-request.js → createRequest.js} +17 -6
- package/dist/cjs/cli/data.js +162 -0
- package/dist/cjs/cli/{generate-client.js → generateClient.js} +8 -4
- package/dist/cjs/cli/loader.js +5 -2
- package/dist/cjs/common/constants.js +3 -0
- package/dist/cjs/runtime/index.js +165 -0
- package/dist/cjs/runtime/response.js +102 -0
- package/dist/cjs/server/index.js +26 -180
- package/dist/esm/cli/{create-request.js → createRequest.js} +42 -3
- package/dist/esm/cli/data.js +820 -0
- package/dist/esm/cli/{generate-client.js → generateClient.js} +1 -1
- package/dist/esm/cli/loader.js +7 -1
- package/dist/esm/common/constants.js +2 -1
- package/dist/esm/runtime/index.js +427 -0
- package/dist/esm/runtime/response.js +284 -0
- package/dist/esm/server/index.js +38 -431
- package/dist/esm-node/cli/{create-request.js → createRequest.js} +19 -4
- package/dist/esm-node/cli/data.js +142 -0
- package/dist/esm-node/cli/{generate-client.js → generateClient.js} +1 -1
- package/dist/esm-node/cli/loader.js +4 -1
- package/dist/esm-node/common/constants.js +2 -0
- package/dist/esm-node/runtime/index.js +147 -0
- package/dist/esm-node/runtime/response.js +79 -0
- package/dist/esm-node/server/index.js +21 -180
- package/dist/types/cli/{create-request.d.ts → createRequest.d.ts} +2 -1
- package/dist/types/cli/data.d.ts +6 -0
- package/dist/types/common/constants.d.ts +2 -1
- package/dist/types/runtime/index.d.ts +15 -0
- package/dist/types/runtime/response.d.ts +2 -0
- package/dist/types/server/index.d.ts +0 -15
- package/package.json +19 -9
- /package/dist/types/cli/{generate-client.d.ts → generateClient.d.ts} +0 -0
package/dist/cjs/server/index.js
CHANGED
|
@@ -17,188 +17,23 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
return to;
|
|
18
18
|
};
|
|
19
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
20
24
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
25
|
mod
|
|
22
26
|
));
|
|
23
27
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
28
|
var server_exports = {};
|
|
25
29
|
__export(server_exports, {
|
|
26
|
-
default: () => server_default
|
|
27
|
-
getPathWithoutEntry: () => getPathWithoutEntry,
|
|
28
|
-
handleRequest: () => handleRequest,
|
|
29
|
-
isRedirectResponse: () => isRedirectResponse,
|
|
30
|
-
isResponse: () => isResponse
|
|
30
|
+
default: () => server_default
|
|
31
31
|
});
|
|
32
32
|
module.exports = __toCommonJS(server_exports);
|
|
33
33
|
var import_path = __toESM(require("path"));
|
|
34
|
-
var
|
|
35
|
-
var import_react_router_dom = require("react-router-dom");
|
|
34
|
+
var import_fs = __toESM(require("fs"));
|
|
36
35
|
var import_utils = require("@modern-js/utils");
|
|
37
|
-
var
|
|
38
|
-
(0, import_node.installGlobals)();
|
|
39
|
-
const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
40
|
-
function isRedirectResponse(response) {
|
|
41
|
-
return redirectStatusCodes.has(response.status);
|
|
42
|
-
}
|
|
43
|
-
function isResponse(value) {
|
|
44
|
-
return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
|
|
45
|
-
}
|
|
46
|
-
const json = (data, init = {}) => {
|
|
47
|
-
const responseInit = typeof init === "number" ? { status: init } : init;
|
|
48
|
-
const headers = new Headers(responseInit.headers);
|
|
49
|
-
if (!headers.has("Content-Type")) {
|
|
50
|
-
headers.set("Content-Type", "application/json; charset=utf-8");
|
|
51
|
-
}
|
|
52
|
-
return new import_node.Response(JSON.stringify(data), {
|
|
53
|
-
...responseInit,
|
|
54
|
-
headers
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
const callRouteLoader = async ({
|
|
58
|
-
routeId,
|
|
59
|
-
loader,
|
|
60
|
-
params,
|
|
61
|
-
request,
|
|
62
|
-
loadContext
|
|
63
|
-
}) => {
|
|
64
|
-
if (!loader) {
|
|
65
|
-
throw new Error(
|
|
66
|
-
`You made a ${request.method} request to ${request.url} but did not provide a default component or \`loader\` for route "${routeId}", so there is no way to handle the request.`
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
let result;
|
|
70
|
-
try {
|
|
71
|
-
result = await loader({
|
|
72
|
-
request,
|
|
73
|
-
params
|
|
74
|
-
});
|
|
75
|
-
} catch (error) {
|
|
76
|
-
if (!isResponse(error)) {
|
|
77
|
-
throw error;
|
|
78
|
-
}
|
|
79
|
-
result = error;
|
|
80
|
-
}
|
|
81
|
-
if (result === void 0) {
|
|
82
|
-
throw new Error(
|
|
83
|
-
`You defined a loader for route "${routeId}" but didn't return anything from your \`loader\` function. Please return a value or \`null\`.`
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
return isResponse(result) ? result : json(result);
|
|
87
|
-
};
|
|
88
|
-
const createLoaderHeaders = (requestHeaders) => {
|
|
89
|
-
const headers = new Headers();
|
|
90
|
-
for (const [key, values] of Object.entries(requestHeaders)) {
|
|
91
|
-
if (values) {
|
|
92
|
-
if (Array.isArray(values)) {
|
|
93
|
-
for (const value of values) {
|
|
94
|
-
headers.append(key, value);
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
97
|
-
headers.set(key, values);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return headers;
|
|
102
|
-
};
|
|
103
|
-
const createLoaderRequest = (context) => {
|
|
104
|
-
const origin = `${context.protocol}://${context.host}`;
|
|
105
|
-
const url = new URL(context.url, origin);
|
|
106
|
-
const controller = new AbortController();
|
|
107
|
-
const init = {
|
|
108
|
-
method: context.method,
|
|
109
|
-
headers: createLoaderHeaders(context.headers),
|
|
110
|
-
signal: controller.signal
|
|
111
|
-
};
|
|
112
|
-
return new Request(url.href, init);
|
|
113
|
-
};
|
|
114
|
-
const sendLoaderResponse = async (res, nodeResponse) => {
|
|
115
|
-
res.statusMessage = nodeResponse.statusText;
|
|
116
|
-
res.statusCode = nodeResponse.status;
|
|
117
|
-
for (const [key, value] of nodeResponse.headers.entries()) {
|
|
118
|
-
res.setHeader(key, value);
|
|
119
|
-
}
|
|
120
|
-
if (nodeResponse.body) {
|
|
121
|
-
await (0, import_node.writeReadableStreamToWritable)(nodeResponse.body, res);
|
|
122
|
-
} else {
|
|
123
|
-
res.end();
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
const getPathWithoutEntry = (pathname, entryPath) => {
|
|
127
|
-
if (entryPath === "/") {
|
|
128
|
-
return pathname;
|
|
129
|
-
}
|
|
130
|
-
return pathname.replace(entryPath, "");
|
|
131
|
-
};
|
|
132
|
-
const matchEntry = (pathname, entries) => {
|
|
133
|
-
return entries.find((entry) => pathname.startsWith(entry.urlPath));
|
|
134
|
-
};
|
|
135
|
-
const handleRequest = async ({
|
|
136
|
-
context,
|
|
137
|
-
serverRoutes,
|
|
138
|
-
distDir
|
|
139
|
-
}) => {
|
|
140
|
-
const { method, query } = context;
|
|
141
|
-
const routeId = query[import_constants.LOADER_ID_PARAM];
|
|
142
|
-
if (!routeId || method.toLowerCase() !== "get") {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
const entry = matchEntry(context.path, serverRoutes);
|
|
146
|
-
if (!entry) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
const routesPath = import_path.default.join(
|
|
150
|
-
distDir,
|
|
151
|
-
import_utils.SERVER_BUNDLE_DIRECTORY,
|
|
152
|
-
`${entry.entryName || import_utils.MAIN_ENTRY_NAME}-server-loaders`
|
|
153
|
-
);
|
|
154
|
-
const { routes } = await Promise.resolve().then(() => __toESM(require(routesPath)));
|
|
155
|
-
if (!routes) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
const { res } = context;
|
|
159
|
-
const pathname = getPathWithoutEntry(context.path, entry.urlPath);
|
|
160
|
-
const matches = (0, import_react_router_dom.matchRoutes)(routes, pathname);
|
|
161
|
-
if (!matches) {
|
|
162
|
-
res.statusCode = 403;
|
|
163
|
-
res.end(`Route ${pathname} was not matched`);
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
const match = matches == null ? void 0 : matches.find((match2) => match2.route.id === routeId);
|
|
167
|
-
if (!match) {
|
|
168
|
-
res.statusCode = 403;
|
|
169
|
-
res.end(`Route ${routeId} does not match URL ${context.path}`);
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
const request = createLoaderRequest(context);
|
|
173
|
-
let response;
|
|
174
|
-
try {
|
|
175
|
-
response = await callRouteLoader({
|
|
176
|
-
loader: match.route.loader,
|
|
177
|
-
routeId: match.route.id,
|
|
178
|
-
params: match.params,
|
|
179
|
-
request,
|
|
180
|
-
loadContext: {}
|
|
181
|
-
});
|
|
182
|
-
if (isRedirectResponse(response)) {
|
|
183
|
-
const headers = new Headers(response.headers);
|
|
184
|
-
headers.set("X-Modernjs-Redirect", headers.get("Location"));
|
|
185
|
-
headers.delete("Location");
|
|
186
|
-
response = new import_node.Response(null, {
|
|
187
|
-
status: 204,
|
|
188
|
-
headers
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
} catch (error) {
|
|
192
|
-
const message = String(error);
|
|
193
|
-
response = new import_node.Response(message, {
|
|
194
|
-
status: 500,
|
|
195
|
-
headers: {
|
|
196
|
-
"Content-Type": "text/plain"
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
sendLoaderResponse(res, response);
|
|
201
|
-
};
|
|
36
|
+
var import_runtime2 = require("../runtime");
|
|
202
37
|
var server_default = () => ({
|
|
203
38
|
name: "@modern-js/plugin-data-loader",
|
|
204
39
|
setup: () => ({
|
|
@@ -207,19 +42,30 @@ var server_default = () => ({
|
|
|
207
42
|
distDir
|
|
208
43
|
}) {
|
|
209
44
|
return async (context) => {
|
|
45
|
+
const entry = (0, import_runtime2.matchEntry)(context.path, serverRoutes);
|
|
46
|
+
if (!entry) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const routesPath = import_path.default.join(
|
|
50
|
+
distDir,
|
|
51
|
+
import_utils.SERVER_BUNDLE_DIRECTORY,
|
|
52
|
+
`${entry.entryName || import_utils.MAIN_ENTRY_NAME}-server-loaders.js`
|
|
53
|
+
);
|
|
54
|
+
if (!import_fs.default.existsSync(routesPath)) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const { routes, handleRequest } = await Promise.resolve().then(() => __toESM(require(routesPath)));
|
|
58
|
+
if (!routes) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
210
61
|
return handleRequest({
|
|
211
62
|
serverRoutes,
|
|
212
|
-
|
|
213
|
-
|
|
63
|
+
context,
|
|
64
|
+
routes
|
|
214
65
|
});
|
|
215
66
|
};
|
|
216
67
|
}
|
|
217
68
|
})
|
|
218
69
|
});
|
|
219
70
|
// Annotate the CommonJS export names for ESM import in node:
|
|
220
|
-
0 && (module.exports = {
|
|
221
|
-
getPathWithoutEntry,
|
|
222
|
-
handleRequest,
|
|
223
|
-
isRedirectResponse,
|
|
224
|
-
isResponse
|
|
225
|
-
});
|
|
71
|
+
0 && (module.exports = {});
|
|
@@ -124,7 +124,8 @@ var __generator = this && this.__generator || function(thisArg, body) {
|
|
|
124
124
|
};
|
|
125
125
|
import { compile } from "path-to-regexp";
|
|
126
126
|
import { redirect } from "react-router-dom";
|
|
127
|
-
import { LOADER_ID_PARAM, DIRECT_PARAM } from "../common/constants";
|
|
127
|
+
import { LOADER_ID_PARAM, DIRECT_PARAM, CONTENT_TYPE_DEFERRED } from "../common/constants";
|
|
128
|
+
import { parseDeferredReadableStream } from "./data";
|
|
128
129
|
var getRequestUrl = function(param) {
|
|
129
130
|
var params = param.params, request = param.request, routeId = param.routeId;
|
|
130
131
|
var url = new URL(request.url);
|
|
@@ -141,10 +142,41 @@ var handleRedirectResponse = function(res) {
|
|
|
141
142
|
var headers = res.headers;
|
|
142
143
|
var location = headers.get("X-Modernjs-Redirect");
|
|
143
144
|
if (location) {
|
|
144
|
-
|
|
145
|
+
throw redirect(location);
|
|
145
146
|
}
|
|
146
147
|
return res;
|
|
147
148
|
};
|
|
149
|
+
var handleDeferredResponse = function() {
|
|
150
|
+
var _ref = _asyncToGenerator(function(res) {
|
|
151
|
+
var _res_headers_get;
|
|
152
|
+
return __generator(this, function(_state) {
|
|
153
|
+
switch(_state.label){
|
|
154
|
+
case 0:
|
|
155
|
+
if (!(((_res_headers_get = res.headers.get("Content-Type")) === null || _res_headers_get === void 0 ? void 0 : _res_headers_get.match(CONTENT_TYPE_DEFERRED)) && res.body)) return [
|
|
156
|
+
3,
|
|
157
|
+
2
|
|
158
|
+
];
|
|
159
|
+
return [
|
|
160
|
+
4,
|
|
161
|
+
parseDeferredReadableStream(res.body)
|
|
162
|
+
];
|
|
163
|
+
case 1:
|
|
164
|
+
return [
|
|
165
|
+
2,
|
|
166
|
+
_state.sent()
|
|
167
|
+
];
|
|
168
|
+
case 2:
|
|
169
|
+
return [
|
|
170
|
+
2,
|
|
171
|
+
res
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
return function handleDeferredResponse(res) {
|
|
177
|
+
return _ref.apply(this, arguments);
|
|
178
|
+
};
|
|
179
|
+
}();
|
|
148
180
|
var createRequest = function(routeId) {
|
|
149
181
|
var method = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "get";
|
|
150
182
|
return function() {
|
|
@@ -171,9 +203,16 @@ var createRequest = function(routeId) {
|
|
|
171
203
|
if (!res.ok) {
|
|
172
204
|
throw res;
|
|
173
205
|
}
|
|
206
|
+
res = handleRedirectResponse(res);
|
|
207
|
+
return [
|
|
208
|
+
4,
|
|
209
|
+
handleDeferredResponse(res)
|
|
210
|
+
];
|
|
211
|
+
case 2:
|
|
212
|
+
res = _state.sent();
|
|
174
213
|
return [
|
|
175
214
|
2,
|
|
176
|
-
|
|
215
|
+
res
|
|
177
216
|
];
|
|
178
217
|
}
|
|
179
218
|
});
|