Skip to content

Commit f75304a

Browse files
committed
Update library to support Swift Lambda runtime v2
1 parent 4cb7c71 commit f75304a

11 files changed

+71
-115
lines changed

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.DS_Store
2-
/.aws-sam/
3-
/.build
4-
/.swiftpm
2+
.aws-sam/
3+
.build
4+
.index-build
5+
.swiftpm
56
samconfig.toml
67
Package.resolved
78
/*.xcodeproj
@@ -10,4 +11,3 @@ DerivedData/
1011
.swiftpm/config/registries.json
1112
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
1213
*key
13-

Package.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import PackageDescription
55

66
let package = Package(
77
name: "swift-openapi-lambda",
8-
platforms: [.macOS(.v12)],
8+
platforms: [.macOS(.v15)],
99
products: [.library(name: "OpenAPILambda", targets: ["OpenAPILambda"])],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.0.0"),
12-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha.3"),
13-
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.0.0"),
14-
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"),
11+
.package(url: "https://github.com/apple/swift-openapi-runtime.git", from: "1.8.2"),
12+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "2.0.0-beta.2"),
13+
.package(url: "https://github.com/swift-server/swift-aws-lambda-events.git", from: "1.2.0"),
1514
],
1615
targets: [
1716
.target(
@@ -21,8 +20,7 @@ let package = Package(
2120
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
2221
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
2322
],
24-
path: "Sources",
25-
swiftSettings: [.enableExperimentalFeature("StrictConcurrency=complete")]
23+
path: "Sources"
2624
),
2725
// test targets
2826
.testTarget(

[email protected]

Lines changed: 0 additions & 35 deletions
This file was deleted.

[email protected]

Lines changed: 0 additions & 35 deletions
This file was deleted.

Sources/Exports.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift OpenAPI Lambda open source project
4+
//
5+
// Copyright (c) 2025 Amazon.com, Inc. or its affiliates
6+
// and the Swift OpenAPI Lambda project authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
// re-export AWSLambdaRuntime and AWSLambdaEvents so that users don't have to.
17+
@_exported import AWSLambdaRuntime
18+
@_exported import AWSLambdaEvents

Sources/OpenAPILambda.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
//===----------------------------------------------------------------------===//
1515
import Foundation
1616
import AWSLambdaRuntime
17+
import Logging
1718
import OpenAPIRuntime
1819
import HTTPTypes
1920

2021
/// A Lambda function implemented with a OpenAPI server (implementing `APIProtocol` from Swift OpenAPIRuntime)
21-
public protocol OpenAPILambda {
22+
public protocol OpenAPILambda: Sendable {
2223

2324
associatedtype Event: Decodable
2425
associatedtype Output: Encodable
@@ -40,10 +41,19 @@ public protocol OpenAPILambda {
4041
}
4142

4243
extension OpenAPILambda {
43-
/// Initializes and runs the Lambda function.
44-
///
45-
/// If you precede your ``EventLoopLambdaHandler`` conformer's declaration with the
46-
/// [@main](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html#ID626)
47-
/// attribute, the system calls the conformer's `main()` method to launch the lambda function.
48-
public static func main() throws { OpenAPILambdaHandler<Self>.main() }
44+
/// Returns the Lambda handler function for this OpenAPI Lambda implementation.
45+
/// - Returns: A handler function that can be used with AWS Lambda Runtime
46+
public static func handler() throws -> (Event, LambdaContext) async throws -> Output {
47+
try OpenAPILambdaHandler<Self>().handler
48+
}
49+
50+
/// Start the Lambda Runtime with the Lambda handler function for this OpenAPI Lambda implementation with a custom logger,
51+
/// when one is given.
52+
/// - Parameter logger: The logger to use for Lambda runtime logging
53+
public static func run(logger: Logger? = nil) async throws {
54+
55+
let _logger = logger ?? Logger(label: "OpenAPILambda")
56+
let lambdaRuntime = try LambdaRuntime(logger: _logger, body: Self.handler())
57+
try await lambdaRuntime.run()
58+
}
4959
}

Sources/OpenAPILambdaHandler.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import OpenAPIRuntime
1818
import HTTPTypes
1919

2020
/// Specialization of LambdaHandler which runs an OpenAPILambda
21-
public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
21+
struct OpenAPILambdaHandler<L: OpenAPILambda> {
22+
23+
private let router: OpenAPILambdaRouter
24+
private let transport: OpenAPILambdaTransport
25+
private let lambda: L
2226

2327
/// the input type for this Lambda handler (received from the `OpenAPILambda`)
2428
public typealias Event = L.Event
@@ -31,7 +35,7 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
3135
/// Create application, set it up and create `OpenAPILambda` from application and create responder
3236
/// - Parameters
3337
/// - context: Lambda initialization context
34-
public init(context: LambdaInitializationContext) throws {
38+
init() throws {
3539
self.router = TrieRouter()
3640
self.transport = OpenAPILambdaTransport(router: self.router)
3741
self.lambda = try .init(transport: self.transport)
@@ -45,17 +49,17 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
4549
/// - context: Runtime ``LambdaContext``.
4650
///
4751
/// - Returns: A Lambda result ot type `Output`.
48-
public func handle(_ request: Event, context: LambdaContext) async throws -> Output {
52+
func handler(event: L.Event, context: LambdaContext) async throws -> L.Output {
4953

50-
// by default return HTTP 500
54+
// by default returns HTTP 500
5155
var lambdaResponse: OpenAPILambdaResponse = (HTTPResponse(status: .internalServerError), "unknown error")
5256

5357
do {
5458
// convert Lambda event source to OpenAPILambdaRequest
55-
let request = try lambda.request(context: context, from: request)
59+
let request = try lambda.request(context: context, from: event)
5660

5761
// route the request to find the handlers and extract the paramaters
58-
let (handler, parameters) = try await router.route(method: request.0.method, path: request.0.path!)
62+
let (handler, parameters) = try router.route(method: request.0.method, path: request.0.path!)
5963

6064
// call the request handler (and extract the HTTPRequest and HTTPBody)
6165
let httpRequest = request.0
@@ -103,8 +107,4 @@ public struct OpenAPILambdaHandler<L: OpenAPILambda>: LambdaHandler {
103107
// transform the OpenAPILambdaResponse to the Lambda Output
104108
return lambda.output(from: lambdaResponse)
105109
}
106-
107-
let router: OpenAPILambdaRouter
108-
let transport: OpenAPILambdaTransport
109-
let lambda: L
110110
}

Sources/OpenAPILambdaTransport.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ public typealias OpenAPILambdaResponse = (HTTPResponse, String?)
3030
public typealias OpenAPILambdaRequestParameters = [String: Substring]
3131

3232
/// an OpenAPI handler
33-
public typealias OpenAPIHandler = @Sendable (HTTPRequest, HTTPBody?, ServerRequestMetadata) async throws -> (
33+
public typealias OpenAPIHandler = (HTTPRequest, HTTPBody?, ServerRequestMetadata) async throws -> (
3434
HTTPResponse, HTTPBody?
3535
)
3636

3737
/// Lambda Transport for OpenAPI generator
3838
public struct OpenAPILambdaTransport: ServerTransport {
3939

40-
private var router: OpenAPILambdaRouter
40+
private let router: OpenAPILambdaRouter
4141

4242
/// Create a `OpenAPILambdaTransport` with the given `OpenAPILambdaRouter`
43-
public init(router: OpenAPILambdaRouter) { self.router = router }
43+
init(router: OpenAPILambdaRouter) { self.router = router }
4444

4545
/// Registers an HTTP operation handler at the provided path and method.
4646
/// - Parameters:

Sources/Router/OpenAPILambdaRouter.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public enum OpenAPILambdaRouterError: Error {
2424
}
2525

2626
/// A router API
27-
public protocol OpenAPILambdaRouter {
27+
protocol OpenAPILambdaRouter {
2828
/// add a route for a given HTTP method and path and associate a handler
2929
func add(method: HTTPRequest.Method, path: String, handler: @escaping OpenAPIHandler) throws
3030

3131
/// Retrieve the handler and path parameter for a given HTTP method and path
32-
func route(method: HTTPRequest.Method, path: String) async throws -> (
32+
func route(method: HTTPRequest.Method, path: String) throws -> (
3333
OpenAPIHandler, OpenAPILambdaRequestParameters
3434
)
3535
}

Sources/Router/OpenAPILambdaRouterNode.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@ import HTTPTypes
2222
/// - a path element (represented as a `String`),
2323
/// - a path parameter (represented as a `String`)
2424
/// - a handler, only for leaf nodes (represented as `OpenAPIHandler`)
25-
class Node {
25+
final class Node {
2626
let value: NodeValue
2727
var children: [String: Node] = [:]
2828

2929
/// Default init, create a root node
30-
public init() { value = .root }
30+
init() { value = .root }
3131

3232
/// Creates a node for an HTTP method
33-
public init(httpMethod: HTTPRequest.Method) { value = .httpMethod(httpMethod) }
33+
init(httpMethod: HTTPRequest.Method) { value = .httpMethod(httpMethod) }
3434

3535
/// Creates a node for a path element
36-
public init(pathElement: String) { value = .pathElement(pathElement) }
36+
init(pathElement: String) { value = .pathElement(pathElement) }
3737

3838
/// Creates a node for a path parameter
39-
public init(parameterName: String) { value = .pathParameter(parameterName) }
39+
init(parameterName: String) { value = .pathParameter(parameterName) }
4040

4141
/// Creates a node for an OpenAPI handler
42-
public init(handler: @escaping OpenAPIHandler) { value = .handler(handler) }
42+
init(handler: @escaping OpenAPIHandler) { value = .handler(handler) }
4343

4444
/// Creates a node for an existing node value
4545
private init(value: NodeValue) { self.value = value }
@@ -62,8 +62,8 @@ class Node {
6262
/// - Throws:
6363
/// - URIPathCollectionError.canNotAddChildToHandlerNode when trying to add a child to leaf node of type `.handler`
6464
/// - URIPathCollectionError.canNotHaveMultipleParamChilds when trying to add multiple child node of type `.parameter`
65-
6665
func add(pathElement: String) throws -> Node { try add(child: NodeValue.pathElement(pathElement)) }
66+
6767
/// Convenience method to add a child node of type path parameter to this node
6868
/// - Parameter:
6969
/// - pathParameter: the name of a path parameter. A path parameter is a `{name}` usually found between `/` characters in the URI
@@ -72,8 +72,8 @@ class Node {
7272
/// - Throws:
7373
/// - URIPathCollectionError.canNotAddChildToHandlerNode when trying to add a child to leaf node of type `.handler`
7474
/// - URIPathCollectionError.canNotHaveMultipleParamChilds when trying to add multiple child node of type `.parameter`
75-
7675
func add(parameter: String) throws -> Node { try add(child: NodeValue.pathParameter(parameter)) }
76+
7777
/// Convenience method to add a child node of type handler to this node
7878
/// - Parameter:
7979
/// - handler: a function handler. A handler MUST be a leaf node (it has no children) and is of type `OpenAPIHandler`

0 commit comments

Comments
 (0)