Skip to content

Commit

Permalink
type-erased HTMLResponse (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
sliemeobn authored Oct 4, 2024
1 parent e0a9840 commit dba4fbe
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
12 changes: 6 additions & 6 deletions Sources/HummingbirdElementary/HTMLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import Hummingbird
/// }
/// }
/// ```
public struct HTMLResponse<Content: HTML & Sendable>: Sendable {
public struct HTMLResponse {
// NOTE: The Sendable requirement on Content can probably be removed in Swift 6 using a sending parameter, and some fancy ~Copyable @unchecked Sendable box type.
// We only need to pass the HTML value to the response generator body closure
private let content: Content
private let content: any HTML & Sendable

/// The number of bytes to write to the response body at a time.
///
Expand All @@ -44,12 +44,12 @@ public struct HTMLResponse<Content: HTML & Sendable>: Sendable {
/// - chunkSize: The number of bytes to write to the response body at a time.
/// - additionalHeaders: Additional headers to be merged with predefined headers.
/// - content: The `HTML` content to render in the response.
public init(chunkSize: Int = 1024, additionalHeaders: HTTPFields = [:], @HTMLBuilder content: () -> Content) {
public init(chunkSize: Int = 1024, additionalHeaders: HTTPFields = [:], @HTMLBuilder content: () -> some HTML & Sendable) {
self.chunkSize = chunkSize
if additionalHeaders.contains(.contentType) {
self.headers = additionalHeaders
} else {
self.headers = [.contentType: "text/html; charset=utf-8"] + additionalHeaders
self.headers.append(contentsOf: additionalHeaders)
}
self.content = content()
}
Expand All @@ -60,8 +60,8 @@ extension HTMLResponse: ResponseGenerator {
.init(
status: .ok,
headers: self.headers,
body: .init { [self] writer in
try await writer.writeHTML(self.content, chunkSize: self.chunkSize)
body: .init { [content, chunkSize] writer in
try await writer.writeHTML(content, chunkSize: chunkSize)
try await writer.finish(nil)
}
)
Expand Down
1 change: 1 addition & 0 deletions Tests/HummingbirdElementaryTests/HTMLResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ final class HTMLResponseTests: XCTestCase {
headers: [:],
body: .init { writer in
try await writer.writeHTML(p { "Hello" })
try await writer.finish(nil)
}
)
}
Expand Down

0 comments on commit dba4fbe

Please sign in to comment.