-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add NewWithCustomCtx initialization helper #3476
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
Changes from 1 commit
6da4783
4fbcebf
c956f6c
40dea8e
fe01057
4bacc1a
01c6c91
b8a0ed0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -512,12 +512,14 @@ | |
func (app *App) ErrorHandler(ctx Ctx, err error) error | ||
``` | ||
|
||
## NewCtxFunc | ||
|
||
Check failure on line 515 in docs/api/app.md
|
||
`NewCtxFunc` allows you to customize the `ctx` struct as needed. | ||
## NewWithCustomCtx | ||
|
||
`NewWithCustomCtx` creates a new `*App` and sets the custom context factory | ||
function at construction time. | ||
|
||
```go title="Signature" | ||
func (app *App) NewCtxFunc(function func(app *App) CustomCtx) | ||
func NewWithCustomCtx(fn func(app *App) CustomCtx, config ...Config) *App | ||
``` | ||
|
||
```go title="Example" | ||
|
@@ -533,22 +535,18 @@ | |
fiber.DefaultCtx | ||
} | ||
|
||
// Custom method | ||
func (c *CustomCtx) Params(key string, defaultValue ...string) string { | ||
return "prefix_" + c.DefaultCtx.Params(key) | ||
} | ||
|
||
func main() { | ||
app := fiber.New() | ||
|
||
app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx { | ||
app := fiber.NewWithCustomCtx(func(app *fiber.App) fiber.CustomCtx { | ||
return &CustomCtx{ | ||
DefaultCtx: *fiber.NewDefaultCtx(app), | ||
} | ||
}) | ||
Comment on lines
541
to
546
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fix incorrect return type in factory signature app := fiber.NewWithCustomCtx(func(app *fiber.App) CustomCtx {
return &CustomCtx{ DefaultCtx: *fiber.NewDefaultCtx(app) }
}) Or explicitly return the interface type: func(app *fiber.App) fiber.Ctx { … } 🤖 Prompt for AI Agents
|
||
|
||
app.Get("/:id", func(c fiber.Ctx) error { | ||
// Use custom method - output: prefix_123 | ||
return c.SendString(c.Params("id")) | ||
}) | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.