Fix typo for functions in Docs, Improve middleware/cors.go

pull/4/head
Vic Shóstak 2020-02-02 12:30:59 +03:00
parent 939d63f4f6
commit 342b34b80b
6 changed files with 44 additions and 41 deletions

View File

@ -141,7 +141,7 @@ app.Get("/", func(c *fiber.Ctx) {
}) })
``` ```
#### BaseUrl #### BaseURL
#### AcceptsLanguages #### AcceptsLanguages
@ -149,12 +149,12 @@ Returns the base URL, protocol and hostname combined.
```go ```go
// Function signature // Function signature
c.BaseUrl() bool c.BaseURL() bool
// Example // Example
app.Get("/", func(c *fiber.Ctx) { app.Get("/", func(c *fiber.Ctx) {
// http://webtech.oregonstate.edu/faq/page/2?sort=date // http://webtech.oregonstate.edu/faq/page/2?sort=date
c.BaseUrl() c.BaseURL()
// => "http://webtech.oregonstate.edu" // => "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. 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 ```go
// Function signature // Function signature
c.Json(v interface{}) error c.JSON(v interface{}) error
// Example // Example
type SomeStruct struct { type SomeStruct struct {
@ -532,10 +532,10 @@ app.Get("/json", func(c *fiber.Ctx) {
Name: "Grame", Name: "Grame",
Age: 20, Age: 20,
} }
c.Json(data) c.JSON(data)
// => "{"Name": "Grame", "Age": 20}" // => "{"Name": "Grame", "Age": 20}"
c.Json("Hello, World!") c.JSON("Hello, World!")
// => "Hello, World!" // => "Hello, World!"
}) })
app.Listen(8080) app.Listen(8080)
@ -549,73 +549,73 @@ app.Get("/json", func(c *fiber.Ctx) {
Name: "Grame", Name: "Grame",
Age: 20, Age: 20,
} }
if err := c.Json(data); err != nil { if err := c.JSON(data); err != nil {
c.Status(500).Send("Bad Request") c.Status(500).Send("Bad Request")
} }
// => "{"Name": "Grame", "Age": 20}" // => "{"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. 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 ```go
// Function signature // Function signature
c.Json(json []byte) c.JSON(json []byte)
// Example // Example
app := fiber.New() app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) { app.Get("/json", func(c *fiber.Ctx) {
c.JsonBytes([]byte(`"{"hello": "world"}"`)) c.JSONBytes([]byte(`"{"hello": "world"}"`))
}) })
app.Listen(8080) 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. By default, the JSONP callback name is simply callback. Override this by passing a named string in the function.
```go ```go
// Function signature // Function signature
c.Jsonp(v interface{}) error c.JSONP(v interface{}) error
c.Jsonp(v interface{}, callback string) error c.JSONP(v interface{}, callback string) error
// Example // Example
type JsonStruct struct { type SomeStruct struct {
name string name string
age uint8 age uint8
} }
app := fiber.New() app := fiber.New()
app.Get("/", func(c *fiber.Ctx) { app.Get("/", func(c *fiber.Ctx) {
data := JsonStruct{ data := SomeStruct{
name: "Grame", name: "Grame",
age: 20, age: 20,
} }
c.Jsonp(data) c.JSONP(data)
// => callback({"name": "Grame", "age": 20}) // => callback({"name": "Grame", "age": 20})
c.Jsonp(data, "customFunc") c.JSONP(data, "customFunc")
// => customFunc({"name": "Grame", "age": 20}) // => customFunc({"name": "Grame", "age": 20})
}) })
app.Listen(8080) 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. 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 ```go
// Function signature // Function signature
c.Json(json string) c.JSON(json string)
// Example // Example
app := fiber.New() app := fiber.New()
app.Get("/json", func(c *fiber.Ctx) { app.Get("/json", func(c *fiber.Ctx) {
c.JsonString(`"{"hello": "world"}"`) c.JSONString(`"{"hello": "world"}"`)
}) })
app.Listen(8080) app.Listen(8080)
``` ```
@ -775,18 +775,18 @@ app.Get("/", func(c *fiber.Ctx) {
}) })
``` ```
#### OriginalUrl #### OriginalURL
Contains the original request URL. Contains the original request URL.
```go ```go
// Function signature // Function signature
c.OriginalUrl() string c.OriginalURL() string
// Example // Example
app.Get("/", func(c *fiber.Ctx) { app.Get("/", func(c *fiber.Ctx) {
// GET /search?q=something // GET /search?q=something
c.OriginalUrl() c.OriginalURL()
// => '/search?q=something' // => '/search?q=something'
}) })
``` ```
@ -1164,28 +1164,28 @@ app.Get("/", func(c *fiber.Ctx) {
}) })
``` ```
#### Xhr #### XHR
A Boolean property that is true if the requests **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/). A Boolean property that is true if the requests **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 ```go
// Function signature // Function signature
c.Xhr() bool c.XHR() bool
// Example // Example
app.Get("/", func(c *fiber.Ctx) { app.Get("/", func(c *fiber.Ctx) {
c.Xhr() c.XHR()
// => true // => 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 ```go
// Function signature // Function signature
c.Xml(xml interface{}) error c.XML(xml interface{}) error
// Example // Example
type person struct { type person struct {
@ -1195,7 +1195,7 @@ type person struct {
app := fiber.New() app := fiber.New()
app.Get("/", func(c *fiber.Ctx) { app.Get("/", func(c *fiber.Ctx) {
c.Xml(person{"John", 50}) c.XML(person{"John", 50})
// => Content-Type: application/xml // => Content-Type: application/xml
// => <person><name>John</name><stars>50</stars></person> // => <person><name>John</name><stars>50</stars></person>

View File

@ -109,7 +109,7 @@ func main() {
Name: "John", `json:"name"` Name: "John", `json:"name"`
Age: 20, `json:"age"` Age: 20, `json:"age"`
} }
err := c.Json(data) err := c.JSON(data)
if err != nil { if err != nil {
c.SendStatus(500) c.SendStatus(500)
} }

View File

@ -23,14 +23,16 @@ IP
Ips Ips
Is Is
JSON JSON
Jsonp JSONP
JSONString
JSONBytes
Links Links
Locals Locals
Location Location
Method Method
MultipartForm MultipartForm
Next Next
OriginalUrl OriginalURL
Params Params
Path Path
Protocol Protocol
@ -54,5 +56,5 @@ Status
Subdomains Subdomains
Type Type
Vary Vary
Xhr XHR
XML XML

View File

@ -4,6 +4,7 @@ import "github.com/gofiber/fiber"
// Cors : Enable cross-origin resource sharing (CORS) with various options. // Cors : Enable cross-origin resource sharing (CORS) with various options.
func Cors(c *fiber.Ctx, d string) { 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() c.Next()
} }

View File

@ -366,7 +366,7 @@ func (ctx *Ctx) Subdomains() (subs []string) {
return subs return subs
} }
// Xhr : https://gofiber.github.io/fiber/#/context?id=xhr // XHR : https://gofiber.github.io/fiber/#/context?id=xhr
func (ctx *Ctx) Xhr() bool { func (ctx *Ctx) XHR() bool {
return ctx.Get("X-Requested-With") == "XMLHttpRequest" return ctx.Get("X-Requested-With") == "XMLHttpRequest"
} }

View File

@ -175,8 +175,8 @@ func (ctx *Ctx) JSONBytes(raw []byte) {
ctx.Fasthttp.Response.SetBodyString(getString(raw)) ctx.Fasthttp.Response.SetBodyString(getString(raw))
} }
// Jsonp : https://gofiber.github.io/fiber/#/context?id=jsonp // JSONP : https://gofiber.github.io/fiber/#/context?id=jsonp
func (ctx *Ctx) Jsonp(v interface{}, cb ...string) error { func (ctx *Ctx) JSONP(v interface{}, cb ...string) error {
raw, err := jsoniter.Marshal(&v) raw, err := jsoniter.Marshal(&v)
if err != nil { if err != nil {
return err return err