@innet/server 1.5.5 → 1.6.1
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.
|
@@ -40,4 +40,4 @@ export declare class Action<O extends ActionOptions = ActionOptions> {
|
|
|
40
40
|
};
|
|
41
41
|
get path(): string;
|
|
42
42
|
}
|
|
43
|
-
export declare function useAction<O extends ActionOptions>(): Action<O>;
|
|
43
|
+
export declare function useAction<T extends Partial<ActionOptions>, O extends ActionOptions = ActionOptions & T>(): Action<O>;
|
package/package.json
CHANGED
|
@@ -15,11 +15,14 @@ function getStatus(status) {
|
|
|
15
15
|
return status;
|
|
16
16
|
return redirectStatuses[status] || 301;
|
|
17
17
|
}
|
|
18
|
+
function customEncode(url) {
|
|
19
|
+
return encodeURI(url.replaceAll('%20', ' '));
|
|
20
|
+
}
|
|
18
21
|
function redirect({ props, children }, handler) {
|
|
19
22
|
const { res } = handler[ACTION];
|
|
20
23
|
const { to, status = 301, encode } = props;
|
|
21
24
|
res.writeHead(getStatus(status), {
|
|
22
|
-
location: encode ?
|
|
25
|
+
location: encode ? customEncode(to) : to,
|
|
23
26
|
});
|
|
24
27
|
return null;
|
|
25
28
|
}
|
|
@@ -19,12 +19,15 @@ function getStatus(status) {
|
|
|
19
19
|
return status;
|
|
20
20
|
return redirectStatuses[status] || 301;
|
|
21
21
|
}
|
|
22
|
+
function customEncode(url) {
|
|
23
|
+
return encodeURI(url.replaceAll('%20', ' '));
|
|
24
|
+
}
|
|
22
25
|
function redirect(_a, handler) {
|
|
23
26
|
var props = _a.props; _a.children;
|
|
24
27
|
var res = handler[Action.ACTION].res;
|
|
25
28
|
var to = props.to, _b = props.status, status = _b === void 0 ? 301 : _b, encode = props.encode;
|
|
26
29
|
res.writeHead(getStatus(status), {
|
|
27
|
-
location: encode ?
|
|
30
|
+
location: encode ? customEncode(to) : to,
|
|
28
31
|
});
|
|
29
32
|
return null;
|
|
30
33
|
}
|
|
@@ -4,7 +4,7 @@ import { ACTION } from '../../action/Action/Action.es6.js';
|
|
|
4
4
|
|
|
5
5
|
function getMatchReg(props) {
|
|
6
6
|
const { ish, path } = props;
|
|
7
|
-
return `^${path
|
|
7
|
+
return `^${path}${ish ? '(?<rest>/.*)?' : ''}$`;
|
|
8
8
|
}
|
|
9
9
|
const ROUTER = Symbol('Parent Router');
|
|
10
10
|
function useRouter() {
|
|
@@ -17,25 +17,25 @@ function router({ props, children }, handler) {
|
|
|
17
17
|
if (!props)
|
|
18
18
|
return children;
|
|
19
19
|
const action = handler[ACTION];
|
|
20
|
-
|
|
21
|
-
if (!req) {
|
|
20
|
+
if (!action) {
|
|
22
21
|
throw Error('`router` should be used inside `server`');
|
|
23
22
|
}
|
|
23
|
+
const { req, path } = action;
|
|
24
24
|
if (props.method && props.method !== req.method) {
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
27
|
const parent = handler[ROUTER];
|
|
28
28
|
const parentPrefix = (parent === null || parent === void 0 ? void 0 : parent.prefix) || '';
|
|
29
|
-
if (parentPrefix && !
|
|
29
|
+
if (parentPrefix && !path.startsWith(parentPrefix)) {
|
|
30
30
|
return;
|
|
31
31
|
}
|
|
32
|
-
const url = parentPrefix ?
|
|
32
|
+
const url = parentPrefix ? path.slice(parentPrefix.length) : path;
|
|
33
33
|
const newHandler = Object.create(handler);
|
|
34
34
|
const current = newHandler[ROUTER] = Object.create(parent || null);
|
|
35
35
|
if (props.prefix) {
|
|
36
36
|
current.prefix = parentPrefix + props.prefix;
|
|
37
37
|
}
|
|
38
|
-
if (props.path
|
|
38
|
+
if (props.path) {
|
|
39
39
|
const urlReg = new RegExp(getMatchReg(props));
|
|
40
40
|
if (urlReg.test(url)) {
|
|
41
41
|
current.params = url.match(urlReg).groups;
|
package/plugins/router/router.js
CHANGED
|
@@ -12,7 +12,7 @@ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
|
|
|
12
12
|
|
|
13
13
|
function getMatchReg(props) {
|
|
14
14
|
var ish = props.ish, path = props.path;
|
|
15
|
-
return "^".concat(path
|
|
15
|
+
return "^".concat(path).concat(ish ? '(?<rest>/.*)?' : '', "$");
|
|
16
16
|
}
|
|
17
17
|
var ROUTER = Symbol('Parent Router');
|
|
18
18
|
function useRouter() {
|
|
@@ -26,25 +26,25 @@ function router(_a, handler) {
|
|
|
26
26
|
if (!props)
|
|
27
27
|
return children;
|
|
28
28
|
var action = handler[Action.ACTION];
|
|
29
|
-
|
|
30
|
-
if (!req) {
|
|
29
|
+
if (!action) {
|
|
31
30
|
throw Error('`router` should be used inside `server`');
|
|
32
31
|
}
|
|
32
|
+
var req = action.req, path = action.path;
|
|
33
33
|
if (props.method && props.method !== req.method) {
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
36
36
|
var parent = handler[ROUTER];
|
|
37
37
|
var parentPrefix = (parent === null || parent === void 0 ? void 0 : parent.prefix) || '';
|
|
38
|
-
if (parentPrefix && !
|
|
38
|
+
if (parentPrefix && !path.startsWith(parentPrefix)) {
|
|
39
39
|
return;
|
|
40
40
|
}
|
|
41
|
-
var url = parentPrefix ?
|
|
41
|
+
var url = parentPrefix ? path.slice(parentPrefix.length) : path;
|
|
42
42
|
var newHandler = Object.create(handler);
|
|
43
43
|
var current = newHandler[ROUTER] = Object.create(parent || null);
|
|
44
44
|
if (props.prefix) {
|
|
45
45
|
current.prefix = parentPrefix + props.prefix;
|
|
46
46
|
}
|
|
47
|
-
if (props.path
|
|
47
|
+
if (props.path) {
|
|
48
48
|
var urlReg = new RegExp(getMatchReg(props));
|
|
49
49
|
if (urlReg.test(url)) {
|
|
50
50
|
current.params = url.match(urlReg).groups;
|