1
+ //// This module provides types and adapters to write AWS Lambda functions that target the Node runtime.
2
+ ////
3
+ //// # Example
4
+ ////
5
+ //// You can use handle API Gateway events using Gleam HTTP types
6
+ ////
7
+ //// ```gleam
8
+ //// fn handle_request(request: Request(Option(String)), ctx: Context) -> Promise(Response(Option(String))) {
9
+ //// Response(200, [], None)
10
+ //// |> promise.resolve
11
+ //// }
12
+ ////
13
+ //// // this is the actual lambda function
14
+ //// fn handler(event, ctx) {
15
+ //// glambda.http_handler(handle_request)
16
+ //// }
17
+ //// ```
18
+ ////
19
+ //// This module also supports other types of events, such as EventBridge.
20
+ ////
21
+ //// ```gleam
22
+ //// fn handle_request(event: EventBridgeEvent, ctx: Context) -> Promise(Nil) {
23
+ //// io.debug(event)
24
+ //// promise.resolve(Nil)
25
+ //// }
26
+ ////
27
+ //// // this is the actual lambda function
28
+ //// fn handler(event, ctx) {
29
+ //// glambda.eventbridge_event_handler(handle_request)
30
+ //// }
31
+ //// ```
32
+
1
33
import gleam/bit_array
2
34
import gleam/dict . { type Dict }
3
35
import gleam/dynamic . { type Dynamic }
@@ -11,18 +43,23 @@ import gleam/regex
11
43
import gleam/result
12
44
import gleam/string
13
45
46
+ /// Represents the raw JavaScript event that invoked the lambda.
14
47
pub type JsEvent
15
48
49
+ /// Represents the raw JavaScript context for the lambda.
16
50
pub type JsContext
17
51
52
+ /// Represents the raw JavaScript response for the lambda.
18
53
pub type JsResult {
19
54
JsResult
20
55
Void
21
56
}
22
57
58
+ /// Represents the raw Javascript handler for the lambda.
23
59
pub type JsHandler =
24
60
fn ( JsEvent , JsContext ) -> Promise ( JsResult )
25
61
62
+ /// Alias for a strongly typed lambda function
26
63
pub type Handler ( event, result) =
27
64
fn ( event, Context ) -> Promise ( result)
28
65
@@ -247,6 +284,8 @@ pub type SqsBatchItemFailure {
247
284
248
285
// --- Adapters ---------------------------------------------------------------
249
286
287
+ /// Wraps an HTTP handler to create a lambda function to handle API Gateway proxy
288
+ /// events
250
289
pub fn http_handler (
251
290
handler : Handler ( Request ( Option ( String ) ) , Response ( Option ( String ) ) ) ,
252
291
) -> JsHandler {
@@ -344,6 +383,8 @@ fn is_content_encoding_binary(content_encoding: String) -> Bool {
344
383
regex . check ( re , content_encoding )
345
384
}
346
385
386
+ /// Wraps a handler to create a lambda function to handle API Gateway proxy
387
+ /// events
347
388
pub fn api_gateway_proxy_v2_handler (
348
389
handler : Handler ( ApiGatewayProxyEventV2 , ApiGatewayProxyResultV2 ) ,
349
390
) -> JsHandler {
@@ -355,6 +396,8 @@ pub fn api_gateway_proxy_v2_handler(
355
396
}
356
397
}
357
398
399
+ /// Wraps a handler to create a lambda function to handle EventBridge
400
+ /// events
358
401
pub fn eventbridge_handler ( handler : Handler ( EventBridgeEvent , Nil ) ) -> JsHandler {
359
402
fn ( event : JsEvent , ctx : JsContext ) -> Promise ( JsResult ) {
360
403
let event = to_eventbridge_event ( event )
@@ -364,6 +407,8 @@ pub fn eventbridge_handler(handler: Handler(EventBridgeEvent, Nil)) -> JsHandler
364
407
}
365
408
}
366
409
410
+ /// Wraps a handler to create a lambda function to handle SQS
411
+ /// events
367
412
pub fn sqs_handler (
368
413
handler : Handler ( SqsEvent , Option ( SqsBatchResponse ) ) ,
369
414
) -> JsHandler {
0 commit comments