11/**
22 * @since 1.4.0
33 */
4- import type { HttpApi , HttpRouter , HttpServerError } from "@effect/platform" ;
4+ import type { HttpApi , HttpRouter } from "@effect/platform" ;
55import { HttpApiBuilder , HttpApp } from "@effect/platform" ;
66import type { Cause } from "effect" ;
77import { Context , Effect , Function , Layer } from "effect" ;
88import { getEventSource } from "./internal/index.js" ;
9- import type { EventSource , LambdaEvent , LambdaResult } from "./internal/types.js" ;
9+ import * as internal from "./internal/lambdaHandler.js" ;
10+ import type { EventSource } from "./internal/types.js" ;
1011import { encodeBase64 , isContentEncodingBinary , isContentTypeBinary } from "./internal/utils.js" ;
1112import * as LambdaRuntime from "./LambdaRuntime.js" ;
12- import type { EffectHandler , EffectHandlerWithLayer , Handler } from "./Types.js" ;
13+ import type {
14+ ALBEvent ,
15+ ALBResult ,
16+ APIGatewayProxyEvent ,
17+ APIGatewayProxyEventV2 ,
18+ APIGatewayProxyResult ,
19+ APIGatewayProxyResultV2 ,
20+ CloudFrontRequestEvent ,
21+ DynamoDBStreamEvent ,
22+ EffectHandler ,
23+ EffectHandlerWithLayer ,
24+ EventBridgeEvent ,
25+ Handler ,
26+ KinesisStreamEvent ,
27+ LambdaContext ,
28+ S3Event ,
29+ SelfManagedKafkaEvent ,
30+ SNSEvent ,
31+ SQSEvent ,
32+ } from "./Types.js" ;
33+
34+ /**
35+ * @since 1.4.0
36+ */
37+ export declare namespace LambdaHandler {
38+ /**
39+ * @since 1.4.0
40+ * @category model
41+ */
42+ export type Event =
43+ | ALBEvent
44+ | APIGatewayProxyEvent
45+ | APIGatewayProxyEventV2
46+ | EventBridgeEvent < string , unknown >
47+ | DynamoDBStreamEvent
48+ | KinesisStreamEvent
49+ | S3Event
50+ | SelfManagedKafkaEvent
51+ | SNSEvent
52+ | SQSEvent
53+ | CloudFrontRequestEvent ;
54+
55+ /**
56+ * @since 1.4.0
57+ * @category model
58+ */
59+ export type Result =
60+ | ALBResult
61+ | APIGatewayProxyResult
62+ | APIGatewayProxyResultV2
63+ | void ;
64+ }
65+
66+ /**
67+ * @since 1.4.0
68+ * @category context
69+ */
70+ export const event = < T extends LambdaHandler . Event > ( ) : Effect . Effect < T > =>
71+ Effect . map (
72+ Effect . context < never > ( ) ,
73+ ( context ) => Context . unsafeGet ( context , internal . lambdaEventTag ) ,
74+ ) as Effect . Effect < T > ;
75+
76+ /**
77+ * @since 1.4.0
78+ * @category context
79+ */
80+ export const context = ( ) : Effect . Effect < LambdaContext > =>
81+ Effect . map (
82+ Effect . context < never > ( ) ,
83+ ( context ) => Context . unsafeGet ( context , internal . lambdaContextTag ) ,
84+ ) ;
1385
1486/**
1587 * Makes a lambda handler from the given EffectHandler and optional global layer.
1688 * The global layer is used to provide a runtime which will gracefully handle lambda termination during down-scaling.
1789 *
1890 * @example
19- * import { LambdaHandler } from "@effect-aws/lambda"
20- * import { Context } from "aws-lambda";
91+ * import { LambdaHandler, LambdaContext } from "@effect-aws/lambda"
2192 * import { Effect } from "effect";
2293 *
23- * const effectHandler = (event: unknown, context: Context ) => {
94+ * const effectHandler = (event: unknown, context: LambdaContext ) => {
2495 * return Effect.logInfo("Hello, world!");
2596 * };
2697 *
2798 * export const handler = LambdaHandler.make(effectHandler);
2899 *
29100 * @example
30- * import { LambdaHandler } from "@effect-aws/lambda"
31- * import { Context } from "aws-lambda";
101+ * import { LambdaHandler, LambdaContext } from "@effect-aws/lambda"
32102 * import { Effect, Logger } from "effect";
33103 *
34- * const effectHandler = (event: unknown, context: Context ) => {
104+ * const effectHandler = (event: unknown, context: LambdaContext ) => {
35105 * return Effect.logInfo("Hello, world!");
36106 * };
37107 *
@@ -107,11 +177,18 @@ const WebHandler = Context.GenericTag<WebHandler>("@effect-aws/lambda/WebHandler
107177export const makeWebHandler = ( options ?: Pick < HttpApiOptions , "middleware" > ) : Effect . Effect <
108178 WebHandler ,
109179 never ,
110- HttpApiBuilder . Router | HttpApi . Api | HttpRouter . HttpRouter . DefaultServices | HttpApiBuilder . Middleware
180+ | HttpApiBuilder . Router
181+ | HttpApi . Api
182+ | HttpRouter . HttpRouter . DefaultServices
183+ | HttpApiBuilder . Middleware
184+ | LambdaHandler . Event
185+ | LambdaContext
111186> =>
112187 Effect . gen ( function * ( ) {
113188 const app = yield * HttpApiBuilder . httpApp ;
114- const rt = yield * Effect . runtime < HttpRouter . HttpRouter . DefaultServices > ( ) ;
189+ const rt = yield * Effect . runtime <
190+ HttpRouter . HttpRouter . DefaultServices | LambdaHandler . Event | LambdaContext
191+ > ( ) ;
115192 return HttpApp . toWebHandlerRuntime ( rt ) (
116193 options ?. middleware ? options . middleware ( app as any ) as any : app ,
117194 ) ;
@@ -123,14 +200,15 @@ export const makeWebHandler = (options?: Pick<HttpApiOptions, "middleware">): Ef
123200 * @since 1.4.0
124201 * @category constructors
125202 */
126- export const httpApiHandler : EffectHandler <
127- LambdaEvent ,
128- WebHandler ,
129- HttpServerError . ResponseError | Cause . UnknownException ,
130- LambdaResult
131- > = ( event ) =>
203+ export const httpApiHandler = ( options ?: Pick < HttpApiOptions , "middleware" > ) : EffectHandler <
204+ LambdaHandler . Event ,
205+ HttpApi . Api | HttpApiBuilder . Router | HttpRouter . HttpRouter . DefaultServices | HttpApiBuilder . Middleware ,
206+ Cause . UnknownException ,
207+ LambdaHandler . Result
208+ > =>
209+ ( event , context ) =>
132210 Effect . gen ( function * ( ) {
133- const eventSource = getEventSource ( event ) as EventSource < LambdaEvent , LambdaResult > ;
211+ const eventSource = getEventSource ( event ) as EventSource < LambdaHandler . Event , LambdaHandler . Result > ;
134212 const requestValues = eventSource . getRequest ( event ) ;
135213
136214 const req = new Request (
@@ -142,7 +220,11 @@ export const httpApiHandler: EffectHandler<
142220 } ,
143221 ) ;
144222
145- const res = yield * WebHandler . pipe ( Effect . andThen ( ( handler ) => handler ( req ) ) ) ;
223+ const res = yield * makeWebHandler ( options ) . pipe (
224+ Effect . provideService ( internal . lambdaEventTag , event ) ,
225+ Effect . provideService ( internal . lambdaContextTag , context ) ,
226+ Effect . andThen ( ( handler ) => handler ( req ) ) ,
227+ ) ;
146228
147229 const contentType = res . headers . get ( "content-type" ) ;
148230 let isBase64Encoded = contentType && isContentTypeBinary ( contentType ) ? true : false ;
@@ -213,9 +295,7 @@ export const httpApiHandler: EffectHandler<
213295export const fromHttpApi = < LA , LE > (
214296 layer : Layer . Layer < LA | HttpApi . Api | HttpRouter . HttpRouter . DefaultServices , LE > ,
215297 options ?: HttpApiOptions ,
216- ) : Handler < LambdaEvent , LambdaResult > => {
217- const httpApiLayer = Layer . effect ( WebHandler , makeWebHandler ( options ) ) . pipe (
218- Layer . provide ( Layer . mergeAll ( layer , HttpApiBuilder . Router . Live , HttpApiBuilder . Middleware . layer ) ) ,
219- ) ;
220- return make ( { handler : httpApiHandler , layer : httpApiLayer , memoMap : options ?. memoMap } ) ;
298+ ) : Handler < LambdaHandler . Event , LambdaHandler . Result > => {
299+ const httpApiLayer = Layer . mergeAll ( layer , HttpApiBuilder . Router . Live , HttpApiBuilder . Middleware . layer ) ;
300+ return make ( { handler : httpApiHandler ( options ) , layer : httpApiLayer , memoMap : options ?. memoMap } ) ;
221301} ;
0 commit comments