0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-30 01:56:18 +01:00

refactor(context): executionCtx always returns ExecutionContext. (#399)

This commit is contained in:
Taku Amano 2022-07-17 18:07:39 +09:00 committed by GitHub
parent f573687abd
commit 2e3919aa4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,8 +11,8 @@ type Env = Record<string, any>
export interface Context<RequestParamKeyType extends string = string, E = Env> {
req: Request<RequestParamKeyType>
env: E
event: FetchEvent | undefined
executionCtx: ExecutionContext | undefined
event: FetchEvent
executionCtx: ExecutionContext
finalized: boolean
get res(): Response
@ -37,11 +37,10 @@ export class HonoContext<RequestParamKeyType extends string = string, E = Env>
{
req: Request<RequestParamKeyType>
env: E
event: FetchEvent | undefined
executionCtx: ExecutionContext | undefined
finalized: boolean
_status: StatusCode = 200
private _executionCtx: FetchEvent | ExecutionContext | undefined
private _pretty: boolean = false
private _prettySpace: number = 2
private _map: Record<string, any> | undefined
@ -52,22 +51,33 @@ export class HonoContext<RequestParamKeyType extends string = string, E = Env>
constructor(
req: Request,
env: E | undefined = undefined,
eventOrExecutionCtx: FetchEvent | ExecutionContext | undefined = undefined,
executionCtx: FetchEvent | ExecutionContext | undefined = undefined,
notFoundHandler: NotFoundHandler = () => new Response()
) {
this._executionCtx = executionCtx
this.req = req
this.env = env ? env : ({} as E)
if (eventOrExecutionCtx && 'respondWith' in eventOrExecutionCtx) {
this.event = eventOrExecutionCtx
} else {
this.executionCtx = eventOrExecutionCtx
}
this.notFoundHandler = notFoundHandler
this.finalized = false
}
get event(): FetchEvent {
if (this._executionCtx instanceof FetchEvent) {
return this._executionCtx
} else {
throw Error('This context has no FetchEvent')
}
}
get executionCtx(): ExecutionContext {
if (this._executionCtx) {
return this._executionCtx
} else {
throw Error('This context has no ExecutionContext')
}
}
get res(): Response {
return (this._res ||= new Response())
}