/
types.d.ts
250 lines (229 loc) · 6.04 KB
/
types.d.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// deno-lint-ignore-file no-explicit-any
import { Cookie, ServeInit } from "./server/deps.ts";
export type SetOptions = {
isExpired?: boolean;
expirySeconds?: number;
};
export interface Container {
objects: () => Map<string, Data>;
set: <T>(
key: string,
value: T,
options?: SetOptions,
) => void;
get: <T>(key: string) => T | null;
size: () => number;
clear: () => void;
delete: (key: string) => boolean;
clearInterval: () => void;
}
export type Data = {
key: string;
value: any;
isExpired: boolean;
expiryTime: number;
timeoutId?: number;
};
export interface Next {
(error?: Error): void;
}
export type HttpResponse = {
deleteCookie: (
name: string,
attributes?: {
path?: string | undefined;
domain?: string | undefined;
} | undefined,
) => HttpResponse;
setCookie: (cookie: Cookie) => HttpResponse;
headers: (headers: Headers) => HttpResponse;
authorization: (type: string) => HttpResponse;
contentType: (type: string) => HttpResponse;
status: (status: number) => HttpResponse;
send: (object: unknown) => Response | Promise<Response>;
json: (object: unknown) => Response | Promise<Response>;
ssr: (ssr: SSR) => SSR;
html: (html: string) => Response | Promise<Response>;
jsx: (element: JSX.Element) => Response | Promise<Response>;
};
export class HttpRequest extends Request {
match: URLPatternResult | null;
container: () => Container;
get: <T>(key: string) => T | null;
set: <T>(key: string, value: T, options?: SetOptions) => void;
delete: (key: string) => void;
params: (name?: string) => Record<string, string> | string;
query: (name?: string) => Record<string, string> | string | null;
}
export type RouteMidleware = {
method: string;
path: string;
handler: MiddlewareArgument;
};
export type AppMidleware = {
path: string;
handler: MiddlewareArgument;
};
export type RequestHandler = (
request: HttpRequest,
response: HttpResponse,
) =>
| Response
| Promise<Response>;
export type AppMiddlewareArgument = string | MiddlewareArgument;
export type MiddlewareArgument = (
request: HttpRequest,
response: HttpResponse,
next: Next,
) =>
| void
| Promise<void>
| string
| Promise<string>
| Response
| Promise<Response>
| any
| Promise<any>;
export type HandlerArgument =
| Deno.ServeHandler
| RequestHandler
| MiddlewareArgument;
export type Route = {
method: string;
path: string;
handler: HandlerArgument;
};
export type Fastro = {
/**
* Accept incoming connections on the given listener, and handle requests on these connections with the given handler.
*
* HTTP/2 support is only enabled if the provided Deno.Listener returns TLS connections and was configured with "h2" in the ALPN protocols.
*
* Throws a server closed error if called after the server has been closed.
*
* @param options
*/
serve(options?: ServeInit): Promise<void>;
/**
* Immediately close the server listeners and associated HTTP connections.
*
* Throws a server closed error if called after the server has been closed.
*/
close(): void;
/**
* Add url endpoint for static files.
*
* Place your files in the public folder
*
* ```ts
* import application from "./mod.ts";
* const app = application();
* app.static("/public");
* await app.serve();
* ```
*
* Place your files in the static folder
*
* ```ts
* app.static("/static");
* ```
*
* Place your files in the app root folder
*
* ```ts
* app.static("/");
* ```
*
* @param path The base endpoint to access a file
* @param maxAge Cache control of the file
*/
static(path: string, maxAge?: number): Fastro;
use(...middleware: Array<MiddlewareArgument>): Fastro;
get(path: string, ...handler: Array<HandlerArgument>): Fastro;
post(path: string, ...handler: Array<HandlerArgument>): Fastro;
put(path: string, ...handler: Array<HandlerArgument>): Fastro;
delete(path: string, ...handler: Array<HandlerArgument>): Fastro;
patch(path: string, ...handler: Array<HandlerArgument>): Fastro;
options(path: string, ...handler: Array<HandlerArgument>): Fastro;
head(path: string, ...handler: Array<HandlerArgument>): Fastro;
flash(isFlash: boolean): Fastro;
build(isBuild: boolean): Fastro;
/**
* Set data to container
*
* ```ts
* app.set("key1", "value1")
* ```
*
* you can access them from handler
*
* ```ts
* req.get("key1")
* ```
*
* @param key
* @param value
* @param options
*/
set<T>(key: string, value: T, options?: SetOptions): Fastro;
page(
path: string,
ssr: SSR,
handler: HandlerArgument,
): Fastro;
container: Container;
};
export type SSRHandler = {
path: string;
ssr: SSR;
handler: HandlerArgument;
};
export type StartOptions = {
flash: boolean;
};
export type ExecHandler = (
request?: HttpRequest,
response?: HttpResponse,
) => any | Promise<any>;
export type RenderOptions = {
title: string;
style?: string;
link?: string;
script?: string;
meta?: string;
bundle?: string;
};
export interface SSR {
dir: (dir: string) => SSR;
title: (title: string) => SSR;
meta: (meta: string) => SSR;
script: (script: string) => SSR;
style: (style: string) => SSR;
link: (link: string) => SSR;
htmlAttr: (attr: string) => SSR;
bodyAttr: (attr: string) => SSR;
rootAttr: (attr: string) => SSR;
lang: (lang: string) => SSR;
cdn: (cdn: string) => SSR;
ogTitle: (title: string) => SSR;
ogType: (type: string) => SSR;
ogImage: (image: string) => SSR;
ogURL: (url: string) => SSR;
ogSiteName: (name: string) => SSR;
ogDesc: (desc: string) => SSR;
twitterCard: (card: string) => SSR;
metaDesc: (desc: string) => SSR;
props: (props: any) => SSR;
request: (req: HttpRequest) => SSR;
cache: (cache: Container) => SSR;
render: () => Response;
_createBundle: (
bundle?: string,
rootComponent?: string,
rootTSX?: string,
) => void;
_getBundleName: () => string;
bundle: (name: string) => SSR;
}
export type JSXHandler = (props?: any) => JSX.Element;
export type Row = Record<string, any>;