Skip to content

Commit c189edc

Browse files
committed
add generic for DRY
1 parent cea36f1 commit c189edc

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

Sources/Router/OpenAPILambdaRouter+SimplifiedAPI.swift

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,7 @@ extension OpenAPILambdaRouter {
4949
_ path: String,
5050
handler: @escaping @Sendable (HTTPRequest, HTTPBody?) async throws -> String
5151
) throws {
52-
53-
let openAPIHandler: OpenAPIHandler = {
54-
(request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse, HTTPBody?) in
55-
56-
do {
57-
let response = try await handler(request, body)
58-
return (.init(status: .ok), .init(response))
59-
}
60-
catch {
61-
return (.init(status: .internalServerError), nil)
62-
}
63-
}
64-
try add(method: .get, path: path, handler: openAPIHandler)
52+
try generic(method: .post, path: path, handler: handler)
6553
}
6654

6755
/// Adds a POST route to the router for the given path.
@@ -76,6 +64,14 @@ extension OpenAPILambdaRouter {
7664
_ path: String,
7765
handler: @escaping @Sendable (HTTPRequest, HTTPBody?) async throws -> String
7866
) throws {
67+
try generic(method: .post, path: path, handler: handler)
68+
}
69+
70+
func generic<Response: StringProtocol & Sendable>(
71+
method: HTTPRequest.Method,
72+
path: String,
73+
handler: @escaping @Sendable (HTTPRequest, HTTPBody?) async throws -> Response
74+
) throws {
7975

8076
let openAPIHandler: OpenAPIHandler = {
8177
(request: HTTPRequest, body: HTTPBody?, metadata: ServerRequestMetadata) -> (HTTPResponse, HTTPBody?) in
@@ -88,6 +84,6 @@ extension OpenAPILambdaRouter {
8884
}
8985
}
9086

91-
try add(method: .post, path: path, handler: openAPIHandler)
87+
try add(method: method, path: path, handler: openAPIHandler)
9288
}
9389
}

0 commit comments

Comments
 (0)