-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
response.ts
58 lines (55 loc) 路 2.16 KB
/
response.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
import { ServerResponse } from 'http'
import type { SerializeOptions } from '@tinyhttp/cookie'
import { Request } from './request.js'
import { App, TemplateEngineOptions } from './app.js'
import type { ReadStreamOptions, FormatProps, DownloadOptions } from '@tinyhttp/res'
export const renderTemplate =
<O>(_req: Request, res: Response, app: App) =>
(file: string, data?: Record<string, any>, options?: TemplateEngineOptions<O>): Response => {
app.render(
file,
data ? { ...data, ...res.locals } : res.locals,
(err: unknown, html: unknown) => {
if (err) throw err
res.send(html)
},
options
)
return res
}
export interface Response<O = any> extends ServerResponse {
header(field: string | Record<string, unknown>, val?: string | any[]): Response
set(field: string | Record<string, unknown>, val?: string | any[]): Response
get(field: string): string | number | string[]
send(body: unknown): Response
sendFile(path: string, options?: ReadStreamOptions, cb?: (err?: any) => void): Response
json(body: unknown): Response
status(status: number): Response
sendStatus(statusCode: number): Response
cookie(
name: string,
value: string | Record<string, unknown>,
options?: SerializeOptions & Partial<{ signed: boolean }>
): Response
clearCookie(name: string, options?: SerializeOptions): Response
location(url: string): Response
links(links: { [key: string]: string }): Response
render(file: string, data?: Record<string, any>, options?: TemplateEngineOptions<O>): Response
vary(field: string): Response
format(obj: FormatProps): Response
redirect(url: string, status?: number): Response
type(type: string): Response
download(path: string, filename: string, options?: DownloadOptions, cb?: (err?: any) => void): Response
attachment(filename?: string): Response
app?: App
locals?: Record<string, any>
/**
* Send JSON response with JSONP callback support.
*
* To enable this method, install the `@tinyhttp/jsonp` package and attach the method to `res.jsonp` property.
*
* @param obj Response object
*/
jsonp(obj: any): Response
append(field: string, value: any): Response
}