From 342b34b80bcfcbb612527c09d78cf7c5c28a47d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vic=20Sh=C3=B3stak?= Date: Sun, 2 Feb 2020 12:30:59 +0300 Subject: [PATCH] Fix typo for functions in Docs, Improve middleware/cors.go --- docs/context.md | 64 +++++++++++++++++++++++----------------------- docs/examples.md | 2 +- docs/functions.txt | 8 +++--- middleware/cors.go | 3 ++- request.go | 4 +-- response.go | 4 +-- 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/docs/context.md b/docs/context.md index 60178a17..33b8faa9 100644 --- a/docs/context.md +++ b/docs/context.md @@ -141,7 +141,7 @@ app.Get("/", func(c *fiber.Ctx) { }) ``` -#### BaseUrl +#### BaseURL #### AcceptsLanguages @@ -149,12 +149,12 @@ Returns the base URL, protocol and hostname combined. ```go // Function signature -c.BaseUrl() bool +c.BaseURL() bool // Example app.Get("/", func(c *fiber.Ctx) { // http://webtech.oregonstate.edu/faq/page/2?sort=date - c.BaseUrl() + c.BaseURL() // => "http://webtech.oregonstate.edu" }) ``` @@ -512,13 +512,13 @@ app.Get("/", func(c *fiber.Ctx) { }) ``` -#### Json +#### JSON Converts any interface or string to json using [Jsoniter](https://github.com/json-iterator/go), this function also sets the content header to application/json. ```go // Function signature -c.Json(v interface{}) error +c.JSON(v interface{}) error // Example type SomeStruct struct { @@ -532,10 +532,10 @@ app.Get("/json", func(c *fiber.Ctx) { Name: "Grame", Age: 20, } - c.Json(data) + c.JSON(data) // => "{"Name": "Grame", "Age": 20}" - c.Json("Hello, World!") + c.JSON("Hello, World!") // => "Hello, World!" }) app.Listen(8080) @@ -549,73 +549,73 @@ app.Get("/json", func(c *fiber.Ctx) { Name: "Grame", Age: 20, } - if err := c.Json(data); err != nil { + if err := c.JSON(data); err != nil { c.Status(500).Send("Bad Request") } // => "{"Name": "Grame", "Age": 20}" }) ``` -#### JsonBytes +#### JSONBytes This function accepts raw []byte bodies and sets the content header to application/json. This function is used if you do not need type assertion. ```go // Function signature -c.Json(json []byte) +c.JSON(json []byte) // Example app := fiber.New() app.Get("/json", func(c *fiber.Ctx) { - c.JsonBytes([]byte(`"{"hello": "world"}"`)) + c.JSONBytes([]byte(`"{"hello": "world"}"`)) }) app.Listen(8080) ``` -#### Jsonp +#### JSONP -Sends a JSON response with JSONP support. This method is identical to [Json()](#json), except that it opts-in to JSONP callback support. +Sends a JSON response with JSONP support. This method is identical to [JSON()](#json), except that it opts-in to JSONP callback support. By default, the JSONP callback name is simply callback. Override this by passing a named string in the function. ```go // Function signature -c.Jsonp(v interface{}) error -c.Jsonp(v interface{}, callback string) error +c.JSONP(v interface{}) error +c.JSONP(v interface{}, callback string) error // Example -type JsonStruct struct { +type SomeStruct struct { name string age uint8 } app := fiber.New() app.Get("/", func(c *fiber.Ctx) { - data := JsonStruct{ + data := SomeStruct{ name: "Grame", age: 20, } - c.Jsonp(data) + c.JSONP(data) // => callback({"name": "Grame", "age": 20}) - c.Jsonp(data, "customFunc") + c.JSONP(data, "customFunc") // => customFunc({"name": "Grame", "age": 20}) }) app.Listen(8080) ``` -#### JsonString +#### JSONString This function accepts raw string body and sets the content header to application/json. This function is used if you do not need type assertion. ```go // Function signature -c.Json(json string) +c.JSON(json string) // Example app := fiber.New() app.Get("/json", func(c *fiber.Ctx) { - c.JsonString(`"{"hello": "world"}"`) + c.JSONString(`"{"hello": "world"}"`) }) app.Listen(8080) ``` @@ -775,18 +775,18 @@ app.Get("/", func(c *fiber.Ctx) { }) ``` -#### OriginalUrl +#### OriginalURL Contains the original request URL. ```go // Function signature -c.OriginalUrl() string +c.OriginalURL() string // Example app.Get("/", func(c *fiber.Ctx) { // GET /search?q=something - c.OriginalUrl() + c.OriginalURL() // => '/search?q=something' }) ``` @@ -1164,28 +1164,28 @@ app.Get("/", func(c *fiber.Ctx) { }) ``` -#### Xhr +#### XHR A Boolean property that is true if the request’s **X-Requested-With** header field is **XMLHttpRequest**, indicating that the request was issued by a client library such as [jQuery](https://api.jquery.com/jQuery.ajax/). ```go // Function signature -c.Xhr() bool +c.XHR() bool // Example app.Get("/", func(c *fiber.Ctx) { - c.Xhr() + c.XHR() // => true }) ``` -#### Xml +#### XML -Xml sets the header to "application/xml" and marshals your interface to xml. +XML sets the header to "application/xml" and marshals your interface to xml. ```go // Function signature -c.Xml(xml interface{}) error +c.XML(xml interface{}) error // Example type person struct { @@ -1195,7 +1195,7 @@ type person struct { app := fiber.New() app.Get("/", func(c *fiber.Ctx) { - c.Xml(person{"John", 50}) + c.XML(person{"John", 50}) // => Content-Type: application/xml // => John50 diff --git a/docs/examples.md b/docs/examples.md index 87400fc3..20839b45 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -109,7 +109,7 @@ func main() { Name: "John", `json:"name"` Age: 20, `json:"age"` } - err := c.Json(data) + err := c.JSON(data) if err != nil { c.SendStatus(500) } diff --git a/docs/functions.txt b/docs/functions.txt index a8365fff..5b9c6254 100644 --- a/docs/functions.txt +++ b/docs/functions.txt @@ -23,14 +23,16 @@ IP Ips Is JSON -Jsonp +JSONP +JSONString +JSONBytes Links Locals Location Method MultipartForm Next -OriginalUrl +OriginalURL Params Path Protocol @@ -54,5 +56,5 @@ Status Subdomains Type Vary -Xhr +XHR XML diff --git a/middleware/cors.go b/middleware/cors.go index 572592e5..170219bb 100644 --- a/middleware/cors.go +++ b/middleware/cors.go @@ -4,6 +4,7 @@ import "github.com/gofiber/fiber" // Cors : Enable cross-origin resource sharing (CORS) with various options. func Cors(c *fiber.Ctx, d string) { - c.Set("Access-Control-Allow-Origin", d) + c.Set("Access-Control-Allow-Origin", d) // Set d to "*" for allow all domains + c.Set("Access-Control-Allow-Headers", "X-Requested-With") c.Next() } diff --git a/request.go b/request.go index 4038c0e7..22eb0142 100644 --- a/request.go +++ b/request.go @@ -366,7 +366,7 @@ func (ctx *Ctx) Subdomains() (subs []string) { return subs } -// Xhr : https://gofiber.github.io/fiber/#/context?id=xhr -func (ctx *Ctx) Xhr() bool { +// XHR : https://gofiber.github.io/fiber/#/context?id=xhr +func (ctx *Ctx) XHR() bool { return ctx.Get("X-Requested-With") == "XMLHttpRequest" } diff --git a/response.go b/response.go index c4ad49a3..f21ce8cb 100644 --- a/response.go +++ b/response.go @@ -175,8 +175,8 @@ func (ctx *Ctx) JSONBytes(raw []byte) { ctx.Fasthttp.Response.SetBodyString(getString(raw)) } -// Jsonp : https://gofiber.github.io/fiber/#/context?id=jsonp -func (ctx *Ctx) Jsonp(v interface{}, cb ...string) error { +// JSONP : https://gofiber.github.io/fiber/#/context?id=jsonp +func (ctx *Ctx) JSONP(v interface{}, cb ...string) error { raw, err := jsoniter.Marshal(&v) if err != nil { return err