Skip to content

Commit 7d39361

Browse files
Refactor APIGateway+Extension
1 parent d96ab16 commit 7d39361

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

Examples/LambdaFunctions/Sources/ProductAPI/APIGateway+Extensions.swift

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,36 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import AWSLambdaEvents
16-
import Foundation
16+
import class Foundation.JSONEncoder
17+
import class Foundation.JSONDecoder
1718

1819
extension APIGateway.V2.Request {
19-
public func object<T: Codable>() throws -> T {
20-
let decoder = JSONDecoder()
20+
21+
static private let decoder = JSONDecoder()
22+
23+
public func bodyObject<T: Codable>() throws -> T {
2124
guard let body = self.body,
2225
let dataBody = body.data(using: .utf8)
2326
else {
2427
throw APIError.invalidRequest
2528
}
26-
return try decoder.decode(T.self, from: dataBody)
29+
return try Self.decoder.decode(T.self, from: dataBody)
2730
}
2831
}
2932

3033
extension APIGateway.V2.Response {
3134

32-
static let defaultHeaders = [
35+
private static let encoder = JSONEncoder()
36+
37+
public static let defaultHeaders = [
3338
"Content-Type": "application/json",
39+
//Security warning: XSS are enabled
3440
"Access-Control-Allow-Origin": "*",
3541
"Access-Control-Allow-Methods": "OPTIONS,GET,POST,PUT,DELETE",
3642
"Access-Control-Allow-Credentials": "true",
3743
]
3844

39-
init(with error: Error, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
45+
public init(with error: Error, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
4046
self.init(
4147
statusCode: statusCode,
4248
headers: APIGateway.V2.Response.defaultHeaders,
@@ -46,10 +52,9 @@ extension APIGateway.V2.Response {
4652
)
4753
}
4854

49-
init<Out: Encodable>(with object: Out, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
50-
let encoder = JSONEncoder()
55+
public init<Out: Encodable>(with object: Out, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
5156
var body: String = "{}"
52-
if let data = try? encoder.encode(object) {
57+
if let data = try? Self.encoder.encode(object) {
5358
body = String(data: data, encoding: .utf8) ?? body
5459
}
5560
self.init(
@@ -60,13 +65,4 @@ extension APIGateway.V2.Response {
6065
isBase64Encoded: false
6166
)
6267
}
63-
64-
init<Out: Encodable>(with result: Result<Out, Error>, statusCode: AWSLambdaEvents.HTTPResponseStatus) {
65-
switch result {
66-
case .success(let value):
67-
self.init(with: value, statusCode: statusCode)
68-
case .failure(let error):
69-
self.init(with: error, statusCode: statusCode)
70-
}
71-
}
7268
}

Examples/LambdaFunctions/Sources/ProductAPI/ProductLambdaHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct ProductLambdaHandler: EventLoopLambdaHandler {
4242
}
4343

4444
func createLambdaHandler(context: Lambda.Context, event: APIGateway.V2.Request) -> EventLoopFuture<APIGateway.V2.Response> {
45-
guard let product: Product = try? event.object() else {
45+
guard let product: Product = try? event.bodyObject() else {
4646
let error = APIError.invalidRequest
4747
return context.eventLoop.makeFailedFuture(error)
4848
}
@@ -70,7 +70,7 @@ struct ProductLambdaHandler: EventLoopLambdaHandler {
7070
}
7171

7272
func updateLambdaHandler(context: Lambda.Context, event: APIGateway.V2.Request) -> EventLoopFuture<APIGateway.V2.Response> {
73-
guard let product: Product = try? event.object() else {
73+
guard let product: Product = try? event.bodyObject() else {
7474
let error = APIError.invalidRequest
7575
return context.eventLoop.makeFailedFuture(error)
7676
}

0 commit comments

Comments
 (0)