diff --git a/src/adapter/cloudflare-pages/handler.test.ts b/src/adapter/cloudflare-pages/handler.test.ts index d08e31dc..747d11e0 100644 --- a/src/adapter/cloudflare-pages/handler.test.ts +++ b/src/adapter/cloudflare-pages/handler.test.ts @@ -230,6 +230,18 @@ describe('Middleware adapter for Cloudflare Pages', () => { await expect(handler(eventContext)).rejects.toThrowError('Something went wrong') expect(next).not.toHaveBeenCalled() }) + + it('Should set the data in eventContext.data', async () => { + const next = vi.fn() + const eventContext = createEventContext({ next }) + const handler = handleMiddleware(async (c, next) => { + c.env.eventContext.data.user = 'Joe' + await next() + }) + expect(eventContext.data.user).toBeUndefined() + await handler(eventContext) + expect(eventContext.data.user).toBe('Joe') + }) }) describe('serveStatic()', () => { diff --git a/src/adapter/cloudflare-pages/handler.ts b/src/adapter/cloudflare-pages/handler.ts index a3b98857..6eb7df40 100644 --- a/src/adapter/cloudflare-pages/handler.ts +++ b/src/adapter/cloudflare-pages/handler.ts @@ -9,7 +9,7 @@ import type { BlankSchema, Env, Input, MiddlewareHandler, Schema } from '../../t type Params

= Record // eslint-disable-next-line @typescript-eslint/no-explicit-any -export type EventContext = { +export type EventContext> = { request: Request functionPath: string waitUntil: (promise: Promise) => void @@ -43,12 +43,20 @@ export const handle = } // eslint-disable-next-line @typescript-eslint/no-explicit-any -export function handleMiddleware( - middleware: MiddlewareHandler +export function handleMiddleware( + middleware: MiddlewareHandler< + E & { + Bindings: { + eventContext: EventContext + } + }, + P, + I + > ): PagesFunction { return async (executionCtx) => { const context = new Context(executionCtx.request, { - env: executionCtx.env, + env: { ...executionCtx.env, eventContext: executionCtx }, executionCtx, })