Skip to content

Commit

Permalink
Lambda doc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Mar 12, 2024
1 parent aeab519 commit 1aeaa09
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -4,17 +4,17 @@ Run Hummingbird inside an AWS Lambda

## Usage

Create struct conforming to `Lambda`. Setup your application in the `init` function: add your middleware, add route handlers etc
Create struct conforming to `LambdaFunction`. Setup your application in the `init` function: add your middleware, add route handlers etc

```swift
@main
struct MyHandler: Lambda {
struct MyHandler: LambdaFunction {
// define input and output
typealias Event = APIGatewayRequest
typealias Output = APIGatewayResponse

init(_ app: Application) {
app.middleware.add(HBLogRequestsMiddleware(.debug))
app.middleware.add(LogRequestsMiddleware(.debug))
app.router.get("hello") { _, _ in
return "Hello"
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdLambda/HBLambdaRequestContext.swift
Expand Up @@ -18,8 +18,8 @@ import NIOCore
/// A Request Context that is initialized with the Event that triggered the Lambda
///
/// All Hummingbird Lambdas require that your request context conforms to
/// `HBLambdaRequestContext`. By default ``HBLambda`` will use ``HBBasicLambdaRequestContext``
/// for a request context. To get ``HBLambda`` to use a custom context you need to set the
/// LambdaRequestContext`. By default ``LambdaFunction`` will use ``BasicLambdaRequestContext``
/// for a request context. To get ``LambdaFunction`` to use a custom context you need to set the
/// `Context` associatedtype.
public protocol LambdaRequestContext<Event>: BaseRequestContext {
/// The type of event that can trigger the Lambda
Expand Down
14 changes: 7 additions & 7 deletions Sources/HummingbirdLambda/LambdaFunction.swift
Expand Up @@ -21,14 +21,14 @@ import NIOPosix

/// Protocol for Hummingbird Lambdas.
///
/// Defines the `Event` and `Output` types, how you convert from `Event` to ``HummingbirdCore/HBRequest``
/// and ``HummingbirdCore/HBResponse`` to `Output`. Create a type conforming to this protocol and tag it
/// Defines the `Event` and `Output` types, how you convert from `Event` to ``HummingbirdCore/Request``
/// and ``HummingbirdCore/Response`` to `Output`. Create a type conforming to this protocol and tag it
/// with `@main`.
/// ```swift
/// struct MyLambda: LambdaFunction {
/// typealias Event = APIGatewayRequest
/// typealias Output = APIGatewayResponse
/// typealias Context = MyLambdaRequestContext // must conform to `HBLambdaRequestContext`
/// typealias Context = MyLambdaRequestContext // must conform to `LambdaRequestContext`
///
/// init(context: LambdaInitializationContext) {}
///
Expand All @@ -42,7 +42,7 @@ import NIOPosix
/// }
/// }
/// ```
/// - SeeAlso: ``HBAPIGatewayLambda`` and ``HBAPIGatewayV2Lambda`` for specializations of this protocol.
/// - SeeAlso: ``APIGatewayLambdaFunction`` and ``APIGatewayV2LambdaFunction`` for specializations of this protocol.
public protocol LambdaFunction: Sendable {
/// Event that triggers the lambda
associatedtype Event: Decodable
Expand All @@ -61,13 +61,13 @@ public protocol LambdaFunction: Sendable {
/// Called when Lambda is terminating. This is where you can cleanup any resources
func shutdown() async throws

/// Convert from `In` type to `HBRequest`
/// Convert from `In` type to `Request`
/// - Parameters:
/// - context: Lambda context
/// - from: input type
func request(context: LambdaContext, from: Event) throws -> Request

/// Convert from `HBResponse` to `Out` type
/// Convert from `Response` to `Out` type
/// - Parameter from: response from Hummingbird
func output(from: Response) async throws -> Output
}
Expand All @@ -79,7 +79,7 @@ extension LambdaFunction {
/// [@main](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID626)
/// attribute, the system calls the conformer's `main()` method to launch the lambda function.
public static func main() throws {
HBLambdaHandler<Self>.main()
LambdaFunctionHandler<Self>.main()
}

public func shutdown() async throws {}
Expand Down
6 changes: 3 additions & 3 deletions Sources/HummingbirdLambda/LambdaHandler.swift
Expand Up @@ -19,16 +19,16 @@ import NIOCore
import NIOPosix

/// Specialization of LambdaHandler which runs an Lambda
struct HBLambdaHandler<L: LambdaFunction>: LambdaHandler {
struct LambdaFunctionHandler<L: LambdaFunction>: LambdaHandler {
public typealias Event = L.Event
public typealias Output = L.Output

let lambda: L
let responder: L.Responder

/// Initialize `HBLambdaHandler`.
/// Initialize `LambdaHandler`.
///
/// Create application, set it up and create `HBLambda` from application and create responder
/// Create `LambdaFunction` from context and create responder
/// - Parameter context: Lambda initialization context
public init(context: LambdaInitializationContext) async throws {
let lambda = try await L(context: context)
Expand Down
8 changes: 4 additions & 4 deletions Sources/HummingbirdLambdaTesting/HBLambdaTestFramework.swift
Expand Up @@ -21,12 +21,12 @@ import Logging
import NIOCore
import NIOPosix

class HBLambdaTestFramework<Lambda: LambdaFunction> where Lambda.Event: LambdaTestableEvent {
class LambdaTestFramework<Lambda: LambdaFunction> where Lambda.Event: LambdaTestableEvent {
let context: LambdaContext
var terminator: LambdaTerminator

init(logLevel: Logger.Level) {
var logger = Logger(label: "HBTestLambda")
var logger = Logger(label: "TestLambda")
logger.logLevel = logLevel
self.context = .init(
requestID: UUID().uuidString,
Expand All @@ -52,7 +52,7 @@ class HBLambdaTestFramework<Lambda: LambdaFunction> where Lambda.Event: LambdaTe
}

func run<Value>(_ test: @escaping @Sendable (LambdaTestClient<Lambda>) async throws -> Value) async throws -> Value {
let handler = try await HBLambdaHandler<Lambda>(context: self.initializationContext)
let handler = try await LambdaFunctionHandler<Lambda>(context: self.initializationContext)
let value = try await test(LambdaTestClient(handler: handler, context: context))
try await self.terminator.terminate(eventLoop: self.context.eventLoop).get()
self.terminator = .init()
Expand All @@ -62,7 +62,7 @@ class HBLambdaTestFramework<Lambda: LambdaFunction> where Lambda.Event: LambdaTe

/// Client used to send requests to lambda test framework
public struct LambdaTestClient<Lambda: LambdaFunction> where Lambda.Event: LambdaTestableEvent {
let handler: HBLambdaHandler<Lambda>
let handler: LambdaFunctionHandler<Lambda>
let context: LambdaContext

func execute(uri: String, method: HTTPRequest.Method, headers: HTTPFields, body: ByteBuffer?) async throws -> Lambda.Output {
Expand Down
8 changes: 4 additions & 4 deletions Sources/HummingbirdLambdaTesting/Lambda+Testing.swift
Expand Up @@ -16,19 +16,19 @@ import HummingbirdLambda
import Logging

extension LambdaFunction where Event: LambdaTestableEvent {
/// Test `HBLambda`
/// Test `LambdaFunction`
///
/// The `test` closure uses the provided test client to make calls to the
/// lambda via `execute`. You can verify the contents of the output
/// event returned.
///
/// The example below is using the `.router` framework to test
/// ```swift
/// struct HelloLambda: HBAPIGatewayLambda {
/// struct HelloLambda: APIGatewayLambdaFunction {
/// init(context: LambdaInitializationContext) {}
///
/// func buildResponder() -> some HTTPResponder<Context> {
/// let router = HBRouter(context: Context.self)
/// let router = Router(context: Context.self)
/// router.get("hello") { request, _ in
/// return "Hello"
/// }
Expand All @@ -45,7 +45,7 @@ extension LambdaFunction where Event: LambdaTestableEvent {
logLevel: Logger.Level = .debug,
_ test: @escaping @Sendable (LambdaTestClient<Self>) async throws -> Value
) async throws -> Value {
let lambda = HBLambdaTestFramework<Self>(logLevel: logLevel)
let lambda = LambdaTestFramework<Self>(logLevel: logLevel)
return try await lambda.run(test)
}
}

0 comments on commit 1aeaa09

Please sign in to comment.