koatty 3.4.2 → 3.4.10
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 +8 -0
- package/dist/controller/BaseController.d.ts +0 -97
- package/dist/controller/BaseController.js +2 -146
- package/dist/controller/BaseController.js.map +1 -1
- package/dist/controller/HttpController.d.ts +131 -0
- package/dist/controller/HttpController.js +215 -0
- package/dist/controller/HttpController.js.map +1 -0
- package/dist/core/Bootstrap.js +9 -8
- package/dist/core/Bootstrap.js.map +1 -1
- package/dist/core/Component.d.ts +2 -11
- package/dist/core/Component.js.map +1 -1
- package/dist/core/Loader.d.ts +13 -18
- package/dist/core/Loader.js +42 -50
- package/dist/core/Loader.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/service/BaseService.js +1 -2
- package/dist/service/BaseService.js.map +1 -1
- package/dist/util/Logger.d.ts +21 -1
- package/dist/util/Logger.js +32 -3
- package/dist/util/Logger.js.map +1 -1
- package/jest_html_reporters.html +1 -1
- package/package.json +9 -9
package/CHANGELOG.md
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
4
4
|
|
5
|
+
### [3.4.10](https://github.com/thinkkoa/koatty/compare/v3.4.8...v3.4.10) (2021-11-26)
|
6
|
+
|
7
|
+
### [3.4.8](https://github.com/thinkkoa/koatty/compare/v3.4.4...v3.4.8) (2021-11-25)
|
8
|
+
|
9
|
+
### [3.4.6](https://github.com/thinkkoa/koatty/compare/v3.4.4...v3.4.6) (2021-11-25)
|
10
|
+
|
11
|
+
### [3.4.4](https://github.com/thinkkoa/koatty/compare/v3.4.2...v3.4.4) (2021-11-20)
|
12
|
+
|
5
13
|
### [3.4.2](https://github.com/thinkkoa/koatty/compare/v3.3.6...v3.4.2) (2021-11-19)
|
6
14
|
|
7
15
|
### [3.3.6](https://github.com/thinkkoa/koatty/compare/v3.2.12...v3.3.6) (2021-07-13)
|
@@ -26,103 +26,6 @@ export declare class BaseController implements IController {
|
|
26
26
|
* @memberof BaseController
|
27
27
|
*/
|
28
28
|
protected init(): void;
|
29
|
-
/**
|
30
|
-
* Whether it is a GET request
|
31
|
-
*
|
32
|
-
* @public
|
33
|
-
* @returns {boolean}
|
34
|
-
* @memberof BaseController
|
35
|
-
*/
|
36
|
-
isGet(): boolean;
|
37
|
-
/**
|
38
|
-
* Whether it is a POST request
|
39
|
-
*
|
40
|
-
* @public
|
41
|
-
* @returns {boolean}
|
42
|
-
* @memberof BaseController
|
43
|
-
*/
|
44
|
-
isPost(): boolean;
|
45
|
-
/**
|
46
|
-
* Determines whether the METHOD request is specified
|
47
|
-
*
|
48
|
-
* @public
|
49
|
-
* @param {string} method
|
50
|
-
* @returns {boolean}
|
51
|
-
* @memberof BaseController
|
52
|
-
*/
|
53
|
-
isMethod(method: string): boolean;
|
54
|
-
/**
|
55
|
-
* Get/Set headers.
|
56
|
-
*
|
57
|
-
* @public
|
58
|
-
* @param {string} [name]
|
59
|
-
* @param {*} [value]
|
60
|
-
* @returns {*}
|
61
|
-
* @memberof BaseController
|
62
|
-
*/
|
63
|
-
header(name?: string, value?: any): any;
|
64
|
-
/**
|
65
|
-
* Get POST/GET parameters, the POST value is priority.
|
66
|
-
*
|
67
|
-
* @param {string} [name]
|
68
|
-
* @returns
|
69
|
-
* @memberof BaseController
|
70
|
-
*/
|
71
|
-
param(name?: string): Promise<any>;
|
72
|
-
/**
|
73
|
-
* Set response content-type
|
74
|
-
*
|
75
|
-
* @public
|
76
|
-
* @param {string} contentType
|
77
|
-
* @param {(string | boolean)} [encoding]
|
78
|
-
* @returns {string}
|
79
|
-
* @memberof BaseController
|
80
|
-
*/
|
81
|
-
type(contentType: string, encoding?: string | boolean): string;
|
82
|
-
/**
|
83
|
-
* set cache-control and expires header
|
84
|
-
*
|
85
|
-
* @public
|
86
|
-
* @param {number} [timeout=30]
|
87
|
-
* @returns {void}
|
88
|
-
* @memberof BaseController
|
89
|
-
*/
|
90
|
-
expires(timeout?: number): void;
|
91
|
-
/**
|
92
|
-
* Url redirect
|
93
|
-
*
|
94
|
-
* @param {string} urls
|
95
|
-
* @param {string} [alt]
|
96
|
-
* @returns {void}
|
97
|
-
* @memberof BaseController
|
98
|
-
*/
|
99
|
-
redirect(urls: string, alt?: string): void;
|
100
|
-
/**
|
101
|
-
* Block access
|
102
|
-
*
|
103
|
-
* @param {number} [code=403]
|
104
|
-
* @returns {Promise<any>}
|
105
|
-
* @memberof BaseController
|
106
|
-
*/
|
107
|
-
deny(code?: number): Promise<any>;
|
108
|
-
/**
|
109
|
-
* Set response Body content
|
110
|
-
*
|
111
|
-
* @param {*} data
|
112
|
-
* @param {string} [contentType]
|
113
|
-
* @param {string} [encoding]
|
114
|
-
* @returns {Promise<any>}
|
115
|
-
* @memberof BaseController
|
116
|
-
*/
|
117
|
-
body(data: any, contentType?: string, encoding?: string): Promise<any>;
|
118
|
-
/**
|
119
|
-
* Respond to json formatted content
|
120
|
-
*
|
121
|
-
* @param {*} data
|
122
|
-
* @returns {Promise<any>}
|
123
|
-
* @memberof BaseController
|
124
|
-
*/
|
125
|
-
json(data: any): Promise<any>;
|
126
29
|
/**
|
127
30
|
* Format api interface data format
|
128
31
|
*
|
@@ -53,150 +53,6 @@ class BaseController {
|
|
53
53
|
// public __after(): Promise<any> {
|
54
54
|
// return Promise.resolve();
|
55
55
|
// }
|
56
|
-
/**
|
57
|
-
* Whether it is a GET request
|
58
|
-
*
|
59
|
-
* @public
|
60
|
-
* @returns {boolean}
|
61
|
-
* @memberof BaseController
|
62
|
-
*/
|
63
|
-
isGet() {
|
64
|
-
return this.ctx.method === "GET";
|
65
|
-
}
|
66
|
-
/**
|
67
|
-
* Whether it is a POST request
|
68
|
-
*
|
69
|
-
* @public
|
70
|
-
* @returns {boolean}
|
71
|
-
* @memberof BaseController
|
72
|
-
*/
|
73
|
-
isPost() {
|
74
|
-
return this.ctx.method === "POST";
|
75
|
-
}
|
76
|
-
/**
|
77
|
-
* Determines whether the METHOD request is specified
|
78
|
-
*
|
79
|
-
* @public
|
80
|
-
* @param {string} method
|
81
|
-
* @returns {boolean}
|
82
|
-
* @memberof BaseController
|
83
|
-
*/
|
84
|
-
isMethod(method) {
|
85
|
-
return this.ctx.method === method.toUpperCase();
|
86
|
-
}
|
87
|
-
/**
|
88
|
-
* Get/Set headers.
|
89
|
-
*
|
90
|
-
* @public
|
91
|
-
* @param {string} [name]
|
92
|
-
* @param {*} [value]
|
93
|
-
* @returns {*}
|
94
|
-
* @memberof BaseController
|
95
|
-
*/
|
96
|
-
header(name, value) {
|
97
|
-
if (name === undefined) {
|
98
|
-
return this.ctx.headers;
|
99
|
-
}
|
100
|
-
if (value === undefined) {
|
101
|
-
return this.ctx.get(name);
|
102
|
-
}
|
103
|
-
return this.ctx.set(name, value);
|
104
|
-
}
|
105
|
-
/**
|
106
|
-
* Get POST/GET parameters, the POST value is priority.
|
107
|
-
*
|
108
|
-
* @param {string} [name]
|
109
|
-
* @returns
|
110
|
-
* @memberof BaseController
|
111
|
-
*/
|
112
|
-
param(name) {
|
113
|
-
return this.ctx.bodyParser().then((body) => {
|
114
|
-
const getParams = this.ctx.queryParser() || {};
|
115
|
-
const postParams = (body.post ? body.post : body) || {};
|
116
|
-
if (name !== undefined) {
|
117
|
-
return postParams[name] === undefined ? getParams[name] : postParams[name];
|
118
|
-
}
|
119
|
-
return { ...getParams, ...postParams };
|
120
|
-
});
|
121
|
-
}
|
122
|
-
/**
|
123
|
-
* Set response content-type
|
124
|
-
*
|
125
|
-
* @public
|
126
|
-
* @param {string} contentType
|
127
|
-
* @param {(string | boolean)} [encoding]
|
128
|
-
* @returns {string}
|
129
|
-
* @memberof BaseController
|
130
|
-
*/
|
131
|
-
type(contentType, encoding) {
|
132
|
-
if (encoding !== false && !contentType.includes("charset")) {
|
133
|
-
contentType = `${contentType}; charset=${encoding || this.app.config("encoding")}`;
|
134
|
-
}
|
135
|
-
this.ctx.type = contentType;
|
136
|
-
return contentType;
|
137
|
-
}
|
138
|
-
/**
|
139
|
-
* set cache-control and expires header
|
140
|
-
*
|
141
|
-
* @public
|
142
|
-
* @param {number} [timeout=30]
|
143
|
-
* @returns {void}
|
144
|
-
* @memberof BaseController
|
145
|
-
*/
|
146
|
-
expires(timeout = 30) {
|
147
|
-
timeout = Helper_1.Helper.toNumber(timeout) * 1000;
|
148
|
-
const date = new Date(Date.now() + timeout);
|
149
|
-
this.ctx.set("Cache-Control", `max-age=${timeout}`);
|
150
|
-
return this.ctx.set("Expires", date.toUTCString());
|
151
|
-
}
|
152
|
-
/**
|
153
|
-
* Url redirect
|
154
|
-
*
|
155
|
-
* @param {string} urls
|
156
|
-
* @param {string} [alt]
|
157
|
-
* @returns {void}
|
158
|
-
* @memberof BaseController
|
159
|
-
*/
|
160
|
-
redirect(urls, alt) {
|
161
|
-
return this.ctx.redirect(urls, alt);
|
162
|
-
}
|
163
|
-
/**
|
164
|
-
* Block access
|
165
|
-
*
|
166
|
-
* @param {number} [code=403]
|
167
|
-
* @returns {Promise<any>}
|
168
|
-
* @memberof BaseController
|
169
|
-
*/
|
170
|
-
deny(code = 403) {
|
171
|
-
return this.ctx.throw(code);
|
172
|
-
}
|
173
|
-
/**
|
174
|
-
* Set response Body content
|
175
|
-
*
|
176
|
-
* @param {*} data
|
177
|
-
* @param {string} [contentType]
|
178
|
-
* @param {string} [encoding]
|
179
|
-
* @returns {Promise<any>}
|
180
|
-
* @memberof BaseController
|
181
|
-
*/
|
182
|
-
body(data, contentType, encoding) {
|
183
|
-
contentType = contentType || "text/plain";
|
184
|
-
encoding = encoding || this.app.config("encoding") || "utf-8";
|
185
|
-
this.type(contentType, encoding);
|
186
|
-
this.ctx.body = data;
|
187
|
-
// return this.app.prevent();
|
188
|
-
return null;
|
189
|
-
}
|
190
|
-
/**
|
191
|
-
* Respond to json formatted content
|
192
|
-
*
|
193
|
-
* @param {*} data
|
194
|
-
* @returns {Promise<any>}
|
195
|
-
* @memberof BaseController
|
196
|
-
*/
|
197
|
-
json(data) {
|
198
|
-
return this.body(data, "application/json");
|
199
|
-
}
|
200
56
|
/**
|
201
57
|
* Format api interface data format
|
202
58
|
*
|
@@ -238,7 +94,7 @@ class BaseController {
|
|
238
94
|
*/
|
239
95
|
ok(msg, data, code = 0) {
|
240
96
|
const obj = this.formatApiData(msg, data, code);
|
241
|
-
return
|
97
|
+
return Promise.resolve(obj);
|
242
98
|
}
|
243
99
|
/**
|
244
100
|
* Response to normalize json format content for fail
|
@@ -251,7 +107,7 @@ class BaseController {
|
|
251
107
|
*/
|
252
108
|
fail(msg, data, code = 1) {
|
253
109
|
const obj = this.formatApiData(msg, data, code);
|
254
|
-
return
|
110
|
+
return Promise.resolve(obj);
|
255
111
|
}
|
256
112
|
}
|
257
113
|
exports.BaseController = BaseController;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"BaseController.js","sourceRoot":"","sources":["../../src/controller/BaseController.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,2CAAwC;AAKxC;;;;;;GAMG;AACH,MAAa,cAAc;IAMvB;;;;;OAKG;IACH,YAAsB,GAAkB;QACpC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,EAAE,CAAC;IAChB,CAAC;IAED;;;;;OAKG;IACO,IAAI;IAEd,CAAC;IAED,MAAM;IACN,6HAA6H;IAC7H,KAAK;IACL,6BAA6B;IAC7B,8BAA8B;IAC9B,MAAM;IACN,oCAAoC;IACpC,gCAAgC;IAChC,IAAI;IAEJ,MAAM;IACN,qHAAqH;IACrH,KAAK;IACL,aAAa;IACb,kBAAkB;IAClB,8BAA8B;IAC9B,MAAM;IACN,mCAAmC;IACnC,gCAAgC;IAChC,IAAI;IAEJ
|
1
|
+
{"version":3,"file":"BaseController.js","sourceRoot":"","sources":["../../src/controller/BaseController.ts"],"names":[],"mappings":";;;AA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Ac,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAC3D,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IAED;;;;;;;;OAQG;IACI,IAAI,CAAC,GAA8B,EAAE,IAAU,EAAE,IAAI,GAAG,CAAC;QAC5D,MAAM,GAAG,GAAc,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;QAC3D,OAAO,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;CAEJ;AAzGD,wCAyGC;AAGD,8CAA8C;AAC9C,kDAAkD;AAClD,0CAA0C;AAC1C,kEAAkE;AAClE,gEAAgE;AAChE,uCAAuC;AACvC,gEAAgE;AAChE,mBAAmB;AACnB,mEAAmE;AACnE,YAAY;AACZ,SAAS;AACT,oCAAoC;AACpC,6DAA6D;AAC7D,SAAS;AACT,2CAA2C;AAC3C,yDAAyD;AACzD,uGAAuG;AACvG,2FAA2F;AAC3F,gBAAgB;AAChB,cAAc;AACd,6DAA6D;AAC7D,QAAQ;AACR,MAAM"}
|
@@ -0,0 +1,131 @@
|
|
1
|
+
import { Koatty, KoattyContext } from 'koatty_core';
|
2
|
+
import { ApiInput, ApiOutput } from '../core/Component';
|
3
|
+
import { BaseController } from "./BaseController";
|
4
|
+
/**
|
5
|
+
* HTTP controller
|
6
|
+
*
|
7
|
+
* @export
|
8
|
+
* @class HttpController
|
9
|
+
* @implements {IController}
|
10
|
+
*/
|
11
|
+
export declare class HttpController extends BaseController {
|
12
|
+
app: Koatty;
|
13
|
+
ctx: KoattyContext;
|
14
|
+
/**
|
15
|
+
* Whether it is a GET request
|
16
|
+
*
|
17
|
+
* @public
|
18
|
+
* @returns {boolean}
|
19
|
+
* @memberof HttpController
|
20
|
+
*/
|
21
|
+
isGet(): boolean;
|
22
|
+
/**
|
23
|
+
* Whether it is a POST request
|
24
|
+
*
|
25
|
+
* @public
|
26
|
+
* @returns {boolean}
|
27
|
+
* @memberof HttpController
|
28
|
+
*/
|
29
|
+
isPost(): boolean;
|
30
|
+
/**
|
31
|
+
* Determines whether the METHOD request is specified
|
32
|
+
*
|
33
|
+
* @public
|
34
|
+
* @param {string} method
|
35
|
+
* @returns {boolean}
|
36
|
+
* @memberof HttpController
|
37
|
+
*/
|
38
|
+
isMethod(method: string): boolean;
|
39
|
+
/**
|
40
|
+
* Get/Set headers.
|
41
|
+
*
|
42
|
+
* @public
|
43
|
+
* @param {string} [name]
|
44
|
+
* @param {*} [value]
|
45
|
+
* @returns {*}
|
46
|
+
* @memberof HttpController
|
47
|
+
*/
|
48
|
+
header(name?: string, value?: any): any;
|
49
|
+
/**
|
50
|
+
* Get POST/GET parameters, the POST value is priority.
|
51
|
+
*
|
52
|
+
* @param {string} [name]
|
53
|
+
* @returns
|
54
|
+
* @memberof HttpController
|
55
|
+
*/
|
56
|
+
param(name?: string): Promise<any>;
|
57
|
+
/**
|
58
|
+
* Set response content-type
|
59
|
+
*
|
60
|
+
* @public
|
61
|
+
* @param {string} contentType
|
62
|
+
* @param {(string | boolean)} [encoding]
|
63
|
+
* @returns {string}
|
64
|
+
* @memberof HttpController
|
65
|
+
*/
|
66
|
+
type(contentType: string, encoding?: string | boolean): string;
|
67
|
+
/**
|
68
|
+
* set cache-control and expires header
|
69
|
+
*
|
70
|
+
* @public
|
71
|
+
* @param {number} [timeout=30]
|
72
|
+
* @returns {void}
|
73
|
+
* @memberof HttpController
|
74
|
+
*/
|
75
|
+
expires(timeout?: number): void;
|
76
|
+
/**
|
77
|
+
* Url redirect
|
78
|
+
*
|
79
|
+
* @param {string} urls
|
80
|
+
* @param {string} [alt]
|
81
|
+
* @returns {void}
|
82
|
+
* @memberof HttpController
|
83
|
+
*/
|
84
|
+
redirect(urls: string, alt?: string): void;
|
85
|
+
/**
|
86
|
+
* Block access
|
87
|
+
*
|
88
|
+
* @param {number} [code=403]
|
89
|
+
* @returns {Promise<any>}
|
90
|
+
* @memberof HttpController
|
91
|
+
*/
|
92
|
+
deny(code?: number): Promise<any>;
|
93
|
+
/**
|
94
|
+
* Set response Body content
|
95
|
+
*
|
96
|
+
* @param {*} data
|
97
|
+
* @param {string} [contentType]
|
98
|
+
* @param {string} [encoding]
|
99
|
+
* @returns {Promise<any>}
|
100
|
+
* @memberof HttpController
|
101
|
+
*/
|
102
|
+
body(data: any, contentType?: string, encoding?: string): Promise<any>;
|
103
|
+
/**
|
104
|
+
* Respond to json formatted content
|
105
|
+
*
|
106
|
+
* @param {*} data
|
107
|
+
* @returns {Promise<any>}
|
108
|
+
* @memberof HttpController
|
109
|
+
*/
|
110
|
+
json(data: any): Promise<any>;
|
111
|
+
/**
|
112
|
+
* Response to normalize json format content for success
|
113
|
+
*
|
114
|
+
* @param {(string | ApiInput)} msg 待处理的message消息
|
115
|
+
* @param {*} [data] 待处理的数据
|
116
|
+
* @param {number} [code=200] 错误码,默认0
|
117
|
+
* @returns {Promise<ApiOutput>}
|
118
|
+
* @memberof HttpController
|
119
|
+
*/
|
120
|
+
ok(msg: string | ApiInput, data?: any, code?: number): Promise<ApiOutput>;
|
121
|
+
/**
|
122
|
+
* Response to normalize json format content for fail
|
123
|
+
*
|
124
|
+
* @param {(string | ApiInput)} msg
|
125
|
+
* @param {*} [data]
|
126
|
+
* @param {number} [code=1]
|
127
|
+
* @returns {Promise<ApiOutput>}
|
128
|
+
* @memberof HttpController
|
129
|
+
*/
|
130
|
+
fail(msg: Error | string | ApiInput, data?: any, code?: number): Promise<ApiOutput>;
|
131
|
+
}
|
@@ -0,0 +1,215 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.HttpController = void 0;
|
4
|
+
/**
|
5
|
+
* @ author: richen
|
6
|
+
* @ copyright: Copyright (c) - <richenlin(at)gmail.com>
|
7
|
+
* @ license: BSD (3-Clause)
|
8
|
+
* @ version: 2020-05-20 15:45:24
|
9
|
+
*/
|
10
|
+
const Helper_1 = require("../util/Helper");
|
11
|
+
const BaseController_1 = require("./BaseController");
|
12
|
+
/**
|
13
|
+
* HTTP controller
|
14
|
+
*
|
15
|
+
* @export
|
16
|
+
* @class HttpController
|
17
|
+
* @implements {IController}
|
18
|
+
*/
|
19
|
+
class HttpController extends BaseController_1.BaseController {
|
20
|
+
/**
|
21
|
+
* Whether it is a GET request
|
22
|
+
*
|
23
|
+
* @public
|
24
|
+
* @returns {boolean}
|
25
|
+
* @memberof HttpController
|
26
|
+
*/
|
27
|
+
isGet() {
|
28
|
+
return this.ctx.method === "GET";
|
29
|
+
}
|
30
|
+
/**
|
31
|
+
* Whether it is a POST request
|
32
|
+
*
|
33
|
+
* @public
|
34
|
+
* @returns {boolean}
|
35
|
+
* @memberof HttpController
|
36
|
+
*/
|
37
|
+
isPost() {
|
38
|
+
return this.ctx.method === "POST";
|
39
|
+
}
|
40
|
+
/**
|
41
|
+
* Determines whether the METHOD request is specified
|
42
|
+
*
|
43
|
+
* @public
|
44
|
+
* @param {string} method
|
45
|
+
* @returns {boolean}
|
46
|
+
* @memberof HttpController
|
47
|
+
*/
|
48
|
+
isMethod(method) {
|
49
|
+
return this.ctx.method === method.toUpperCase();
|
50
|
+
}
|
51
|
+
/**
|
52
|
+
* Get/Set headers.
|
53
|
+
*
|
54
|
+
* @public
|
55
|
+
* @param {string} [name]
|
56
|
+
* @param {*} [value]
|
57
|
+
* @returns {*}
|
58
|
+
* @memberof HttpController
|
59
|
+
*/
|
60
|
+
header(name, value) {
|
61
|
+
if (name === undefined) {
|
62
|
+
return this.ctx.headers;
|
63
|
+
}
|
64
|
+
if (value === undefined) {
|
65
|
+
return this.ctx.get(name);
|
66
|
+
}
|
67
|
+
return this.ctx.set(name, value);
|
68
|
+
}
|
69
|
+
/**
|
70
|
+
* Get POST/GET parameters, the POST value is priority.
|
71
|
+
*
|
72
|
+
* @param {string} [name]
|
73
|
+
* @returns
|
74
|
+
* @memberof HttpController
|
75
|
+
*/
|
76
|
+
param(name) {
|
77
|
+
return this.ctx.bodyParser().then((body) => {
|
78
|
+
const getParams = this.ctx.queryParser() || {};
|
79
|
+
const postParams = (body.post ? body.post : body) || {};
|
80
|
+
if (name !== undefined) {
|
81
|
+
return postParams[name] === undefined ? getParams[name] : postParams[name];
|
82
|
+
}
|
83
|
+
return { ...getParams, ...postParams };
|
84
|
+
});
|
85
|
+
}
|
86
|
+
/**
|
87
|
+
* Set response content-type
|
88
|
+
*
|
89
|
+
* @public
|
90
|
+
* @param {string} contentType
|
91
|
+
* @param {(string | boolean)} [encoding]
|
92
|
+
* @returns {string}
|
93
|
+
* @memberof HttpController
|
94
|
+
*/
|
95
|
+
type(contentType, encoding) {
|
96
|
+
if (encoding !== false && !contentType.includes("charset")) {
|
97
|
+
contentType = `${contentType}; charset=${encoding || this.app.config("encoding")}`;
|
98
|
+
}
|
99
|
+
this.ctx.type = contentType;
|
100
|
+
return contentType;
|
101
|
+
}
|
102
|
+
/**
|
103
|
+
* set cache-control and expires header
|
104
|
+
*
|
105
|
+
* @public
|
106
|
+
* @param {number} [timeout=30]
|
107
|
+
* @returns {void}
|
108
|
+
* @memberof HttpController
|
109
|
+
*/
|
110
|
+
expires(timeout = 30) {
|
111
|
+
timeout = Helper_1.Helper.toNumber(timeout) * 1000;
|
112
|
+
const date = new Date(Date.now() + timeout);
|
113
|
+
this.ctx.set("Cache-Control", `max-age=${timeout}`);
|
114
|
+
return this.ctx.set("Expires", date.toUTCString());
|
115
|
+
}
|
116
|
+
/**
|
117
|
+
* Url redirect
|
118
|
+
*
|
119
|
+
* @param {string} urls
|
120
|
+
* @param {string} [alt]
|
121
|
+
* @returns {void}
|
122
|
+
* @memberof HttpController
|
123
|
+
*/
|
124
|
+
redirect(urls, alt) {
|
125
|
+
return this.ctx.redirect(urls, alt);
|
126
|
+
}
|
127
|
+
/**
|
128
|
+
* Block access
|
129
|
+
*
|
130
|
+
* @param {number} [code=403]
|
131
|
+
* @returns {Promise<any>}
|
132
|
+
* @memberof HttpController
|
133
|
+
*/
|
134
|
+
deny(code = 403) {
|
135
|
+
return this.ctx.throw(code);
|
136
|
+
}
|
137
|
+
/**
|
138
|
+
* Set response Body content
|
139
|
+
*
|
140
|
+
* @param {*} data
|
141
|
+
* @param {string} [contentType]
|
142
|
+
* @param {string} [encoding]
|
143
|
+
* @returns {Promise<any>}
|
144
|
+
* @memberof HttpController
|
145
|
+
*/
|
146
|
+
body(data, contentType, encoding) {
|
147
|
+
contentType = contentType || "text/plain";
|
148
|
+
encoding = encoding || this.app.config("encoding") || "utf-8";
|
149
|
+
this.type(contentType, encoding);
|
150
|
+
this.ctx.body = data;
|
151
|
+
// return this.app.prevent();
|
152
|
+
return null;
|
153
|
+
}
|
154
|
+
/**
|
155
|
+
* Respond to json formatted content
|
156
|
+
*
|
157
|
+
* @param {*} data
|
158
|
+
* @returns {Promise<any>}
|
159
|
+
* @memberof HttpController
|
160
|
+
*/
|
161
|
+
json(data) {
|
162
|
+
return this.body(data, "application/json");
|
163
|
+
}
|
164
|
+
/**
|
165
|
+
* Response to normalize json format content for success
|
166
|
+
*
|
167
|
+
* @param {(string | ApiInput)} msg 待处理的message消息
|
168
|
+
* @param {*} [data] 待处理的数据
|
169
|
+
* @param {number} [code=200] 错误码,默认0
|
170
|
+
* @returns {Promise<ApiOutput>}
|
171
|
+
* @memberof HttpController
|
172
|
+
*/
|
173
|
+
ok(msg, data, code = 0) {
|
174
|
+
const obj = this.formatApiData(msg, data, code);
|
175
|
+
return this.json(obj);
|
176
|
+
}
|
177
|
+
/**
|
178
|
+
* Response to normalize json format content for fail
|
179
|
+
*
|
180
|
+
* @param {(string | ApiInput)} msg
|
181
|
+
* @param {*} [data]
|
182
|
+
* @param {number} [code=1]
|
183
|
+
* @returns {Promise<ApiOutput>}
|
184
|
+
* @memberof HttpController
|
185
|
+
*/
|
186
|
+
fail(msg, data, code = 1) {
|
187
|
+
const obj = this.formatApiData(msg, data, code);
|
188
|
+
return this.json(obj);
|
189
|
+
}
|
190
|
+
}
|
191
|
+
exports.HttpController = HttpController;
|
192
|
+
// const properties = ["constructor", "init"];
|
193
|
+
// export const HttpController = new Proxy(Base, {
|
194
|
+
// set(target, key, value, receiver) {
|
195
|
+
// if (Reflect.get(target, key, receiver) === undefined) {
|
196
|
+
// return Reflect.set(target, key, value, receiver);
|
197
|
+
// } else if (key === "init") {
|
198
|
+
// return Reflect.set(target, key, value, receiver);
|
199
|
+
// } else {
|
200
|
+
// throw Error("Cannot redefine getter-only property");
|
201
|
+
// }
|
202
|
+
// },
|
203
|
+
// deleteProperty(target, key) {
|
204
|
+
// throw Error("Cannot delete getter-only property");
|
205
|
+
// },
|
206
|
+
// construct(target, args, newTarget) {
|
207
|
+
// Reflect.ownKeys(target.prototype).map((n) => {
|
208
|
+
// if (newTarget.prototype.hasOwnProperty(n) && !properties.includes(Helper.toString(n))) {
|
209
|
+
// throw Error(`Cannot override the final method "${Helper.toString(n)}"`);
|
210
|
+
// }
|
211
|
+
// });
|
212
|
+
// return Reflect.construct(target, args, newTarget);
|
213
|
+
// }
|
214
|
+
// });
|
215
|
+
//# sourceMappingURL=HttpController.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"HttpController.js","sourceRoot":"","sources":["../../src/controller/HttpController.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,2CAAwC;AAGxC,qDAAkD;AAElD;;;;;;GAMG;AACH,MAAa,cAAe,SAAQ,+BAAc;IAI9C;;;;;;OAMG;IACI,KAAK;QACR,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,KAAK,KAAK,CAAC;IACrC,CAAC;IAED;;;;;;OAMG;IACI,MAAM;QACT,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC;IACtC,CAAC;IAED;;;;;;;OAOG;IACI,QAAQ,CAAC,MAAc;QAC1B,OAAO,IAAI,CAAC,GAAG,CAAC,MAAM,KAAK,MAAM,CAAC,WAAW,EAAE,CAAC;IACpD,CAAC;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