-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.ts
142 lines (121 loc) · 2.89 KB
/
model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
63
64
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
import { Response, ServerRequest } from "./deps.ts";
import { Cookie } from "./cookie.ts";
export type ReqMethod =
| "get"
| "post"
| "put"
| "delete"
| "patch"
| "options"
| "head"
| "connect"
| "trace";
export type ReqBody = {
type: string;
value: string | number | Record<string, string | number | Uint8Array>;
};
export type HandlerFunc = (req: Req, res: Res) => Promise<void> | void;
export type NextFunc = () => Promise<void>;
export type MiddlewareFunc = (
req: Req,
res: Res,
next: NextFunc,
) => Promise<void> | void;
export type MethodFuncArgument = Array<MiddlewareFunc>;
export interface AppConfig {
server: ListenOptions;
}
export interface RoutesConfig {
url: string;
method: string;
func: HandlerFunc;
middleware?: MethodFuncArgument;
}
export interface RouteHandlers {
middleware?: Array<MiddlewareFunc>;
handler: HandlerFunc;
}
export interface RouteValue {
query: Record<string, string>;
url: string;
params: Record<string, string>;
handler: HandlerFunc;
middleware: Array<MiddlewareFunc>;
}
export interface SingleRoute {
middleware: Array<MiddlewareFunc>;
handler: HandlerFunc;
paramsNames: Record<string, string>;
}
export interface NewRoute {
next: Record<string, NewRoute> | null;
middleware: Array<MiddlewareFunc>;
handler: HandlerFunc | null;
paramsNames: Record<string, string>;
}
export interface CorsOptions {
allowMethods?: Array<string>;
allowHeaders?: Array<string>;
origin?: string | ((req: Req) => Promise<string>);
allowCredentials?: boolean;
maxAge?: number;
exposeHeaders?: Array<string>;
}
export interface ListenOptions {
port: number;
hostname: string;
certFile?: string;
isHttps?: boolean;
secure?: boolean;
keyFile?: string;
}
export interface RealUrl {
url: string;
query?: Record<string, string> | null;
prefix?: string;
params?: string | null;
paramsName?: string;
extName?: string;
}
export type AssetsOptions = {
// developing
path: string;
onerror: (e: Error) => void;
};
export type AssetsArgument = string | AssetsOptions;
export interface Req {
query: Record<string, string>;
body: ReqBody;
url: string;
method: ReqMethod;
headers: Headers;
request: ServerRequest;
params: Record<string, string>;
cookies: Map<string, string | boolean>;
[key: string]: unknown;
}
export interface Res {
response: Response;
// 返回值类型可以是任意的
// deno-lint-ignore no-explicit-any
body: any;
headers: Headers;
status: number;
done: boolean;
redirect: (url: string) => void;
render: (path: string) => Promise<void>;
send: (req: Req, res: Res) => void;
cookies: Cookie;
}
export interface MinConfig {
server: ListenOptions;
routes: Array<RoutesConfig>;
cors?: CorsOptions;
assets?: string | Record<string, string | number | boolean>;
}
export type ErrorMessage = {
path: string;
req: Req;
error: string;
position: string;
};