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