package resp import ( "errors" "strings" "github.com/gofiber/fiber/v3" ) type res struct { Status int `json:"status"` Msg string `json:"msg"` Data any `json:"data"` Err any `json:"err"` } func R200(c fiber.Ctx, data any, msgs ...string) error { r := &res{ Status: 200, Msg: Msg200, Data: data, } if len(msgs) > 0 && msgs[0] != "" { r.Msg = msgs[0] } return c.JSON(r) } func R201(c fiber.Ctx, data any, msgs ...string) error { r := &res{ Status: 201, Msg: Msg201, Data: data, } if len(msgs) > 0 && msgs[0] != "" { r.Msg = msgs[0] } return c.JSON(r) } func R202(c fiber.Ctx, data any, msgs ...string) error { r := &res{ Status: 202, Msg: Msg202, Data: data, } if len(msgs) > 0 && msgs[0] != "" { r.Msg = msgs[0] } return c.JSON(r) } func RC(c fiber.Ctx, status int, args ...any) error { return _r(c, &res{Status: status}, args...) } func RE(c fiber.Ctx, err error) error { var re *Error if errors.As(err, &re) { return RC(c, re.Status, re.Msg, re.Data, re.Err) } estr := strings.ToLower(err.Error()) if strings.Contains(estr, "duplicate") { return R400(c, Msg400Duplicate, nil, estr) } return R500(c, "", nil, err) } func _r(c fiber.Ctx, r *res, args ...any) error { length := len(args) if length == 0 { goto END } if length >= 1 { if msg, ok := args[0].(string); ok { r.Msg = msg } } if length >= 2 { r.Data = args[1] } if length >= 3 { if ee, ok := args[2].(error); ok { r.Err = ee.Error() } else { r.Err = args[2] } } END: if r.Msg == "" { r.Msg = Msg(r.Status) } // todo: i18n r.Msg // r.Msg = t(r.Msg) return c.Status(r.Status).JSON(r) } // R400 // // args[0]: should be msg to display to user(defaulted) // args[1]: could be extra data to send with(no default) // args[2]: could be error msg to send to with debug mode func R400(c fiber.Ctx, args ...any) error { r := &res{ Status: 400, } return _r(c, r, args...) } // R401 // // args[0]: should be msg to display to user(defaulted) // args[1]: could be extra data to send with(no default) // args[2]: could be error msg to send to with debug mode func R401(c fiber.Ctx, args ...any) error { r := &res{ Status: 401, } return _r(c, r, args...) } // R403 // // args[0]: should be msg to display to user(defaulted) // args[1]: could be extra data to send with(no default) // args[2]: could be error msg to send to with debug mode func R403(c fiber.Ctx, args ...any) error { r := &res{ Status: 403, } return _r(c, r, args...) } func R404(c fiber.Ctx, args ...any) error { r := &res{ Status: 404, } return _r(c, r, args...) } // R500 // // args[0]: should be msg to display to user(defaulted) // args[1]: could be extra data to send with(no default) // args[2]: could be error msg to send to with debug mode func R500(c fiber.Ctx, args ...any) error { r := &res{ Status: 500, } return _r(c, r, args...) } // R501 // // args[0]: should be msg to display to user(defaulted) // args[1]: could be extra data to send with(no default) // args[2]: could be error msg to send to with debug mode func R501(c fiber.Ctx, args ...any) error { r := &res{ Status: 501, } return _r(c, r, args...) }