Skip to content

Commit 6a3aa0f

Browse files
committed
simplify code and add the same initializer on APIGatewayResponse
1 parent 25ea446 commit 6a3aa0f

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

Sources/AWSLambdaEvents/APIGateway+V2+Encodable.swift renamed to Sources/AWSLambdaEvents/APIGateway+Encodable.swift

+30-14
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,29 @@ import class Foundation.JSONEncoder
2222
import struct Foundation.Data
2323
#endif
2424

25-
public enum APIGatewayResponseError: Error {
26-
case failedToEncodeBody(Error)
25+
extension Encodable {
26+
fileprivate func string() throws -> String {
27+
let encoded = try JSONEncoder().encode(self)
28+
return String(decoding: encoded, as: UTF8.self)
29+
}
30+
}
31+
32+
extension APIGatewayResponse {
33+
34+
public init<Input: Encodable>(
35+
statusCode: HTTPResponse.Status,
36+
headers: HTTPHeaders? = nil,
37+
multiValueHeaders: HTTPMultiValueHeaders? = nil,
38+
body: Input
39+
) throws {
40+
self.init(
41+
statusCode: statusCode,
42+
headers: headers,
43+
multiValueHeaders: multiValueHeaders,
44+
body: try body.string(),
45+
isBase64Encoded: nil
46+
)
47+
}
2748
}
2849

2950
extension APIGatewayV2Response {
@@ -32,19 +53,14 @@ extension APIGatewayV2Response {
3253
statusCode: HTTPResponse.Status,
3354
headers: HTTPHeaders? = nil,
3455
body: Input,
35-
isBase64Encoded: Bool? = nil,
3656
cookies: [String]? = nil
3757
) throws {
38-
let encodedBody: Data
39-
do {
40-
encodedBody = try JSONEncoder().encode(body)
41-
} catch {
42-
throw APIGatewayResponseError.failedToEncodeBody(error)
43-
}
44-
self.statusCode = statusCode
45-
self.headers = headers
46-
self.body = String(data: encodedBody, encoding: .utf8) ?? ""
47-
self.isBase64Encoded = isBase64Encoded
48-
self.cookies = cookies
58+
self.init(
59+
statusCode: statusCode,
60+
headers: headers,
61+
body: try body.string(),
62+
isBase64Encoded: nil,
63+
cookies: cookies
64+
)
4965
}
5066
}

Tests/AWSLambdaEventsTests/APIGateway+V2+EncodableTests.swift renamed to Tests/AWSLambdaEventsTests/APIGateway+EncodableTests.swift

+26-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Testing
1717

1818
@testable import AWSLambdaEvents
1919

20-
struct APIGatewayV2EncodableResponseTests {
20+
struct APIGatewayEncodableResponseTests {
2121

2222
// MARK: Encoding
2323
struct BusinessResponse: Codable, Equatable {
@@ -26,7 +26,7 @@ struct APIGatewayV2EncodableResponseTests {
2626
}
2727

2828
@Test
29-
func testResponseEncoding() throws {
29+
func testResponseEncodingV2() throws {
3030

3131
// given
3232
let businessResponse = BusinessResponse(message: "Hello World", code: 200)
@@ -48,4 +48,28 @@ struct APIGatewayV2EncodableResponseTests {
4848
#expect(encodedBody == businessResponse)
4949
}
5050
}
51+
52+
@Test
53+
func testResponseEncoding() throws {
54+
55+
// given
56+
let businessResponse = BusinessResponse(message: "Hello World", code: 200)
57+
58+
var response: APIGatewayResponse? = nil
59+
#expect(throws: Never.self) {
60+
try response = APIGatewayResponse(statusCode: .ok, body: businessResponse)
61+
}
62+
try #require(response?.body != nil)
63+
64+
// when
65+
let body = response?.body?.data(using: .utf8)
66+
try #require(body != nil)
67+
68+
#expect(throws: Never.self) {
69+
let encodedBody = try JSONDecoder().decode(BusinessResponse.self, from: body!)
70+
71+
// then
72+
#expect(encodedBody == businessResponse)
73+
}
74+
}
5175
}

Tests/AWSLambdaEventsTests/APIGatewayTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ class APIGatewayTests: XCTestCase {
9595
let isBase64Encoded: Bool?
9696
}
9797

98-
func testResponseEncoding() {
99-
let resp = APIGatewayResponse(
98+
func testResponseEncoding() throws {
99+
let resp = try APIGatewayResponse(
100100
statusCode: .ok,
101101
headers: ["Server": "Test"],
102102
body: "abc123"

0 commit comments

Comments
 (0)