@@ -5,25 +5,32 @@ import { Context, Effect, type ManagedRuntime, type Scope } from "effect";
55import { AppRuntime } from "./runtime" ;
66import { AstroContext } from "./tags" ;
77
8- export const makeAstroEndpoint = async < E > (
9- app : HttpApp . Default < E , AstroContext | Scope . Scope | ManagedRuntime . ManagedRuntime . Context < typeof AppRuntime > > ,
8+ export const makeAstroEndpoint = < E > (
9+ app : HttpApp . Default <
10+ E ,
11+ | AstroContext
12+ | Scope . Scope
13+ | HttpServerRequest . HttpServerRequest
14+ | ManagedRuntime . ManagedRuntime . Context < typeof AppRuntime >
15+ > ,
1016 middleware ?: HttpMiddleware . HttpMiddleware | undefined
11- ) : Promise < APIRoute > => {
12- const runtime = await AppRuntime . runtime ( ) ;
13- const emptyAstroContext = Context . empty ( ) as Context . Context < AstroContext > ;
17+ ) : APIRoute => {
18+ type R = ManagedRuntime . ManagedRuntime . Context < typeof AppRuntime > ;
19+
20+ let cachedHandler :
21+ | ( ( request : Request , context ?: Context . Context < never > | undefined ) => Promise < Response > )
22+ | undefined = undefined ;
23+
1424 const appWithoutAstroContext = Effect . mapInputContext (
1525 app ,
16- (
17- requiredContext : Context . Context <
18- | Scope . Scope
19- | HttpServerRequest . HttpServerRequest
20- | ManagedRuntime . ManagedRuntime . Context < typeof AppRuntime >
21- >
22- ) => Context . merge ( requiredContext , emptyAstroContext )
26+ ( requiredContext : Context . Context < Scope . Scope | HttpServerRequest . HttpServerRequest | R > ) =>
27+ Context . merge ( requiredContext , Context . empty ( ) as Context . Context < AstroContext > )
2328 ) ;
24- const handler = HttpApp . toWebHandlerRuntime ( runtime ) ( appWithoutAstroContext , middleware ) ;
29+
2530 return async ( apiContext ) => {
31+ const runtime = await AppRuntime . runtime ( ) ;
32+ cachedHandler ??= HttpApp . toWebHandlerRuntime ( runtime ) ( appWithoutAstroContext , middleware ) ;
2633 const context = Context . make ( AstroContext , apiContext ) ;
27- return await handler ( apiContext . request , context ) ;
34+ return await cachedHandler ( apiContext . request , context ) ;
2835 } ;
2936} ;
0 commit comments