Skip to content

Commit

Permalink
feat!: accept any for TriggerObject
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Rename `TriggerKeyValue` to `TriggerObject`
  • Loading branch information
angelofallars committed Nov 24, 2023
1 parent 380ce30 commit cb88868
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ Define event triggers:

- `htmx.Trigger(eventName string)` - A trigger with no details.
- `htmx.TriggerValue(eventName string, detail string)` - A trigger with one detail.
- `htmx.TriggerKeyValue(eventName string, details map[string]string)` - A trigger with key/value
details.
- `htmx.TriggerObject(eventName string, object any)` - A trigger with a JSON-serializable detail
object. Recommended to pass in either `map[string]string` or structs with JSON field tags.

Set trigger headers using the triggers above:

Expand All @@ -134,7 +134,7 @@ htmx.NewResponse().
htmx.NewResponse().
AddTrigger(
htmx.TriggerValue("hello", "world"),
htmx.TriggerKeyValue("myEvent", map[string]string{
htmx.TriggerObject("myEvent", map[string]string{
"level": "info",
"message": "Here Is A Message",
}),
Expand Down
25 changes: 13 additions & 12 deletions respheaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ type (
value string
}
// Unexported with a public constructor function for type safety reasons
triggerKeyValue struct {
triggerObject struct {
eventName string
value map[string]string
object any
}
)

Expand All @@ -282,8 +282,8 @@ func (t triggerPlain) htmxTrigger() {}
// TriggerValue satisfies htmx.trigger
func (t triggerValue) htmxTrigger() {}

// TriggerKeyValue satisfies htmx.trigger
func (t triggerKeyValue) htmxTrigger() {}
// TriggerObject satisfies htmx.trigger
func (t triggerObject) htmxTrigger() {}

// Trigger returns an event trigger with no additional details.
//
Expand Down Expand Up @@ -319,12 +319,13 @@ func TriggerValue(eventName string, detail string) triggerValue {
}
}

// TriggerKeyValue returns an event trigger with key/value items.
// Will be encoded as JSON.
// TriggerObject returns an event trigger with a given detail object that **must** be serializable to JSON.
//
// Structs with JSON tags can work, and so does `map[string]string` values which are safe to serialize.
//
// Example:
//
// htmx.TriggerKeyValue("showMessage", map[string]string{
// htmx.TriggerObject("showMessage", map[string]string{
// "level": "info",
// "message": "Here Is A Message",
// })
Expand All @@ -334,10 +335,10 @@ func TriggerValue(eventName string, detail string) triggerValue {
// HX-Trigger: {"showMessage":{"level" : "info", "message" : "Here Is A Message"}}
//
// For more info, see https://htmx.org/headers/hx-trigger/
func TriggerKeyValue(eventName string, details map[string]string) triggerKeyValue {
return triggerKeyValue{
func TriggerObject(eventName string, object any) triggerObject {
return triggerObject{
eventName: eventName,
value: details,
object: object,
}
}

Expand All @@ -351,8 +352,8 @@ func triggersToString(triggers []EventTrigger) (string, error) {
switch v := t.(type) {
case triggerPlain:
simpleEvents = append(simpleEvents, string(v))
case triggerKeyValue:
detailEvents[v.eventName] = v.value
case triggerObject:
detailEvents[v.eventName] = v.object
case triggerValue:
detailEvents[v.eventName] = v.value
}
Expand Down

0 comments on commit cb88868

Please sign in to comment.