@cicctencent/midwayjs-base 1.0.35 → 1.0.37
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/README.md +347 -1
- package/dist/config/config.default.d.ts +2 -2
- package/dist/config/config.default.js +33 -57
- package/dist/config/env.js +6 -9
- package/dist/configuration.js +26 -38
- package/dist/filter/default.filter.js +5 -9
- package/dist/filter/notfound.filter.js +5 -10
- package/dist/guard/api.d.ts +0 -3
- package/dist/guard/api.js +12 -29
- package/dist/guard/auth.d.ts +0 -3
- package/dist/guard/auth.js +34 -57
- package/dist/index.js +19 -39
- package/dist/lib/decorator.d.ts +3 -33
- package/dist/lib/decorator.js +15 -57
- package/dist/lib/jwt.d.ts +3 -104
- package/dist/lib/jwt.js +8 -24
- package/dist/lib/request.d.ts +0 -5
- package/dist/lib/request.js +3 -7
- package/dist/lib/utils.d.ts +1 -7
- package/dist/lib/utils.js +10 -42
- package/dist/middleware/apiResultFormatter.d.ts +4 -5
- package/dist/middleware/apiResultFormatter.js +26 -30
- package/dist/middleware/detect.middleware.d.ts +3 -4
- package/dist/middleware/detect.middleware.js +14 -20
- package/dist/middleware/requestInit.d.ts +4 -5
- package/dist/middleware/requestInit.js +54 -54
- package/dist/service/session.service.d.ts +1 -25
- package/dist/service/session.service.js +16 -44
- package/dist/service/tencent.service.d.ts +5 -13
- package/dist/service/tencent.service.js +35 -44
- package/dist/types/base.controller.js +5 -12
- package/dist/types/base.model.service.d.ts +1 -9
- package/dist/types/base.model.service.js +5 -19
- package/dist/types/base.service.d.ts +1 -10
- package/dist/types/base.service.js +16 -39
- package/dist/types/session.service.interface.d.ts +1 -15
- package/dist/types/session.service.interface.js +8 -16
- package/package.json +13 -1
package/dist/lib/jwt.d.ts
CHANGED
|
@@ -1,135 +1,34 @@
|
|
|
1
|
+
import * as jwt from 'jsonwebtoken';
|
|
1
2
|
export type Algorithm = 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512' | 'ES256' | 'ES384' | 'ES512' | 'PS256' | 'PS384' | 'PS512';
|
|
2
3
|
export interface VerifyOptions {
|
|
3
|
-
/**
|
|
4
|
-
* 允许的算法列表
|
|
5
|
-
* 如果未指定,将根据提供的密钥类型使用默认值:
|
|
6
|
-
* - secret: ['HS256', 'HS384', 'HS512']
|
|
7
|
-
* - rsa: ['RS256', 'RS384', 'RS512']
|
|
8
|
-
* - ec: ['ES256', 'ES384', 'ES512']
|
|
9
|
-
* - 默认: ['RS256', 'RS384', 'RS512']
|
|
10
|
-
*/
|
|
11
4
|
algorithms?: Algorithm[];
|
|
12
|
-
/**
|
|
13
|
-
* 用于验证受众(aud 声明)
|
|
14
|
-
* 可以是字符串、正则表达式或它们的数组
|
|
15
|
-
* 例如: "urn:foo", /urn:f[o]{2}/, [/urn:f[o]{2}/, "urn:bar"]
|
|
16
|
-
*/
|
|
17
5
|
audience?: string | RegExp | (string | RegExp)[];
|
|
18
|
-
/**
|
|
19
|
-
* 如果为 true,返回包含 decoded { payload, header, signature } 的对象
|
|
20
|
-
* 而不仅仅是 payload 的内容
|
|
21
|
-
*/
|
|
22
6
|
complete?: boolean;
|
|
23
|
-
/**
|
|
24
|
-
* issuer 声明的有效值(iss 字段),可以是字符串或字符串数组
|
|
25
|
-
*/
|
|
26
7
|
issuer?: string | string[];
|
|
27
|
-
/**
|
|
28
|
-
* 用于验证 JWT ID(jti 声明)的值
|
|
29
|
-
*/
|
|
30
8
|
jwtid?: string;
|
|
31
|
-
/**
|
|
32
|
-
* 如果为 true,则不验证令牌的过期时间(exp 声明)
|
|
33
|
-
*/
|
|
34
9
|
ignoreExpiration?: boolean;
|
|
35
|
-
/**
|
|
36
|
-
* 如果为 true,则不验证令牌的生效时间(nbf 声明)
|
|
37
|
-
*/
|
|
38
10
|
ignoreNotBefore?: boolean;
|
|
39
|
-
/**
|
|
40
|
-
* 用于验证主题(sub 声明)的值
|
|
41
|
-
*/
|
|
42
11
|
subject?: string;
|
|
43
|
-
/**
|
|
44
|
-
* 检查 nbf 和 exp 声明时允许的秒数偏差
|
|
45
|
-
* 用于处理不同服务器之间的时钟差异
|
|
46
|
-
*/
|
|
47
12
|
clockTolerance?: number;
|
|
48
|
-
/**
|
|
49
|
-
* 令牌仍然有效的最大允许年龄
|
|
50
|
-
* 可以是秒数或描述时间跨度的字符串
|
|
51
|
-
* 例如: 1000, "2 days", "10h", "7d"
|
|
52
|
-
* 数值被解释为秒数,字符串需提供时间单位
|
|
53
|
-
*/
|
|
54
13
|
maxAge?: number | string;
|
|
55
|
-
/**
|
|
56
|
-
* 用于所有必要比较的当前时间(以秒为单位)
|
|
57
|
-
*/
|
|
58
14
|
clockTimestamp?: number;
|
|
59
|
-
/**
|
|
60
|
-
* 用于验证 nonce 声明的值
|
|
61
|
-
* 在 OpenID 中用于 ID 令牌
|
|
62
|
-
*/
|
|
63
15
|
nonce?: string;
|
|
64
|
-
/**
|
|
65
|
-
* 如果为 true,允许与指定算法不匹配的非对称密钥
|
|
66
|
-
* 此选项仅用于向后兼容,应避免使用
|
|
67
|
-
*/
|
|
68
16
|
allowInvalidAsymmetricKeyTypes?: boolean;
|
|
69
17
|
}
|
|
70
18
|
export interface SignOptions {
|
|
71
|
-
/**
|
|
72
|
-
* 用于签名的算法
|
|
73
|
-
* @default 'HS256'
|
|
74
|
-
*/
|
|
75
19
|
algorithm?: Algorithm;
|
|
76
|
-
/**
|
|
77
|
-
* 令牌过期时间
|
|
78
|
-
* 可以是秒数或描述时间跨度的字符串(如 "2 days"、"10h")
|
|
79
|
-
* 不能与 payload 中的 exp 同时存在
|
|
80
|
-
*/
|
|
81
20
|
expiresIn?: number | string;
|
|
82
|
-
/**
|
|
83
|
-
* 令牌生效时间(在此时间前令牌无效)
|
|
84
|
-
* 可以是秒数或描述时间跨度的字符串(如 "2 days"、"10h")
|
|
85
|
-
* 不能与 payload 中的 nbf 同时存在
|
|
86
|
-
*/
|
|
87
21
|
notBefore?: number | string;
|
|
88
|
-
/**
|
|
89
|
-
* 受众声明(aud)
|
|
90
|
-
* 不能与 payload 中的 aud 同时存在
|
|
91
|
-
*/
|
|
92
22
|
audience?: string | string[];
|
|
93
|
-
/**
|
|
94
|
-
* 发行者声明(iss)
|
|
95
|
-
* 不能与 payload 中的 iss 同时存在
|
|
96
|
-
*/
|
|
97
23
|
issuer?: string;
|
|
98
|
-
/**
|
|
99
|
-
* JWT ID 声明(jti)
|
|
100
|
-
*/
|
|
101
24
|
jwtid?: string;
|
|
102
|
-
/**
|
|
103
|
-
* 主题声明(sub)
|
|
104
|
-
* 不能与 payload 中的 sub 同时存在
|
|
105
|
-
*/
|
|
106
25
|
subject?: string;
|
|
107
|
-
/**
|
|
108
|
-
* 如果为 true,则不在令牌中包含 iat(发行时间)声明
|
|
109
|
-
*/
|
|
110
26
|
noTimestamp?: boolean;
|
|
111
|
-
/**
|
|
112
|
-
* 自定义 JWT 头部信息
|
|
113
|
-
*/
|
|
114
27
|
header?: Record<string, any>;
|
|
115
|
-
/**
|
|
116
|
-
* 用于头部中的 kid(密钥 ID)声明
|
|
117
|
-
*/
|
|
118
28
|
keyid?: string;
|
|
119
|
-
/**
|
|
120
|
-
* 如果为 true,签名函数将直接修改 payload 对象
|
|
121
|
-
* 在需要获取应用声明后但编码前的原始 payload 引用时有用
|
|
122
|
-
*/
|
|
123
29
|
mutatePayload?: boolean;
|
|
124
|
-
/**
|
|
125
|
-
* 如果为 true,允许使用 modulus 小于 2048 的私钥进行 RSA 签名
|
|
126
|
-
*/
|
|
127
30
|
allowInsecureKeySizes?: boolean;
|
|
128
|
-
/**
|
|
129
|
-
* 如果为 true,允许与指定算法不匹配的非对称密钥
|
|
130
|
-
* 此选项仅用于向后兼容,应避免使用
|
|
131
|
-
*/
|
|
132
31
|
allowInvalidAsymmetricKeyTypes?: boolean;
|
|
133
32
|
}
|
|
134
|
-
export declare function decodeJWT(token: string, secretKey: string, options?: VerifyOptions):
|
|
135
|
-
export declare function encodeJWT(data: any, secretKey: string, options?: SignOptions):
|
|
33
|
+
export declare function decodeJWT(token: string, secretKey: string, options?: VerifyOptions): jwt.Jwt | null;
|
|
34
|
+
export declare function encodeJWT(data: any, secretKey: string, options?: SignOptions): string;
|
package/dist/lib/jwt.js
CHANGED
|
@@ -1,38 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.decodeJWT = decodeJWT;
|
|
4
|
-
exports.encodeJWT = encodeJWT;
|
|
5
|
-
const jwt = require("jsonwebtoken");
|
|
6
|
-
// 验证jwt
|
|
7
|
-
function decodeJWT(token, secretKey, options) {
|
|
1
|
+
import * as jwt from 'jsonwebtoken';
|
|
2
|
+
export function decodeJWT(token, secretKey, options) {
|
|
8
3
|
try {
|
|
9
4
|
const decoded = jwt.verify(token, secretKey, {
|
|
10
|
-
|
|
11
|
-
...(options || {})
|
|
5
|
+
algorithms: ['HS256'],
|
|
6
|
+
...(options || {}),
|
|
12
7
|
});
|
|
13
8
|
return decoded;
|
|
14
9
|
}
|
|
15
10
|
catch (err) {
|
|
16
|
-
console.error('Token 验证失败:', err
|
|
11
|
+
console.error('Token 验证失败:', err?.message);
|
|
17
12
|
return null;
|
|
18
13
|
}
|
|
19
14
|
}
|
|
20
|
-
|
|
21
|
-
id: string;
|
|
22
|
-
code?: string;
|
|
23
|
-
loginId: string;
|
|
24
|
-
userId?: number;
|
|
25
|
-
user?: User;
|
|
26
|
-
account?: Account;
|
|
27
|
-
appId?: number;
|
|
28
|
-
status?: EStatus;
|
|
29
|
-
lastDate: Date;
|
|
30
|
-
*/
|
|
31
|
-
function encodeJWT(data, secretKey, options) {
|
|
15
|
+
export function encodeJWT(data, secretKey, options) {
|
|
32
16
|
const token = jwt.sign(data, secretKey, {
|
|
33
17
|
algorithm: 'HS256',
|
|
34
|
-
...(options || {})
|
|
18
|
+
...(options || {}),
|
|
35
19
|
});
|
|
36
20
|
return token;
|
|
37
21
|
}
|
|
38
|
-
//# sourceMappingURL=
|
|
22
|
+
//# sourceMappingURL=jwt.js.map
|
package/dist/lib/request.d.ts
CHANGED
package/dist/lib/request.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const axios_1 = require("@fefeding/common/dist/utils/axios");
|
|
5
|
-
Object.defineProperty(exports, "requestApi", { enumerable: true, get: function () { return axios_1.requestApi; } });
|
|
6
|
-
Object.defineProperty(exports, "requestServer", { enumerable: true, get: function () { return axios_1.requestServer; } });
|
|
7
|
-
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicmVxdWVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9saWIvcmVxdWVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7QUFBQSw2REFBOEU7QUFPckUsMkZBUEEsa0JBQVUsT0FPQTtBQUFFLDhGQVBBLHFCQUFhLE9BT0EifQ==
|
|
1
|
+
import { requestApi, requestServer } from '@fefeding/common/dist/utils/axios';
|
|
2
|
+
export { requestApi, requestServer };
|
|
3
|
+
//# sourceMappingURL=request.js.map
|
package/dist/lib/utils.d.ts
CHANGED
|
@@ -3,13 +3,7 @@ import { CookieSetOptions, CookieGetOptions } from '@midwayjs/cookies';
|
|
|
3
3
|
export * from './jwt';
|
|
4
4
|
import { loadEnv } from '../config/env';
|
|
5
5
|
export declare function checkUrlHost(url: string, host: string | Array<string>): boolean;
|
|
6
|
-
/**
|
|
7
|
-
* render模板
|
|
8
|
-
* @param ctx
|
|
9
|
-
* @param file
|
|
10
|
-
* @returns
|
|
11
|
-
*/
|
|
12
6
|
export declare function getDefaultTemplate(ctx: Context, file?: string, data?: any): Promise<string>;
|
|
13
7
|
export declare function getAuthToken(ctx: Context, option?: CookieGetOptions): any;
|
|
14
8
|
export declare function setAuthToken(ctx: Context, token: string, option?: CookieSetOptions): void;
|
|
15
|
-
export { loadEnv
|
|
9
|
+
export { loadEnv };
|
package/dist/lib/utils.js
CHANGED
|
@@ -1,29 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.loadEnv = void 0;
|
|
18
|
-
exports.checkUrlHost = checkUrlHost;
|
|
19
|
-
exports.getDefaultTemplate = getDefaultTemplate;
|
|
20
|
-
exports.getAuthToken = getAuthToken;
|
|
21
|
-
exports.setAuthToken = setAuthToken;
|
|
22
|
-
__exportStar(require("./jwt"), exports);
|
|
23
|
-
const env_1 = require("../config/env");
|
|
24
|
-
Object.defineProperty(exports, "loadEnv", { enumerable: true, get: function () { return env_1.loadEnv; } });
|
|
25
|
-
//判断url是否是对应的域名
|
|
26
|
-
function checkUrlHost(url, host) {
|
|
1
|
+
export * from './jwt';
|
|
2
|
+
import { loadEnv } from '../config/env';
|
|
3
|
+
export function checkUrlHost(url, host) {
|
|
27
4
|
if (/^\/[^\/]{1,}/.test(url))
|
|
28
5
|
return true;
|
|
29
6
|
if (Array.isArray(host)) {
|
|
@@ -36,14 +13,7 @@ function checkUrlHost(url, host) {
|
|
|
36
13
|
const reg = new RegExp('^(http(s)?:)?//' + host);
|
|
37
14
|
return reg.test(url);
|
|
38
15
|
}
|
|
39
|
-
|
|
40
|
-
* render模板
|
|
41
|
-
* @param ctx
|
|
42
|
-
* @param file
|
|
43
|
-
* @returns
|
|
44
|
-
*/
|
|
45
|
-
function getDefaultTemplate(ctx, file = 'index.html', data = {}) {
|
|
46
|
-
// 前端端口,如果为空,说明就是开发环境了
|
|
16
|
+
export function getDefaultTemplate(ctx, file = 'index.html', data = {}) {
|
|
47
17
|
const viteTarget = process.env.VITE_PORT;
|
|
48
18
|
const config = ctx.app.getConfig();
|
|
49
19
|
const prefix = config.koa.globalPrefix;
|
|
@@ -62,28 +32,26 @@ function getDefaultTemplate(ctx, file = 'index.html', data = {}) {
|
|
|
62
32
|
prefix,
|
|
63
33
|
viteTarget: viteTarget ? `//${ctx.hostname}:${viteTarget}` : '',
|
|
64
34
|
};
|
|
65
|
-
//console.log(file, data);
|
|
66
35
|
return ctx.render(file, option);
|
|
67
36
|
}
|
|
68
|
-
|
|
69
|
-
function getAuthToken(ctx, option) {
|
|
37
|
+
export function getAuthToken(ctx, option) {
|
|
70
38
|
const body = (ctx.request.body || {});
|
|
71
39
|
const token = ctx.URL.searchParams.get('token') ||
|
|
72
40
|
ctx.request.header['x-auth-token'] ||
|
|
73
41
|
ctx.cookies.get('token', {
|
|
74
42
|
signed: false,
|
|
75
|
-
...(option || {})
|
|
43
|
+
...(option || {}),
|
|
76
44
|
}) ||
|
|
77
45
|
ctx.request.header['authorization'] ||
|
|
78
46
|
body.token;
|
|
79
47
|
return token;
|
|
80
48
|
}
|
|
81
|
-
|
|
82
|
-
function setAuthToken(ctx, token, option) {
|
|
49
|
+
export function setAuthToken(ctx, token, option) {
|
|
83
50
|
ctx.cookies.set('token', token, {
|
|
84
51
|
httpOnly: true,
|
|
85
52
|
maxAge: 1000 * 60 * 60 * 24 * 30,
|
|
86
|
-
...(option || {})
|
|
53
|
+
...(option || {}),
|
|
87
54
|
});
|
|
88
55
|
}
|
|
89
|
-
|
|
56
|
+
export { loadEnv };
|
|
57
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { IMiddleware } from '@midwayjs/core';
|
|
2
|
-
|
|
3
|
-
export declare class ApiResultFormatterMiddleware implements IMiddleware<Context, NextFunction> {
|
|
1
|
+
import { IMiddleware, IMidwayContext } from '@midwayjs/core';
|
|
2
|
+
export declare class ApiResultFormatterMiddleware implements IMiddleware<IMidwayContext, any> {
|
|
4
3
|
apiOption: any;
|
|
5
|
-
ignore(ctx:
|
|
6
|
-
resolve(): (ctx:
|
|
4
|
+
ignore(ctx: IMidwayContext): boolean;
|
|
5
|
+
resolve(): (ctx: IMidwayContext, next: any) => Promise<void>;
|
|
7
6
|
canFormat(data: any): boolean;
|
|
8
7
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
2
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
3
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -8,20 +7,16 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
const core_1 = require("@midwayjs/core");
|
|
15
|
-
const stream_1 = require("stream");
|
|
10
|
+
import { Middleware, Config, } from '@midwayjs/core';
|
|
11
|
+
import { Stream } from 'stream';
|
|
16
12
|
let ApiResultFormatterMiddleware = class ApiResultFormatterMiddleware {
|
|
17
|
-
apiOption;
|
|
18
13
|
ignore(ctx) {
|
|
19
|
-
|
|
14
|
+
const koaCtx = ctx;
|
|
20
15
|
if (this.apiOption?.ignore) {
|
|
21
16
|
for (const ig of this.apiOption.ignore) {
|
|
22
|
-
if (typeof ig === 'string' &&
|
|
17
|
+
if (typeof ig === 'string' && koaCtx.path === ig)
|
|
23
18
|
return true;
|
|
24
|
-
if (ig?.test?.(
|
|
19
|
+
if (ig?.test?.(koaCtx.path))
|
|
25
20
|
return true;
|
|
26
21
|
}
|
|
27
22
|
}
|
|
@@ -29,38 +24,36 @@ let ApiResultFormatterMiddleware = class ApiResultFormatterMiddleware {
|
|
|
29
24
|
}
|
|
30
25
|
resolve() {
|
|
31
26
|
return async (ctx, next) => {
|
|
32
|
-
const
|
|
33
|
-
|
|
27
|
+
const koaCtx = ctx;
|
|
28
|
+
const isApi = this.apiOption?.reg?.test(koaCtx.request.path);
|
|
34
29
|
if (isApi) {
|
|
35
30
|
try {
|
|
36
31
|
await next();
|
|
37
|
-
const res =
|
|
38
|
-
// 是否是可格式化的类型
|
|
32
|
+
const res = koaCtx.body;
|
|
39
33
|
if (this.canFormat(res)) {
|
|
40
|
-
|
|
34
|
+
koaCtx.body = Object.assign({
|
|
41
35
|
ret: 0,
|
|
42
36
|
msg: 'success',
|
|
43
37
|
data: null,
|
|
44
38
|
}, typeof res?.ret === 'number' ? res : { data: res });
|
|
45
|
-
//ctx.logger.info(ctx.request.rawBody, ctx.body);
|
|
46
39
|
}
|
|
47
40
|
}
|
|
48
41
|
catch (err) {
|
|
49
42
|
console.error(err);
|
|
50
|
-
if (typeof err
|
|
51
|
-
|
|
43
|
+
if (typeof err?.ret === 'number') {
|
|
44
|
+
koaCtx.body = err;
|
|
52
45
|
}
|
|
53
46
|
else {
|
|
54
|
-
|
|
47
|
+
koaCtx.body = {
|
|
55
48
|
ret: 1001,
|
|
56
|
-
msg: err
|
|
49
|
+
msg: err?.message || 'Error',
|
|
57
50
|
};
|
|
58
51
|
}
|
|
59
|
-
|
|
52
|
+
koaCtx.logger.error(koaCtx.request.rawBody, koaCtx.body);
|
|
60
53
|
}
|
|
61
54
|
finally {
|
|
62
|
-
if (
|
|
63
|
-
|
|
55
|
+
if (koaCtx.status !== 302)
|
|
56
|
+
koaCtx.status = 200;
|
|
64
57
|
}
|
|
65
58
|
}
|
|
66
59
|
else {
|
|
@@ -69,16 +62,19 @@ let ApiResultFormatterMiddleware = class ApiResultFormatterMiddleware {
|
|
|
69
62
|
};
|
|
70
63
|
}
|
|
71
64
|
canFormat(data) {
|
|
72
|
-
return data !== '' &&
|
|
73
|
-
!(data
|
|
65
|
+
return (data !== '' &&
|
|
66
|
+
!Buffer.isBuffer(data) &&
|
|
67
|
+
!(data instanceof Stream ||
|
|
68
|
+
data instanceof Uint8Array ||
|
|
69
|
+
data instanceof Blob));
|
|
74
70
|
}
|
|
75
71
|
};
|
|
76
|
-
exports.ApiResultFormatterMiddleware = ApiResultFormatterMiddleware;
|
|
77
72
|
__decorate([
|
|
78
|
-
|
|
73
|
+
Config('apiOption'),
|
|
79
74
|
__metadata("design:type", Object)
|
|
80
75
|
], ApiResultFormatterMiddleware.prototype, "apiOption", void 0);
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
ApiResultFormatterMiddleware = __decorate([
|
|
77
|
+
Middleware()
|
|
83
78
|
], ApiResultFormatterMiddleware);
|
|
84
|
-
|
|
79
|
+
export { ApiResultFormatterMiddleware };
|
|
80
|
+
//# sourceMappingURL=apiResultFormatter.js.map
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { IMiddleware } from '@midwayjs/core';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
resolve(): (ctx: Context, next: NextFunction) => Promise<any>;
|
|
1
|
+
import { IMiddleware, IMidwayContext } from '@midwayjs/core';
|
|
2
|
+
export declare class DetectMiddleware implements IMiddleware<IMidwayContext, any> {
|
|
3
|
+
resolve(): (ctx: IMidwayContext, next: any) => Promise<any>;
|
|
5
4
|
}
|
|
@@ -1,44 +1,38 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
2
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
3
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
6
|
};
|
|
8
|
-
|
|
9
|
-
exports.DetectMiddleware = void 0;
|
|
10
|
-
const core_1 = require("@midwayjs/core");
|
|
7
|
+
import { Middleware } from '@midwayjs/core';
|
|
11
8
|
let DetectMiddleware = class DetectMiddleware {
|
|
12
9
|
resolve() {
|
|
13
10
|
return async (ctx, next) => {
|
|
14
|
-
const
|
|
11
|
+
const koaCtx = ctx;
|
|
12
|
+
const UA = koaCtx.get('user-agent') || '';
|
|
15
13
|
const IS_MOBILE = /Mobile/i.test(UA);
|
|
16
14
|
const VER_MQQ = UA.match(/QQ\/([\d.]+)/i) && UA.match(/QQ\/([\d.]+)/i)?.[1];
|
|
17
15
|
const IS_MQQ = Boolean(VER_MQQ);
|
|
18
|
-
// 微信版本号可能分4段,影响业务代码里使用semver比较版本号(会报错),如果业务需要4段版本号可以自己从ua里取
|
|
19
16
|
const VER_WEIXIN = UA.match(/MicroMessenger\/([\d.]+)/i) &&
|
|
20
17
|
UA.match(/MicroMessenger\/([\d.]+)/i)?.[1]
|
|
21
18
|
.split('.')
|
|
22
19
|
.slice(0, 3)
|
|
23
20
|
.join('.');
|
|
24
21
|
const IS_WEIXIN = Boolean(VER_WEIXIN);
|
|
25
|
-
// 是否是开发者工具
|
|
26
22
|
const IS_WECHATTOOLS = /wechatdevtools\//i.test(UA);
|
|
27
|
-
// @ts-ignore
|
|
28
23
|
const IS_PCWEIXIN = /(Windows|Mac)Wechat/i.test(UA);
|
|
29
|
-
// 是否是企业微信
|
|
30
24
|
const IS_WXWORK = /wxwork\//i.test(UA);
|
|
31
|
-
// @ts-ignore
|
|
32
25
|
const VER_MINA = /miniProgram/i.test(UA);
|
|
33
26
|
const IS_MINA = Boolean(VER_MINA);
|
|
34
|
-
let requestIP = (
|
|
35
|
-
|
|
27
|
+
let requestIP = (koaCtx.request.header['x-forwarded-for'] ||
|
|
28
|
+
koaCtx.request.ip ||
|
|
29
|
+
'').toString();
|
|
36
30
|
if (requestIP.includes(','))
|
|
37
31
|
requestIP = requestIP.split(',')[0];
|
|
38
|
-
const clientIP = (
|
|
39
|
-
const hostname =
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
const clientIP = (koaCtx.request.header['x-client-ip'] || requestIP).toString();
|
|
33
|
+
const hostname = koaCtx.get('tencent-acceleration-domain-name') ||
|
|
34
|
+
koaCtx.request.hostname;
|
|
35
|
+
koaCtx.detect = {
|
|
42
36
|
IS_MOBILE,
|
|
43
37
|
IS_MQQ,
|
|
44
38
|
IS_WEIXIN,
|
|
@@ -54,8 +48,8 @@ let DetectMiddleware = class DetectMiddleware {
|
|
|
54
48
|
};
|
|
55
49
|
}
|
|
56
50
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
(0, core_1.Middleware)()
|
|
51
|
+
DetectMiddleware = __decorate([
|
|
52
|
+
Middleware()
|
|
60
53
|
], DetectMiddleware);
|
|
61
|
-
|
|
54
|
+
export { DetectMiddleware };
|
|
55
|
+
//# sourceMappingURL=detect.middleware.js.map
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { IMiddleware } from '@midwayjs/core';
|
|
2
|
-
|
|
3
|
-
export declare class RequestInitMiddleware implements IMiddleware<Context, NextFunction> {
|
|
1
|
+
import { IMiddleware, IMidwayContext } from '@midwayjs/core';
|
|
2
|
+
export declare class RequestInitMiddleware implements IMiddleware<IMidwayContext, any> {
|
|
4
3
|
loggerConfig: any;
|
|
5
4
|
private staticExtensions;
|
|
6
|
-
ignore(ctx:
|
|
7
|
-
resolve(): (ctx:
|
|
5
|
+
ignore(ctx: IMidwayContext): boolean;
|
|
6
|
+
resolve(): (ctx: IMidwayContext, next: any) => Promise<any>;
|
|
8
7
|
}
|