31 lines
794 B
Swift
31 lines
794 B
Swift
import Dependencies
|
|
import Elementary
|
|
import Vapor
|
|
import VaporElementary
|
|
|
|
extension Request {
|
|
|
|
var isHtmxRequest: Bool {
|
|
headers.contains(name: "hx-request")
|
|
}
|
|
|
|
func render<C: HTML>(
|
|
@HTMLBuilder html: () async throws -> C
|
|
) async rethrows -> HTMLResponse where C: Sendable {
|
|
let html = try await html()
|
|
return HTMLResponse { html }
|
|
}
|
|
|
|
// Render the html if we're an htmx request, otherwise render the main page.
|
|
func render<C: HTML, D: SendableHTMLDocument>(
|
|
mainPage: (C) async throws -> D,
|
|
@HTMLBuilder html: () async throws -> C
|
|
) async rethrows -> HTMLResponse where C: Sendable {
|
|
let html = try await html()
|
|
guard isHtmxRequest else {
|
|
return try await render { try await mainPage(html) }
|
|
}
|
|
return HTMLResponse { html }
|
|
}
|
|
}
|