Tags: swift-server/swift-aws-lambda-events
Tags
Add an encodable version of the APIGatewayResponse (#86) Add a convenience initializer to the APIGatewayResponse v2 that accepts an Encodable object ### Motivation: Most Lambda developers will return a JSON object when exposing their function through the API Gateway. The current initializer only accepts a `String` body as per the AWS message definition. This obliges all developers to write two lines of code + error handling to encode their object and transform the `Data` to a string. ### Modifications: Add a new initializer that accepts a `body` as `Encodable` ```swift public init<Input: Encodable> ( statusCode: HTTPResponse.Status, headers: HTTPHeaders? = nil, body: Input, isBase64Encoded: Bool? = nil, cookies: [String]? = nil ) throws { ... } ``` ### Result: Developers can now pass a Swift struct to the APIGatewayResponse. ```swift let businessResponse = BusinessResponse(message: "Hello World", code: 200) return APIGatewayV2Response(statusCode: .ok, body: businessResponse) ```