/
APIGatewayV2Lambda.swift
89 lines (82 loc) · 3.21 KB
/
APIGatewayV2Lambda.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import AWSLambdaEvents
import AWSLambdaRuntimeCore
import Hummingbird
import NIOCore
import NIOHTTP1
/// Protocol for Hummingbird Lambdas that use APIGatewayV2
///
/// With this protocol you no longer need to set the `Event` and `Output`
/// associated values.
/// ```swift
/// struct MyLambda: HBAPIGatewayLambda {
/// typealias Context = MyLambdaRequestContext
///
/// init(context: LambdaInitializationContext) {}
///
/// /// build responder that will create a response from a request
/// func buildResponder() -> some HBResponder<Context> {
/// let router = HBRouter(context: Context.self)
/// router.get("hello") { _,_ in
/// "Hello"
/// }
/// return router.buildResponder()
/// }
/// }
/// ```
public protocol HBAPIGatewayV2Lambda: HBLambda where Event == APIGatewayV2Request, Output == APIGatewayV2Response {
associatedtype Context = HBBasicLambdaRequestContext<APIGatewayV2Request>
}
extension HBLambda where Event == APIGatewayV2Request {
/// Specialization of HBLambda.request where `Event` is `APIGatewayV2Request`
public func request(context: LambdaContext, from: Event) throws -> HBRequest {
return try HBRequest(context: context, from: from)
}
}
extension HBLambda where Output == APIGatewayV2Response {
/// Specialization of HBLambda.request where `Output` is `APIGatewayV2Response`
public func output(from response: HBResponse) async throws -> Output {
return try await response.apiResponse()
}
}
// conform `APIGatewayV2Request` to `APIRequest` so we can use HBRequest.init(context:application:from)
extension APIGatewayV2Request: APIRequest {
var path: String {
return context.http.path
}
var httpMethod: AWSLambdaEvents.HTTPMethod { context.http.method }
var queryString: String { self.rawQueryString }
var httpHeaders: [(name: String, value: String)] {
self.headers.flatMap { header in
let headers = header.value
.split(separator: ",")
.map { (name: header.key, value: String($0.drop(while: \.isWhitespace))) }
return headers
}
}
}
// conform `APIGatewayV2Response` to `APIResponse` so we can use HBResponse.apiReponse()
extension APIGatewayV2Response: APIResponse {
init(
statusCode: AWSLambdaEvents.HTTPResponseStatus,
headers: AWSLambdaEvents.HTTPHeaders?,
multiValueHeaders: HTTPMultiValueHeaders?,
body: String?,
isBase64Encoded: Bool?
) {
precondition(multiValueHeaders == nil || multiValueHeaders?.count == 0, "Multi value headers are unavailable in APIGatewayV2")
self.init(statusCode: statusCode, headers: headers, body: body, isBase64Encoded: isBase64Encoded, cookies: nil)
}
}