Skip to content

Commit 96726af

Browse files
author
Ryan Miville
committed
docs
1 parent 5fb629c commit 96726af

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/glambda.gleam

+45
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
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+
133
import gleam/bit_array
234
import gleam/dict.{type Dict}
335
import gleam/dynamic.{type Dynamic}
@@ -11,18 +43,23 @@ import gleam/regex
1143
import gleam/result
1244
import gleam/string
1345

46+
/// Represents the raw JavaScript event that invoked the lambda.
1447
pub type JsEvent
1548

49+
/// Represents the raw JavaScript context for the lambda.
1650
pub type JsContext
1751

52+
/// Represents the raw JavaScript response for the lambda.
1853
pub type JsResult {
1954
JsResult
2055
Void
2156
}
2257

58+
/// Represents the raw Javascript handler for the lambda.
2359
pub type JsHandler =
2460
fn(JsEvent, JsContext) -> Promise(JsResult)
2561

62+
/// Alias for a strongly typed lambda function
2663
pub type Handler(event, result) =
2764
fn(event, Context) -> Promise(result)
2865

@@ -247,6 +284,8 @@ pub type SqsBatchItemFailure {
247284

248285
// --- Adapters ---------------------------------------------------------------
249286

287+
/// Wraps an HTTP handler to create a lambda function to handle API Gateway proxy
288+
/// events
250289
pub fn http_handler(
251290
handler: Handler(Request(Option(String)), Response(Option(String))),
252291
) -> JsHandler {
@@ -344,6 +383,8 @@ fn is_content_encoding_binary(content_encoding: String) -> Bool {
344383
regex.check(re, content_encoding)
345384
}
346385

386+
/// Wraps a handler to create a lambda function to handle API Gateway proxy
387+
/// events
347388
pub fn api_gateway_proxy_v2_handler(
348389
handler: Handler(ApiGatewayProxyEventV2, ApiGatewayProxyResultV2),
349390
) -> JsHandler {
@@ -355,6 +396,8 @@ pub fn api_gateway_proxy_v2_handler(
355396
}
356397
}
357398

399+
/// Wraps a handler to create a lambda function to handle EventBridge
400+
/// events
358401
pub fn eventbridge_handler(handler: Handler(EventBridgeEvent, Nil)) -> JsHandler {
359402
fn(event: JsEvent, ctx: JsContext) -> Promise(JsResult) {
360403
let event = to_eventbridge_event(event)
@@ -364,6 +407,8 @@ pub fn eventbridge_handler(handler: Handler(EventBridgeEvent, Nil)) -> JsHandler
364407
}
365408
}
366409

410+
/// Wraps a handler to create a lambda function to handle SQS
411+
/// events
367412
pub fn sqs_handler(
368413
handler: Handler(SqsEvent, Option(SqsBatchResponse)),
369414
) -> JsHandler {

0 commit comments

Comments
 (0)