Skip to content

Commit

Permalink
[v3-Maintenance]-Consolidate-and-Document-Core-Changes-in-v3
Browse files Browse the repository at this point in the history
  • Loading branch information
ReneWerner87 committed Apr 20, 2024
1 parent f30e24b commit 0e29268
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 349 deletions.
2 changes: 1 addition & 1 deletion docs/core/api/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: client
title: 🌎 Client
description: The Client struct represents the Fiber HTTP Client.
sidebar_position: 5
sidebar_position: 6
---

## Start request
Expand Down
4 changes: 2 additions & 2 deletions docs/core/api/constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: constants
title: 📋 Constants
description: Some constants for Fiber.
sidebar_position: 4
sidebar_position: 9
---

HTTP methods were copied from net/http.
Expand Down Expand Up @@ -288,4 +288,4 @@ const (
HeaderXRobotsTag = "X-Robots-Tag"
HeaderXUACompatible = "X-UA-Compatible"
)
```
```
187 changes: 62 additions & 125 deletions docs/core/api/ctx.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ description: >-
sidebar_position: 3
---

:::caution

Documentation is still in progress.

:::

## Accepts

Checks, if the specified **extensions** or **content** **types** are acceptable.
Expand Down Expand Up @@ -226,6 +220,7 @@ app.Get("/", func(c fiber.Ctx) error {
## Bind

Bind is a method that support supports bindings for the request/response body, query parameters, URL parameters, cookies and much more.
It returns a pointer to the [Bind](./bind.md) struct which contains all the methods to bind the request/response data.

For detailed information check the [Bind](./bind.md) documentation.

Expand Down Expand Up @@ -370,37 +365,6 @@ func (c Ctx) Context() *fasthttp.RequestCtx
Please read the [Fasthttp Documentation](https://pkg.go.dev/github.com/valyala/fasthttp?tab=doc) for more information.
:::


## Convert

[//]: # (TODO: put it in a different section for generics)
[//]: # (TODO: add Locals, Params, Query, GetReqHeader method)

Converts a string value to a specified type, handling errors and optional default values.
This function simplifies the conversion process by encapsulating error handling and the management of default values, making your code cleaner and more consistent.

```go title="Signature"
func Convert[T any](value string, convertor func(string) (T, error), defaultValue ...T) (*T, error)
```

```go title="Example"
// GET http://example.com/id/bb70ab33-d455-4a03-8d78-d3c1dacae9ff
app.Get("/id/:id", func(c fiber.Ctx) error {
fiber.Convert(c.Params("id"), uuid.Parse) // UUID(bb70ab33-d455-4a03-8d78-d3c1dacae9ff), nil


// GET http://example.com/search?id=65f6f54221fb90e6a6b76db7
app.Get("/search", func(c fiber.Ctx) error) {
fiber.Convert(c.Query("id"), mongo.ParseObjectID) // objectid(65f6f54221fb90e6a6b76db7), nil
fiber.Convert(c.Query("id"), uuid.Parse) // uuid.Nil, error(cannot parse given uuid)
fiber.Convert(c.Query("id"), uuid.Parse, mongo.NewObjectID) // new object id generated and return nil as error.
}

// ...
})
```


## Cookie

Set cookie
Expand Down Expand Up @@ -1461,103 +1425,24 @@ app.Get("/", func(c fiber.Ctx) error {
## Redirect
[//]: # (TODO: put it in a different file -> like binding)
Returns the Redirect reference.
Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an HTTP status code.
:::info
If **not** specified, status defaults to **302 Found**.
:::
For detailed information check the [Redirect](./redirect.md) documentation.
```go title="Signature"
func (c Ctx) Redirect(location string, status ...int) error
func (c Ctx) Redirect() *Redirect
```
```go title="Example"
app.Get("/coffee", func(c fiber.Ctx) error {
return c.Redirect("/teapot")
return c.Redirect().To("/teapot")
})
app.Get("/teapot", func(c fiber.Ctx) error {
return c.Status(fiber.StatusTeapot).Send("🍵 short and stout 🍵")
})
```
```go title="More examples"
app.Get("/", func(c fiber.Ctx) error {
return c.Redirect("/foo/bar")
return c.Redirect("../login")
return c.Redirect("http://example.com")
return c.Redirect("http://example.com", 301)
return c.Status(fiber.StatusTeapot).Send("🍵 short and stout 🍵")
})
```
## RedirectToRoute
[//]: # (TODO: put it in a different file -> like binding)
Redirects to the specific route along with the parameters and with specified status, a positive integer that corresponds to an HTTP status code.
:::info
If **not** specified, status defaults to **302 Found**.
:::
:::info
If you want to send queries to route, you must add **"queries"** key typed as **map[string]string** to params.
:::
```go title="Signature"
func (c Ctx) RedirectToRoute(routeName string, params fiber.Map, status ...int) error
```
```go title="Example"
app.Get("/", func(c fiber.Ctx) error {
// /user/fiber
return c.RedirectToRoute("user", fiber.Map{
"name": "fiber"
})
})
app.Get("/with-queries", func(c fiber.Ctx) error {
// /user/fiber?data[0][name]=john&data[0][age]=10&test=doe
return c.RedirectToRoute("user", fiber.Map{
"name": "fiber",
"queries": map[string]string{"data[0][name]": "john", "data[0][age]": "10", "test": "doe"},
})
})
app.Get("/user/:name", func(c fiber.Ctx) error {
return c.SendString(c.Params("name"))
}).Name("user")
```
## RedirectBack
[//]: # (TODO: put it in a different file -> like binding)
Redirects back to refer URL. It redirects to fallback URL if refer header doesn't exists, with specified status, a positive integer that corresponds to an HTTP status code.

:::info
If **not** specified, status defaults to **302 Found**.
:::

```go title="Signature"
func (c Ctx) RedirectBack(fallback string, status ...int) error
```

```go title="Example"
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Home page")
})
app.Get("/test", func(c fiber.Ctx) error {
c.Set("Content-Type", "text/html")
return c.SendString(`<a href="/back">Back</a>`)
})
app.Get("/back", func(c fiber.Ctx) error {
return c.RedirectBack("/")
})
```
## Render
Expand Down Expand Up @@ -1598,7 +1483,15 @@ app.Get("/", func(c fiber.Ctx) error {
})
```
[//]: # (TODO: add RESET method)
## Reset
Reset the context fields by given request when to use server handlers.
```go title="Signature"
func (c Ctx) Reset(fctx *fasthttp.RequestCtx)
```
It is used outside of the Fiber Handlers to reset the context for the next request.
## RestartRouting
Expand Down Expand Up @@ -1850,9 +1743,35 @@ app.Get("/not-found", func(c fiber.Ctx) error {
})
```

[//]: # (TODO: add SendStream)
[//]: # (TODO: add SendString)
## SendStream

Sets response body to a stream of data and add optional body size.

```go title="Signature"
func (c Ctx) SendStream(stream io.Reader, size ...int) error
```

```go title="Example"
app.Get("/", func(c fiber.Ctx) error {
return c.SendStream(bytes.NewReader([]byte("Hello, World!")))
// => "Hello, World!"
})
```

## SendString

Sets the response body to a string.

```go title="Signature"
func (c Ctx) SendString(body string) error
```

```go title="Example"
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
// => "Hello, World!"
})
```

## Set

Expand Down Expand Up @@ -1924,7 +1843,21 @@ app.Get("/world", func(c fiber.Ctx) error {
})
```

[//]: # (TODO: add String)
## String

Returns unique string representation of the ctx.

```go title="Signature"
func (c Ctx) String() string
```

```go title="Example"
app.Get("/", func(c fiber.Ctx) error {
c.String() // => "#0000000100000001 - 127.0.0.1:3000 <-> 127.0.0.1:61516 - GET http://localhost:3000/"
// ...
})
```

## Subdomains

Expand All @@ -1951,6 +1884,10 @@ app.Get("/", func(c fiber.Ctx) error {

Sets the [Content-Type](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type) HTTP header to the MIME type listed [here](https://github.com/nginx/nginx/blob/master/conf/mime.types) specified by the file **extension**.

:::info
Method is a **chainable**.
:::

```go title="Signature"
func (c Ctx) Type(ext string, charset ...string) Ctx
```
Expand Down
2 changes: 1 addition & 1 deletion docs/core/api/hooks.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: hooks
title: 🎣 Hooks
sidebar_position: 7
sidebar_position: 8
---

import Tabs from '@theme/Tabs';
Expand Down
2 changes: 1 addition & 1 deletion docs/core/api/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
id: log
title: 📃 Log
description: Fiber's built-in log package
sidebar_position: 6
sidebar_position: 7
---

We can use logs to observe program behavior, diagnose problems, or configure corresponding alarms.
Expand Down
2 changes: 1 addition & 1 deletion docs/core/api/middleware/_category_.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"label": "🧬 Middleware",
"position": 10,
"position": 11,
"collapsed": true,
"link": {
"type": "generated-index",
Expand Down
Loading

0 comments on commit 0e29268

Please sign in to comment.