/
types.d.ts
328 lines (298 loc) · 8.77 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* An interface that aligns to the parts of the `Aleph` class.
*/
export interface Aleph {
readonly mode: 'development' | 'production'
readonly workingDir: string
readonly config: RequiredConfig
addDist(path: string, content: Uint8Array): Promise<void>
addModule(specifier: string, sourceCode: string, forceRefresh?: boolean): Promise<Module>
fetchModule(specifier: string): Promise<{ content: Uint8Array, contentType: string | null }>
onResolve(test: RegExp, resolve: (specifier: string) => ResolveResult): void
onLoad(test: RegExp, load: (input: LoadInput) => LoadOutput | Promise<LoadOutput>): void
onTransform(test: 'hmr' | 'main' | RegExp, transform: (input: TransformInput) => TransformOutput | void | Promise<TransformOutput | void>): void
onRender(callback: (input: RenderOutput) => Promise<void> | void): void
}
/**
* The configuration for aleph application.
*/
export type Config = {
/** `framework` specifies the framework (default is 'react'). */
framework?: 'react'
/** `basePath` specifies the path prefix (default is '/'). */
basePath?: string
/** `i18n` specifies the options for **Internationalization**. */
i18n?: I18nOptions
/** `css` specifies the css processing options. */
css?: CSSOptions
/** `build` specifies the options for **ES Build**. */
build?: BuildOptions
/** `ssr` specifies the options for **SSR**. */
ssr?: boolean | GlobalSSROptions
/** `plugins` specifies some plugins to extend Aleph runtime. */
plugins?: Plugin[]
/** `server` specifies the options for **Server**. */
server?: ServerOptions
}
/* The Requred configuration for aleph application. */
export type RequiredConfig = Required<Config> & {
i18n: Required<I18nOptions>
build: Required<BuildOptions>
server: Required<ServerOptions>
css: Required<CSSOptions>
}
/**
* The plugin to enhance Aleph server runtime.
*/
export type Plugin = {
/** `name` gives the plugin a name. */
name: string
/** `setup` inits the plugin. */
setup(aleph: Aleph): Promise<void> | void
}
/**
* The result from the `onResolve` hook.
*/
export type ResolveResult = {
specifier?: string
asPage?: { path: string, isIndex?: boolean }
acceptHMR?: boolean
external?: boolean
data?: any
}
/**
* The input to the `onLoad` hook.
*/
export type LoadInput = {
specifier: string
data?: any
}
/**
* The output of the `onLoad` hook.
*/
export type LoadOutput = {
/** The transformed code type (default is 'js'). */
type?: 'css' | 'js' | 'jsx' | 'ts' | 'tsx'
/** The transformed code. */
code: string
/** The source map if available. */
map?: string
}
/**
* The input of the `onTransform` hook.
*/
export type TransformInput = {
module: Omit<Module, 'jsBuffer' | 'ready'>
code: string
map?: string
}
/**
* The output of the `onTransform` hook.
*/
export type TransformOutput = {
code: string
extraDeps?: DependencyDescriptor[]
map?: string
}
/** A module includes the compilation details. */
export type Module = {
readonly specifier: string
deps: DependencyDescriptor[]
external?: boolean
isStyle?: boolean
httpExternal?: boolean
ssrPropsFn?: string
ssgPathsFn?: boolean
denoHooks?: string[]
jsxStaticClassNames?: string[]
sourceHash: string
jsFile: string
jsBuffer?: Uint8Array
ready: Promise<void>
}
/** The Dependency Descriptor. */
type DependencyDescriptor = {
readonly specifier: string
virtual?: boolean
isDynamic?: boolean
hashLoc?: number
}
/** The HTML Descriptor. */
type HtmlDescriptor = {
lang: string,
headElements: string[],
scripts: (string | { id?: string, type?: string, src?: string, innerText?: string, async?: boolean, preload?: boolean, nomodule?: boolean })[],
body: string,
bodyAttrs: Record<string, string>,
}
/** SSR data with expires. */
export type SSRData = {
value: any
expires: number
}
/** render output includes html and data. */
export type RenderOutput = {
path: string
html: HtmlDescriptor
data: Record<string, SSRData> | null
}
/**
* The options for **Internationalization**.
*/
export type I18nOptions = {
/** `defaultLocale` specifies the default locale (default is the first locale). */
defaultLocale?: string
/** `locales` specifies all the available locales. */
locales: string[]
}
/**
* The built target of esbuild.
*/
export type BuildTarget = 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'es2019' | 'es2020' | 'es2021' | 'esnext'
/**
* The supported borwser name of esbuild.
*/
export type BrowserName = 'chrome' | 'edge' | 'firefox' | 'ios' | 'safari'
/**
* The config for ES Build.
*/
export type BuildOptions = {
/** `target` specifies the build target in production mode (default is [**es2015**]). */
target?: BuildTarget
/** `browsers` specifies the target browsers for esbuild. */
browsers?: Record<BrowserName, number>
/** `outputDir` specifies the output directory for `build` command (default is '**dist**'). */
outputDir?: string
}
/**
* The import maps.
*/
export type ImportMap = {
imports: Record<string, string>
scopes: Record<string, Record<string, string>>
}
/**
* The config for builtin CSS loader.
*/
export type CSSOptions = {
/** `cache` caches remote css to local if it is true. */
cache?: boolean | RegExp | RegExp[]
/** `postcss` specifies the postcss plugins. */
postcss?: { plugins: PostCSSPlugin[] }
/** `modules` specifies CSS modules behavior. */
modules?: CSSModulesOptions
}
/**
* The plugin type of postcss.
*/
export type PostCSSPlugin = string | [string, any] | Record<string, any> | CallableFunction
/**
* The options are passed on to postcss-modules.
*/
export type CSSModulesOptions = {
exportGlobals?: boolean
generateScopedName?: string | ((name: string, filename: string, css: string) => string)
globalModulePaths?: string[]
hashPrefix?: string
localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly'
scopeBehaviour?: 'global' | 'local'
}
/**
* Global **SSR** options.
*/
export type GlobalSSROptions = {
/** A list of RegExp for paths to use **SSR**. */
include?: RegExp[]
/** A list of RegExp for paths to skip **SSR**. */
exclude?: RegExp[]
}
/**
* The **SSR** props.
*/
export type SSRProps = {
[key: string]: any
$revalidate?: number
}
/**
* The **SSR** options for pages.
*/
export type SSROptions = {
props?(router: RouterURL): (SSRProps | Promise<SSRProps>)
paths?(): (string[] | Promise<string[]>)
}
/**
* The options for Server.
*/
export type ServerOptions = {
/** A list of `APIMiddleware` for api requests. */
middlewares?: APIMiddleware[]
/** `headers` appends custom headers for server requests. */
headers?: Record<string, string>
/** `rewrites` specifies the server rewrite map. */
rewrites?: Record<string, string>
/** `compress` enbles compression(gzip/brotli) for static files and SSR content (default is **true**). */
compress?: boolean
}
/**
* The handler for API requests.
*/
export type APIHandler = {
(context: APIContext): Promise<void> | void
}
/**
* The middleware for API requests.
*/
export type APIMiddleware = {
(context: APIContext, next: () => void): Promise<void> | void
}
/**
* An interface that aligns to the `Deno.RequestEvent`
*/
export interface APIContext extends Deno.RequestEvent {
/** The data handled by middlewares. */
readonly data: Map<string, any>
/** An interface that aligns to the parts of the `Response` with helper methods */
readonly response: APIResponse
/** The router by the api routing. */
readonly router: RouterURL
}
/**
* An interface that aligns to the parts of the `Response` with helpers.
*/
export interface APIResponse {
status: number
headers: Headers
body?: string | Uint8Array | ArrayBuffer | ReadableStream<Uint8Array>
/**
* `addHeader` adds a new value onto an existing response header of the request, or
* adds the header if it does not already exist.
*/
addHeader(key: string, value: string): this
/**
* `setHeader` sets a new value for an existing response header of the request, or adds
* the header if it does not already exist.
*/
setHeader(key: string, value: string): this
/** `removeHeader` removes the value for an existing response header of the request. */
removeHeader(key: string): this
/** `redirect` replies to redirect the client to another URL with optional response `status` defaulting to 302. */
redirect(url: string, status?: number): this
/** `json` replies to the request with a json content. */
json(data: any, space?: string | number): this
}
/**
* The router URL object of the routing.
*/
export type RouterURL = {
readonly basePath: string
readonly routePath: string
readonly locale: string
readonly defaultLocale: string
readonly locales: string[]
readonly pathname: string
readonly params: Record<string, string>
readonly query: URLSearchParams
toString(): string
push(url: string): void
replace(url: string): void
}