2022-09-20 03:11:34 +02:00
|
|
|
import type {
|
|
|
|
Environment,
|
|
|
|
NotFoundHandler,
|
|
|
|
ContextVariableMap,
|
|
|
|
Bindings,
|
|
|
|
ValidatedData,
|
|
|
|
} from './hono.ts'
|
2022-09-02 14:13:47 +02:00
|
|
|
import { defaultNotFoundMessage } from './hono.ts'
|
2022-07-10 17:17:29 +02:00
|
|
|
import type { CookieOptions } from './utils/cookie.ts'
|
|
|
|
import { serialize } from './utils/cookie.ts'
|
2022-07-02 08:09:45 +02:00
|
|
|
import type { StatusCode } from './utils/http-status.ts'
|
|
|
|
|
2022-09-20 14:41:45 +02:00
|
|
|
type HeaderField = [string, string]
|
|
|
|
type Headers = Record<string, string | string[]>
|
2022-07-02 08:09:45 +02:00
|
|
|
export type Data = string | ArrayBuffer | ReadableStream
|
|
|
|
|
2022-08-23 13:53:25 +02:00
|
|
|
export interface Context<
|
|
|
|
RequestParamKeyType extends string = string,
|
2022-09-20 03:11:34 +02:00
|
|
|
E extends Partial<Environment> = any,
|
|
|
|
D extends ValidatedData = ValidatedData
|
2022-08-23 13:53:25 +02:00
|
|
|
> {
|
2022-09-20 03:11:34 +02:00
|
|
|
req: Request<RequestParamKeyType, D>
|
2022-08-27 16:57:33 +02:00
|
|
|
env: E['Bindings']
|
2022-07-17 11:11:09 +02:00
|
|
|
event: FetchEvent
|
|
|
|
executionCtx: ExecutionContext
|
2022-07-02 08:09:45 +02:00
|
|
|
finalized: boolean
|
|
|
|
|
2022-07-10 16:44:23 +02:00
|
|
|
get res(): Response
|
|
|
|
set res(_res: Response)
|
2022-09-20 14:41:45 +02:00
|
|
|
header: (name: string, value: string, options?: { append?: boolean }) => void
|
2022-07-10 16:44:23 +02:00
|
|
|
status: (status: StatusCode) => void
|
2022-07-31 15:19:28 +02:00
|
|
|
set: {
|
|
|
|
<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void
|
2022-08-23 13:53:25 +02:00
|
|
|
<Key extends keyof E['Variables']>(key: Key, value: E['Variables'][Key]): void
|
2022-07-31 15:19:28 +02:00
|
|
|
(key: string, value: any): void
|
|
|
|
}
|
|
|
|
get: {
|
|
|
|
<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key]
|
2022-08-23 13:53:25 +02:00
|
|
|
<Key extends keyof E['Variables']>(key: Key): E['Variables'][Key]
|
2022-07-31 15:19:28 +02:00
|
|
|
<T = any>(key: string): T
|
|
|
|
}
|
2022-07-10 16:44:23 +02:00
|
|
|
pretty: (prettyJSON: boolean, space?: number) => void
|
|
|
|
newResponse: (data: Data | null, status: StatusCode, headers: Headers) => Response
|
|
|
|
body: (data: Data | null, status?: StatusCode, headers?: Headers) => Response
|
|
|
|
text: (text: string, status?: StatusCode, headers?: Headers) => Response
|
|
|
|
json: <T>(object: T, status?: StatusCode, headers?: Headers) => Response
|
|
|
|
html: (html: string, status?: StatusCode, headers?: Headers) => Response
|
|
|
|
redirect: (location: string, status?: StatusCode) => Response
|
2022-07-10 17:17:29 +02:00
|
|
|
cookie: (name: string, value: string, options?: CookieOptions) => void
|
2022-07-10 16:44:23 +02:00
|
|
|
notFound: () => Response | Promise<Response>
|
|
|
|
}
|
|
|
|
|
2022-08-23 13:53:25 +02:00
|
|
|
export class HonoContext<
|
|
|
|
RequestParamKeyType extends string = string,
|
2022-09-20 03:11:34 +02:00
|
|
|
E extends Partial<Environment> = Environment,
|
|
|
|
D extends ValidatedData = ValidatedData
|
|
|
|
> implements Context<RequestParamKeyType, E, D>
|
2022-07-10 16:44:23 +02:00
|
|
|
{
|
2022-09-20 03:11:34 +02:00
|
|
|
req: Request<RequestParamKeyType, D>
|
2022-08-27 16:57:33 +02:00
|
|
|
env: E['Bindings']
|
2022-07-10 16:44:23 +02:00
|
|
|
finalized: boolean
|
|
|
|
|
|
|
|
_status: StatusCode = 200
|
2022-07-17 11:11:09 +02:00
|
|
|
private _executionCtx: FetchEvent | ExecutionContext | undefined
|
2022-07-02 08:09:45 +02:00
|
|
|
private _pretty: boolean = false
|
|
|
|
private _prettySpace: number = 2
|
|
|
|
private _map: Record<string, any> | undefined
|
2022-09-20 14:41:45 +02:00
|
|
|
private _headers: Record<string, string[]> | undefined
|
2022-07-02 08:09:45 +02:00
|
|
|
private _res: Response | undefined
|
2022-09-10 11:00:00 +02:00
|
|
|
private notFoundHandler: NotFoundHandler<E>
|
2022-07-02 08:09:45 +02:00
|
|
|
|
|
|
|
constructor(
|
2022-09-20 03:11:34 +02:00
|
|
|
req: Request<RequestParamKeyType>,
|
2022-08-23 13:53:25 +02:00
|
|
|
env: E['Bindings'] | undefined = undefined,
|
2022-07-17 11:11:09 +02:00
|
|
|
executionCtx: FetchEvent | ExecutionContext | undefined = undefined,
|
2022-09-10 11:00:00 +02:00
|
|
|
notFoundHandler: NotFoundHandler<E> = () => new Response()
|
2022-07-02 08:09:45 +02:00
|
|
|
) {
|
2022-07-17 11:11:09 +02:00
|
|
|
this._executionCtx = executionCtx
|
2022-09-20 03:11:34 +02:00
|
|
|
this.req = req as Request<RequestParamKeyType, D>
|
2022-08-28 11:16:51 +02:00
|
|
|
this.env = env || ({} as Bindings)
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2022-07-17 11:11:09 +02:00
|
|
|
this.notFoundHandler = notFoundHandler
|
|
|
|
this.finalized = false
|
|
|
|
}
|
|
|
|
|
|
|
|
get event(): FetchEvent {
|
|
|
|
if (this._executionCtx instanceof FetchEvent) {
|
|
|
|
return this._executionCtx
|
2022-07-02 08:09:45 +02:00
|
|
|
} else {
|
2022-07-17 11:11:09 +02:00
|
|
|
throw Error('This context has no FetchEvent')
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
2022-07-17 11:11:09 +02:00
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
|
2022-07-17 11:11:09 +02:00
|
|
|
get executionCtx(): ExecutionContext {
|
|
|
|
if (this._executionCtx) {
|
|
|
|
return this._executionCtx
|
|
|
|
} else {
|
|
|
|
throw Error('This context has no ExecutionContext')
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get res(): Response {
|
2022-09-02 14:13:47 +02:00
|
|
|
return (this._res ||= new Response(defaultNotFoundMessage, { status: 404 }))
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set res(_res: Response) {
|
|
|
|
this._res = _res
|
|
|
|
this.finalized = true
|
|
|
|
}
|
|
|
|
|
2022-09-20 14:41:45 +02:00
|
|
|
header(name: string, value: string, options?: { append?: boolean }): void {
|
2022-07-02 08:09:45 +02:00
|
|
|
this._headers ||= {}
|
2022-09-20 14:41:45 +02:00
|
|
|
const key = name.toLowerCase()
|
|
|
|
|
|
|
|
let shouldAppend = false
|
|
|
|
if (options && options.append) {
|
|
|
|
const vAlreadySet = this._headers[key]
|
|
|
|
if (vAlreadySet && vAlreadySet.length) {
|
|
|
|
shouldAppend = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldAppend) {
|
|
|
|
this._headers[key].push(value)
|
|
|
|
} else {
|
|
|
|
this._headers[key] = [value]
|
|
|
|
}
|
|
|
|
|
2022-07-02 08:09:45 +02:00
|
|
|
if (this.finalized) {
|
2022-09-20 14:41:45 +02:00
|
|
|
if (shouldAppend) {
|
|
|
|
this.res.headers.append(name, value)
|
|
|
|
} else {
|
|
|
|
this.res.headers.set(name, value)
|
|
|
|
}
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
status(status: StatusCode): void {
|
|
|
|
this._status = status
|
|
|
|
}
|
|
|
|
|
2022-08-12 04:22:30 +02:00
|
|
|
set<Key extends keyof ContextVariableMap>(key: Key, value: ContextVariableMap[Key]): void
|
2022-08-23 13:53:25 +02:00
|
|
|
set<Key extends keyof E['Variables']>(key: Key, value: E['Variables'][Key]): void
|
2022-08-12 04:22:30 +02:00
|
|
|
set(key: string, value: any): void
|
2022-07-02 08:09:45 +02:00
|
|
|
set(key: string, value: any): void {
|
|
|
|
this._map ||= {}
|
|
|
|
this._map[key] = value
|
|
|
|
}
|
|
|
|
|
2022-08-12 04:22:30 +02:00
|
|
|
get<Key extends keyof ContextVariableMap>(key: Key): ContextVariableMap[Key]
|
2022-08-23 13:53:25 +02:00
|
|
|
get<Key extends keyof E['Variables']>(key: Key): E['Variables'][Key]
|
2022-08-12 04:22:30 +02:00
|
|
|
get<T = any>(key: string): T
|
2022-07-02 08:09:45 +02:00
|
|
|
get(key: string) {
|
|
|
|
if (!this._map) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return this._map[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
pretty(prettyJSON: boolean, space: number = 2): void {
|
|
|
|
this._pretty = prettyJSON
|
|
|
|
this._prettySpace = space
|
|
|
|
}
|
|
|
|
|
|
|
|
newResponse(data: Data | null, status: StatusCode, headers: Headers = {}): Response {
|
2022-09-20 14:41:45 +02:00
|
|
|
return new Response(data, {
|
|
|
|
status: status || this._status || 200,
|
|
|
|
headers: this._finalizeHeaders(headers),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private _finalizeHeaders(incomingHeaders: Headers): HeaderField[] {
|
|
|
|
const finalizedHeaders: HeaderField[] = []
|
|
|
|
const headersKv = this._headers || {}
|
|
|
|
// If Response is already set
|
2022-07-02 08:09:45 +02:00
|
|
|
if (this._res) {
|
|
|
|
this._res.headers.forEach((v, k) => {
|
2022-09-20 14:41:45 +02:00
|
|
|
headersKv[k] = [v]
|
2022-07-02 08:09:45 +02:00
|
|
|
})
|
|
|
|
}
|
2022-09-20 14:41:45 +02:00
|
|
|
for (const key of Object.keys(incomingHeaders)) {
|
|
|
|
const value = incomingHeaders[key]
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
finalizedHeaders.push([key, value])
|
|
|
|
} else {
|
|
|
|
for (const v of value) {
|
|
|
|
finalizedHeaders.push([key, v])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete headersKv[key]
|
|
|
|
}
|
|
|
|
for (const key of Object.keys(headersKv)) {
|
|
|
|
for (const value of headersKv[key]) {
|
|
|
|
const kv: HeaderField = [key, value]
|
|
|
|
finalizedHeaders.push(kv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return finalizedHeaders
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
body(data: Data | null, status: StatusCode = this._status, headers: Headers = {}): Response {
|
|
|
|
return this.newResponse(data, status, headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
text(text: string, status: StatusCode = this._status, headers: Headers = {}): Response {
|
2022-08-10 14:34:33 +02:00
|
|
|
headers['content-type'] = 'text/plain; charset=UTF-8'
|
2022-07-02 08:09:45 +02:00
|
|
|
return this.body(text, status, headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
json<T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response {
|
|
|
|
const body = this._pretty
|
|
|
|
? JSON.stringify(object, null, this._prettySpace)
|
|
|
|
: JSON.stringify(object)
|
2022-08-10 14:34:33 +02:00
|
|
|
headers['content-type'] = 'application/json; charset=UTF-8'
|
2022-07-02 08:09:45 +02:00
|
|
|
return this.body(body, status, headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response {
|
2022-08-10 14:34:33 +02:00
|
|
|
headers['content-type'] = 'text/html; charset=UTF-8'
|
2022-07-02 08:09:45 +02:00
|
|
|
return this.body(html, status, headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
redirect(location: string, status: StatusCode = 302): Response {
|
|
|
|
return this.newResponse(null, status, {
|
|
|
|
Location: location,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-10 17:17:29 +02:00
|
|
|
cookie(name: string, value: string, opt?: CookieOptions): void {
|
|
|
|
const cookie = serialize(name, value, opt)
|
2022-09-20 14:41:45 +02:00
|
|
|
this.header('set-cookie', cookie, { append: true })
|
2022-07-10 17:17:29 +02:00
|
|
|
}
|
|
|
|
|
2022-07-02 08:09:45 +02:00
|
|
|
notFound(): Response | Promise<Response> {
|
|
|
|
return this.notFoundHandler(this as any)
|
|
|
|
}
|
|
|
|
}
|