Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docs for hooks. #243

Merged
merged 4 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
* [📝 Templates](guide/templates.md)
* [🐛 Error Handling](guide/error-handling.md)
* [🔎 Validation](guide/validation.md)
* [🪝 Hooks](guide/hooks.md)


## Extra

Expand Down
9 changes: 9 additions & 0 deletions api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,12 @@ if resp.StatusCode == fiber.StatusOK {
```
{% endcode %}

## Hooks

Hooks is a method to return [hooks](../guide/hooks.md) property.

{% code title="Signature" %}
```go
func (app *App) Hooks() *Hooks
```
{% endcode %}
136 changes: 136 additions & 0 deletions guide/hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# 🪝 Hooks

With Fiber v2.29.0, you can execute custom user functions when to run some methods. Here is a list of this hooks:
efectn marked this conversation as resolved.
Show resolved Hide resolved
- [OnRoute](#onroute)
- [OnName](#onname)
- [OnGroup](#ongroup)
- [OnGroupName](#ongroupname)
- [OnListen](#onlisten)
- [OnShutdown](#onshutdown)

## Constants
```go
// Handlers define a function to create hooks for Fiber.
type OnRouteHandler = func(Route) error
type OnNameHandler = OnRouteHandler
type OnGroupHandler = func(Group) error
type OnGroupNameHandler = OnGroupHandler
type OnListenHandler = func() error
type OnShutdownHandler = OnListenHandler
```

## OnRoute

OnRoute is a hook to execute user functions on each route registeration. Also you can get route properties by **route** parameter.

{% code title="Signature" %}
```go
func (app *App) OnRoute(handler ...OnRouteHandler)
```
{% endcode %}

## OnName

OnName is a hook to execute user functions on each route naming. Also you can get route properties by **route** parameter.

**WARN:** OnName only works with naming routes, not groups.

{% code title="Signature" %}
```go
func (app *App) OnName(handler ...OnNameHandler)
```
{% endcode %}

## OnGroup

OnGroup is a hook to execute user functions on each group registeration. Also you can get group properties by **group** parameter.

{% code title="Signature" %}
```go
func (app *App) OnGroup(handler ...OnGroupHandler)
```
{% endcode %}

## OnGroupName

OnGroupName is a hook to execute user functions on each group naming. Also you can get group properties by **group** parameter.

**WARN:** OnGroupName only works with naming groups, not routes.

{% code title="Signature" %}
```go
func (app *App) OnGroupName(handler ...OnGroupNameHandler)
```
{% endcode %}

## OnListen

OnListen is a hook to execute user functions on Listen, ListenTLS, Listener.

{% code title="Signature" %}
```go
func (app *App) OnListen(handler ...OnListenHandler)
```
{% endcode %}

## OnShutdown

OnShutdown is a hook to execute user functions after Shutdown.

{% code title="Signature" %}
```go
func (app *App) OnShutdown(handler ...OnShutdownHandler)
```
{% endcode %}


{% tabs %}
{% tab title="OnName Example" %}
```go
package main

import (
"fmt"

"github.com/gofiber/fiber/v2"
)

func main() {
app := fiber.New()

app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("index")

app.Hooks().OnName(func(r fiber.Route) error {
fmt.Print("Name: " + r.Name + ", ")

return nil
})

app.Hooks().OnName(func(r fiber.Route) error {
fmt.Print("Method: " + r.Method + "\n")

return nil
})

app.Get("/add/user", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("addUser")

app.Delete("/destroy/user", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("destroyUser")

app.Listen(":5000")
}

// Results:
// Name: addUser, Method: GET
// Name: destroyUser, Method: DELETE
```
{% endtab %}

{% endtabs %}