koatty 3.3.8 → 3.4.8
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/.eslintrc.js +2 -1
- package/.vscode/settings.json +3 -0
- package/CHANGELOG.md +8 -0
- package/LICENSE +1 -1
- package/README.md +5 -0
- package/babel.config.js +0 -5
- package/demo/static/test.css +3 -0
- package/demo/tsconfig.json +66 -0
- package/dist/config/config.d.ts +1 -1
- package/dist/config/config.js +1 -1
- package/dist/config/config.js.map +1 -1
- package/dist/config/middleware.d.ts +4 -6
- package/dist/config/middleware.js +7 -16
- package/dist/config/middleware.js.map +1 -1
- package/dist/config/router.js +20 -16
- package/dist/config/router.js.map +1 -1
- 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.d.ts +19 -0
- package/dist/core/Bootstrap.js +63 -15
- 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 -36
- package/dist/core/Loader.js +47 -77
- package/dist/core/Loader.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/middleware/TraceMiddleware.d.ts +11 -0
- package/dist/middleware/TraceMiddleware.js +22 -0
- package/dist/middleware/TraceMiddleware.js.map +1 -0
- 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.config.js +19 -1
- package/jest_html_reporters.html +1 -1
- package/package.json +29 -24
- package/tsconfig.json +1 -1
- package/dist/core/Router.d.ts +0 -15
- package/dist/core/Router.js +0 -19
- package/dist/core/Router.js.map +0 -1
- package/dist/core/Serve.d.ts +0 -8
- package/dist/core/Serve.js +0 -62
- package/dist/core/Serve.js.map +0 -1
package/.eslintrc.js
CHANGED
@@ -22,12 +22,13 @@ module.exports = {
|
|
22
22
|
},
|
23
23
|
rules: {
|
24
24
|
"@typescript-eslint/no-explicit-any": "off",
|
25
|
-
"@typescript-eslint/no-require-imports": "off",
|
25
|
+
// "@typescript-eslint/no-require-imports": "off",
|
26
26
|
"@typescript-eslint/no-var-requires": "off",
|
27
27
|
"@typescript-eslint/member-ordering": "off",
|
28
28
|
"@typescript-eslint/consistent-type-assertions": "off",
|
29
29
|
"@typescript-eslint/no-param-reassign": "off",
|
30
30
|
"@typescript-eslint/no-empty-function": "off",
|
31
|
+
"@typescript-eslint/no-empty-interface": "off",
|
31
32
|
"@typescript-eslint/explicit-module-boundary-types": "off",
|
32
33
|
"@typescript-eslint/ban-types": ["error",
|
33
34
|
{
|
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.8](https://github.com/thinkkoa/koatty/compare/v3.4.4...v3.4.8) (2021-11-25)
|
6
|
+
|
7
|
+
### [3.4.6](https://github.com/thinkkoa/koatty/compare/v3.4.4...v3.4.6) (2021-11-25)
|
8
|
+
|
9
|
+
### [3.4.4](https://github.com/thinkkoa/koatty/compare/v3.4.2...v3.4.4) (2021-11-20)
|
10
|
+
|
11
|
+
### [3.4.2](https://github.com/thinkkoa/koatty/compare/v3.3.6...v3.4.2) (2021-11-19)
|
12
|
+
|
5
13
|
### [3.3.6](https://github.com/thinkkoa/koatty/compare/v3.2.12...v3.3.6) (2021-07-13)
|
6
14
|
|
7
15
|
### [3.3.4](https://github.com/thinkkoa/koatty/compare/v3.3.3...v3.3.4) (2021-07-07)
|
package/LICENSE
CHANGED
package/README.md
CHANGED
@@ -6,6 +6,11 @@ Use Typescript's decorator implement IOC and AOP.
|
|
6
6
|
|
7
7
|
[](https://www.npmjs.com/package/koatty)[](https://npmcharts.com/compare/koatty?minimal=true)
|
8
8
|
|
9
|
+
## New
|
10
|
+
|
11
|
+
* HTTP、HTTPS、HTTP2 .
|
12
|
+
* gRPC server.
|
13
|
+
* WebSocket server. (unstable)
|
9
14
|
## Documentation
|
10
15
|
|
11
16
|
[koatty_doc_CN](https://koatty.github.io/koatty_doc/) (In progress💪)
|
package/babel.config.js
CHANGED
@@ -1,11 +1,6 @@
|
|
1
1
|
/*
|
2
2
|
* @Description : babel配置
|
3
3
|
* @usage : 用于jest执行用例
|
4
|
-
* @Date : 2020-10-19 23:08:40
|
5
|
-
* @Author : fankerwang<fankerwang@tencent.com>
|
6
|
-
* @LastEditors : fankerwang<fankerwang@tencent.com>
|
7
|
-
* @LastEditTime : 2021-05-21 20:27:11
|
8
|
-
* @FilePath : /tkoatty/babel.config.js
|
9
4
|
*/
|
10
5
|
|
11
6
|
module.exports = {
|
@@ -0,0 +1,66 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
/* Basic Options */
|
4
|
+
"target": "es2018" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */,
|
5
|
+
"module": "commonjs" /* Specify module code generation: 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
|
6
|
+
"lib": [
|
7
|
+
"es2019"
|
8
|
+
]
|
9
|
+
/* Specify library files to be included in the compilation: */ ,
|
10
|
+
// "allowJs": true /* Allow javascript files to be compiled. */,
|
11
|
+
// "checkJs": true, /* Report errors in .js files. */
|
12
|
+
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
13
|
+
"declaration": true /* Generates corresponding '.d.ts' file. */,
|
14
|
+
"sourceMap": true /* Generates corresponding '.map' file. */,
|
15
|
+
// "outFile": "./", /* Concatenate and emit output to single file. */
|
16
|
+
"outDir": "./dist/" /* Redirect output structure to the directory. */,
|
17
|
+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
18
|
+
"removeComments": true /* Do not emit comments to output. */,
|
19
|
+
// "noEmit": true /* Do not emit outputs. */,
|
20
|
+
"importHelpers": true /* Import emit helpers from 'tslib'. */,
|
21
|
+
"downlevelIteration": true /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */,
|
22
|
+
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
23
|
+
/* Strict Type-Checking Options */
|
24
|
+
"stripInternal": true /* Enable all strict type-checking options. */,
|
25
|
+
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
|
26
|
+
// "strictNullChecks": true, /* Enable strict null checks. */
|
27
|
+
"noImplicitThis": false /* Raise error on 'this' expressions with an implied 'any' type. */,
|
28
|
+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
29
|
+
"pretty": true, /* pretty source file. */
|
30
|
+
/* Additional Checks */
|
31
|
+
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
32
|
+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
33
|
+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
34
|
+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
35
|
+
/* Module Resolution Options */
|
36
|
+
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
|
37
|
+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
38
|
+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
39
|
+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
40
|
+
"typeRoots": [
|
41
|
+
"node_modules/@types"
|
42
|
+
// "./typings/*"
|
43
|
+
] /* List of folders to include type definitions from. */,
|
44
|
+
"types": [
|
45
|
+
"node"
|
46
|
+
] /* Type declaration files to be included in compilation. */,
|
47
|
+
"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
|
48
|
+
"resolveJsonModule": true,
|
49
|
+
"esModuleInterop": true,
|
50
|
+
/* Source Map Options */
|
51
|
+
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
52
|
+
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
|
53
|
+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
54
|
+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
55
|
+
/* Experimental Options */
|
56
|
+
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
|
57
|
+
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */
|
58
|
+
},
|
59
|
+
"exclude": [
|
60
|
+
"node_modules"
|
61
|
+
],
|
62
|
+
"include": [
|
63
|
+
"src",
|
64
|
+
"src/**/*.json"
|
65
|
+
]
|
66
|
+
}
|
package/dist/config/config.d.ts
CHANGED
package/dist/config/config.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,kBAAe;IACX,cAAc;IACd,QAAQ,EAAE,IAAI;IACd,QAAQ,EAAE,EAAE;IACZ,
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config/config.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,kBAAe;IACX,cAAc;IACd,QAAQ,EAAE,IAAI;IACd,QAAQ,EAAE,EAAE;IACZ,QAAQ,EAAE,MAAM;IAChB,UAAU,EAAE,KAAK;IACjB,YAAY,EAAE,EAAE;IAChB,QAAQ,EAAE,EAAE;IACZ,QAAQ,EAAE,EAAE;IACZ,QAAQ,EAAE,OAAO;IAEjB,mGAAmG;IACnG,YAAY,EAAE,IAAI;IAClB,UAAU,EAAE,KAAK;IACjB,gBAAgB,EAAE,MAAM;IACxB,SAAS,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS,GAAG,OAAO,EAAE,qBAAqB;CAEpE,CAAC"}
|
@@ -1,12 +1,10 @@
|
|
1
|
-
/**
|
2
|
-
* @ author: richen
|
3
|
-
* @ copyright: Copyright (c) - <richenlin(at)gmail.com>
|
4
|
-
* @ license: BSD (3-Clause)
|
5
|
-
* @ version: 2020-04-30 14:52:34
|
6
|
-
*/
|
7
1
|
declare const _default: {
|
8
2
|
list: any[];
|
9
3
|
config: {
|
4
|
+
TraceMiddleware: {
|
5
|
+
HeaderName: string;
|
6
|
+
IdFactory: (<T extends ArrayLike<number>>(options: import("uuid").V4Options, buffer: T, offset?: number) => T) & ((options?: import("uuid").V4Options) => string);
|
7
|
+
};
|
10
8
|
PayloadMiddleware: {
|
11
9
|
extTypes: {
|
12
10
|
json: string[];
|
@@ -1,30 +1,21 @@
|
|
1
1
|
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
2
3
|
/**
|
3
4
|
* @ author: richen
|
4
5
|
* @ copyright: Copyright (c) - <richenlin(at)gmail.com>
|
5
6
|
* @ license: BSD (3-Clause)
|
6
7
|
* @ version: 2020-04-30 14:52:34
|
7
8
|
*/
|
8
|
-
|
9
|
+
const uuid_1 = require("uuid");
|
9
10
|
exports.default = {
|
10
11
|
// List of loaded middleware(except for the middleware loaded by default),
|
11
12
|
// executed in the order of elements
|
12
13
|
list: [],
|
13
14
|
config: {
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
// alias: {}, // object map of aliases. See below
|
19
|
-
// gzip: true, // when request's accept-encoding include gzip, files will compressed by gzip.
|
20
|
-
// usePrecompiledGzip: false, // try use gzip files, loaded from disk, like nginx gzip_static
|
21
|
-
// buffer: false, // store the files in memory instead of streaming from the filesystem on each request
|
22
|
-
// filter: [], // (function | array) - filter files at init dir, for example - skip non build (source) files. If array set - allow only listed files
|
23
|
-
// maxAge: 3600 * 24 * 7, // cache control max age for the files, 0 by default.
|
24
|
-
// preload: false, // caches the assets on initialization or not, default to true. always work together with options.dynamic.
|
25
|
-
// cache: false // dynamic load file which not cached on initialization.
|
26
|
-
// },
|
27
|
-
// "StaticMiddleware": false,
|
15
|
+
"TraceMiddleware": {
|
16
|
+
HeaderName: 'X-Request-Id',
|
17
|
+
IdFactory: uuid_1.v4,
|
18
|
+
},
|
28
19
|
"PayloadMiddleware": {
|
29
20
|
"extTypes": {
|
30
21
|
"json": ['application/json'],
|
@@ -37,7 +28,7 @@ exports.default = {
|
|
37
28
|
"encoding": 'utf-8',
|
38
29
|
"multiples": true,
|
39
30
|
"keepExtensions": true
|
40
|
-
}
|
31
|
+
},
|
41
32
|
}
|
42
33
|
};
|
43
34
|
//# sourceMappingURL=middleware.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/config/middleware.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/config/middleware.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,+BAAoC;AAEpC,kBAAe;IACX,2EAA2E;IAC3E,oCAAoC;IACpC,IAAI,EAAE,EAAE;IACR,MAAM,EAAE;QACJ,iBAAiB,EAAE;YACf,UAAU,EAAE,cAAc;YAC1B,SAAS,EAAE,SAAM;SACpB;QACD,mBAAmB,EAAE;YACjB,UAAU,EAAE;gBACR,MAAM,EAAE,CAAC,kBAAkB,CAAC;gBAC5B,MAAM,EAAE,CAAC,mCAAmC,CAAC;gBAC7C,MAAM,EAAE,CAAC,YAAY,CAAC;gBACtB,WAAW,EAAE,CAAC,qBAAqB,CAAC;gBACpC,KAAK,EAAE,CAAC,UAAU,CAAC;aACtB;YACD,OAAO,EAAE,MAAM;YACf,UAAU,EAAE,OAAO;YACnB,WAAW,EAAE,IAAI;YACjB,gBAAgB,EAAE,IAAI;SACzB;KACJ;CACJ,CAAC"}
|
package/dist/config/router.js
CHANGED
@@ -8,21 +8,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
*/
|
9
9
|
exports.default = {
|
10
10
|
// prefix: string;
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
// methods
|
15
|
-
// routerPath
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
// sensitive
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
// strict
|
11
|
+
/**
|
12
|
+
* Methods which should be supported by the router.
|
13
|
+
*/
|
14
|
+
// methods?: string[];
|
15
|
+
// routerPath?: string;
|
16
|
+
/**
|
17
|
+
* Whether or not routing should be case-sensitive.
|
18
|
+
*/
|
19
|
+
// sensitive?: boolean;
|
20
|
+
/**
|
21
|
+
* Whether or not routes should matched strictly.
|
22
|
+
*
|
23
|
+
* If strict matching is enabled, the trailing slash is taken into
|
24
|
+
* account when matching routes.
|
25
|
+
*/
|
26
|
+
// strict?: boolean;
|
27
|
+
/**
|
28
|
+
* Other extended configuration
|
29
|
+
*/
|
30
|
+
// ext?: any;
|
27
31
|
};
|
28
32
|
//# sourceMappingURL=router.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/config/router.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,kBAAe;AACX,kBAAkB;
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/config/router.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,kBAAe;AACX,kBAAkB;AAElB;;GAEG;AACH,sBAAsB;AAEtB,uBAAuB;AAEvB;;GAEG;AACH,uBAAuB;AAEvB;;;;;GAKG;AACH,oBAAoB;AAEpB;;GAEG;AACH,aAAa;CAChB,CAAC"}
|
@@ -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":";;;AAAA;;;;;GAKG;AACH,2CAAwC;AAKxC;;;;;;GAMG;AACH,MAAa,cAAc;IAMvB;;;;;OAKG;IACH,YAAsB,GAAkB;QACpC,IAAI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