@@ -18,17 +18,17 @@ import OpenAPIRuntime
1818import HTTPTypes
1919
2020/// Specialization of LambdaHandler which runs an OpenAPILambda
21- struct OpenAPILambdaHandler < L : OpenAPILambda > {
21+ public struct OpenAPILambdaHandler < OALS : OpenAPILambdaService > : Sendable {
2222
2323 private let router : OpenAPILambdaRouter
2424 private let transport : OpenAPILambdaTransport
25- private let lambda : L
25+ private let openAPIService : OALS
2626
2727 /// the input type for this Lambda handler (received from the `OpenAPILambda`)
28- public typealias Event = L . Event
28+ public typealias Event = OALS . Event
2929
3030 /// the output type for this Lambda handler (received from the `OpenAPILambda`)
31- public typealias Output = L . Output
31+ public typealias Output = OALS . Output
3232
3333 /// Initialize `OpenAPILambdaHandler`.
3434 ///
@@ -38,7 +38,23 @@ struct OpenAPILambdaHandler<L: OpenAPILambda> {
3838 init ( ) throws {
3939 self . router = TrieRouter ( )
4040 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)
4258 }
4359
4460 /// The Lambda handling method.
@@ -49,14 +65,14 @@ struct OpenAPILambdaHandler<L: OpenAPILambda> {
4965 /// - context: Runtime ``LambdaContext``.
5066 ///
5167 /// - 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 {
5369
5470 // by default returns HTTP 500
5571 var lambdaResponse : OpenAPILambdaResponse = ( HTTPResponse ( status: . internalServerError) , " unknown error " )
5672
5773 do {
5874 // 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)
6076
6177 // route the request to find the handlers and extract the paramaters
6278 let ( handler, parameters) = try router. route ( method: request. 0 . method, path: request. 0 . path!)
@@ -105,6 +121,6 @@ struct OpenAPILambdaHandler<L: OpenAPILambda> {
105121 }
106122
107123 // transform the OpenAPILambdaResponse to the Lambda Output
108- return lambda . output ( from: lambdaResponse)
124+ return openAPIService . output ( from: lambdaResponse)
109125 }
110126}
0 commit comments