@kevisual/router 0.2.3 → 0.2.4

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.
@@ -0,0 +1,109 @@
1
+ import { Key } from 'path-to-regexp';
2
+ import { IncomingMessage, ServerResponse, Server } from 'node:http';
3
+ import { ListenOptions } from 'node:net';
4
+
5
+ type Req = IncomingMessage & {
6
+ params?: Record<string, string>;
7
+ };
8
+ type SimpleObject = {
9
+ [key: string]: any;
10
+ };
11
+ interface Route {
12
+ method: string;
13
+ regexp: RegExp;
14
+ keys: Key[];
15
+ handlers: Array<(req: Req, res: ServerResponse) => Promise<void> | void>;
16
+ }
17
+ /**
18
+ * SimpleRouter
19
+ */
20
+ declare class SimpleRouter {
21
+ routes: Route[];
22
+ exclude: string[];
23
+ constructor(opts?: {
24
+ exclude?: string[];
25
+ });
26
+ getBody(req: Req): Promise<Record<string, any>>;
27
+ getSearch(req: Req): any;
28
+ parseSearchValue: (value?: string, opts?: {
29
+ decode?: boolean;
30
+ }) => any;
31
+ use(method: string, route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
32
+ get(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
33
+ post(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
34
+ sse(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
35
+ all(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
36
+ getJson(v: string | number | boolean | SimpleObject): any;
37
+ isSse(req: Req): boolean;
38
+ /**
39
+ * 解析 req 和 res 请求
40
+ * @param req
41
+ * @param res
42
+ * @returns
43
+ */
44
+ parse(req: Req, res: ServerResponse): Promise<void> | "is_exclude" | "not_found";
45
+ /**
46
+ * 创建一个新的 HttpChain 实例
47
+ * @param req
48
+ * @param res
49
+ * @returns
50
+ */
51
+ chain(req?: Req, res?: ServerResponse): HttpChain;
52
+ static Chain(opts?: HttpChainOpts): HttpChain;
53
+ }
54
+ type HttpChainOpts = {
55
+ req?: Req;
56
+ res?: ServerResponse;
57
+ simpleRouter?: SimpleRouter;
58
+ server?: Server;
59
+ };
60
+ /**
61
+ * HttpChain 类, 用于链式调用,router.get内部使用
62
+ */
63
+ declare class HttpChain {
64
+ /**
65
+ * 请求对象, 每一次请求都是不一样的
66
+ */
67
+ req: Req;
68
+ /**
69
+ * 响应对象, 每一次请求响应都是不一样的
70
+ */
71
+ res: ServerResponse;
72
+ simpleRouter: SimpleRouter;
73
+ server: Server;
74
+ hasSetHeader: boolean;
75
+ isSseSet: boolean;
76
+ constructor(opts?: HttpChainOpts);
77
+ setReq(req: Req): this;
78
+ setRes(res: ServerResponse): this;
79
+ setRouter(router: SimpleRouter): this;
80
+ setServer(server: Server): this;
81
+ /**
82
+ * 兼容 express 的一点功能
83
+ * @param status
84
+ * @returns
85
+ */
86
+ status(status: number): this;
87
+ writeHead(status: number): this;
88
+ json(data: SimpleObject): this;
89
+ /**
90
+ * 兼容 express 的一点功能
91
+ * @param data
92
+ * @returns
93
+ */
94
+ end(data: SimpleObject | string): this;
95
+ listen(opts: ListenOptions, callback?: () => void): this;
96
+ /**
97
+ * 外部 parse 方法
98
+ * @returns
99
+ */
100
+ parse(opts?: {
101
+ listenOptions?: ListenOptions;
102
+ listenCallBack?: () => void;
103
+ }): () => void;
104
+ getString(value: string | SimpleObject): string;
105
+ sse(value: string | SimpleObject): this | undefined;
106
+ close(): this;
107
+ }
108
+
109
+ export { HttpChain, SimpleRouter };