diff --git a/README.md b/README.md index 668c41b..b98d3a9 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ import OpenAPILambda // <-- 1. import this library @main // <-- 2. flag this struct as the executable target entrypoint struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi { // <-- 3. add the OpenAPILambdaHttpApi protocol - init(transport: LambdaOpenAPITransport) throws { // <-- 4. add this constructor (don't remove the call to `registerHandlers(on:)`) + init(transport: OpenAPILambdaTransport) throws { // <-- 4. add this constructor (don't remove the call to `registerHandlers(on:)`) try self.registerHandlers(on: transport) } @@ -247,7 +247,7 @@ import OpenAPILambda @main struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi { - init(transport: LambdaOpenAPITransport) throws { + init(transport: OpenAPILambdaTransport) throws { try self.registerHandlers(on: transport) } @@ -494,7 +494,7 @@ Here is an example using `APIGatewayRequest` and `APIGatewayResponse`: struct QuoteServiceLambda: OpenAPILambda { typealias Event = APIGatewayRequest typealias Output = APIGatewayResponse - public init(transport: LambdaOpenAPITransport) throws { + public init(transport: OpenAPILambdaTransport) throws { let openAPIHandler = QuoteServiceImpl() try openAPIHandler.registerHandlers(on: transport) } @@ -529,7 +529,7 @@ extension APIGatewayRequest { /// Return an `HTTPRequest.Method` for this `APIGatewayRequest` public func httpRequestMethod() throws -> HTTPRequest.Method { guard let method = HTTPRequest.Method(rawValue: self.httpMethod.rawValue) else { - throw LambdaOpenAPIHttpError.invalidMethod(self.httpMethod.rawValue) + throw OpenAPILambdaHttpError.invalidMethod(self.httpMethod.rawValue) } return method } diff --git a/Sources/HttpApi/APIGatewayV2+HTTPRequest.swift b/Sources/HttpApi/APIGatewayV2+HTTPRequest.swift index c8b7b73..7e77807 100644 --- a/Sources/HttpApi/APIGatewayV2+HTTPRequest.swift +++ b/Sources/HttpApi/APIGatewayV2+HTTPRequest.swift @@ -21,7 +21,7 @@ extension APIGatewayV2Request { /// Return an `HTTPRequest.Method` for this `APIGatewayV2Request` public func httpRequestMethod() throws -> HTTPRequest.Method { guard let method = HTTPRequest.Method(rawValue: self.context.http.method.rawValue) else { - throw LambdaOpenAPIHttpError.invalidMethod(self.context.http.method.rawValue) + throw OpenAPILambdaHttpError.invalidMethod(self.context.http.method.rawValue) } return method } diff --git a/Sources/HttpApi/OpenAPILambdaHttpApi.swift b/Sources/HttpApi/OpenAPILambdaHttpApi.swift index c533bf0..6220d90 100644 --- a/Sources/HttpApi/OpenAPILambdaHttpApi.swift +++ b/Sources/HttpApi/OpenAPILambdaHttpApi.swift @@ -19,7 +19,7 @@ import OpenAPIRuntime import HTTPTypes /// The errors that can be generated -public enum LambdaOpenAPIHttpError: Error { +public enum OpenAPILambdaHttpError: Error { case invalidMethod(String) } diff --git a/Sources/OpenAPILambda.swift b/Sources/OpenAPILambda.swift index e3a8fc8..9ae193e 100644 --- a/Sources/OpenAPILambda.swift +++ b/Sources/OpenAPILambda.swift @@ -26,7 +26,7 @@ public protocol OpenAPILambda { /// Initialize application. /// /// This is where you create your OpenAPI service implementation and register the transport - init(transport: LambdaOpenAPITransport) throws + init(transport: OpenAPILambdaTransport) throws /// Convert from `Event` type to `OpenAPILambdaRequest` /// - Parameters: diff --git a/Sources/OpenAPILambdaHandler.swift b/Sources/OpenAPILambdaHandler.swift index 56e7416..cf7db4b 100644 --- a/Sources/OpenAPILambdaHandler.swift +++ b/Sources/OpenAPILambdaHandler.swift @@ -33,7 +33,7 @@ public struct OpenAPILambdaHandler: LambdaHandler { /// - context: Lambda initialization context public init(context: LambdaInitializationContext) throws { self.router = TrieRouter() - self.transport = LambdaOpenAPITransport(router: self.router) + self.transport = OpenAPILambdaTransport(router: self.router) self.lambda = try .init(transport: self.transport) } @@ -67,7 +67,7 @@ public struct OpenAPILambdaHandler: LambdaHandler { return lambda.output(from: lambdaResponse) } - let router: LambdaOpenAPIRouter - let transport: LambdaOpenAPITransport + let router: OpenAPILambdaRouter + let transport: OpenAPILambdaTransport let lambda: L } diff --git a/Sources/OpenAPILambdaTransport.swift b/Sources/OpenAPILambdaTransport.swift index d8d938a..0dd6dc8 100644 --- a/Sources/OpenAPILambdaTransport.swift +++ b/Sources/OpenAPILambdaTransport.swift @@ -35,12 +35,12 @@ public typealias OpenAPIHandler = @Sendable (HTTPRequest, HTTPBody?, ServerReque ) /// Lambda Transport for OpenAPI generator -public struct LambdaOpenAPITransport: ServerTransport { +public struct OpenAPILambdaTransport: ServerTransport { - private var router: LambdaOpenAPIRouter + private var router: OpenAPILambdaRouter - /// Create a `LambdaOpenAPITransport` with the given `LambdaOpenAPIRouter` - public init(router: LambdaOpenAPIRouter) { self.router = router } + /// Create a `OpenAPILambdaTransport` with the given `OpenAPILambdaRouter` + public init(router: OpenAPILambdaRouter) { self.router = router } /// Registers an HTTP operation handler at the provided path and method. /// - Parameters: diff --git a/Sources/Router/OpenAPILambdaRouter.swift b/Sources/Router/OpenAPILambdaRouter.swift index fcf5f49..adaa76d 100644 --- a/Sources/Router/OpenAPILambdaRouter.swift +++ b/Sources/Router/OpenAPILambdaRouter.swift @@ -17,14 +17,14 @@ import Foundation import HTTPTypes /// Errors returned by the router -public enum LambdaOpenAPIRouterError: Error { +public enum OpenAPILambdaRouterError: Error { case noRouteForPath(String) case noHandlerForPath(String) case noRouteForMethod(HTTPRequest.Method) } /// A router API -public protocol LambdaOpenAPIRouter { +public protocol OpenAPILambdaRouter { /// add a route for a given HTTP method and path and associate a handler func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) throws diff --git a/Sources/Router/OpenAPILambdaRouterTrie.swift b/Sources/Router/OpenAPILambdaRouterTrie.swift index 0f63668..7032bf3 100644 --- a/Sources/Router/OpenAPILambdaRouterTrie.swift +++ b/Sources/Router/OpenAPILambdaRouterTrie.swift @@ -15,7 +15,7 @@ import HTTPTypes /// A Trie router implementation -public struct TrieRouter: LambdaOpenAPIRouter { +public struct TrieRouter: OpenAPILambdaRouter { private let uriPath: URIPathCollection = URIPath() /// add a route for a given HTTP method and path and associate a handler @@ -88,16 +88,16 @@ struct URIPath: URIPathCollection { /// - the OpenAPIHandler for this path /// - the OpenAI ServerRequestMetadata (a [String:String] with parameter names and their values /// - Throws: - /// - LambdaOpenAPIRouterError.noRouteForPath when there is no handler in the graph for the given combination of HTTP method and path - /// - LambdaOpenAPIRouterError.noRouteForMethod when there is no handler for that HTTP method - /// - LambdaOpenAPIRouterError.noHandlerForPath when there is no handler as leaf node of the tree. This is a programming error and should not happen + /// - OpenAPILambdaRouterError.noRouteForPath when there is no handler in the graph for the given combination of HTTP method and path + /// - OpenAPILambdaRouterError.noRouteForMethod when there is no handler for that HTTP method + /// - OpenAPILambdaRouterError.noHandlerForPath when there is no handler as leaf node of the tree. This is a programming error and should not happen func find(method: HTTPRequest.Method, path: String) throws -> (OpenAPIHandler, OpenAPILambdaRequestParameters) { var parameters: OpenAPILambdaRequestParameters = [:] let root: Node = root() // first node is the HTTP Method guard let nodeHTTP = root.children[method.rawValue] else { - throw LambdaOpenAPIRouterError.noRouteForMethod(method) + throw OpenAPILambdaRouterError.noRouteForMethod(method) } // search for each path component. If a component is not found, it might be a parameter @@ -125,18 +125,18 @@ struct URIPath: URIPathCollection { currentNode = child } else { - throw LambdaOpenAPIRouterError.noRouteForPath(path) + throw OpenAPILambdaRouterError.noRouteForPath(path) } } } //at this stage, current node must have a handler child guard let handlerNode = currentNode.handlerChild() else { - throw LambdaOpenAPIRouterError.noHandlerForPath(path) + throw OpenAPILambdaRouterError.noHandlerForPath(path) } // did we found an handler ? - guard let handler = handlerNode.value.handler else { throw LambdaOpenAPIRouterError.noHandlerForPath(path) } + guard let handler = handlerNode.value.handler else { throw OpenAPILambdaRouterError.noHandlerForPath(path) } return (handler, parameters) } diff --git a/assets/swift-openapi-lambda.gif b/assets/swift-openapi-lambda.gif index 1706b4b..7ff4383 100644 Binary files a/assets/swift-openapi-lambda.gif and b/assets/swift-openapi-lambda.gif differ