Repository navigation
Expand file tree
/
Copy pathrequest_headers.go
More file actions
141 lines (123 loc) · 4.3 KB
/
Copy pathrequest_headers.go
File metadata and controls
141 lines (123 loc) · 4.3 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package hx
import (
"net/http"
)
// Request Headers
const (
// HxBoosted is set to "true" when the request comes from a boosted element via hx-boost.
//
// Contents: "true" or not set
//
// See https://four.htmx.org/reference/headers/HX-Boosted for more details.
//
// Use the IsBoosted() function to check this header in the request.
HxBoosted = "Hx-Boosted"
// HxCurrentUrl contains the browser’s URL when the request was made.
//
// Contents: URL
//
// See https://four.htmx.org/reference/headers/HX-Current-URL for more details.
//
// Use the GetCurrentUrl() function to fetch this header from the request.
HxCurrentUrl = "Hx-Current-URL"
// HxHistoryRestoreRequest is set to "true" when the user navigates back or forward in history.
//
// Contents: "true" or not set
//
// See https://four.htmx.org/reference/headers/HX-History-Restore-Request for more details.
//
// Use the IsHistoryRestoreRequest() function to check this header in the request.
HxHistoryRestoreRequest = "Hx-History-Restore-Request"
// HxRequest indicates the request was made by htmx.
//
// Contents: "true"
//
// See https://four.htmx.org/reference/headers/HX-Request for more details.
//
// Use the IsRequest() function to check this header in the request.
HxRequest = "Hx-Request"
// HxRequestType indicates if request targets a specific element or the whole page.
//
// Contents: "partial" or "full"
//
// See https://four.htmx.org/reference/headers/HX-Request-Type for more details.
//
// Use the GetRequestType() function to fetch this header from the request.
HxRequestType = "Hx-Request-Type"
// HxSource contains the element that triggered the request. Format is tag#id like button#submit.
//
// Contents: Element reference
//
// See https://four.htmx.org/reference/headers/HX-Source for more details.
//
// Use the GetSource() function to fetch this header from the request.
HxSource = "Hx-Source"
// HxTarget identifies the element that will receive the response.
//
// Contents: Element reference
//
// See https://four.htmx.org/reference/headers/HX-Target for more details.
//
// Use the GetTarget() function to fetch this header from the request.
HxTarget = "Hx-Target"
)
// IsBoosted checks the HxBoosted header
//
// Returns true if the request is a boosted request
func IsBoosted(r *http.Request) bool {
return r.Header.Get(HxBoosted) != ""
}
// GetCurrentUrl extracts the HxCurrentUrl header from an HTTP request.
//
// Returns the current URL of the browser if the header exists.
func GetCurrentUrl(r *http.Request) string {
return r.Header.Get(HxCurrentUrl)
}
// IsHistoryRestoreRequest checks the HxHistoryRestoreRequest header
//
// Returns true if the header is present, otherwise returns false.
func IsHistoryRestoreRequest(r *http.Request) bool {
return r.Header.Get(HxHistoryRestoreRequest) != ""
}
// IsRequest determines if an HTTP request is an HTMX request by checking the HxRequest header
//
// Returns true if the header is present, otherwise returns false.
func IsRequest(r *http.Request) bool {
return r.Header.Get(HxRequest) != ""
}
// IsHtmx determines if an HTTP request is an HTMX request.
//
// Does the same thing as IsRequest, only with a more user-friendly name.
func IsHtmx(r *http.Request) bool {
return IsRequest(r)
}
// GetRequestType extracts the HxRequestType header from an HTTP request.
//
// Returns either "partial" or "full"
func GetRequestType(r *http.Request) string {
return r.Header.Get(HxRequestType)
}
// IsRequestTypeFull determines if the HxRequestType was "full"
//
// Returns true if the HxRequestType was "full", otherwise returns false.
func IsRequestTypeFull(r *http.Request) bool {
return GetRequestType(r) == "full"
}
// IsRequestTypePartial determines if the HxRequestType was "partial"
//
// Returns true if the HxRequestType was "partial", otherwise returns false.
func IsRequestTypePartial(r *http.Request) bool {
return GetRequestType(r) == "partial"
}
// GetSource extracts the HxSource header from an HTTP request.
//
// Returns the element reference of the source if the header exists.
func GetSource(r *http.Request) string {
return r.Header.Get(HxSource)
}
// GetTarget extracts the HxTarget header from an HTTP request.
//
// Returns the element reference of the target if the header exists.
func GetTarget(r *http.Request) string {
return r.Header.Get(HxTarget)
}