querybind

package module
v0.0.0-...-15a1924 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2023 License: MIT Imports: 6 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind[T any](ctx HTTPContext) (*T, error)

Bind binds query parameters to a struct of type T based on `query` tags.

func Fiber

func Fiber(ctx *fiber.Ctx) fiberContext

func ResponseBind

func ResponseBind[T any](ctx HTTPContext, value T, options ...ResponseBindOption)

ResponseBind sets the HX-Push-Url header in the response based on the struct's `query` tags.

Types

type HTTPContext

type HTTPContext interface {
	GetHeader(name string) string
	SetHeader(name, value string)
	OriginalURL() string
	Referer() string
	BaseUrl() string
	Path() string
	QueryParams(params *url.Values)
}

HTTPContext abstracts the HTTP request and response context.

type ResponseBindOption

type ResponseBindOption func(*ResponseBindParams)

func WithPath

func WithPath(path string) ResponseBindOption

type ResponseBindParams

type ResponseBindParams struct {
	Path *string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL