-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ts
40 lines (34 loc) · 1.35 KB
/
context.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
// Copyright 2023-2024 the Sequoia authors. All rights reserved. MIT license.
import { HTTPStatus } from './status.ts'
import { parseCookies } from './cookie.ts'
import { CookieStorage } from './cookie.ts'
import { HTTPContextRequest } from './httprequest.ts'
import { HTTPContextResponse, HTTPResponse } from './httpresponse.ts'
import type { RouteParams } from './router.ts'
import { serveStatic } from './static.ts'
export type SendOptions = {
path: string
root: string
extensions: string[]
}
export class Context<
ParamsT extends Record<string, string> = RouteParams,
RequestT extends HTTPContextRequest<ParamsT> = HTTPContextRequest<ParamsT>,
ResponseT extends HTTPContextResponse = HTTPContextResponse,
> {
readonly request: RequestT
readonly response: ResponseT
readonly cookies: CookieStorage
constructor(request: Request) {
this.response = new HTTPResponse({
status: HTTPStatus.SUCCESS,
body: null,
}) as ResponseT
this.request = new HTTPContextRequest<ParamsT>(request) as RequestT
const cookies = request.headers.get('cookie')
this.cookies = new CookieStorage(cookies ? parseCookies(cookies) : undefined)
}
public send = async (options: SendOptions): Promise<HTTPResponse> => {
return await serveStatic(this.request.url, '/', options.root)
}
}