Update whats_new.md documentation

pull/3222/head
René 2024-11-29 16:04:10 +01:00
parent 6a36f6d4e6
commit 86fd29ac27
1 changed files with 164 additions and 159 deletions

View File

@ -68,7 +68,6 @@ We have made several changes to the Fiber app, including:
- **Listen**: Now has a configuration parameter. - **Listen**: Now has a configuration parameter.
- **Listener**: Now has a configuration parameter. - **Listener**: Now has a configuration parameter.
### CTX interface + customizable ### CTX interface + customizable
Fiber v3 introduces a more customizable `Ctx` interface, allowing developers to create their own context classes. This flexibility enables you to extend the default context with additional methods and properties tailored to your application's needs. Fiber v3 introduces a more customizable `Ctx` interface, allowing developers to create their own context classes. This flexibility enables you to extend the default context with additional methods and properties tailored to your application's needs.
@ -142,7 +141,6 @@ In this example, the `CustomCtx` struct extends the default `fiber.Ctx` with a c
</details> </details>
## 🗺 Router ## 🗺 Router
We have slightly adapted our router interface We have slightly adapted our router interface
@ -509,32 +507,32 @@ curl "http://localhost:3000/convert?value=abc"
package main package main
import ( import (
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
) )
func main() { func main() {
app := fiber.New() app := fiber.New()
app.Use("/user/:id", func(c fiber.Ctx) error { app.Use("/user/:id", func(c fiber.Ctx) error {
// ask database for user // ask database for user
// ... // ...
// set local values from database // set local values from database
fiber.Locals[string](c, "user", "john") fiber.Locals[string](c, "user", "john")
fiber.Locals[int](c, "age", 25) fiber.Locals[int](c, "age", 25)
// ... // ...
return c.Next() return c.Next()
}) })
app.Get("/user/*", func(c fiber.Ctx) error { app.Get("/user/*", func(c fiber.Ctx) error {
// get local values // get local values
name := fiber.Locals[string](c, "user") name := fiber.Locals[string](c, "user")
age := fiber.Locals[int](c, "age") age := fiber.Locals[int](c, "age")
// ... // ...
return c.JSON(fiber.Map{"name": name, "age": age}) return c.JSON(fiber.Map{"name": name, "age": age})
}) })
app.Listen(":3000") app.Listen(":3000")
} }
``` ```
@ -609,6 +607,7 @@ curl "http://localhost:3000/query?age=25"
curl "http://localhost:3000/query?age=abc" curl "http://localhost:3000/query?age=abc"
# Output: 0 # Output: 0
``` ```
</details> </details>
<details> <details>
@ -640,6 +639,7 @@ curl -H "User-Agent: CustomAgent" "http://localhost:3000/header"
curl "http://localhost:3000/header" curl "http://localhost:3000/header"
# Output: "Unknown" # Output: "Unknown"
``` ```
</details> </details>
## 🧬 Middlewares ## 🧬 Middlewares
@ -957,119 +957,119 @@ The `Parser` section in Fiber v3 has undergone significant changes to improve fu
1. **BodyParser**: Use `c.Bind().Body()` instead of `c.BodyParser()`. 1. **BodyParser**: Use `c.Bind().Body()` instead of `c.BodyParser()`.
<details> <details>
<summary>Example</summary> <summary>Example</summary>
```go ```go
// Before // Before
app.Post("/user", func(c *fiber.Ctx) error { app.Post("/user", func(c *fiber.Ctx) error {
var user User var user User
if err := c.BodyParser(&user); err != nil { if err := c.BodyParser(&user); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(user) return c.JSON(user)
}) })
``` ```
```go ```go
// After // After
app.Post("/user", func(c fiber.Ctx) error { app.Post("/user", func(c fiber.Ctx) error {
var user User var user User
if err := c.Bind().Body(&user); err != nil { if err := c.Bind().Body(&user); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(user) return c.JSON(user)
}) })
``` ```
</details> </details>
2. **ParamsParser**: Use `c.Bind().URL()` instead of `c.ParamsParser()`. 2. **ParamsParser**: Use `c.Bind().URL()` instead of `c.ParamsParser()`.
<details> <details>
<summary>Example</summary> <summary>Example</summary>
```go ```go
// Before // Before
app.Get("/user/:id", func(c *fiber.Ctx) error { app.Get("/user/:id", func(c *fiber.Ctx) error {
var params Params var params Params
if err := c.ParamsParser(&params); err != nil { if err := c.ParamsParser(&params); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(params) return c.JSON(params)
}) })
``` ```
```go ```go
// After // After
app.Get("/user/:id", func(c fiber.Ctx) error { app.Get("/user/:id", func(c fiber.Ctx) error {
var params Params var params Params
if err := c.Bind().URL(&params); err != nil { if err := c.Bind().URL(&params); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(params) return c.JSON(params)
}) })
``` ```
</details> </details>
3. **QueryParser**: Use `c.Bind().Query()` instead of `c.QueryParser()`. 3. **QueryParser**: Use `c.Bind().Query()` instead of `c.QueryParser()`.
<details> <details>
<summary>Example</summary> <summary>Example</summary>
```go ```go
// Before // Before
app.Get("/search", func(c *fiber.Ctx) error { app.Get("/search", func(c *fiber.Ctx) error {
var query Query var query Query
if err := c.QueryParser(&query); err != nil { if err := c.QueryParser(&query); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(query) return c.JSON(query)
}) })
``` ```
```go ```go
// After // After
app.Get("/search", func(c fiber.Ctx) error { app.Get("/search", func(c fiber.Ctx) error {
var query Query var query Query
if err := c.Bind().Query(&query); err != nil { if err := c.Bind().Query(&query); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(query) return c.JSON(query)
}) })
``` ```
</details> </details>
4. **CookieParser**: Use `c.Bind().Cookie()` instead of `c.CookieParser()`. 4. **CookieParser**: Use `c.Bind().Cookie()` instead of `c.CookieParser()`.
<details> <details>
<summary>Example</summary> <summary>Example</summary>
```go ```go
// Before // Before
app.Get("/cookie", func(c *fiber.Ctx) error { app.Get("/cookie", func(c *fiber.Ctx) error {
var cookie Cookie var cookie Cookie
if err := c.CookieParser(&cookie); err != nil { if err := c.CookieParser(&cookie); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(cookie) return c.JSON(cookie)
}) })
``` ```
```go ```go
// After // After
app.Get("/cookie", func(c fiber.Ctx) error { app.Get("/cookie", func(c fiber.Ctx) error {
var cookie Cookie var cookie Cookie
if err := c.Bind().Cookie(&cookie); err != nil { if err := c.Bind().Cookie(&cookie); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()}) return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
} }
return c.JSON(cookie) return c.JSON(cookie)
}) })
``` ```
</details> </details>
#### 🔄 Redirect #### 🔄 Redirect
@ -1079,63 +1079,66 @@ Fiber v3 enhances the redirect functionality by introducing new methods and impr
1. **RedirectToRoute**: Use `c.Redirect().Route()` instead of `c.RedirectToRoute()`. 1. **RedirectToRoute**: Use `c.Redirect().Route()` instead of `c.RedirectToRoute()`.
<details> <details>
<summary>Example</summary> <summary>Example</summary>
```go ```go
// Before // Before
app.Get("/old", func(c *fiber.Ctx) error { app.Get("/old", func(c *fiber.Ctx) error {
return c.RedirectToRoute("newRoute") return c.RedirectToRoute("newRoute")
}) })
``` ```
```go ```go
// After // After
app.Get("/old", func(c fiber.Ctx) error { app.Get("/old", func(c fiber.Ctx) error {
return c.Redirect().Route("newRoute") return c.Redirect().Route("newRoute")
}) })
``` ```
</details>
</details>
2. **RedirectBack**: Use `c.Redirect().Back()` instead of `c.RedirectBack()`. 2. **RedirectBack**: Use `c.Redirect().Back()` instead of `c.RedirectBack()`.
<details> <details>
<summary>Example</summary> <summary>Example</summary>
```go ```go
// Before // Before
app.Get("/back", func(c *fiber.Ctx) error { app.Get("/back", func(c *fiber.Ctx) error {
return c.RedirectBack() return c.RedirectBack()
}) })
``` ```
```go ```go
// After // After
app.Get("/back", func(c fiber.Ctx) error { app.Get("/back", func(c fiber.Ctx) error {
return c.Redirect().Back() return c.Redirect().Back()
}) })
``` ```
</details>
</details>
3. **Redirect**: Use `c.Redirect().To()` instead of `c.Redirect()`. 3. **Redirect**: Use `c.Redirect().To()` instead of `c.Redirect()`.
<details> <details>
<summary>Example</summary> <summary>Example</summary>
```go ```go
// Before // Before
app.Get("/old", func(c *fiber.Ctx) error { app.Get("/old", func(c *fiber.Ctx) error {
return c.Redirect("/new") return c.Redirect("/new")
}) })
``` ```
```go ```go
// After // After
app.Get("/old", func(c fiber.Ctx) error { app.Get("/old", func(c fiber.Ctx) error {
return c.Redirect().To("/new") return c.Redirect().To("/new")
}) })
``` ```
</details>
</details>
### 🌎 Client package ### 🌎 Client package
@ -1149,7 +1152,7 @@ Fiber v3 introduces a completely rebuilt client package with numerous new featur
#### Migration Instructions #### Migration Instructions
**Import Path** **Import Path**:
Update the import path to the new client package. Update the import path to the new client package.
@ -1159,6 +1162,7 @@ Update the import path to the new client package.
```go ```go
import "github.com/gofiber/fiber/v2/client" import "github.com/gofiber/fiber/v2/client"
``` ```
</details> </details>
<details> <details>
@ -1167,6 +1171,7 @@ import "github.com/gofiber/fiber/v2/client"
```go ```go
import "github.com/gofiber/fiber/v3/client" import "github.com/gofiber/fiber/v3/client"
``` ```
</details> </details>
:::caution :::caution