-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
413 lines (380 loc) · 14.1 KB
/
main.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package hx
import (
"encoding/json"
"net/http"
"strings"
)
type HeaderResponseWriter interface {
Header() http.Header
}
type HeaderDecorator func(w HeaderResponseWriter) error
// SetHeaders sets custom HTTP headers in the provided http.ResponseWriter.
//
// This function applies one or more custom header decorators to the given response writer `w`.
// A decorator function modifies the response by adding or modifying HTTP headers as per the provided configuration.
//
// Parameters:
//
// w: http.ResponseWriter - The response writer to which custom headers will be applied.
// decorators: ...HeaderDecorator - Any number of HeaderDecorators for adding HTMX headers.
// Each HeaderDecorator is responsible for setting specific HTTP headers.
//
// Returns:
//
// error: An error if any of the HeaderDecorators encounter an issue while setting the headers.
// It returns nil if all the decorators are applied successfully.
// Most HeaderDecorators always return a nill error.
//
// Example usage:
//
// import "github.com/thisisthemurph/hx"
// _ := hx.SetHeaders(w, hx.Retarget("/login"), hx.Reswap(hx.SwapOuterHTML))
//
// Note:
//
// If an error is returned, the function will not add any of the remaining headers, but will leave all
// previously set headers, it is your responsibility to remove these headers.
//
// The order of decorators matters. Headers set by decorators earlier in the slice may be overwritten
// by subsequent decorators.
func SetHeaders(w HeaderResponseWriter, funcs ...HeaderDecorator) error {
for _, fn := range funcs {
if err := fn(w); err != nil {
return err
}
}
return nil
}
// SetHeader returns a function for setting the header on the http.ResponseWriter.
// The returned function will always return a nil error.
func SetHeader(key, value string) HeaderDecorator {
return func(w HeaderResponseWriter) error {
w.Header().Set(key, value)
return nil
}
}
// Location allows you to do a client-side redirect that does not do a full page reload.
// https://htmx.org/headers/hx-location/
//
// Never returns an error.
func Location(location string) HeaderDecorator {
return SetHeader(HeaderLocation, location)
}
// PushURL pushes a new url into the history stack.
// https://htmx.org/headers/hx-push-url/
func PushURL(url string) HeaderDecorator {
return SetHeader(HeaderPushURL, url)
}
// PreventPushURL prevents the browser’s history from being updated by setting the HX-Push-Url header to "false".
// https://htmx.org/headers/hx-push-url/
func PreventPushURL() HeaderDecorator {
return PushURL("false")
}
// Redirect can be used to do a client-side redirect to a new location.
// https://htmx.org/reference/#response_headers
//
// Never returns an error.
func Redirect(path string) HeaderDecorator {
return SetHeader(HeaderRedirect, path)
}
// Refresh forces the client-side to do a full refresh of the page.
// https://htmx.org/reference/#response_headers
func Refresh() HeaderDecorator {
return SetHeader(HeaderRefresh, "true")
}
// PreventRefresh prevents the client-side from doing a full refresh of the page by setting
// the HX-Refresh header to "false".
// https://htmx.org/reference/#response_headers
func PreventRefresh() HeaderDecorator {
return SetHeader(HeaderRefresh, "false")
}
// ReplaceURL replaces the current URL in the location bar.
// https://htmx.org/headers/hx-replace-url/
func ReplaceURL(url string) HeaderDecorator {
return SetHeader(HeaderReplaceURL, url)
}
// PreventReplaceURL prevents replacing the current URL in the location bar by setting the
// HX-Replace-Url header to "false".
// https://htmx.org/headers/hx-replace-url/
func PreventReplaceURL() HeaderDecorator {
return ReplaceURL("false")
}
// Reselect a CSS selector that allows you to choose which part of the response is used to be swapped in.
// Overrides an existing hx-select on the triggering element
// https://htmx.org/reference/#response_headers
//
// Never returns an error.
func Reselect(selector string) HeaderDecorator {
return SetHeader(HeaderReselect, selector)
}
// Reswap allows you to override how the response will be swapped.
// https://htmx.org/reference/#response_headers
//
// Never returns an error.
func Reswap(swap Swap) HeaderDecorator {
return SetHeader(HeaderReswap, swap.String())
}
// Retarget a CSS selector that overrides the target of the content update to
// a different element on the page.
//
// Never returns an error.
func Retarget(target string) HeaderDecorator {
return SetHeader(HeaderRetarget, target)
}
// Trigger can be used to trigger client side actions on the target element within a response to HTMX.
// You can trigger a single event or as many uniquely named events as you would like.
//
// The header is determined by the value of the when parameter.
//
// - TriggerImmediately -> HX-Trigger
// - TriggerAfterSettle -> HX-Trigger-After-Settle
// - TriggerAfterSwap -> HX-Trigger-After-Swap
//
// If the header already includes values, these will be retained.
// If the current header value is a JSON object, the new headers will be added as part of the JSON object,
// rather than a list of comma separated event names; as with the TriggerWithDetail function.
//
// The returned function will return an error if existing headers require events to be JSON encoded and marshalling fails.
//
// Parameters:
//
// header: string - Specifies which header should be used to trigger the event.
// eventNames: ...string - Uniquely named events to be triggered.
//
// Example usage:
//
// err := hx.SetHeaders(hx.trigger(hx.HeaderTrigger, "myFirstEvent", "someOtherEvent"))
//
// Or passing a slice of events:
//
// events := []string {"event1", "event2"}
// err := hx.SetHeaders(hx.trigger(hx.TriggerAfterSwap, events...))
//
// https://htmx.org/headers/hx-trigger/
func trigger(header string, eventNames ...string) HeaderDecorator {
return func(w HeaderResponseWriter) error {
// eventMap := make(map[string]string)
eventList := make([]string, 0)
currentHeaderValues := w.Header().Get(header)
if currentHeaderValues != "" {
// If header has JSON data, the current event names must also be added as JSON.
// Default to using the TriggerWithDetail function.
var js interface{}
err := json.Unmarshal([]byte(currentHeaderValues), &js)
if err == nil {
events := make([]TriggerEvent, 0)
for _, event := range eventNames {
events = append(events, TriggerEvent{
Name: event,
Detail: nil,
})
}
return triggerWithDetail(header, events...)(w)
}
// If the data is not JSON, we must maintain the data and append the new
for _, eventName := range strings.Split(currentHeaderValues, ",") {
eventList = append(eventList, strings.TrimSpace(eventName))
}
}
for _, eventName := range eventNames {
eventList = append(eventList, strings.TrimSpace(eventName))
}
w.Header().Set(header, strings.Join(eventList, ", "))
return nil
}
}
// Trigger sets the HX-Trigger header with the given event names to trigger client side
// actions on the front end.
//
// You can trigger a single event or as many uniquely named events as you would like.
//
// If the HX-Trigger header already includes events, these will be retained.
// If the header value is a list of comma separated strings, these will be converted to
// JSON objects with null detail.
//
// The returned function will return an error if the provided detail cannot be serialized into JSON.
//
// Parameters:
//
// eventNames: ...string - Uniquely named events to be triggered.
//
// Example usage:
//
// err := hx.SetHeaders(hx.Trigger("event1", "event2"))
//
// https://htmx.org/headers/hx-trigger/
func Trigger(eventNames ...string) HeaderDecorator {
return trigger(HeaderTrigger, eventNames...)
}
// TriggerAfterSwap sets the HX-Trigger-After-Swap header with the given event names to trigger client side
// actions on the front end.
//
// You can trigger a single event or as many uniquely named events as you would like.
//
// If the HX-Trigger-After-Swap header already includes events, these will be retained.
// If the header value is a list of comma separated strings, these will be converted to
// JSON objects with null detail.
//
// The returned function will return an error if the provided detail cannot be serialized into JSON.
//
// Parameters:
//
// eventNames: ...string - Uniquely named events to be triggered.
//
// Example usage:
//
// err := hx.SetHeaders(hx.TriggerAfterSwap("event1", "event2"))
//
// https://htmx.org/headers/hx-trigger/
func TriggerAfterSwap(eventNames ...string) HeaderDecorator {
return trigger(HeaderTriggerAfterSwap, eventNames...)
}
// TriggerAfterSwap sets the HX-Trigger-After-Settle header with the given event names to trigger client side
// actions on the front end.
//
// You can trigger a single event or as many uniquely named events as you would like.
//
// If the HX-Trigger-After-Settle header already includes events, these will be retained.
// If the header value is a list of comma separated strings, these will be converted to
// JSON objects with null detail.
//
// The returned function will return an error if the provided detail cannot be serialized into JSON.
//
// Parameters:
//
// eventNames: ...string - Uniquely named events to be triggered.
//
// Example usage:
//
// err := hx.SetHeaders(hx.TriggerAfterSettle("event1", "event2"))
//
// https://htmx.org/headers/hx-trigger/
func TriggerAfterSettle(eventNames ...string) HeaderDecorator {
return trigger(HeaderTriggerAfterSettle, eventNames...)
}
// triggerWithDetail can be used to trigger client side actions on the target element within a response to HTMX.
// You can trigger a single event or as many uniquely named events as you would like.
//
// The header is determined by the value of the when parameter.
//
// - TriggerImmediately -> HX-Trigger
// - TriggerAfterSettle -> HX-Trigger-After-Settle
// - TriggerAfterSwap -> HX-Trigger-After-Swap
//
// If the header already includes values, these will be retained.
// If the current header value is a list of comma separated strings, these will be converted to
// JSON objects with null detail.
//
// The returned function will return an error if the provided detail cannot be serialized into JSON.
//
// Parameters:
//
// header: string - Specifies which header should be used to trigger the event.
// events: ...TriggerEvent - The events (name and detail) to be triggered.
//
// Example usage:
//
// event := hx.NewTriggerEvent("eventName", myStruct)
// err := hx.SetHeaders(hx.triggerWithDetail(hx.HeaderTrigger, event))
//
// https://htmx.org/headers/hx-trigger/
func triggerWithDetail(header string, events ...TriggerEvent) HeaderDecorator {
return func(w HeaderResponseWriter) error {
triggerEvents := make(map[string]any)
currentHeaderValue := w.Header().Get(header)
// If the header already has events present, we want to maintain these.
if currentHeaderValue != "" {
// Attempt to parse the header value as a JSON object.
if err := json.Unmarshal([]byte(currentHeaderValue), &triggerEvents); err != nil {
// If not JSON, assume a comma separated list of event names.
// Convert these to TriggerEvent structs.
eventNames := strings.Split(currentHeaderValue, ",")
for _, ev := range eventNames {
eventName := strings.TrimSpace(ev)
triggerEvents[eventName] = nil
}
}
}
for _, event := range events {
triggerEvents[event.Name] = event.Detail
}
data, err := json.Marshal(triggerEvents)
if err != nil {
return err
}
w.Header().Set(header, string(data))
return nil
}
}
// TriggerWithDetail sets the HX-Trigger header with the given TriggerEvent to trigger client side
// actions on the front end.
//
// You can trigger a single event or as many uniquely named events as you would like.
//
// If the HX-Trigger header already includes events, these will be retained.
// If the header value is a list of comma separated strings, these will be converted to
// JSON objects with null detail.
//
// The returned function will return an error if the provided detail cannot be serialized into JSON.
//
// Parameters:
//
// events: ...TriggerEvent - The events (name and detail) to be triggered.
//
// Example usage:
//
// event := hx.NewTriggerEvent("eventName", myStruct)
// err := hx.SetHeaders(hx.TriggerWithDetail(event))
//
// https://htmx.org/headers/hx-trigger/
func TriggerWithDetail(events ...TriggerEvent) HeaderDecorator {
return triggerWithDetail(HeaderTrigger, events...)
}
// TriggerAfterSettleWithDetail sets the HX-Trigger-After-Settle header with the given TriggerEvent
// to trigger client side actions on the front end.
//
// You can trigger a single event or as many uniquely named events as you would like.
//
// If the HX-Trigger-After-Target header already includes events, these will be retained.
// If the header value is a list of comma separated strings, these will be converted to
// JSON objects with null detail.
//
// The returned function will return an error if the provided detail cannot be serialized into JSON.
//
// Parameters:
//
// events: ...TriggerEvent - The events (name and detail) to be triggered.
//
// Example usage:
//
// event := hx.NewTriggerEvent("eventName", myStruct)
// err := hx.SetHeaders(hx.TriggerAfterSettleWithDetail(event))
//
// https://htmx.org/headers/hx-trigger/
func TriggerAfterSettleWithDetail(events ...TriggerEvent) HeaderDecorator {
return triggerWithDetail(HeaderTriggerAfterSettle, events...)
}
// TriggerAfterSwapWithDetail sets the HX-Trigger-After-Swap header with the given TriggerEvent
// to trigger client side actions on the front end.
//
// You can trigger a single event or as many uniquely named events as you would like.
//
// If the HX-Trigger-After-Swap header already includes events, these will be retained.
// If the header value is a list of comma separated strings, these will be converted to
// JSON objects with null detail.
//
// The returned function will return an error if the provided detail cannot be serialized into JSON.
//
// Parameters:
//
// events: ...TriggerEvent - The events (name and detail) to be triggered.
//
// Example usage:
//
// event := hx.NewTriggerEvent("eventName", myStruct)
// err := hx.SetHeaders(hx.TriggerAfterSwapWithDetail(event))
//
// https://htmx.org/headers/hx-trigger/
func TriggerAfterSwapWithDetail(events ...TriggerEvent) HeaderDecorator {
return triggerWithDetail(HeaderTriggerAfterSwap, events...)
}