Files
artalkjs/server/common/params.go
T
qwqcode 589850d311 refactor(api): refactor HTTP API to version 2 (#735)
* refactor(api): refactor HTTP API to version 2

* refactor: remove forced site_origin check

* refactor: update http client

* fix

* add more api description and error code

* reduce nestings in response structures

* separate unread

* refactor(api/comments): get comments

* refactor: frontend api module

* chore: remove vite temporary file

* chore: restore swagger file

* update

* 移除为管理员分配站点名的站点隔离功能,简化实现

* refactor pagesAllFetch

* fix ci

* remove useless codes

* limiter protected paths

* add test for version endpoint

* rename handler code files

* code file separate

* fix some bug of interacting with backend

* add fetch error class for handling

* fix and rename

* refactor: checker support both `Promise` and callback fn

* fix new checker captcha iframe url

* rename and add swagger id field

* replace tab with spaces in go comment

* chore: refine

* complete the validate tag for request fields and enable `--defaultRequired`

90% of the request fields of our API are required,100% of response fields in our API are required.
Each field add validate:"required" is easy to miss.

* replace original api client with auto-generated codes

* artalk pkg apply new api client

* artalk-sidebar pkg apply new api client

* fix issues

* fix sidebar locale conf and refactor user login

* fix and refactor captcha and limiter

* fix ui testing

* add script to build http client by swagger.json
2024-01-24 21:33:30 +08:00

97 lines
2.1 KiB
Go

package common
import (
"fmt"
"reflect"
"strings"
"github.com/ArtalkJS/Artalk/internal/log"
"github.com/gofiber/fiber/v2"
)
type Map = map[string]interface{}
func ParamsDecode(c *fiber.Ctx, destParams interface{}) (isContinue bool, resp error) {
reqMethod := c.Method()
// validate required
if isContinue, resp := ValidateRequired(c, destParams); !isContinue {
return false, resp
}
// parse query
if err := c.QueryParser(destParams); err != nil {
return false, err
}
// parse path params
if err := c.ParamsParser(destParams); err != nil {
return false, err
}
// parse body
if reqMethod == "POST" || reqMethod == "PUT" {
if err := c.BodyParser(destParams); err != nil {
return false, err
}
}
return true, nil
}
func ValidateRequired(c *fiber.Ctx, destParams interface{}) (isContinue bool, resp error) {
reqMethod := c.Method()
reqPath := c.Path()
refVal := reflect.ValueOf(destParams)
for i := 0; i < refVal.Elem().Type().NumField(); i++ {
k := refVal.Elem().Type().Field(i)
validateTag := k.Tag.Get("validate")
requiredField := (validateTag == "required")
if !requiredField {
continue // only check required field
}
// get param key
paramKey := ""
tagNames := []string{"query", "json", "form"}
for _, tagName := range tagNames {
paramKey = k.Tag.Get(tagName)
if paramKey != "" {
break
}
}
if paramKey == "" {
log.Errorf("[Validator] %s field '%s' real param key not found", reqPath, k.Name)
continue
}
// get param value
paramVal := func() string {
switch {
case reqMethod == "GET" || reqMethod == "DELETE":
return c.Query(paramKey)
case reqMethod == "POST" || reqMethod == "PUT":
var destMap map[string]interface{}
err := c.BodyParser(&destMap)
if err != nil {
log.Error("[Validator] "+reqPath+" field '"+k.Name+"' ", err)
return ""
}
if v, ok := destMap[paramKey]; ok {
return fmt.Sprintf("%v", v)
}
return ""
}
return ""
}()
// check required param
if strings.TrimSpace(paramVal) == "" {
return false, RespError(c, 400, "Param `"+paramKey+"` is required")
}
}
return true, nil
}