Skip to content

Commit

Permalink
wip: replace methods
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Feb 5, 2024
1 parent 956a5d8 commit 8de6c32
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions htmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func HxFromContext(c *fiber.Ctx) *Hx {

// ContextWithHx ...
func ContextWithHx(c *fiber.Ctx) *fiber.Ctx {
c.Locals(htmxContext, HxFromContext(c))
_ = c.Locals(htmxContext, HxFromContext(c))

return c
}
Expand Down Expand Up @@ -176,6 +176,21 @@ func (h *Htmx) Write(data []byte) (n int, err error) {
return h.ctx.Write(data)
}

// WriteHTML ...
func (h *Htmx) WriteHTML(html template.HTML) (n int, err error) {
return h.Write([]byte(html))

Check failure on line 181 in htmx.go

View workflow job for this annotation

GitHub Actions / lint

preferStringWriter: h.WriteString(html) should be preferred to the h.Write([]byte(html)) (gocritic)
}

// WriteJSON ...
func (h *Htmx) WriteJSON(data any) (n int, err error) {
payload, err := json.Marshal(data)
if err != nil {
return 0, err
}

return h.Write(payload)
}

// WriteString ...
func (h *Htmx) WriteString(s string) (n int, err error) {
return h.Write([]byte(s))

Check failure on line 196 in htmx.go

View workflow job for this annotation

GitHub Actions / lint

preferStringWriter: h.WriteString(s) should be preferred to the h.Write([]byte(s)) (gocritic)
Expand All @@ -191,24 +206,34 @@ func (h *Htmx) Ctx() *fiber.Ctx {
return h.ctx
}

// WriteJSON ...
func (h *Htmx) WriteJSON(data any) (n int, err error) {
payload, err := json.Marshal(data)
if err != nil {
return 0, err
}
// Redirect ..
func (h *Htmx) Redirect(url string) {
h.ctx.Set(HXRedirect.String(), url)
}

return h.Write(payload)
// ReplaceURL ...
func (h *Htmx) ReplaceURL(url string) {
h.ctx.Set(HXReplaceUrl.String(), url)
}

// Redirect ..
func (h *Htmx) Redirect(url string) {
h.ctx.Append(HXRedirect.String(), url)
// ReSwap ...
func (h *Htmx) ReSwap(target string) {
h.ctx.Set(HXReswap.String(), target)
}

// WriteHTML ...
func (h *Htmx) WriteHTML(html template.HTML) (n int, err error) {
return h.Write([]byte(html))
// ReTarget ...
func (h *Htmx) ReTarget(target string) {
h.ctx.Set(HXRetarget.String(), target)
}

// ReSelect ...
func (h *Htmx) ReSelect(target string) {
h.ctx.Set(HXReselect.String(), target)
}

// Trigger ...
func (h *Htmx) Trigger(target string) {
h.ctx.Set(HXTrigger.String(), target)
}

// HtmxHandler ...
Expand All @@ -219,6 +244,7 @@ func NewHtmxHandler(handler HtmxHandlerFunc, config ...Config) fiber.Handler {
cfg := configDefault(config...)

return func(c *fiber.Ctx) error {
c = ContextWithHx(c)
hx := HxFromContext(c)

h := &Htmx{hx, c}
Expand Down

0 comments on commit 8de6c32

Please sign in to comment.