koatty 3.11.4-2 → 3.11.7
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/.rollup.config.js +59 -62
- package/.vscode/launch.json +41 -41
- package/CHANGELOG.md +365 -349
- package/LICENSE +29 -29
- package/README.md +179 -168
- package/dist/LICENSE +29 -29
- package/dist/README.md +179 -168
- package/dist/index.d.ts +5 -128
- package/dist/index.js +328 -459
- package/dist/index.mjs +322 -435
- package/dist/package.json +106 -99
- package/package.json +106 -99
- package/tsconfig.test.json +14 -14
- package/.vscode/settings.json +0 -5
package/dist/README.md
CHANGED
@@ -1,168 +1,179 @@
|
|
1
|
-
# koatty
|
2
|
-
|
3
|
-
Koa2 + Typescript + IOC = koatty.
|
4
|
-
|
5
|
-
Use Typescript's decorator implement IOC and AOP.
|
6
|
-
|
7
|
-
[](https://www.npmjs.com/package/koatty)[](https://npmcharts.com/compare/koatty?minimal=true)
|
8
|
-
|
9
|
-
## New features
|
10
|
-
|
11
|
-
* HTTP、HTTPS、HTTP2、gRPC、WebSocket server
|
12
|
-
* Support loading environment configuration, parsing command line parameters (process.argv) and environment variables (process.env)
|
13
|
-
* `@ExceptionHandler()` Register global exception handling
|
14
|
-
* graceful shutdown and pre-exit event
|
15
|
-
* custom decorator based on app events
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
```
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
import {
|
63
|
-
|
64
|
-
import {
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
});
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
"
|
151
|
-
"
|
152
|
-
"
|
153
|
-
"
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
1
|
+
# koatty
|
2
|
+
|
3
|
+
Koa2 + Typescript + IOC = koatty.
|
4
|
+
|
5
|
+
Use Typescript's decorator implement IOC and AOP.
|
6
|
+
|
7
|
+
[](https://www.npmjs.com/package/koatty)[](https://npmcharts.com/compare/koatty?minimal=true)
|
8
|
+
|
9
|
+
## New features
|
10
|
+
|
11
|
+
* HTTP、HTTPS、HTTP2、gRPC、WebSocket server.✔️
|
12
|
+
* Support loading environment configuration, parsing command line parameters (process.argv) and environment variables (process.env).✔️
|
13
|
+
* `@ExceptionHandler()` Register global exception handling.✔️
|
14
|
+
* graceful shutdown and pre-exit event.✔️
|
15
|
+
* custom decorator based on app events.✔️
|
16
|
+
* GraphQL supporting. 💪
|
17
|
+
* OpenTelemetry . 💪
|
18
|
+
|
19
|
+
|
20
|
+
## Documentation
|
21
|
+
|
22
|
+
[koatty_doc_CN](https://koatty.org/) (In progress💪)
|
23
|
+
|
24
|
+
|
25
|
+
## Installation CLI tools
|
26
|
+
|
27
|
+
```shell
|
28
|
+
npm i -g koatty_cli
|
29
|
+
```
|
30
|
+
|
31
|
+
## Quick Start
|
32
|
+
|
33
|
+
### 1.Create Project
|
34
|
+
|
35
|
+
```shell
|
36
|
+
kt new projectName
|
37
|
+
|
38
|
+
```
|
39
|
+
|
40
|
+
### 2. Install deps
|
41
|
+
|
42
|
+
```
|
43
|
+
cd ./projectName
|
44
|
+
|
45
|
+
npm i
|
46
|
+
```
|
47
|
+
|
48
|
+
### 3. Start up
|
49
|
+
|
50
|
+
```
|
51
|
+
npm run dev
|
52
|
+
|
53
|
+
// or
|
54
|
+
npm start
|
55
|
+
```
|
56
|
+
|
57
|
+
## Code style
|
58
|
+
|
59
|
+
default Controller:
|
60
|
+
|
61
|
+
```javascript
|
62
|
+
import { Controller, Autowired, GetMapping, RequestBody, PathVariable,
|
63
|
+
PostMapping, RequestMapping, RequestMethod, Valid, Output } from "koatty";
|
64
|
+
import { TestDTO } from "../model/dto/TestDTO";
|
65
|
+
import { TestService } from "../service/TestService";
|
66
|
+
import { App } from "../App";
|
67
|
+
|
68
|
+
@Controller()
|
69
|
+
export class IndexController {
|
70
|
+
app: App;
|
71
|
+
ctx: KoattyContext;
|
72
|
+
|
73
|
+
@Autowired()
|
74
|
+
private testService: TestService;
|
75
|
+
|
76
|
+
/**
|
77
|
+
* constructor
|
78
|
+
*
|
79
|
+
*/
|
80
|
+
constructor(ctx: KoattyContext) {
|
81
|
+
this.ctx = ctx;
|
82
|
+
}
|
83
|
+
|
84
|
+
@GetMapping('/')
|
85
|
+
index() {
|
86
|
+
return Output.ok("Hello, koatty!");
|
87
|
+
}
|
88
|
+
|
89
|
+
@RequestMapping("/:name", RequestMethod.ALL)
|
90
|
+
async default(@PathVariable("name") @Valid("IsNotEmpty") name: string) {
|
91
|
+
try {
|
92
|
+
const info = await this.testService.sayHello(name);
|
93
|
+
return Output.ok(this.ctx, "success", info);
|
94
|
+
} catch (err: Error) {
|
95
|
+
return Output.fail(this.ctx, err.message));
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
@PostMapping("/test")
|
100
|
+
@Validated() //need DTOClass
|
101
|
+
test(@RequestParam() params: TestDTO) {
|
102
|
+
return Output.ok(this.ctx, "test", params);
|
103
|
+
}
|
104
|
+
}
|
105
|
+
```
|
106
|
+
|
107
|
+
## How to do Unit Testing
|
108
|
+
|
109
|
+
>only support `jest` UT framework now
|
110
|
+
|
111
|
+
```javascript
|
112
|
+
import request from 'supertest';
|
113
|
+
import { ExecBootStrap } from 'koatty';
|
114
|
+
import { App } from '../src/App';
|
115
|
+
|
116
|
+
describe('UT example', () => {
|
117
|
+
|
118
|
+
let app: KoattyApplication;
|
119
|
+
beforeAll(async () => {
|
120
|
+
jest.useFakeTimers();
|
121
|
+
// test env
|
122
|
+
process.env.KOATTY_ENV = 'ts-node';
|
123
|
+
app = await ExecBootStrap()(App);
|
124
|
+
// app.use(async (ctx: any) => {
|
125
|
+
// ctx.body = 'Hello, World!';
|
126
|
+
// });
|
127
|
+
});
|
128
|
+
|
129
|
+
afterAll(done => {
|
130
|
+
done();
|
131
|
+
jest.clearAllMocks();
|
132
|
+
});
|
133
|
+
|
134
|
+
it('request', async () => {
|
135
|
+
const res = await request(app.callback()).get('/');
|
136
|
+
expect(res.status).toBe(200);
|
137
|
+
});
|
138
|
+
});
|
139
|
+
|
140
|
+
```
|
141
|
+
|
142
|
+
## How to debug
|
143
|
+
|
144
|
+
if you use vscode , edit the `.vscode/launch.json` , like this:
|
145
|
+
```
|
146
|
+
{
|
147
|
+
"version": "0.2.0",
|
148
|
+
"configurations": [
|
149
|
+
{
|
150
|
+
"type": "node",
|
151
|
+
"request": "launch",
|
152
|
+
"name": "TS Program",
|
153
|
+
"args": [
|
154
|
+
"${workspaceRoot}/src/App.ts"
|
155
|
+
],
|
156
|
+
"runtimeArgs": [
|
157
|
+
"--nolazy",
|
158
|
+
"-r",
|
159
|
+
"ts-node/register"
|
160
|
+
],
|
161
|
+
"sourceMaps": true,
|
162
|
+
"cwd": "${workspaceRoot}",
|
163
|
+
"protocol": "inspector",
|
164
|
+
"outputCapture": "std",
|
165
|
+
"internalConsoleOptions": "neverOpen"
|
166
|
+
}
|
167
|
+
]
|
168
|
+
}
|
169
|
+
```
|
170
|
+
Select `TS Program` to debug run. Try to call `http://localhost:3000/` .
|
171
|
+
|
172
|
+
## Example
|
173
|
+
|
174
|
+
Check out the [quick start example][quick-example].
|
175
|
+
|
176
|
+
[quick-example]: https://github.com/Koatty/koatty_template
|
177
|
+
|
178
|
+
|
179
|
+
|
package/dist/index.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
/*!
|
2
2
|
* @Author: richen
|
3
|
-
* @Date: 2024-
|
3
|
+
* @Date: 2024-12-06 09:38:35
|
4
4
|
* @License: BSD (3-Clause)
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
6
6
|
* @HomePage: https://koatty.org/
|
@@ -9,20 +9,15 @@ import { AppEvent } from 'koatty_core';
|
|
9
9
|
import { Config } from 'koatty_config';
|
10
10
|
import { EventHookFunc } from 'koatty_core';
|
11
11
|
import { Helper } from 'koatty_lib';
|
12
|
-
import {
|
13
|
-
import { Koatty } from 'koatty_core';
|
14
|
-
import { KoattyContext } from 'koatty_core';
|
12
|
+
import { KoattyApplication } from 'koatty_core';
|
15
13
|
import { Logger as Logger_2 } from 'koatty_logger';
|
16
|
-
import { Middleware as Middleware_2 } from 'koa';
|
17
|
-
import { Next } from 'koa';
|
18
|
-
import { Value } from 'koatty_config';
|
19
14
|
|
20
15
|
/**
|
21
16
|
* @description: bind App event hook func
|
22
17
|
* example:
|
23
18
|
* export function TestDecorator(): ClassDecorator {
|
24
19
|
* return (target: Function) => {
|
25
|
-
* BindEventHook(AppEvent.appBoot, (app:
|
20
|
+
* BindEventHook(AppEvent.appBoot, (app: KoattyApplication) => {
|
26
21
|
* // todo
|
27
22
|
* return Promise.resolve();
|
28
23
|
* }, target)
|
@@ -36,7 +31,7 @@ import { Value } from 'koatty_config';
|
|
36
31
|
export declare function BindEventHook(eventName: AppEvent, eventFunc: EventHookFunc, target: any): void;
|
37
32
|
|
38
33
|
/**
|
39
|
-
* Bootstrap application
|
34
|
+
* Bootstrap application decorator
|
40
35
|
*
|
41
36
|
* @export
|
42
37
|
* @param {Function} [bootFunc]
|
@@ -44,15 +39,6 @@ export declare function BindEventHook(eventName: AppEvent, eventFunc: EventHookF
|
|
44
39
|
*/
|
45
40
|
export declare function Bootstrap(bootFunc?: Function): ClassDecorator;
|
46
41
|
|
47
|
-
/**
|
48
|
-
* Indicates that an decorated class is a "component".
|
49
|
-
*
|
50
|
-
* @export
|
51
|
-
* @param {string} [identifier] component name
|
52
|
-
* @returns {ClassDecorator}
|
53
|
-
*/
|
54
|
-
export declare function Component(identifier?: string): ClassDecorator;
|
55
|
-
|
56
42
|
/**
|
57
43
|
* Define project scan path
|
58
44
|
*
|
@@ -73,15 +59,6 @@ export { Config }
|
|
73
59
|
*/
|
74
60
|
export declare function ConfigurationScan(scanPath?: string | string[]): ClassDecorator;
|
75
61
|
|
76
|
-
/**
|
77
|
-
* Indicates that an decorated class is a "controller".
|
78
|
-
*
|
79
|
-
* @export
|
80
|
-
* @param {string} [path] controller router path
|
81
|
-
* @returns {ClassDecorator}
|
82
|
-
*/
|
83
|
-
export declare function Controller(path?: string): ClassDecorator;
|
84
|
-
|
85
62
|
/**
|
86
63
|
* Actively perform dependency injection
|
87
64
|
* Parse the decorator, return the instantiated app.
|
@@ -89,116 +66,16 @@ export declare function Controller(path?: string): ClassDecorator;
|
|
89
66
|
* @param {Function} [bootFunc] callback function
|
90
67
|
* @returns
|
91
68
|
*/
|
92
|
-
export declare function ExecBootStrap(bootFunc?: Function): (target: any) => Promise<
|
69
|
+
export declare function ExecBootStrap(bootFunc?: Function): (target: any) => Promise<KoattyApplication>;
|
93
70
|
|
94
71
|
export { Helper }
|
95
72
|
|
96
|
-
/**
|
97
|
-
* Interface for Controller
|
98
|
-
*/
|
99
|
-
export declare interface IController {
|
100
|
-
readonly app: Koatty;
|
101
|
-
readonly ctx: KoattyContext;
|
102
|
-
}
|
103
|
-
|
104
|
-
/**
|
105
|
-
* Interface for Middleware
|
106
|
-
*/
|
107
|
-
export declare interface IMiddleware {
|
108
|
-
run: (options: any, app: Koatty) => (ctx: KoattyContext, next: Next) => Promise<any>;
|
109
|
-
}
|
110
|
-
|
111
|
-
/**
|
112
|
-
* check is implements Aspect Interface
|
113
|
-
* @param cls
|
114
|
-
* @returns
|
115
|
-
*/
|
116
|
-
export declare function implementsAspectInterface(cls: any): cls is IAspect;
|
117
|
-
|
118
|
-
/**
|
119
|
-
* check is implements Controller Interface
|
120
|
-
* @param cls
|
121
|
-
* @returns
|
122
|
-
*/
|
123
|
-
export declare function implementsControllerInterface(cls: any): cls is IController;
|
124
|
-
|
125
|
-
/**
|
126
|
-
* check is implements Middleware Interface
|
127
|
-
* @param cls
|
128
|
-
* @returns
|
129
|
-
*/
|
130
|
-
export declare function implementsMiddlewareInterface(cls: any): cls is IMiddleware;
|
131
|
-
|
132
|
-
/**
|
133
|
-
* check is implements Plugin Interface
|
134
|
-
* @param cls
|
135
|
-
* @returns
|
136
|
-
*/
|
137
|
-
export declare function implementsPluginInterface(cls: any): cls is IPlugin;
|
138
|
-
|
139
|
-
/**
|
140
|
-
* check is implements Service Interface
|
141
|
-
* @param cls
|
142
|
-
* @returns
|
143
|
-
*/
|
144
|
-
export declare function implementsServiceInterface(cls: any): cls is IService;
|
145
|
-
|
146
|
-
/**
|
147
|
-
* Interface for Plugin
|
148
|
-
*/
|
149
|
-
export declare interface IPlugin {
|
150
|
-
run: (options: any, app: Koatty) => Promise<any>;
|
151
|
-
}
|
152
|
-
|
153
|
-
/**
|
154
|
-
* Interface for Service
|
155
|
-
*/
|
156
|
-
export declare interface IService {
|
157
|
-
readonly app: Koatty;
|
158
|
-
}
|
159
|
-
|
160
|
-
/**
|
161
|
-
* Interface for Middleware
|
162
|
-
*/
|
163
|
-
export declare interface KoattyMiddleware extends Middleware_2 {
|
164
|
-
}
|
165
|
-
|
166
73
|
export declare const Logger: Logger_2;
|
167
74
|
|
168
|
-
/**
|
169
|
-
* Indicates that an decorated class is a "middleware".
|
170
|
-
*
|
171
|
-
* @export
|
172
|
-
* @param {string} [identifier] class name
|
173
|
-
* @returns {ClassDecorator}
|
174
|
-
*/
|
175
|
-
export declare function Middleware(identifier?: string): ClassDecorator;
|
176
|
-
|
177
|
-
/**
|
178
|
-
* Indicates that an decorated class is a "plugin".
|
179
|
-
*
|
180
|
-
* @export
|
181
|
-
* @param {string} [identifier] class name
|
182
|
-
* @returns {ClassDecorator}
|
183
|
-
*/
|
184
|
-
export declare function Plugin(identifier?: string): ClassDecorator;
|
185
|
-
|
186
|
-
/**
|
187
|
-
* Indicates that an decorated class is a "service".
|
188
|
-
*
|
189
|
-
* @export
|
190
|
-
* @param {string} [identifier] class name
|
191
|
-
* @returns {ClassDecorator}
|
192
|
-
*/
|
193
|
-
export declare function Service(identifier?: string): ClassDecorator;
|
194
|
-
|
195
|
-
export { Value }
|
196
|
-
|
197
75
|
|
198
76
|
export * from "koatty_container";
|
199
77
|
export * from "koatty_core";
|
200
78
|
export * from "koatty_exception";
|
201
79
|
export * from "koatty_router";
|
202
|
-
export * from "koatty_serve";
|
203
80
|
|
204
81
|
export { }
|