-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
request.go
93 lines (84 loc) · 2.82 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package htmx
import (
"net/http"
)
// IsHTMX returns true if the given request
// was made by HTMX.
//
// This can be used to add special logic for HTMX requests.
//
// Checks if header 'HX-Request' is 'true'.
func IsHTMX(r *http.Request) bool {
return r.Header.Get(HeaderRequest) == "true"
}
// IsBoosted returns true if the given request
// was made via an element using 'hx-boost'.
//
// This can be used to add special logic for boosted requests.
//
// Checks if header 'HX-Boosted' is 'true'.
//
// For more info, see https://htmx.org/attributes/hx-boost/
func IsBoosted(r *http.Request) bool {
return r.Header.Get(HeaderBoosted) == "true"
}
// IsHistoryRestoreRequest returns true if the given request
// is for history restoration after a miss in the local history cache.
//
// Checks if header 'HX-History-Restore-Request' is 'true'.
func IsHistoryRestoreRequest(r *http.Request) bool {
return r.Header.Get(HeaderHistoryRestoreRequest) == "true"
}
// GetCurrentURL returns the current URL that HTMX made this request from.
//
// Returns false if header 'HX-Current-URL' does not exist.
func GetCurrentURL(r *http.Request) (string, bool) {
if _, ok := r.Header[http.CanonicalHeaderKey(HeaderCurrentURL)]; !ok {
return "", false
}
return r.Header.Get(HeaderCurrentURL), true
}
// GetPrompt returns the user response to an hx-prompt from a given request.
//
// Returns false if header 'HX-Prompt' does not exist.
//
// For more info, see https://htmx.org/attributes/hx-prompt/
func GetPrompt(r *http.Request) (string, bool) {
if _, ok := r.Header[http.CanonicalHeaderKey(HeaderPrompt)]; !ok {
return "", false
}
return r.Header.Get(HeaderPrompt), true
}
// GetTarget returns the ID of the target element if it exists from a given request.
//
// Returns false if header 'HX-Target' does not exist.
//
// For more info, see https://htmx.org/attributes/hx-target/
func GetTarget(r *http.Request) (string, bool) {
if _, ok := r.Header[http.CanonicalHeaderKey(HeaderTarget)]; !ok {
return "", false
}
return r.Header.Get(HeaderTarget), true
}
// GetTriggerName returns the 'name' of the triggered element if it exists from a given request.
//
// Returns false if header 'HX-Trigger-Name' does not exist.
//
// For more info, see https://htmx.org/attributes/hx-trigger/
func GetTriggerName(r *http.Request) (string, bool) {
if _, ok := r.Header[http.CanonicalHeaderKey(HeaderTriggerName)]; !ok {
return "", false
}
return r.Header.Get(HeaderTriggerName), true
}
// GetTrigger returns the ID of the triggered element if it exists from a given request.
//
// Returns false if header 'HX-Trigger' does not exist.
//
// For more info, see https://htmx.org/attributes/hx-trigger/
func GetTrigger(r *http.Request) (string, bool) {
if _, ok := r.Header[http.CanonicalHeaderKey(HeaderTrigger)]; !ok {
return "", false
}
return r.Header.Get(HeaderTrigger), true
}