-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
187 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Vapor | ||
|
||
public struct HXResponseHeaders { | ||
var location: HXLocationHeader? | ||
var pushUrl: HXPushUrlHeader? | ||
var redirect: HXRedirectHeader? | ||
var refresh: HXRefreshHeader? | ||
var replaceUrl: HXReplaceUrlHeader? | ||
var reselect: HXReselectHeader? | ||
var reswap: HXReswapHeader? | ||
var retarget: HXRetargetHeader? | ||
var trigger: HXTriggerHeader? | ||
var triggerAfterSettle: HXTriggerAfterSettleHeader? | ||
var triggerAfterSwap: HXTriggerAfterSwapHeader? | ||
|
||
public func add(to resp: Response) { | ||
location.map { $0.add(to: resp) } | ||
pushUrl.map { $0.add(to: resp) } | ||
redirect.map { $0.add(to: resp) } | ||
refresh.map { $0.add(to: resp) } | ||
replaceUrl.map { $0.add(to: resp) } | ||
reselect.map { $0.add(to: resp) } | ||
reswap.map { $0.add(to: resp) } | ||
retarget.map { $0.add(to: resp) } | ||
trigger.map { $0.add(to: resp) } | ||
triggerAfterSettle.map { $0.add(to: resp) } | ||
triggerAfterSwap.map { $0.add(to: resp) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import Vapor | ||
|
||
public enum HXTriggerEvent { | ||
case basic([String]) | ||
case custom([HXTriggerEventKind]) | ||
} | ||
|
||
public enum HXTriggerEventKind { | ||
case message(name: String, value: String) | ||
case object(name: String, value: any Encodable) | ||
} | ||
|
||
public struct HXTriggerHeader { | ||
let value: HXTriggerEvent | ||
|
||
func serialise() -> String { | ||
value.serialise() | ||
} | ||
|
||
func add(to resp: Response) { | ||
let serialised = serialise() | ||
|
||
if !serialised.isEmpty { | ||
resp.headers.replaceOrAdd(name: "HX-Trigger", value: serialised) | ||
} | ||
} | ||
} | ||
|
||
public struct HXTriggerAfterSettleHeader { | ||
let value: HXTriggerEvent | ||
|
||
func serialise() -> String { | ||
value.serialise() | ||
} | ||
|
||
func add(to resp: Response) { | ||
let serialised = serialise() | ||
|
||
if !serialised.isEmpty { | ||
resp.headers.replaceOrAdd(name: "HX-Trigger-After-Settle", value: serialised) | ||
} | ||
} | ||
} | ||
|
||
public struct HXTriggerAfterSwapHeader { | ||
let value: HXTriggerEvent | ||
|
||
func serialise() -> String { | ||
value.serialise() | ||
} | ||
|
||
func add(to resp: Response) { | ||
let serialised = serialise() | ||
|
||
if !serialised.isEmpty { | ||
resp.headers.replaceOrAdd(name: "HX-Trigger-After-Swap", value: serialised) | ||
} | ||
} | ||
} | ||
|
||
extension [HXTriggerEventKind] { | ||
// The solution taken from: | ||
// https://forums.swift.org/t/how-to-encode-objects-of-unknown-type/12253/2 | ||
private struct AnyEncodable: Encodable { | ||
private let _encode: (Encoder) throws -> Void | ||
public init(_ wrapped: some Encodable) { | ||
_encode = wrapped.encode | ||
} | ||
|
||
func encode(to encoder: Encoder) throws { | ||
try _encode(encoder) | ||
} | ||
} | ||
|
||
func serialise() -> String { | ||
var data: [String: AnyEncodable] = [:] | ||
|
||
for i in self { | ||
switch i { | ||
case let .message(name: name, value: value): | ||
data[name] = AnyEncodable(value) | ||
case let .object(name: name, value: value): | ||
data[name] = AnyEncodable(value) | ||
} | ||
} | ||
|
||
guard let values = try? String(data: JSONEncoder().encode(data), encoding: .utf8) else { | ||
return "" | ||
} | ||
|
||
return values | ||
} | ||
} | ||
|
||
extension HXTriggerEvent { | ||
func serialise() -> String { | ||
switch self { | ||
case let .basic(events): | ||
events.joined(separator: ", ") | ||
case let .custom(events): | ||
events.serialise() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters