A pagination middleware for Fiber
go get -u github.com/garrettladley/fiberpaginate/v2
Property | Type | Description | Default |
---|---|---|---|
Next | func(*fiber.Ctx) bool |
Next defines a function to skip this middleware when returned true. | nil |
PageKey | string |
PageKey is the key for the page number in the query string. | "page" |
DefaultPage | int |
DefaultPage is the default page number to use when not provided as a query paramater in the url. If the page number is less than 1, it will be set to the default page number, 1. | 1 |
LimitKey | string |
LimitKey is the key for the limit number in the query string. | "limit" |
DefaultLimit | int |
DefaultLimit is the default limit to use when not provided as a query paramater in the url. If the limit is less than 1, it will be set to the default limit, 10. | 10 |
package main
import (
"log"
"github.com/garrettladley/fiberpaginate/v2"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Use(fiber_pagnate.New())
app.Get("/", func(c *fiber.Ctx) error {
// when given a query string like ?page=2&limit=5,
// the middleware will parse the query string and set the values
pageInfo, ok := fiberpaginate.FromContext(c)
if !ok {
return fiber.ErrBadRequest
}
return c.JSON(
fiber.Map{
"page": pageInfo.Page,
"limit": pageInfo.Limit,
"start": pageInfo.Start(),
},
)
})
log.Fatal(app.Listen(":3000"))
}
If DefaultPage is configured to be less than 1, the middleware will use 1 as the default value for the page. If DefaultLimit is configured to be less than 1, the middleware will use 10 as the default value for the limit.
package main
import (
"log"
"github.com/garrettladley/fiberpaginate/v2"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Use(fiberpaginate.New(fiberpaginate.Config{
DefaultPage: -1,
DefaultLimit: -1,
}))
app.Get("/", func(c *fiber.Ctx) error {
pageInfo, ok := fiberpaginate.FromContext(c)
if !ok {
return fiber.ErrBadRequest
}
return c.JSON(
fiber.Map{
"page": pageInfo.Page, // 1
"limit": pageInfo.Limit, // 10
"start": pageInfo.Start(), // 0
},
)
})
log.Fatal(app.Listen(":3000"))
}
If the client provides an invalid type to page or limit, the middleware will use 0 as the value stored for the page or limit.
package main
import (
"log"
"github.com/garrettladley/fiberpaginate/v2"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Use(fiberpaginate.New())
app.Get("/", func(c *fiber.Ctx) error {
// when given a query string like ?page=foo&limit=bar,
// the middleware will parse the query string and set
// the values to 0 due to the invalid types
pageInfo, ok := fiberpaginate.FromContext(c)
if !ok {
return fiber.ErrBadRequest
}
return c.JSON(
fiber.Map{
"page": pageInfo.Page, // 0
"limit": pageInfo.Limit, // 0
"start": pageInfo.Start(), // 0
},
)
})
log.Fatal(app.Listen(":3000"))
}