/
types.ts
277 lines (244 loc) · 7.07 KB
/
types.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import type { ConnInfo, ServeInit } from "https://deno.land/std@0.180.0/http/server.ts";
import type { Comment, Element, TextChunk } from "https://deno.land/x/lol_html@0.0.6/types.d.ts";
import type { RouteModule } from "../framework/core/router.ts";
export type AlephConfig = {
/** The TLS options. */
tls?: TLSOptions;
/** The router options for the file-system based routing. */
router?: RouterInit;
/** The module loaders. */
loaders?: ModuleLoader[];
/** The server middlewares. */
middlewares?: Middleware[];
/** The options for session. */
session?: SessionOptions;
/** The options for SSR. */
ssr?: SSR;
/** The options for build. */
build?: BuildOptions;
/** The atomic CSS engine. */
atomicCSS?: AtomicCSSEngine;
/** The plugins. */
plugins?: Plugin[];
/** The error handler. */
onError?: ErrorHandler;
};
export interface Plugin {
name?: string;
setup(config: AlephConfig, env: PluginENV): void | Promise<void>;
}
export type PluginENV = {
isDev: boolean;
};
export interface AtomicCSSGenerateOptions {
/**
* Filepath of the file being processed.
*/
id?: string;
/**
* Generate preflights (if defined)
*
* @default true
*/
preflights?: boolean;
/**
* Generate minified CSS
* @default false
*/
minify?: boolean;
}
export interface AtomicCSSGenerateResult {
css: string;
matched: Set<string>;
}
export interface AtomicCSSEngine {
name?: string;
version?: string;
test?: RegExp;
resetCSS?: string;
generate(input: string | string[], options?: AtomicCSSGenerateOptions): Promise<AtomicCSSGenerateResult>;
}
/** The router options for the file-system based routing. */
export interface RouterInit {
/** The base url of the router. Dafault is `/` */
basePath?: string;
/** The glob to match routes. */
glob?: string;
/** The directory of the FS routing. Default is `./routes` */
dir?: string;
/** The extnames to match routes. */
exts?: string[];
/** The route module URL. */
moduleURL?: string;
/** The pre-built module. */
modules?: Record<string, Record<string, unknown>>;
/** The handler to handle the router change. */
onChange?(): void;
}
export type CookieOptions = {
expires?: number | Date;
maxAge?: number;
domain?: string;
path?: string;
httpOnly?: boolean;
secure?: boolean;
sameSite?: "lax" | "strict" | "none";
};
export interface Cookies {
get(key: string): string | undefined;
}
export interface SessionStorage {
get(sid: string): Promise<Record<string, unknown> | undefined>;
set(sid: string, data: Record<string, unknown>, expires: number): Promise<void>;
delete(sid: string): Promise<void>;
}
export type SessionCookieOptions = {
name?: string;
domain?: string;
path?: string;
secure?: boolean;
sameSite?: "lax" | "strict" | "none";
};
export type SessionOptions = {
storage?: SessionStorage;
cookie?: SessionCookieOptions;
secret?: string;
maxAge?: number;
};
export interface Session<T> {
store: T | undefined;
update(store: T | ((store: T | undefined) => T)): Promise<void>;
end(): Promise<void>;
redirect(url: string | URL): Response;
}
export interface HTMLRewriterHandlers {
element?: (element: Element) => void;
comments?: (comment: Comment) => void;
text?: (text: TextChunk) => void;
}
export interface HTMLRewriter {
on: (selector: string, handlers: HTMLRewriterHandlers) => void;
}
export interface Context extends Record<string, unknown> {
/** The request connection info. */
readonly connInfo?: ConnInfo;
/** The params of dynamic routes. */
readonly params: Record<string, string>;
/** The cookies from client. */
readonly cookies: Cookies;
/** The HtmlRewriter to rewrite the html output. */
readonly htmlRewriter: HTMLRewriter;
/** Returns the `Session` object. */
getSession: <T extends Record<string, unknown> = Record<string, unknown>>() => Promise<Session<T>>;
/** Calls next middleware/handler. */
next: () => Promise<Response> | Response;
/** Renders the route page. */
render: () => Promise<Response>;
}
/** The Middleare for Aleph server. */
export interface Middleware {
/** The middleware name. */
readonly name?: string;
/** The middleware fetch method. */
fetch(request: Request, context: Context): Promise<Response> | Response;
}
export type ImportMap = {
readonly __filename: string;
readonly imports: Record<string, string>;
readonly scopes: Record<string, Record<string, string>>;
};
export type JSXConfig = {
jsx?: "automatic" | "classic" | "preserve";
jsxPragma?: string;
jsxPragmaFrag?: string;
jsxImportSource?: string;
};
export type ModuleLoaderEnv = {
isDev?: boolean;
importMap?: ImportMap;
jsxConfig?: JSXConfig;
sourceMap?: boolean;
spaMode?: boolean;
ssr?: boolean;
};
export type ModuleLoaderOutput = {
code: string;
inlineCSS?: string;
lang?: "js" | "jsx" | "ts" | "tsx";
map?: string;
};
export interface ModuleLoader {
test(path: string): boolean;
load(specifier: string, content: string, env: ModuleLoaderEnv): Promise<ModuleLoaderOutput> | ModuleLoaderOutput;
}
/** The optimization options for the server. */
export type BuildOptions = {
/** The built target for esbuild, default is "es2018". */
buildTarget?: "es2015" | "es2016" | "es2017" | "es2018" | "es2019" | "es2020" | "es2021" | "es2022";
/** The output directory, default is "./out". */
outputDir?: string;
/** The SSG options for the FS routing. */
ssg?: boolean | SSGOptions;
/** The source map options for esbuild. */
sourceMap?: boolean;
};
/** The SSG options for the FS routing. */
export type SSGOptions = {
include?: RegExp;
exclude?: RegExp;
getStaticPaths?: () => string[] | Promise<string[]>;
clientHeaders?: HeadersInit;
};
export type SSRContext = {
readonly url: URL;
readonly modules: RouteModule[];
readonly headCollection: string[];
readonly signal: AbortSignal;
readonly nonce?: string;
setStatus(code: number): void;
setSuspenseMarker(selector: string, test: (el: Element) => boolean): void;
};
export type SuspenseMarker = {
selector: string;
test: (el: Element) => boolean;
};
export type SSRFn = {
(ssr: SSRContext): Promise<ReadableStream | string> | ReadableStream | string;
};
/**
* Options for the content-security-policy.
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
*/
export type CSP = {
nonce?: boolean;
getPolicy: (url: URL, nonce?: string) => string | null;
};
export type SSROptions = {
/** The selector of root to append SSR ouput, default is "#root". */
root?: string;
include?: RegExp | RegExp[];
exclude?: RegExp | RegExp[];
/**
* Options for the content-security-policy.
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
*/
CSP?: CSP;
};
export type SSR =
| SSRFn
| SSROptions & {
render: SSRFn;
};
export type ErrorHandler = {
(
error: unknown,
cause: "fetch-route" | "fetch-route-data" | "ssr" | "transform" | "fs" | "middleware",
request: Request,
context?: Context,
): Response | void;
};
export type TLSOptions = {
certFile: string;
keyFile: string;
};
export type { ConnInfo, ServeInit };