@jayfong/x-server 2.35.2 → 2.37.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/lib/_cjs/core/handler.js +5 -0
- package/lib/_cjs/core/http_redirect.js +11 -0
- package/lib/_cjs/core/server.js +1 -1
- package/lib/_cjs/index.js +6 -0
- package/lib/core/handler.js +5 -0
- package/lib/core/http_redirect.d.ts +5 -0
- package/lib/core/http_redirect.js +6 -0
- package/lib/core/server.js +1 -1
- package/lib/core/types.d.ts +4 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -1
- package/package.json +1 -1
package/lib/_cjs/core/handler.js
CHANGED
|
@@ -10,6 +10,7 @@ var vae = _interopRequireWildcard(require("vtils/vae"));
|
|
|
10
10
|
var _validator = require("vtils/validator");
|
|
11
11
|
var _http_error = require("../core/http_error");
|
|
12
12
|
var _dispose = require("../services/dispose");
|
|
13
|
+
var _http_redirect = require("./http_redirect");
|
|
13
14
|
var _server = require("./server");
|
|
14
15
|
_validator.yup.setLocale((0, _validator.getZhCN)({
|
|
15
16
|
getLabel: params => params.label || params.path
|
|
@@ -61,6 +62,10 @@ class Handler {
|
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
let res = await this.options.handle(data, ctx);
|
|
65
|
+
if (res instanceof _http_redirect.HttpRedirect) {
|
|
66
|
+
ctx.redirect(res.url, res.permanently);
|
|
67
|
+
return {};
|
|
68
|
+
}
|
|
64
69
|
|
|
65
70
|
// 设置响应的 Content-Type 头
|
|
66
71
|
if (this.options.responseContentType) {
|
package/lib/_cjs/core/server.js
CHANGED
|
@@ -113,7 +113,7 @@ class Server {
|
|
|
113
113
|
url: url,
|
|
114
114
|
headers: req.headers,
|
|
115
115
|
setHeader: (k, v) => res.header(k, v),
|
|
116
|
-
redirect: url => res.redirect(url),
|
|
116
|
+
redirect: (url, permanently) => res.redirect(permanently ? 301 : 302, url),
|
|
117
117
|
ws: undefined,
|
|
118
118
|
req: req,
|
|
119
119
|
res: res
|
package/lib/_cjs/index.js
CHANGED
|
@@ -67,6 +67,12 @@ Object.keys(_http_method).forEach(function (key) {
|
|
|
67
67
|
if (key in exports && exports[key] === _http_method[key]) return;
|
|
68
68
|
exports[key] = _http_method[key];
|
|
69
69
|
});
|
|
70
|
+
var _http_redirect = require("./core/http_redirect");
|
|
71
|
+
Object.keys(_http_redirect).forEach(function (key) {
|
|
72
|
+
if (key === "default" || key === "__esModule") return;
|
|
73
|
+
if (key in exports && exports[key] === _http_redirect[key]) return;
|
|
74
|
+
exports[key] = _http_redirect[key];
|
|
75
|
+
});
|
|
70
76
|
var _server = require("./core/server");
|
|
71
77
|
Object.keys(_server).forEach(function (key) {
|
|
72
78
|
if (key === "default" || key === "__esModule") return;
|
package/lib/core/handler.js
CHANGED
|
@@ -4,6 +4,7 @@ import * as vae from 'vtils/vae';
|
|
|
4
4
|
import { getZhCN, yup } from 'vtils/validator';
|
|
5
5
|
import { HttpError } from "../core/http_error";
|
|
6
6
|
import { DisposeService } from "../services/dispose";
|
|
7
|
+
import { HttpRedirect } from "./http_redirect";
|
|
7
8
|
import { Server } from "./server";
|
|
8
9
|
yup.setLocale(getZhCN({
|
|
9
10
|
getLabel: params => params.label || params.path
|
|
@@ -55,6 +56,10 @@ export class Handler {
|
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
58
|
let res = await this.options.handle(data, ctx);
|
|
59
|
+
if (res instanceof HttpRedirect) {
|
|
60
|
+
ctx.redirect(res.url, res.permanently);
|
|
61
|
+
return {};
|
|
62
|
+
}
|
|
58
63
|
|
|
59
64
|
// 设置响应的 Content-Type 头
|
|
60
65
|
if (this.options.responseContentType) {
|
package/lib/core/server.js
CHANGED
|
@@ -107,7 +107,7 @@ export class Server {
|
|
|
107
107
|
url: url,
|
|
108
108
|
headers: req.headers,
|
|
109
109
|
setHeader: (k, v) => res.header(k, v),
|
|
110
|
-
redirect: url => res.redirect(url),
|
|
110
|
+
redirect: (url, permanently) => res.redirect(permanently ? 301 : 302, url),
|
|
111
111
|
ws: undefined,
|
|
112
112
|
req: req,
|
|
113
113
|
res: res
|
package/lib/core/types.d.ts
CHANGED
|
@@ -77,8 +77,11 @@ export declare namespace XHandler {
|
|
|
77
77
|
setHeader: (key: string, value: string) => void;
|
|
78
78
|
/**
|
|
79
79
|
* HTTP 跳转
|
|
80
|
+
*
|
|
81
|
+
* 默认是临时跳转,状态码是 302,
|
|
82
|
+
* 可将第二个参数设为 true 表示永久跳转,状态码是 301
|
|
80
83
|
*/
|
|
81
|
-
redirect: (url: string) => void;
|
|
84
|
+
redirect: (url: string, permanently?: boolean) => void;
|
|
82
85
|
/**
|
|
83
86
|
* WS
|
|
84
87
|
*/
|
package/lib/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from './core/handler';
|
|
|
9
9
|
export * from './core/http_error';
|
|
10
10
|
export * from './core/http_header';
|
|
11
11
|
export * from './core/http_method';
|
|
12
|
+
export * from './core/http_redirect';
|
|
12
13
|
export * from './core/server';
|
|
13
14
|
export * from './core/types';
|
|
14
15
|
export * from './plugins/base';
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @index(['./**/*.ts', '!**/*.test.ts', '!./cli/**', '!**/_*'], f => `export * from '${f.path}'
|
|
1
|
+
// @index(['./**/*.ts', '!**/*.test.ts', '!./cli/**', '!**/_*'], f => `export * from '${f.path}';`)
|
|
2
2
|
export * from "./core/define_bus";
|
|
3
3
|
export * from "./core/define_cron";
|
|
4
4
|
export * from "./core/define_handler";
|
|
@@ -10,6 +10,7 @@ export * from "./core/handler";
|
|
|
10
10
|
export * from "./core/http_error";
|
|
11
11
|
export * from "./core/http_header";
|
|
12
12
|
export * from "./core/http_method";
|
|
13
|
+
export * from "./core/http_redirect";
|
|
13
14
|
export * from "./core/server";
|
|
14
15
|
export * from "./core/types";
|
|
15
16
|
export * from "./plugins/base";
|