leaf-library-3/internal/pkg/response/response.go

171 lines
2.9 KiB
Go
Raw Normal View History

2024-12-10 10:22:14 +00:00
package response
2024-12-05 17:44:29 +00:00
import (
"net/http"
2024-12-06 18:44:32 +00:00
"github.com/gofiber/fiber/v2"
2024-12-05 17:44:29 +00:00
)
2024-12-06 15:38:22 +00:00
type IError interface {
Error() string
}
type ValidateError struct {
Message string `json:"message"`
}
// Body 为 HTTP 响应
2024-12-05 17:44:29 +00:00
type Body struct {
Message string `json:"message"`
Error string `json:"error"`
2024-12-10 10:22:14 +00:00
Success bool `json:"success"`
Data any `json:"data,omitempty"`
Wrap bool `json:"-"`
2024-12-05 17:44:29 +00:00
}
2024-12-06 18:44:32 +00:00
type Response struct {
2024-12-05 17:44:29 +00:00
body *Body
httpStatus int
ctx *fiber.Ctx
}
2024-12-06 18:44:32 +00:00
func Ctx(c *fiber.Ctx) *Response {
return &Response{
2024-12-05 17:44:29 +00:00
body: &Body{
2024-12-10 10:22:14 +00:00
Wrap: false,
2024-12-05 17:44:29 +00:00
},
ctx: c,
httpStatus: 0,
}
}
2024-12-06 18:44:32 +00:00
func (r *Response) Message(message string) *Response {
2024-12-05 17:44:29 +00:00
r.body.Message = message
if r.httpStatus == 0 {
r.httpStatus = http.StatusOK
}
return r
}
2024-12-10 10:22:14 +00:00
// Wrap 将在响应中包裹 data
2024-12-06 18:44:32 +00:00
func (r *Response) Wrap() *Response {
2024-12-05 17:44:29 +00:00
r.body.Wrap = true
return r
}
2024-12-06 18:44:32 +00:00
func (r *Response) Data(data any) *Response {
2024-12-05 17:44:29 +00:00
r.body.Data = data
return r
}
2024-12-06 18:44:32 +00:00
func (r *Response) Error(err IError) *Response {
2024-12-05 17:44:29 +00:00
if err != nil {
2024-12-06 15:38:22 +00:00
r.body.Error = err.Error()
2024-12-05 17:44:29 +00:00
if r.httpStatus == 0 {
r.httpStatus = http.StatusBadRequest
}
if r.body.Message == "" {
r.Message("Something went wrong")
}
r.body.Success = false
}
return r
}
2024-12-06 18:44:32 +00:00
func (r *Response) Status(status int) *Response {
2024-12-05 17:44:29 +00:00
r.httpStatus = status
return r
}
2024-12-06 18:44:32 +00:00
func (r *Response) Send() error {
2024-12-05 17:44:29 +00:00
if r.httpStatus == 0 {
r.httpStatus = http.StatusOK
}
// if 20x or 20x, set success
r.body.Success = r.httpStatus >= http.StatusOK && r.httpStatus < http.StatusMultipleChoices
// if 403 or 401 but not have message
if r.httpStatus == http.StatusForbidden || r.httpStatus == http.StatusUnauthorized {
if r.body.Message == "" {
r.Message("Unauthorized")
}
}
2024-12-10 10:22:14 +00:00
// if 400 bad request but not have message
if r.httpStatus == http.StatusBadRequest {
if r.body.Message == "" {
r.Message("Bad request")
}
}
var rspCtx = r.ctx.Status(r.httpStatus)
var rspErr error
if r.body.Data == nil {
return rspCtx.Send([]byte{})
}
2024-12-05 17:44:29 +00:00
if r.body.Wrap {
2024-12-10 10:22:14 +00:00
rspErr = rspCtx.JSON(r.body)
2024-12-05 17:44:29 +00:00
}
2024-12-10 10:22:14 +00:00
rspErr = rspCtx.JSON(r.body.Data)
return rspErr
2024-12-05 17:44:29 +00:00
}
2024-12-06 18:44:32 +00:00
func (r *Response) Success(data any) *Response {
r.body.Data = data
r.body.Success = true
if r.httpStatus == 0 {
r.httpStatus = http.StatusOK
}
return r
}
//func (r *Response) ValidationError(validationErrors *[]ValidateError) *Response {
2024-12-06 15:38:22 +00:00
// if validationErrors == nil || len(*validationErrors) == 0 {
// }
//
// r.body.ValidationErrors = validationErrors
//
// r.Error(errs.ErrBadRequest)
//
// return r
//}
2024-12-05 17:44:29 +00:00
//
//func ResponseMessage(c *gin.Context, code int, message string, data interface{}) {
// c.JSON(code, &Body{
// Message: message,
// Data: data,
// })
// c.Abort()
//}
//
//func ResponseError(c *gin.Context, code int, err error) {
// c.JSON(code, &Body{
// Error: err.Error(),
// })
// c.Abort()
//}
//
//func Response(c *gin.Context, code int, data interface{}) {
// c.JSON(code, &Body{
// Data: data,
// })
// c.Abort()
//}