@@ -18,17 +18,17 @@ import OpenAPIRuntime
18
18
import HTTPTypes
19
19
20
20
/// Specialization of LambdaHandler which runs an OpenAPILambda
21
- struct OpenAPILambdaHandler < L : OpenAPILambda > {
21
+ public struct OpenAPILambdaHandler < OALS : OpenAPILambdaService > : Sendable {
22
22
23
23
private let router : OpenAPILambdaRouter
24
24
private let transport : OpenAPILambdaTransport
25
- private let lambda : L
25
+ private let openAPIService : OALS
26
26
27
27
/// the input type for this Lambda handler (received from the `OpenAPILambda`)
28
- public typealias Event = L . Event
28
+ public typealias Event = OALS . Event
29
29
30
30
/// the output type for this Lambda handler (received from the `OpenAPILambda`)
31
- public typealias Output = L . Output
31
+ public typealias Output = OALS . Output
32
32
33
33
/// Initialize `OpenAPILambdaHandler`.
34
34
///
@@ -38,7 +38,23 @@ struct OpenAPILambdaHandler<L: OpenAPILambda> {
38
38
init ( ) throws {
39
39
self . router = TrieRouter ( )
40
40
self . transport = OpenAPILambdaTransport ( router: self . router)
41
- self . lambda = try . init( transport: self . transport)
41
+
42
+ // decouple the OpenAPILambda creation from the registration of teh transport
43
+ // this allows users to provide their own overloaded init() function to inject dependencies.
44
+ self . openAPIService = OALS ( )
45
+ try self . openAPIService. register ( transport: self . transport)
46
+ }
47
+
48
+ /// Initialize an `OpenAPILambdaHandler` with your own `OpenAPILambda` object.
49
+ ///
50
+ /// Use this function when you need to inject dependencies in your OpenAPILambda
51
+ /// - Parameters:
52
+ /// - lambda: The `OpenAPILambda` instance to use.
53
+ public init ( service: OALS ) throws {
54
+ self . router = TrieRouter ( )
55
+ self . transport = OpenAPILambdaTransport ( router: self . router)
56
+ self . openAPIService = service
57
+ try self . openAPIService. register ( transport: self . transport)
42
58
}
43
59
44
60
/// The Lambda handling method.
@@ -49,14 +65,14 @@ struct OpenAPILambdaHandler<L: OpenAPILambda> {
49
65
/// - context: Runtime ``LambdaContext``.
50
66
///
51
67
/// - Returns: A Lambda result ot type `Output`.
52
- func handler( event: L . Event , context: LambdaContext ) async throws -> L . Output {
68
+ public func handler( event: OALS . Event , context: LambdaContext ) async throws -> OALS . Output {
53
69
54
70
// by default returns HTTP 500
55
71
var lambdaResponse : OpenAPILambdaResponse = ( HTTPResponse ( status: . internalServerError) , " unknown error " )
56
72
57
73
do {
58
74
// convert Lambda event source to OpenAPILambdaRequest
59
- let request = try lambda . request ( context: context, from: event)
75
+ let request = try openAPIService . request ( context: context, from: event)
60
76
61
77
// route the request to find the handlers and extract the paramaters
62
78
let ( handler, parameters) = try router. route ( method: request. 0 . method, path: request. 0 . path!)
@@ -105,6 +121,6 @@ struct OpenAPILambdaHandler<L: OpenAPILambda> {
105
121
}
106
122
107
123
// transform the OpenAPILambdaResponse to the Lambda Output
108
- return lambda . output ( from: lambdaResponse)
124
+ return openAPIService . output ( from: lambdaResponse)
109
125
}
110
126
}
0 commit comments