@zentjs/zentjs 0.0.3 → 0.0.5
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 +26 -0
- package/package.json +15 -10
- package/src/core/application.mjs +12 -8
- package/src/zent.d.mts +726 -0
- /package/src/{index.mjs → zent.mjs} +0 -0
package/README.md
CHANGED
|
@@ -35,6 +35,31 @@ app.get('/', (ctx) => {
|
|
|
35
35
|
app.listen({ port: 3000 });
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
+
## TypeScript (state + decorators)
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import { zent } from '@zentjs/zentjs';
|
|
42
|
+
|
|
43
|
+
type AppState = {
|
|
44
|
+
requestId?: string;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const app = zent<AppState>();
|
|
48
|
+
|
|
49
|
+
const appWithAuth = app.decorate('authenticate', async (ctx) => {
|
|
50
|
+
const token = ctx.req.get('authorization');
|
|
51
|
+
if (!token) {
|
|
52
|
+
ctx.res.status(401).json({ message: 'Unauthorized' });
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
appWithAuth.get('/me', async (ctx) => {
|
|
57
|
+
ctx.state.requestId = crypto.randomUUID();
|
|
58
|
+
await ctx.app.authenticate(ctx);
|
|
59
|
+
return ctx.res.json({ requestId: ctx.state.requestId });
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
38
63
|
## Scripts úteis (desenvolvimento)
|
|
39
64
|
|
|
40
65
|
```bash
|
|
@@ -60,6 +85,7 @@ A documentação detalhada foi separada na pasta `docs/`:
|
|
|
60
85
|
node examples/hello-world.mjs
|
|
61
86
|
node examples/rest-api.mjs
|
|
62
87
|
node examples/with-plugins.mjs
|
|
88
|
+
node --experimental-strip-types examples/typed-app.ts
|
|
63
89
|
```
|
|
64
90
|
|
|
65
91
|
## Licença
|
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zentjs/zentjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Framework web minimalista e performático para Node.js",
|
|
5
5
|
"license": "BSD-3-Clause",
|
|
6
|
-
"author": "Walber Vaz",
|
|
6
|
+
"author": "Walber Vaz <wvs.walber@gmail.com>",
|
|
7
7
|
"type": "module",
|
|
8
|
-
"main": "./src/
|
|
8
|
+
"main": "./src/zent.mjs",
|
|
9
|
+
"types": "./src/zent.d.ts",
|
|
9
10
|
"exports": {
|
|
10
|
-
".":
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./src/zent.d.ts",
|
|
13
|
+
"import": "./src/zent.mjs"
|
|
14
|
+
}
|
|
11
15
|
},
|
|
12
16
|
"files": [
|
|
13
17
|
"src",
|
|
18
|
+
"src/zent.d.ts",
|
|
14
19
|
"README.md",
|
|
15
20
|
"LICENSE"
|
|
16
21
|
],
|
|
@@ -19,12 +24,12 @@
|
|
|
19
24
|
},
|
|
20
25
|
"repository": {
|
|
21
26
|
"type": "git",
|
|
22
|
-
"url": "git+https://github.com/
|
|
27
|
+
"url": "git+https://github.com/zentjs/zentjs.git"
|
|
23
28
|
},
|
|
24
29
|
"bugs": {
|
|
25
|
-
"url": "https://github.com/
|
|
30
|
+
"url": "https://github.com/zentjs/zentjs/issues"
|
|
26
31
|
},
|
|
27
|
-
"homepage": "https://github.com/
|
|
32
|
+
"homepage": "https://github.com/zentjs/zentjs#readme",
|
|
28
33
|
"publishConfig": {
|
|
29
34
|
"access": "public"
|
|
30
35
|
},
|
|
@@ -62,8 +67,8 @@
|
|
|
62
67
|
]
|
|
63
68
|
},
|
|
64
69
|
"devDependencies": {
|
|
65
|
-
"@commitlint/cli": "^20.4.
|
|
66
|
-
"@commitlint/config-conventional": "^20.4.
|
|
70
|
+
"@commitlint/cli": "^20.4.3",
|
|
71
|
+
"@commitlint/config-conventional": "^20.4.3",
|
|
67
72
|
"@eslint/js": "10.0.1",
|
|
68
73
|
"@types/node": "^25.3.3",
|
|
69
74
|
"@vitest/coverage-v8": "^4.0.18",
|
|
@@ -77,7 +82,7 @@
|
|
|
77
82
|
"globals": "17.4.0",
|
|
78
83
|
"husky": "^9.1.7",
|
|
79
84
|
"jiti": "^2.6.1",
|
|
80
|
-
"lint-staged": "^16.3.
|
|
85
|
+
"lint-staged": "^16.3.2",
|
|
81
86
|
"prettier": "3.8.1",
|
|
82
87
|
"supertest": "^7.2.2",
|
|
83
88
|
"vitest": "4.0.18"
|
package/src/core/application.mjs
CHANGED
|
@@ -518,11 +518,8 @@ export class Zent {
|
|
|
518
518
|
const registerMethod =
|
|
519
519
|
(method) =>
|
|
520
520
|
(path, handler, routeOpts = {}) => {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
handler,
|
|
524
|
-
withScopeRouteOpts(routeOpts)
|
|
525
|
-
);
|
|
521
|
+
parent[method](prefix + path, handler, withScopeRouteOpts(routeOpts));
|
|
522
|
+
return scope;
|
|
526
523
|
};
|
|
527
524
|
|
|
528
525
|
const scopeRoute = (def) => {
|
|
@@ -531,12 +528,13 @@ export class Zent {
|
|
|
531
528
|
hooks: def.hooks,
|
|
532
529
|
});
|
|
533
530
|
|
|
534
|
-
|
|
531
|
+
parent.route({
|
|
535
532
|
...def,
|
|
536
533
|
path: prefix + def.path,
|
|
537
534
|
middlewares: routeOpts.middlewares,
|
|
538
535
|
hooks: routeOpts.hooks,
|
|
539
536
|
});
|
|
537
|
+
return scope;
|
|
540
538
|
};
|
|
541
539
|
|
|
542
540
|
const scopeAll = (path, handler, routeOpts = {}) => {
|
|
@@ -576,8 +574,14 @@ export class Zent {
|
|
|
576
574
|
group: scopeGroup,
|
|
577
575
|
use: scopeUse,
|
|
578
576
|
addHook: scopeAddHook,
|
|
579
|
-
setErrorHandler: (fn) =>
|
|
580
|
-
|
|
577
|
+
setErrorHandler: (fn) => {
|
|
578
|
+
parent.setErrorHandler(fn);
|
|
579
|
+
return scope;
|
|
580
|
+
},
|
|
581
|
+
setNotFoundHandler: (fn) => {
|
|
582
|
+
parent.setNotFoundHandler(fn);
|
|
583
|
+
return scope;
|
|
584
|
+
},
|
|
581
585
|
decorate: scopeDecorate,
|
|
582
586
|
hasDecorator: scopeHasDecorator,
|
|
583
587
|
register: (fn, pluginOpts) => {
|
package/src/zent.d.mts
ADDED
|
@@ -0,0 +1,726 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IncomingHttpHeaders,
|
|
3
|
+
IncomingMessage,
|
|
4
|
+
ServerResponse,
|
|
5
|
+
} from 'node:http';
|
|
6
|
+
|
|
7
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
8
|
+
type AnyState = Record<string, unknown>;
|
|
9
|
+
type AnyDecorators = Record<string, unknown>;
|
|
10
|
+
type Merge<TBase, TExtra> = Omit<TBase, keyof TExtra> & TExtra;
|
|
11
|
+
|
|
12
|
+
export type HookPhase =
|
|
13
|
+
| 'onRequest'
|
|
14
|
+
| 'preParsing'
|
|
15
|
+
| 'preValidation'
|
|
16
|
+
| 'preHandler'
|
|
17
|
+
| 'onSend'
|
|
18
|
+
| 'onResponse'
|
|
19
|
+
| 'onError';
|
|
20
|
+
|
|
21
|
+
export type NextFunction = () => MaybePromise<void>;
|
|
22
|
+
|
|
23
|
+
export interface InjectOptions {
|
|
24
|
+
method?: string;
|
|
25
|
+
url?: string;
|
|
26
|
+
headers?: Record<string, string>;
|
|
27
|
+
body?: string | object;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface InjectResponse {
|
|
31
|
+
statusCode: number;
|
|
32
|
+
headers: Record<string, string | number | string[]>;
|
|
33
|
+
body: string;
|
|
34
|
+
json<T = unknown>(): T;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ZentOptions {
|
|
38
|
+
ignoreTrailingSlash?: boolean;
|
|
39
|
+
caseSensitive?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ListenOptions {
|
|
43
|
+
port?: number;
|
|
44
|
+
host?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type ListenCallback = (err: Error | null, address?: string) => void;
|
|
48
|
+
|
|
49
|
+
export type AppInstance<
|
|
50
|
+
TState extends AnyState = AnyState,
|
|
51
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
52
|
+
> = Zent<TState, TDecorators> & TDecorators;
|
|
53
|
+
|
|
54
|
+
export type PluginScopeInstance<
|
|
55
|
+
TState extends AnyState = AnyState,
|
|
56
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
57
|
+
> = ZentPluginScope<TState, TDecorators> & TDecorators;
|
|
58
|
+
|
|
59
|
+
export type RouteHandler<
|
|
60
|
+
TState extends AnyState = AnyState,
|
|
61
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
62
|
+
> = (ctx: Context<TState, TDecorators>) => MaybePromise<unknown>;
|
|
63
|
+
|
|
64
|
+
export type Middleware<
|
|
65
|
+
TState extends AnyState = AnyState,
|
|
66
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
67
|
+
> = (
|
|
68
|
+
ctx: Context<TState, TDecorators>,
|
|
69
|
+
next: NextFunction
|
|
70
|
+
) => MaybePromise<void>;
|
|
71
|
+
|
|
72
|
+
export type OnRequestHook<
|
|
73
|
+
TState extends AnyState = AnyState,
|
|
74
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
75
|
+
> = (ctx: Context<TState, TDecorators>) => MaybePromise<void>;
|
|
76
|
+
|
|
77
|
+
export type PreParsingHook<
|
|
78
|
+
TState extends AnyState = AnyState,
|
|
79
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
80
|
+
> = (ctx: Context<TState, TDecorators>) => MaybePromise<void>;
|
|
81
|
+
|
|
82
|
+
export type PreValidationHook<
|
|
83
|
+
TState extends AnyState = AnyState,
|
|
84
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
85
|
+
> = (ctx: Context<TState, TDecorators>) => MaybePromise<void>;
|
|
86
|
+
|
|
87
|
+
export type PreHandlerHook<
|
|
88
|
+
TState extends AnyState = AnyState,
|
|
89
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
90
|
+
> = (ctx: Context<TState, TDecorators>) => MaybePromise<void>;
|
|
91
|
+
|
|
92
|
+
export type OnResponseHook<
|
|
93
|
+
TState extends AnyState = AnyState,
|
|
94
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
95
|
+
> = (ctx: Context<TState, TDecorators>) => MaybePromise<void>;
|
|
96
|
+
|
|
97
|
+
export type OnErrorHook<
|
|
98
|
+
TState extends AnyState = AnyState,
|
|
99
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
100
|
+
> = (ctx: Context<TState, TDecorators>, error: Error) => MaybePromise<void>;
|
|
101
|
+
|
|
102
|
+
export type OnSendHook<
|
|
103
|
+
TState extends AnyState = AnyState,
|
|
104
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
105
|
+
> = (
|
|
106
|
+
ctx: Context<TState, TDecorators>,
|
|
107
|
+
payload: unknown
|
|
108
|
+
) => MaybePromise<unknown>;
|
|
109
|
+
|
|
110
|
+
export interface RouteHooks<
|
|
111
|
+
TState extends AnyState = AnyState,
|
|
112
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
113
|
+
> {
|
|
114
|
+
onRequest?:
|
|
115
|
+
| OnRequestHook<TState, TDecorators>
|
|
116
|
+
| OnRequestHook<TState, TDecorators>[];
|
|
117
|
+
preParsing?:
|
|
118
|
+
| PreParsingHook<TState, TDecorators>
|
|
119
|
+
| PreParsingHook<TState, TDecorators>[];
|
|
120
|
+
preValidation?:
|
|
121
|
+
| PreValidationHook<TState, TDecorators>
|
|
122
|
+
| PreValidationHook<TState, TDecorators>[];
|
|
123
|
+
preHandler?:
|
|
124
|
+
| PreHandlerHook<TState, TDecorators>
|
|
125
|
+
| PreHandlerHook<TState, TDecorators>[];
|
|
126
|
+
onSend?: OnSendHook<TState, TDecorators> | OnSendHook<TState, TDecorators>[];
|
|
127
|
+
onResponse?:
|
|
128
|
+
| OnResponseHook<TState, TDecorators>
|
|
129
|
+
| OnResponseHook<TState, TDecorators>[];
|
|
130
|
+
onError?:
|
|
131
|
+
| OnErrorHook<TState, TDecorators>
|
|
132
|
+
| OnErrorHook<TState, TDecorators>[];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface RouteOptions<
|
|
136
|
+
TState extends AnyState = AnyState,
|
|
137
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
138
|
+
> {
|
|
139
|
+
middlewares?:
|
|
140
|
+
| Middleware<TState, TDecorators>
|
|
141
|
+
| Middleware<TState, TDecorators>[];
|
|
142
|
+
hooks?: RouteHooks<TState, TDecorators>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface RouteDefinition<
|
|
146
|
+
TState extends AnyState = AnyState,
|
|
147
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
148
|
+
> extends RouteOptions<TState, TDecorators> {
|
|
149
|
+
method: string;
|
|
150
|
+
path: string;
|
|
151
|
+
handler: RouteHandler<TState, TDecorators>;
|
|
152
|
+
[key: string]: unknown;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export interface GroupOptions<
|
|
156
|
+
TState extends AnyState = AnyState,
|
|
157
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
158
|
+
> extends RouteOptions<TState, TDecorators> {}
|
|
159
|
+
|
|
160
|
+
export interface GroupApi<
|
|
161
|
+
TState extends AnyState = AnyState,
|
|
162
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
163
|
+
> {
|
|
164
|
+
route(definition: RouteDefinition<TState, TDecorators>): void;
|
|
165
|
+
all(
|
|
166
|
+
path: string,
|
|
167
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
168
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
169
|
+
): void;
|
|
170
|
+
group(
|
|
171
|
+
prefix: string,
|
|
172
|
+
callback: (group: GroupApi<TState, TDecorators>) => void
|
|
173
|
+
): void;
|
|
174
|
+
group(
|
|
175
|
+
prefix: string,
|
|
176
|
+
opts: GroupOptions<TState, TDecorators> | null,
|
|
177
|
+
callback: (group: GroupApi<TState, TDecorators>) => void
|
|
178
|
+
): void;
|
|
179
|
+
get(
|
|
180
|
+
path: string,
|
|
181
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
182
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
183
|
+
): void;
|
|
184
|
+
post(
|
|
185
|
+
path: string,
|
|
186
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
187
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
188
|
+
): void;
|
|
189
|
+
put(
|
|
190
|
+
path: string,
|
|
191
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
192
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
193
|
+
): void;
|
|
194
|
+
patch(
|
|
195
|
+
path: string,
|
|
196
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
197
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
198
|
+
): void;
|
|
199
|
+
delete(
|
|
200
|
+
path: string,
|
|
201
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
202
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
203
|
+
): void;
|
|
204
|
+
head(
|
|
205
|
+
path: string,
|
|
206
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
207
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
208
|
+
): void;
|
|
209
|
+
options(
|
|
210
|
+
path: string,
|
|
211
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
212
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
213
|
+
): void;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export interface PluginOptions {
|
|
217
|
+
[key: string]: unknown;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export interface ZentPluginScope<
|
|
221
|
+
TState extends AnyState = AnyState,
|
|
222
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
223
|
+
> {
|
|
224
|
+
route(definition: RouteDefinition<TState, TDecorators>): this;
|
|
225
|
+
all(
|
|
226
|
+
path: string,
|
|
227
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
228
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
229
|
+
): this;
|
|
230
|
+
group(
|
|
231
|
+
prefix: string,
|
|
232
|
+
callback: (group: ZentPluginScope<TState, TDecorators>) => void
|
|
233
|
+
): this;
|
|
234
|
+
group(
|
|
235
|
+
prefix: string,
|
|
236
|
+
opts: GroupOptions<TState, TDecorators> | null,
|
|
237
|
+
callback: (group: ZentPluginScope<TState, TDecorators>) => void
|
|
238
|
+
): this;
|
|
239
|
+
get(
|
|
240
|
+
path: string,
|
|
241
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
242
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
243
|
+
): this;
|
|
244
|
+
post(
|
|
245
|
+
path: string,
|
|
246
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
247
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
248
|
+
): this;
|
|
249
|
+
put(
|
|
250
|
+
path: string,
|
|
251
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
252
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
253
|
+
): this;
|
|
254
|
+
patch(
|
|
255
|
+
path: string,
|
|
256
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
257
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
258
|
+
): this;
|
|
259
|
+
delete(
|
|
260
|
+
path: string,
|
|
261
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
262
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
263
|
+
): this;
|
|
264
|
+
head(
|
|
265
|
+
path: string,
|
|
266
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
267
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
268
|
+
): this;
|
|
269
|
+
options(
|
|
270
|
+
path: string,
|
|
271
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
272
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
273
|
+
): this;
|
|
274
|
+
use(middleware: Middleware<TState, TDecorators>): this;
|
|
275
|
+
use(prefix: string, middleware: Middleware<TState, TDecorators>): this;
|
|
276
|
+
addHook(phase: 'onRequest', fn: OnRequestHook<TState, TDecorators>): this;
|
|
277
|
+
addHook(phase: 'preParsing', fn: PreParsingHook<TState, TDecorators>): this;
|
|
278
|
+
addHook(
|
|
279
|
+
phase: 'preValidation',
|
|
280
|
+
fn: PreValidationHook<TState, TDecorators>
|
|
281
|
+
): this;
|
|
282
|
+
addHook(phase: 'preHandler', fn: PreHandlerHook<TState, TDecorators>): this;
|
|
283
|
+
addHook(phase: 'onSend', fn: OnSendHook<TState, TDecorators>): this;
|
|
284
|
+
addHook(phase: 'onResponse', fn: OnResponseHook<TState, TDecorators>): this;
|
|
285
|
+
addHook(phase: 'onError', fn: OnErrorHook<TState, TDecorators>): this;
|
|
286
|
+
setErrorHandler(
|
|
287
|
+
fn: (error: Error, ctx: Context<TState, TDecorators>) => MaybePromise<void>
|
|
288
|
+
): this;
|
|
289
|
+
setNotFoundHandler(
|
|
290
|
+
fn: (ctx: Context<TState, TDecorators>) => MaybePromise<void>
|
|
291
|
+
): this;
|
|
292
|
+
decorate<TKey extends string, TValue>(
|
|
293
|
+
name: TKey,
|
|
294
|
+
value: (ctx: Context<TState, TDecorators>, ...args: unknown[]) => TValue
|
|
295
|
+
): ZentPluginScope<TState, Merge<TDecorators, Record<TKey, typeof value>>> &
|
|
296
|
+
Merge<TDecorators, Record<TKey, typeof value>>;
|
|
297
|
+
decorate<TKey extends string, TValue>(
|
|
298
|
+
name: TKey,
|
|
299
|
+
value: TValue
|
|
300
|
+
): ZentPluginScope<TState, Merge<TDecorators, Record<TKey, TValue>>> &
|
|
301
|
+
Merge<TDecorators, Record<TKey, TValue>>;
|
|
302
|
+
hasDecorator<TKey extends string>(
|
|
303
|
+
name: TKey
|
|
304
|
+
): name is TKey & keyof TDecorators;
|
|
305
|
+
register<TOptions extends PluginOptions = PluginOptions>(
|
|
306
|
+
fn: PluginFunction<TOptions, TState, TDecorators>,
|
|
307
|
+
opts?: TOptions
|
|
308
|
+
): void;
|
|
309
|
+
[key: string]: unknown;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export type PluginFunction<
|
|
313
|
+
TOptions extends PluginOptions = PluginOptions,
|
|
314
|
+
TState extends AnyState = AnyState,
|
|
315
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
316
|
+
> = (
|
|
317
|
+
app: PluginScopeInstance<TState, TDecorators>,
|
|
318
|
+
opts: TOptions
|
|
319
|
+
) => MaybePromise<void>;
|
|
320
|
+
|
|
321
|
+
export class ZentRequest {
|
|
322
|
+
constructor(raw: IncomingMessage);
|
|
323
|
+
get raw(): IncomingMessage;
|
|
324
|
+
get method(): string;
|
|
325
|
+
get url(): string;
|
|
326
|
+
get path(): string;
|
|
327
|
+
get query(): Record<string, string>;
|
|
328
|
+
get headers(): IncomingHttpHeaders;
|
|
329
|
+
get params(): Record<string, string>;
|
|
330
|
+
set params(value: Record<string, string>);
|
|
331
|
+
get ip(): string;
|
|
332
|
+
get hostname(): string;
|
|
333
|
+
get protocol(): 'http' | 'https';
|
|
334
|
+
get body(): unknown;
|
|
335
|
+
set body(value: unknown);
|
|
336
|
+
get(name: string): string | string[] | undefined;
|
|
337
|
+
is(type: string): boolean;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
export class ZentResponse {
|
|
341
|
+
constructor(raw: ServerResponse);
|
|
342
|
+
get raw(): ServerResponse;
|
|
343
|
+
get sent(): boolean;
|
|
344
|
+
get statusCode(): number;
|
|
345
|
+
status(code: number): this;
|
|
346
|
+
header(name: string, value: string | number): this;
|
|
347
|
+
type(contentType: string): this;
|
|
348
|
+
json(data: unknown): void;
|
|
349
|
+
send(data: string | Buffer): void;
|
|
350
|
+
html(data: string): void;
|
|
351
|
+
redirect(url: string, code?: number): void;
|
|
352
|
+
empty(code?: number): void;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
export class Context<
|
|
356
|
+
TState extends AnyState = AnyState,
|
|
357
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
358
|
+
> {
|
|
359
|
+
req: ZentRequest;
|
|
360
|
+
res: ZentResponse;
|
|
361
|
+
app: AppInstance<TState, TDecorators>;
|
|
362
|
+
state: TState;
|
|
363
|
+
|
|
364
|
+
constructor(
|
|
365
|
+
rawReq: IncomingMessage,
|
|
366
|
+
rawRes: ServerResponse,
|
|
367
|
+
app: Zent<TState, TDecorators>
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
export class Router<
|
|
372
|
+
TState extends AnyState = AnyState,
|
|
373
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
374
|
+
> {
|
|
375
|
+
constructor(opts?: ZentOptions);
|
|
376
|
+
route(definition: RouteDefinition<TState, TDecorators>): void;
|
|
377
|
+
find(
|
|
378
|
+
method: string,
|
|
379
|
+
path: string
|
|
380
|
+
): {
|
|
381
|
+
route: RouteDefinition<TState, TDecorators>;
|
|
382
|
+
params: Record<string, string>;
|
|
383
|
+
};
|
|
384
|
+
all(
|
|
385
|
+
path: string,
|
|
386
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
387
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
388
|
+
): void;
|
|
389
|
+
group(
|
|
390
|
+
prefix: string,
|
|
391
|
+
callback: (group: GroupApi<TState, TDecorators>) => void
|
|
392
|
+
): void;
|
|
393
|
+
group(
|
|
394
|
+
prefix: string,
|
|
395
|
+
opts: GroupOptions<TState, TDecorators> | null,
|
|
396
|
+
callback: (group: GroupApi<TState, TDecorators>) => void
|
|
397
|
+
): void;
|
|
398
|
+
get(
|
|
399
|
+
path: string,
|
|
400
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
401
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
402
|
+
): void;
|
|
403
|
+
post(
|
|
404
|
+
path: string,
|
|
405
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
406
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
407
|
+
): void;
|
|
408
|
+
put(
|
|
409
|
+
path: string,
|
|
410
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
411
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
412
|
+
): void;
|
|
413
|
+
patch(
|
|
414
|
+
path: string,
|
|
415
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
416
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
417
|
+
): void;
|
|
418
|
+
delete(
|
|
419
|
+
path: string,
|
|
420
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
421
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
422
|
+
): void;
|
|
423
|
+
head(
|
|
424
|
+
path: string,
|
|
425
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
426
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
427
|
+
): void;
|
|
428
|
+
options(
|
|
429
|
+
path: string,
|
|
430
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
431
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
432
|
+
): void;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
export const HOOK_PHASES: readonly HookPhase[];
|
|
436
|
+
|
|
437
|
+
export class Lifecycle<
|
|
438
|
+
TState extends AnyState = AnyState,
|
|
439
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
440
|
+
> {
|
|
441
|
+
constructor();
|
|
442
|
+
addHook(phase: HookPhase, fn: Function): void;
|
|
443
|
+
getHooks(phase: HookPhase): Function[];
|
|
444
|
+
hasHooks(phase: HookPhase): boolean;
|
|
445
|
+
run(
|
|
446
|
+
phase: HookPhase,
|
|
447
|
+
ctx: Context<TState, TDecorators>,
|
|
448
|
+
...args: unknown[]
|
|
449
|
+
): Promise<unknown>;
|
|
450
|
+
clone(): Lifecycle<TState, TDecorators>;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
export class ErrorHandler<
|
|
454
|
+
TState extends AnyState = AnyState,
|
|
455
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
456
|
+
> {
|
|
457
|
+
constructor();
|
|
458
|
+
setErrorHandler(
|
|
459
|
+
fn: (error: Error, ctx: Context<TState, TDecorators>) => MaybePromise<void>
|
|
460
|
+
): void;
|
|
461
|
+
handle(error: Error, ctx: Context<TState, TDecorators>): Promise<void>;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export class HttpError extends Error {
|
|
465
|
+
static showStackTrace: boolean;
|
|
466
|
+
statusCode: number;
|
|
467
|
+
error: string;
|
|
468
|
+
constructor(statusCode: number, message: string);
|
|
469
|
+
toJSON(): {
|
|
470
|
+
statusCode: number;
|
|
471
|
+
error: string;
|
|
472
|
+
message: string;
|
|
473
|
+
stack?: string;
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
export class BadRequestError extends HttpError {
|
|
478
|
+
constructor(message?: string);
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
export class UnauthorizedError extends HttpError {
|
|
482
|
+
constructor(message?: string);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
export class ForbiddenError extends HttpError {
|
|
486
|
+
constructor(message?: string);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
export class NotFoundError extends HttpError {
|
|
490
|
+
constructor(message?: string);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
export class MethodNotAllowedError extends HttpError {
|
|
494
|
+
constructor(message?: string);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
export class ConflictError extends HttpError {
|
|
498
|
+
constructor(message?: string);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
export class UnprocessableEntityError extends HttpError {
|
|
502
|
+
constructor(message?: string);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
export class TooManyRequestsError extends HttpError {
|
|
506
|
+
constructor(message?: string);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
export class InternalServerError extends HttpError {
|
|
510
|
+
constructor(message?: string);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export function compose<
|
|
514
|
+
TState extends AnyState = AnyState,
|
|
515
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
516
|
+
>(
|
|
517
|
+
middlewares: Middleware<TState, TDecorators>[]
|
|
518
|
+
): (
|
|
519
|
+
ctx: Context<TState, TDecorators>,
|
|
520
|
+
next?: RouteHandler<TState, TDecorators>
|
|
521
|
+
) => Promise<void>;
|
|
522
|
+
|
|
523
|
+
export interface BodyParserOptions {
|
|
524
|
+
limit?: number;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export function bodyParser<
|
|
528
|
+
TState extends AnyState = AnyState,
|
|
529
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
530
|
+
>(opts?: BodyParserOptions): Middleware<TState, TDecorators>;
|
|
531
|
+
|
|
532
|
+
export type CorsOriginResolver = (
|
|
533
|
+
requestOrigin: string
|
|
534
|
+
) => string | false | Promise<string | false>;
|
|
535
|
+
|
|
536
|
+
export interface CorsOptions {
|
|
537
|
+
origin?: string | string[] | CorsOriginResolver | boolean;
|
|
538
|
+
methods?: string | string[];
|
|
539
|
+
allowedHeaders?: string | string[] | null;
|
|
540
|
+
exposedHeaders?: string | string[] | null;
|
|
541
|
+
credentials?: boolean;
|
|
542
|
+
maxAge?: number | null;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export function cors<
|
|
546
|
+
TState extends AnyState = AnyState,
|
|
547
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
548
|
+
>(opts?: CorsOptions): Middleware<TState, TDecorators>;
|
|
549
|
+
|
|
550
|
+
export interface RequestMetricRecord {
|
|
551
|
+
method: string;
|
|
552
|
+
path: string;
|
|
553
|
+
statusCode: number;
|
|
554
|
+
durationMs: number;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
export interface RequestMetricsOptions<
|
|
558
|
+
TState extends AnyState = AnyState,
|
|
559
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
560
|
+
> {
|
|
561
|
+
onRecord?: (
|
|
562
|
+
record: RequestMetricRecord,
|
|
563
|
+
ctx: Context<TState, TDecorators>
|
|
564
|
+
) => MaybePromise<void>;
|
|
565
|
+
clock?: () => bigint;
|
|
566
|
+
stateKey?: string;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export function requestMetrics<
|
|
570
|
+
TState extends AnyState = AnyState,
|
|
571
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
572
|
+
>(
|
|
573
|
+
opts?: RequestMetricsOptions<TState, TDecorators>
|
|
574
|
+
): {
|
|
575
|
+
onRequest: OnRequestHook<TState, TDecorators>;
|
|
576
|
+
onResponse: OnResponseHook<TState, TDecorators>;
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
export function requestMetricsPlugin<
|
|
580
|
+
TState extends AnyState = AnyState,
|
|
581
|
+
TDecorators extends AnyDecorators = AnyDecorators,
|
|
582
|
+
>(
|
|
583
|
+
opts?: RequestMetricsOptions<TState, TDecorators>
|
|
584
|
+
): (app: ZentPluginScope<TState, TDecorators>) => Promise<void>;
|
|
585
|
+
|
|
586
|
+
export class PluginManager {
|
|
587
|
+
constructor();
|
|
588
|
+
get loaded(): boolean;
|
|
589
|
+
register<TOptions extends PluginOptions = PluginOptions>(
|
|
590
|
+
fn: PluginFunction<TOptions>,
|
|
591
|
+
opts?: TOptions
|
|
592
|
+
): void;
|
|
593
|
+
load(createScope: (opts: PluginOptions) => ZentPluginScope): Promise<void>;
|
|
594
|
+
get size(): number;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
export const HttpStatus: {
|
|
598
|
+
readonly OK: 200;
|
|
599
|
+
readonly CREATED: 201;
|
|
600
|
+
readonly NO_CONTENT: 204;
|
|
601
|
+
readonly FOUND: 302;
|
|
602
|
+
readonly BAD_REQUEST: 400;
|
|
603
|
+
readonly UNAUTHORIZED: 401;
|
|
604
|
+
readonly FORBIDDEN: 403;
|
|
605
|
+
readonly NOT_FOUND: 404;
|
|
606
|
+
readonly METHOD_NOT_ALLOWED: 405;
|
|
607
|
+
readonly CONFLICT: 409;
|
|
608
|
+
readonly UNPROCESSABLE_ENTITY: 422;
|
|
609
|
+
readonly TOO_MANY_REQUESTS: 429;
|
|
610
|
+
readonly INTERNAL_SERVER_ERROR: 500;
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
export const HttpStatusText: {
|
|
614
|
+
readonly [statusCode: number]: string;
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
export class Zent<
|
|
618
|
+
TState extends AnyState = AnyState,
|
|
619
|
+
TDecorators extends AnyDecorators = {},
|
|
620
|
+
> {
|
|
621
|
+
constructor(opts?: ZentOptions);
|
|
622
|
+
|
|
623
|
+
route(definition: RouteDefinition<TState, TDecorators>): this;
|
|
624
|
+
all(
|
|
625
|
+
path: string,
|
|
626
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
627
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
628
|
+
): this;
|
|
629
|
+
group(
|
|
630
|
+
prefix: string,
|
|
631
|
+
callback: (group: GroupApi<TState, TDecorators>) => void
|
|
632
|
+
): this;
|
|
633
|
+
group(
|
|
634
|
+
prefix: string,
|
|
635
|
+
opts: GroupOptions<TState, TDecorators> | null,
|
|
636
|
+
callback: (group: GroupApi<TState, TDecorators>) => void
|
|
637
|
+
): this;
|
|
638
|
+
|
|
639
|
+
use(middleware: Middleware<TState, TDecorators>): this;
|
|
640
|
+
use(prefix: string, middleware: Middleware<TState, TDecorators>): this;
|
|
641
|
+
|
|
642
|
+
addHook(phase: 'onRequest', fn: OnRequestHook<TState, TDecorators>): this;
|
|
643
|
+
addHook(phase: 'preParsing', fn: PreParsingHook<TState, TDecorators>): this;
|
|
644
|
+
addHook(
|
|
645
|
+
phase: 'preValidation',
|
|
646
|
+
fn: PreValidationHook<TState, TDecorators>
|
|
647
|
+
): this;
|
|
648
|
+
addHook(phase: 'preHandler', fn: PreHandlerHook<TState, TDecorators>): this;
|
|
649
|
+
addHook(phase: 'onSend', fn: OnSendHook<TState, TDecorators>): this;
|
|
650
|
+
addHook(phase: 'onResponse', fn: OnResponseHook<TState, TDecorators>): this;
|
|
651
|
+
addHook(phase: 'onError', fn: OnErrorHook<TState, TDecorators>): this;
|
|
652
|
+
|
|
653
|
+
setErrorHandler(
|
|
654
|
+
fn: (error: Error, ctx: Context<TState, TDecorators>) => MaybePromise<void>
|
|
655
|
+
): this;
|
|
656
|
+
setNotFoundHandler(
|
|
657
|
+
fn: (ctx: Context<TState, TDecorators>) => MaybePromise<void>
|
|
658
|
+
): this;
|
|
659
|
+
|
|
660
|
+
decorate<TKey extends string, TValue>(
|
|
661
|
+
name: TKey,
|
|
662
|
+
value: (ctx: Context<TState, TDecorators>, ...args: unknown[]) => TValue
|
|
663
|
+
): Zent<TState, Merge<TDecorators, Record<TKey, typeof value>>> &
|
|
664
|
+
Merge<TDecorators, Record<TKey, typeof value>>;
|
|
665
|
+
|
|
666
|
+
decorate<TKey extends string, TValue>(
|
|
667
|
+
name: TKey,
|
|
668
|
+
value: TValue
|
|
669
|
+
): Zent<TState, Merge<TDecorators, Record<TKey, TValue>>> &
|
|
670
|
+
Merge<TDecorators, Record<TKey, TValue>>;
|
|
671
|
+
hasDecorator<TKey extends string>(
|
|
672
|
+
name: TKey
|
|
673
|
+
): name is TKey & keyof TDecorators;
|
|
674
|
+
|
|
675
|
+
register<TOptions extends PluginOptions = PluginOptions>(
|
|
676
|
+
fn: PluginFunction<TOptions, TState, TDecorators>,
|
|
677
|
+
opts?: TOptions
|
|
678
|
+
): this;
|
|
679
|
+
|
|
680
|
+
listen(opts?: ListenOptions, callback?: ListenCallback): Promise<string>;
|
|
681
|
+
close(): Promise<void>;
|
|
682
|
+
inject(opts: InjectOptions): Promise<InjectResponse>;
|
|
683
|
+
|
|
684
|
+
get(
|
|
685
|
+
path: string,
|
|
686
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
687
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
688
|
+
): this;
|
|
689
|
+
post(
|
|
690
|
+
path: string,
|
|
691
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
692
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
693
|
+
): this;
|
|
694
|
+
put(
|
|
695
|
+
path: string,
|
|
696
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
697
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
698
|
+
): this;
|
|
699
|
+
patch(
|
|
700
|
+
path: string,
|
|
701
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
702
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
703
|
+
): this;
|
|
704
|
+
delete(
|
|
705
|
+
path: string,
|
|
706
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
707
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
708
|
+
): this;
|
|
709
|
+
head(
|
|
710
|
+
path: string,
|
|
711
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
712
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
713
|
+
): this;
|
|
714
|
+
options(
|
|
715
|
+
path: string,
|
|
716
|
+
handler: RouteHandler<TState, TDecorators>,
|
|
717
|
+
opts?: RouteOptions<TState, TDecorators>
|
|
718
|
+
): this;
|
|
719
|
+
|
|
720
|
+
[key: string]: unknown;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
export function zent<
|
|
724
|
+
TState extends AnyState = AnyState,
|
|
725
|
+
TDecorators extends AnyDecorators = {},
|
|
726
|
+
>(opts?: ZentOptions): Zent<TState, TDecorators>;
|
|
File without changes
|