Skip to content

Fiber utility functions to parse queryparams and preserve its state for web applications

License

Notifications You must be signed in to change notification settings

davidroman0O/querybind

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QueryBind

The querybind package is a utility library for the Fiber web framework, designed to simplify query parameter binding and state management in HTMX-driven applications. It provides a way to bind query parameters to Go structs and manage URL state seamlessly.

TODO

  • Integrate more libs/framework, not just fiber
  • WithRedirection: might not want to use HX-Push
  • WithCustom: might want to do something custom ¯_(ツ)_/¯
  • more examples

Features

  • Bind URL query parameters to Go structs dynamically using struct tags.
  • Maintain and manipulate browser URL state with HTMX requests without full page reloads.
  • Provide functional options to customize the behavior of the response binding.

Installation

To use the querybind package in your project, run:

go get github.com/davidroman0O/querybind

Usage

See examples querybind-example

QueryBind

Bind query parameters from the request to a struct:

type FilterParams struct {
    Genres []string `querybind:"genres"`
    Years  []int    `querybind:"years"`
}

// ...

// return refreshed html of a component
app.Get("/page/component", func(c *fiber.Ctx) error {
    var params FilterParams
    if err := querybind.Bind(&params, c); err != nil {
        return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
    }
    // Use params here...
    // return component html
})

ResponseBind

Update the URL state using ResponseBind by setting a HX-Push-Url response header:

// return refreshed html of a component
app.Get("/page/component", func(c *fiber.Ctx) error {
    // Assume params is populated... and you did some processing on some data, whatever
    querybind.ResponseBind(c, params, querybind.WithPath("/page")) // for the component, you might want to keep the path of the page
    // Continue with response... that return the html
})

Options

The ResponseBind function can be customized with the following option:

  • WithPath(path string): Customizes the path used in the HX-Push-Url header.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

  • The Fiber team for creating a fantastic web framework.
  • The creators and contributors of HTMX for enabling modern, dynamic web interactions.

About

Fiber utility functions to parse queryparams and preserve its state for web applications

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages