@modern-js/plugin-data-loader 0.0.0-next-20221202140532 → 0.0.0-next-20221204140532
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/CHANGELOG.md +3 -3
- package/dist/js/modern/cli/create-request.js +18 -34
- package/dist/js/modern/cli/generate-client.js +9 -10
- package/dist/js/modern/cli/loader.js +15 -37
- package/dist/js/modern/server/index.js +63 -99
- package/dist/js/node/cli/create-request.js +25 -51
- package/dist/js/node/cli/generate-client.js +15 -33
- package/dist/js/node/cli/loader.js +21 -56
- package/dist/js/node/server/index.js +74 -126
- package/dist/js/treeshaking/cli/create-request.js +60 -187
- package/dist/js/treeshaking/cli/generate-client.js +18 -35
- package/dist/js/treeshaking/cli/loader.js +31 -145
- package/dist/js/treeshaking/server/index.js +286 -546
- package/dist/types/server/index.d.ts +0 -2
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @modern-js/plugin-data-loader
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-20221204140532
|
|
4
4
|
|
|
5
5
|
### Major Changes
|
|
6
6
|
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
- Updated dependencies [abf3421a7]
|
|
29
29
|
- Updated dependencies [543be9558]
|
|
30
30
|
- Updated dependencies [14b712da8]
|
|
31
|
-
- @modern-js/utils@0.0.0-next-
|
|
32
|
-
- @modern-js/babel-compiler@0.0.0-next-
|
|
31
|
+
- @modern-js/utils@0.0.0-next-20221204140532
|
|
32
|
+
- @modern-js/babel-compiler@0.0.0-next-20221204140532
|
|
@@ -1,25 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
import { compile } from "path-to-regexp";
|
|
22
|
-
const getRequestUrl = ({
|
|
1
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
2
|
+
/* eslint-disable node/prefer-global/url */
|
|
3
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
4
|
+
/* eslint-disable node/no-unsupported-features/node-builtins */
|
|
5
|
+
import { compile } from 'path-to-regexp';
|
|
6
|
+
export const getRequestUrl = ({
|
|
23
7
|
params,
|
|
24
8
|
request,
|
|
25
9
|
routeId
|
|
@@ -30,17 +14,21 @@ const getRequestUrl = ({
|
|
|
30
14
|
});
|
|
31
15
|
const newPathName = toPath(params);
|
|
32
16
|
url.pathname = newPathName;
|
|
33
|
-
url.searchParams.append(
|
|
17
|
+
url.searchParams.append('_loader', routeId);
|
|
34
18
|
return url;
|
|
35
19
|
};
|
|
36
|
-
const createRequest = (routeId, method =
|
|
37
|
-
return
|
|
20
|
+
export const createRequest = (routeId, method = 'get') => {
|
|
21
|
+
return async ({
|
|
38
22
|
params,
|
|
39
23
|
request
|
|
40
|
-
}) {
|
|
41
|
-
const url = getRequestUrl({
|
|
24
|
+
}) => {
|
|
25
|
+
const url = getRequestUrl({
|
|
26
|
+
params,
|
|
27
|
+
request,
|
|
28
|
+
routeId
|
|
29
|
+
});
|
|
42
30
|
try {
|
|
43
|
-
const res =
|
|
31
|
+
const res = await fetch(url, {
|
|
44
32
|
method,
|
|
45
33
|
signal: request.signal
|
|
46
34
|
});
|
|
@@ -49,9 +37,5 @@ const createRequest = (routeId, method = "get") => {
|
|
|
49
37
|
console.error(error);
|
|
50
38
|
throw error;
|
|
51
39
|
}
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
export {
|
|
55
|
-
createRequest,
|
|
56
|
-
getRequestUrl
|
|
57
|
-
};
|
|
40
|
+
};
|
|
41
|
+
};
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import path from
|
|
2
|
-
const generateClient = ({
|
|
1
|
+
import path from 'path';
|
|
2
|
+
export const generateClient = ({
|
|
3
|
+
mapFile
|
|
4
|
+
}) => {
|
|
3
5
|
const loadersMap = require(mapFile);
|
|
4
|
-
const requestCode = Object.keys(loadersMap).map(
|
|
6
|
+
const requestCode = Object.keys(loadersMap).map(loaderId => {
|
|
5
7
|
const routeId = loadersMap[loaderId];
|
|
6
8
|
return `
|
|
7
9
|
const ${loaderId} = createRequest('${routeId}');
|
|
8
10
|
`;
|
|
9
|
-
}).join(
|
|
11
|
+
}).join('');
|
|
10
12
|
let exportsCode = `export {`;
|
|
11
13
|
for (const loader of Object.keys(loadersMap)) {
|
|
12
14
|
exportsCode += `${loader},`;
|
|
13
15
|
}
|
|
14
|
-
exportsCode +=
|
|
15
|
-
const requestCreatorPath = path.join(__dirname,
|
|
16
|
+
exportsCode += '}';
|
|
17
|
+
const requestCreatorPath = path.join(__dirname, './create-request').replace('node', 'treeshaking').replace(/\\/g, '/');
|
|
16
18
|
const importCode = `
|
|
17
19
|
import { createRequest } from '${requestCreatorPath}';
|
|
18
20
|
`;
|
|
@@ -21,7 +23,4 @@ const generateClient = ({ mapFile }) => {
|
|
|
21
23
|
${requestCode}
|
|
22
24
|
${exportsCode}
|
|
23
25
|
`;
|
|
24
|
-
};
|
|
25
|
-
export {
|
|
26
|
-
generateClient
|
|
27
|
-
};
|
|
26
|
+
};
|
|
@@ -1,39 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
1
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
2
|
+
/* eslint-disable @babel/no-invalid-this */
|
|
3
|
+
|
|
21
4
|
import { generateClient } from "./generate-client";
|
|
22
|
-
function loader(source) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
mapFile: options.mapFile
|
|
33
|
-
});
|
|
34
|
-
return code;
|
|
5
|
+
export default async function loader(source) {
|
|
6
|
+
var _this$_compiler;
|
|
7
|
+
this.cacheable();
|
|
8
|
+
const target = (_this$_compiler = this._compiler) === null || _this$_compiler === void 0 ? void 0 : _this$_compiler.options.target;
|
|
9
|
+
if (target === 'node') {
|
|
10
|
+
return source;
|
|
11
|
+
}
|
|
12
|
+
const options = this.getOptions();
|
|
13
|
+
const code = generateClient({
|
|
14
|
+
mapFile: options.mapFile
|
|
35
15
|
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
loader as default
|
|
39
|
-
};
|
|
16
|
+
return code;
|
|
17
|
+
}
|
|
@@ -1,85 +1,49 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __async = (__this, __arguments, generator) => {
|
|
21
|
-
return new Promise((resolve, reject) => {
|
|
22
|
-
var fulfilled = (value) => {
|
|
23
|
-
try {
|
|
24
|
-
step(generator.next(value));
|
|
25
|
-
} catch (e) {
|
|
26
|
-
reject(e);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
var rejected = (value) => {
|
|
30
|
-
try {
|
|
31
|
-
step(generator.throw(value));
|
|
32
|
-
} catch (e) {
|
|
33
|
-
reject(e);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
import {
|
|
41
|
-
installGlobals,
|
|
42
|
-
writeReadableStreamToWritable,
|
|
43
|
-
Response as NodeResponse
|
|
44
|
-
} from "@remix-run/node";
|
|
45
|
-
import {
|
|
46
|
-
matchRoutes
|
|
47
|
-
} from "react-router-dom";
|
|
48
|
-
import { LOADER_ROUTES_DIR } from "@modern-js/utils";
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
2
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
3
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
4
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
|
|
5
|
+
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
6
|
+
import { installGlobals, writeReadableStreamToWritable, Response as NodeResponse } from '@remix-run/node';
|
|
7
|
+
import { matchRoutes } from 'react-router-dom';
|
|
8
|
+
import { LOADER_ROUTES_DIR } from '@modern-js/utils';
|
|
9
|
+
// Polyfill Web Fetch API
|
|
49
10
|
installGlobals();
|
|
50
|
-
const LOADER_SEARCH_PARAM =
|
|
51
|
-
const redirectStatusCodes =
|
|
52
|
-
function isRedirectResponse(response) {
|
|
11
|
+
const LOADER_SEARCH_PARAM = '_loader';
|
|
12
|
+
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
|
|
13
|
+
export function isRedirectResponse(response) {
|
|
53
14
|
return redirectStatusCodes.has(response.status);
|
|
54
15
|
}
|
|
55
|
-
function isResponse(value) {
|
|
56
|
-
return value != null && typeof value.status ===
|
|
16
|
+
export function isResponse(value) {
|
|
17
|
+
return value != null && typeof value.status === 'number' && typeof value.statusText === 'string' && typeof value.headers === 'object' && typeof value.body !== 'undefined';
|
|
57
18
|
}
|
|
58
19
|
const json = (data, init = {}) => {
|
|
59
|
-
const responseInit = typeof init ===
|
|
20
|
+
const responseInit = typeof init === 'number' ? {
|
|
21
|
+
status: init
|
|
22
|
+
} : init;
|
|
60
23
|
const headers = new Headers(responseInit.headers);
|
|
61
|
-
if (!headers.has(
|
|
62
|
-
headers.set(
|
|
24
|
+
if (!headers.has('Content-Type')) {
|
|
25
|
+
headers.set('Content-Type', 'application/json; charset=utf-8');
|
|
63
26
|
}
|
|
64
|
-
return new NodeResponse(JSON.stringify(data),
|
|
27
|
+
return new NodeResponse(JSON.stringify(data), _objectSpread(_objectSpread({}, responseInit), {}, {
|
|
65
28
|
headers
|
|
66
29
|
}));
|
|
67
30
|
};
|
|
68
|
-
|
|
31
|
+
|
|
32
|
+
// TODO: 添加 context
|
|
33
|
+
const callRouteLoader = async ({
|
|
69
34
|
routeId,
|
|
70
35
|
loader,
|
|
71
36
|
params,
|
|
72
37
|
request,
|
|
38
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
73
39
|
loadContext
|
|
74
|
-
}) {
|
|
40
|
+
}) => {
|
|
75
41
|
if (!loader) {
|
|
76
|
-
throw new Error(
|
|
77
|
-
`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.`
|
|
78
|
-
);
|
|
42
|
+
throw new Error(`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.`);
|
|
79
43
|
}
|
|
80
44
|
let result;
|
|
81
45
|
try {
|
|
82
|
-
result =
|
|
46
|
+
result = await loader({
|
|
83
47
|
request,
|
|
84
48
|
params
|
|
85
49
|
});
|
|
@@ -89,14 +53,12 @@ const callRouteLoader = (_0) => __async(void 0, [_0], function* ({
|
|
|
89
53
|
}
|
|
90
54
|
result = error;
|
|
91
55
|
}
|
|
92
|
-
if (result ===
|
|
93
|
-
throw new Error(
|
|
94
|
-
`You defined a loader for route "${routeId}" but didn't return anything from your \`loader\` function. Please return a value or \`null\`.`
|
|
95
|
-
);
|
|
56
|
+
if (result === undefined) {
|
|
57
|
+
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\`.`);
|
|
96
58
|
}
|
|
97
59
|
return isResponse(result) ? result : json(result);
|
|
98
|
-
}
|
|
99
|
-
const createLoaderHeaders =
|
|
60
|
+
};
|
|
61
|
+
const createLoaderHeaders = requestHeaders => {
|
|
100
62
|
const headers = new Headers();
|
|
101
63
|
for (const [key, values] of Object.entries(requestHeaders)) {
|
|
102
64
|
if (values) {
|
|
@@ -111,8 +73,9 @@ const createLoaderHeaders = (requestHeaders) => {
|
|
|
111
73
|
}
|
|
112
74
|
return headers;
|
|
113
75
|
};
|
|
114
|
-
const createLoaderRequest =
|
|
76
|
+
const createLoaderRequest = context => {
|
|
115
77
|
const origin = `${context.protocol}://${context.host}`;
|
|
78
|
+
// eslint-disable-next-line node/no-unsupported-features/node-builtins, node/prefer-global/url
|
|
116
79
|
const url = new URL(context.url, origin);
|
|
117
80
|
const controller = new AbortController();
|
|
118
81
|
const init = {
|
|
@@ -122,46 +85,53 @@ const createLoaderRequest = (context) => {
|
|
|
122
85
|
};
|
|
123
86
|
return new Request(url.href, init);
|
|
124
87
|
};
|
|
125
|
-
const sendLoaderResponse = (res, nodeResponse) =>
|
|
88
|
+
const sendLoaderResponse = async (res, nodeResponse) => {
|
|
126
89
|
res.statusMessage = nodeResponse.statusText;
|
|
127
90
|
res.statusCode = nodeResponse.status;
|
|
128
91
|
for (const [key, value] of nodeResponse.headers.entries()) {
|
|
129
92
|
res.setHeader(key, value);
|
|
130
93
|
}
|
|
131
94
|
if (nodeResponse.body) {
|
|
132
|
-
|
|
95
|
+
await writeReadableStreamToWritable(nodeResponse.body, res);
|
|
133
96
|
} else {
|
|
134
97
|
res.end();
|
|
135
98
|
}
|
|
136
|
-
}
|
|
137
|
-
const getPathWithoutEntry = (pathname, entryPath) => {
|
|
138
|
-
if (entryPath ===
|
|
99
|
+
};
|
|
100
|
+
export const getPathWithoutEntry = (pathname, entryPath) => {
|
|
101
|
+
if (entryPath === '/') {
|
|
139
102
|
return pathname;
|
|
140
103
|
}
|
|
141
|
-
return pathname.replace(entryPath,
|
|
104
|
+
return pathname.replace(entryPath, '');
|
|
142
105
|
};
|
|
143
106
|
const matchEntry = (pathname, entries) => {
|
|
144
|
-
return entries.find(
|
|
107
|
+
return entries.find(entry => pathname.startsWith(entry.urlPath));
|
|
145
108
|
};
|
|
146
|
-
const handleRequest =
|
|
109
|
+
export const handleRequest = async ({
|
|
147
110
|
context,
|
|
148
111
|
serverRoutes,
|
|
149
112
|
distDir
|
|
150
|
-
}) {
|
|
151
|
-
const {
|
|
113
|
+
}) => {
|
|
114
|
+
const {
|
|
115
|
+
method,
|
|
116
|
+
query
|
|
117
|
+
} = context;
|
|
152
118
|
const routeId = query[LOADER_SEARCH_PARAM];
|
|
153
|
-
if (!routeId || method.toLowerCase() !==
|
|
119
|
+
if (!routeId || method.toLowerCase() !== 'get') {
|
|
154
120
|
return;
|
|
155
121
|
}
|
|
156
122
|
const entry = matchEntry(context.path, serverRoutes);
|
|
157
123
|
if (!entry) {
|
|
158
124
|
return;
|
|
159
125
|
}
|
|
160
|
-
const {
|
|
126
|
+
const {
|
|
127
|
+
routes
|
|
128
|
+
} = await import(`${distDir}/${LOADER_ROUTES_DIR}/${entry.entryName}`);
|
|
161
129
|
if (!routes) {
|
|
162
130
|
return;
|
|
163
131
|
}
|
|
164
|
-
const {
|
|
132
|
+
const {
|
|
133
|
+
res
|
|
134
|
+
} = context;
|
|
165
135
|
const pathname = getPathWithoutEntry(context.path, entry.urlPath);
|
|
166
136
|
const matches = matchRoutes(routes, pathname);
|
|
167
137
|
if (!matches) {
|
|
@@ -169,7 +139,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
|
169
139
|
res.end(`Route ${pathname} was not matched`);
|
|
170
140
|
return;
|
|
171
141
|
}
|
|
172
|
-
const match = matches
|
|
142
|
+
const match = matches === null || matches === void 0 ? void 0 : matches.find(match => match.route.id === routeId);
|
|
173
143
|
if (!match) {
|
|
174
144
|
res.statusCode = 403;
|
|
175
145
|
res.end(`Route ${routeId} does not match URL ${context.path}`);
|
|
@@ -178,45 +148,39 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
|
178
148
|
const request = createLoaderRequest(context);
|
|
179
149
|
let response;
|
|
180
150
|
try {
|
|
181
|
-
response =
|
|
151
|
+
response = await callRouteLoader({
|
|
182
152
|
loader: match.route.loader,
|
|
183
153
|
routeId: match.route.id,
|
|
184
154
|
params: match.params,
|
|
185
155
|
request,
|
|
186
156
|
loadContext: {}
|
|
187
157
|
});
|
|
158
|
+
// TODO: 处理 redirect
|
|
188
159
|
} catch (error) {
|
|
189
160
|
const message = String(error);
|
|
190
161
|
response = new NodeResponse(message, {
|
|
191
162
|
status: 500,
|
|
192
163
|
headers: {
|
|
193
|
-
|
|
164
|
+
'Content-Type': 'text/plain'
|
|
194
165
|
}
|
|
195
166
|
});
|
|
196
167
|
}
|
|
197
168
|
sendLoaderResponse(res, response);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
name:
|
|
169
|
+
};
|
|
170
|
+
export default (() => ({
|
|
171
|
+
name: '@modern-js/plugin-data-loader',
|
|
201
172
|
setup: () => ({
|
|
202
173
|
preparebeforeRouteHandler({
|
|
203
174
|
serverRoutes,
|
|
204
175
|
distDir
|
|
205
176
|
}) {
|
|
206
|
-
return
|
|
177
|
+
return async context => {
|
|
207
178
|
return handleRequest({
|
|
208
179
|
serverRoutes,
|
|
209
180
|
distDir,
|
|
210
181
|
context
|
|
211
182
|
});
|
|
212
|
-
}
|
|
183
|
+
};
|
|
213
184
|
}
|
|
214
185
|
})
|
|
215
|
-
});
|
|
216
|
-
export {
|
|
217
|
-
server_default as default,
|
|
218
|
-
getPathWithoutEntry,
|
|
219
|
-
handleRequest,
|
|
220
|
-
isRedirectResponse,
|
|
221
|
-
isResponse
|
|
222
|
-
};
|
|
186
|
+
}));
|
|
@@ -1,69 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var stdin_exports = {};
|
|
19
|
-
__export(stdin_exports, {
|
|
20
|
-
createRequest: () => createRequest,
|
|
21
|
-
getRequestUrl: () => getRequestUrl
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
22
5
|
});
|
|
23
|
-
|
|
24
|
-
var
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
} catch (e) {
|
|
31
|
-
reject(e);
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
var rejected = (value) => {
|
|
35
|
-
try {
|
|
36
|
-
step(generator.throw(value));
|
|
37
|
-
} catch (e) {
|
|
38
|
-
reject(e);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
42
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
43
|
-
});
|
|
44
|
-
};
|
|
6
|
+
exports.getRequestUrl = exports.createRequest = void 0;
|
|
7
|
+
var _pathToRegexp = require("path-to-regexp");
|
|
8
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
9
|
+
/* eslint-disable node/prefer-global/url */
|
|
10
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
11
|
+
/* eslint-disable node/no-unsupported-features/node-builtins */
|
|
12
|
+
|
|
45
13
|
const getRequestUrl = ({
|
|
46
14
|
params,
|
|
47
15
|
request,
|
|
48
16
|
routeId
|
|
49
17
|
}) => {
|
|
50
18
|
const url = new URL(request.url);
|
|
51
|
-
const toPath = (0,
|
|
19
|
+
const toPath = (0, _pathToRegexp.compile)(url.pathname, {
|
|
52
20
|
encode: encodeURIComponent
|
|
53
21
|
});
|
|
54
22
|
const newPathName = toPath(params);
|
|
55
23
|
url.pathname = newPathName;
|
|
56
|
-
url.searchParams.append(
|
|
24
|
+
url.searchParams.append('_loader', routeId);
|
|
57
25
|
return url;
|
|
58
26
|
};
|
|
59
|
-
|
|
60
|
-
|
|
27
|
+
exports.getRequestUrl = getRequestUrl;
|
|
28
|
+
const createRequest = (routeId, method = 'get') => {
|
|
29
|
+
return async ({
|
|
61
30
|
params,
|
|
62
31
|
request
|
|
63
|
-
}) {
|
|
64
|
-
const url = getRequestUrl({
|
|
32
|
+
}) => {
|
|
33
|
+
const url = getRequestUrl({
|
|
34
|
+
params,
|
|
35
|
+
request,
|
|
36
|
+
routeId
|
|
37
|
+
});
|
|
65
38
|
try {
|
|
66
|
-
const res =
|
|
39
|
+
const res = await fetch(url, {
|
|
67
40
|
method,
|
|
68
41
|
signal: request.signal
|
|
69
42
|
});
|
|
@@ -72,5 +45,6 @@ const createRequest = (routeId, method = "get") => {
|
|
|
72
45
|
console.error(error);
|
|
73
46
|
throw error;
|
|
74
47
|
}
|
|
75
|
-
}
|
|
48
|
+
};
|
|
76
49
|
};
|
|
50
|
+
exports.createRequest = createRequest;
|
|
@@ -1,46 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
-
mod
|
|
22
|
-
));
|
|
23
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var stdin_exports = {};
|
|
25
|
-
__export(stdin_exports, {
|
|
26
|
-
generateClient: () => generateClient
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
27
5
|
});
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
|
|
6
|
+
exports.generateClient = void 0;
|
|
7
|
+
var _path = _interopRequireDefault(require("path"));
|
|
8
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
+
const generateClient = ({
|
|
10
|
+
mapFile
|
|
11
|
+
}) => {
|
|
31
12
|
const loadersMap = require(mapFile);
|
|
32
|
-
const requestCode = Object.keys(loadersMap).map(
|
|
13
|
+
const requestCode = Object.keys(loadersMap).map(loaderId => {
|
|
33
14
|
const routeId = loadersMap[loaderId];
|
|
34
15
|
return `
|
|
35
16
|
const ${loaderId} = createRequest('${routeId}');
|
|
36
17
|
`;
|
|
37
|
-
}).join(
|
|
18
|
+
}).join('');
|
|
38
19
|
let exportsCode = `export {`;
|
|
39
20
|
for (const loader of Object.keys(loadersMap)) {
|
|
40
21
|
exportsCode += `${loader},`;
|
|
41
22
|
}
|
|
42
|
-
exportsCode +=
|
|
43
|
-
const requestCreatorPath =
|
|
23
|
+
exportsCode += '}';
|
|
24
|
+
const requestCreatorPath = _path.default.join(__dirname, './create-request').replace('node', 'treeshaking').replace(/\\/g, '/');
|
|
44
25
|
const importCode = `
|
|
45
26
|
import { createRequest } from '${requestCreatorPath}';
|
|
46
27
|
`;
|
|
@@ -50,3 +31,4 @@ const generateClient = ({ mapFile }) => {
|
|
|
50
31
|
${exportsCode}
|
|
51
32
|
`;
|
|
52
33
|
};
|
|
34
|
+
exports.generateClient = generateClient;
|