Giter Club home page Giter Club logo

fiberpaginate's Introduction

fiberpaginate


A pagination middleware for Fiber

Install

go get -u github.com/garrettladley/fiberpaginate/v2

Config

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

Example

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"))
}

Note with negative values in the config

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"))
}

Note with invalid keys

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"))
}

fiberpaginate's People

Contributors

garrettladley avatar

Watchers

 avatar

fiberpaginate's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.