-
-
Notifications
You must be signed in to change notification settings - Fork 701
/
Copy pathhandler.ts
120 lines (106 loc) · 3.31 KB
/
handler.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
import { Context } from '../../context'
import type { Hono } from '../../hono'
import { HTTPException } from '../../http-exception'
import type { BlankSchema, DefaultEnv, Env, Input, MiddlewareHandler, Schema } from '../../types'
// Ref: https://github.com/cloudflare/workerd/blob/main/types/defines/pages.d.ts
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Params<P extends string = any> = Record<P, string | string[]>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type EventContext<Env = {}, P extends string = any, Data = Record<string, unknown>> = {
request: Request
functionPath: string
waitUntil: (promise: Promise<unknown>) => void
passThroughOnException: () => void
next: (input?: Request | string, init?: RequestInit) => Promise<Response>
env: Env & { ASSETS: { fetch: typeof fetch } }
params: Params<P>
data: Data
}
declare type PagesFunction<
Env = unknown,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Params extends string = any,
Data extends Record<string, unknown> = Record<string, unknown>
> = (context: EventContext<Env, Params, Data>) => Response | Promise<Response>
export const handle =
<E extends Env = DefaultEnv, S extends Schema = BlankSchema, BasePath extends string = '/'>(
app: Hono<E, S, BasePath>
): PagesFunction<E['Bindings']> =>
(eventContext) => {
return app.fetch(
eventContext.request,
{ ...eventContext.env, eventContext },
{
waitUntil: eventContext.waitUntil,
passThroughOnException: eventContext.passThroughOnException,
}
)
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function handleMiddleware<E extends Env = {}, P extends string = any, I extends Input = {}>(
middleware: MiddlewareHandler<
E & {
Bindings: {
eventContext: EventContext
}
},
P,
I
>
): PagesFunction<E['Bindings']> {
return async (executionCtx) => {
const context = new Context(executionCtx.request, {
env: { ...executionCtx.env, eventContext: executionCtx },
executionCtx,
})
let response: Response | void = undefined
try {
response = await middleware(context, async () => {
try {
context.res = await executionCtx.next()
} catch (error) {
if (error instanceof Error) {
context.error = error
} else {
throw error
}
}
})
} catch (error) {
if (error instanceof Error) {
context.error = error
} else {
throw error
}
}
if (response) {
return response
}
if (context.error instanceof HTTPException) {
return context.error.getResponse()
}
if (context.error) {
throw context.error
}
return context.res
}
}
declare abstract class FetcherLike {
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>
}
/**
*
* @description `serveStatic()` is for advanced mode:
* https://developers.cloudflare.com/pages/platform/functions/advanced-mode/#set-up-a-function
*
*/
export const serveStatic = (): MiddlewareHandler => {
return async (c) => {
const env = c.env as unknown as { ASSETS: FetcherLike }
const res = await env.ASSETS.fetch(c.req.raw)
if (res.status === 404) {
return c.notFound()
}
return res
}
}