Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#507] Add Abort method for Response #129

Merged
merged 4 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (r *ContextResponse) Cookie(cookie contractshttp.Cookie) contractshttp.Cont
return r
}

func (r *ContextResponse) Data(code int, contentType string, data []byte) contractshttp.Response {
func (r *ContextResponse) Data(code int, contentType string, data []byte) contractshttp.AbortableResponse {
return &DataResponse{code, contentType, data, r.instance}
}

Expand All @@ -64,11 +64,11 @@ func (r *ContextResponse) Header(key, value string) contractshttp.ContextRespons
return r
}

func (r *ContextResponse) Json(code int, obj any) contractshttp.Response {
func (r *ContextResponse) Json(code int, obj any) contractshttp.AbortableResponse {
return &JsonResponse{code, obj, r.instance}
}

func (r *ContextResponse) NoContent(code ...int) contractshttp.Response {
func (r *ContextResponse) NoContent(code ...int) contractshttp.AbortableResponse {
if len(code) == 0 {
code = append(code, http.StatusNoContent)
}
Expand All @@ -80,11 +80,11 @@ func (r *ContextResponse) Origin() contractshttp.ResponseOrigin {
return r.origin
}

func (r *ContextResponse) Redirect(code int, location string) contractshttp.Response {
func (r *ContextResponse) Redirect(code int, location string) contractshttp.AbortableResponse {
return &RedirectResponse{code, location, r.instance}
}

func (r *ContextResponse) String(code int, format string, values ...any) contractshttp.Response {
func (r *ContextResponse) String(code int, format string, values ...any) contractshttp.AbortableResponse {
return &StringResponse{code, format, r.instance, values}
}

Expand Down Expand Up @@ -205,15 +205,15 @@ func NewStatus(instance *fiber.Ctx, code int) contractshttp.ResponseStatus {
return &Status{instance, code}
}

func (r *Status) Data(contentType string, data []byte) contractshttp.Response {
func (r *Status) Data(contentType string, data []byte) contractshttp.AbortableResponse {
return &DataResponse{r.status, contentType, data, r.instance}
}

func (r *Status) Json(obj any) contractshttp.Response {
func (r *Status) Json(obj any) contractshttp.AbortableResponse {
return &JsonResponse{r.status, obj, r.instance}
}

func (r *Status) String(format string, values ...any) contractshttp.Response {
func (r *Status) String(format string, values ...any) contractshttp.AbortableResponse {
return &StringResponse{r.status, format, r.instance, values}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/gofiber/template/html/v2 v2.1.2
github.com/gookit/validate v1.5.4
github.com/goravel/framework v1.14.1-0.20241220071314-f839c5572ddd
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866
github.com/savioxavier/termlink v1.4.1
github.com/spf13/cast v1.7.1
github.com/stretchr/testify v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,8 @@ github.com/gookit/validate v1.5.4 h1:nwBo6vULnVUeNFCOde6RKFRbOCKJXVMnWR0ghedacLg
github.com/gookit/validate v1.5.4/go.mod h1:p9sRPfpvYB4vXICBpEPzv8FoAky+XhUOhWQghgmmat4=
github.com/goravel/file-rotatelogs/v2 v2.4.2 h1:g68AzbePXcm0V2CpUMc9j4qVzcDn7+7aoWSjZ51C0m4=
github.com/goravel/file-rotatelogs/v2 v2.4.2/go.mod h1:23VuSW8cBS4ax5cmbV+5AaiLpq25b8UJ96IhbAkdo8I=
github.com/goravel/framework v1.14.1-0.20241220071314-f839c5572ddd h1:+EjxacwKKlrb8zZLsXO7cOoflS3rAfQjSso2+yVXGpI=
github.com/goravel/framework v1.14.1-0.20241220071314-f839c5572ddd/go.mod h1:9saug/7ORKnkeoPXxB7/QH3tVx4c9Z8y7uH4INUQX1E=
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866 h1:Xz7jgacMzIN8mVzORo5xDlorANNnSDt54BhVtQ2ppM8=
github.com/goravel/framework v1.14.1-0.20241229081312-7de6cc0c5866/go.mod h1:Qo5Xlf+slrosyMxBKbNoxpEmzB6y2C5FI4BHNdVOSyI=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
20 changes: 20 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (r *DataResponse) Render() error {
return r.instance.Status(r.code).Send(r.data)
}

func (r *DataResponse) Abort() error {
return r.Render()
}

type DownloadResponse struct {
filename string
filepath string
Expand Down Expand Up @@ -71,6 +75,10 @@ func (r *JsonResponse) Render() error {
return r.instance.Status(r.code).JSON(r.obj)
}

func (r *JsonResponse) Abort() error {
return r.Render()
}

type NoContentResponse struct {
code int
instance *fiber.Ctx
Expand All @@ -84,6 +92,10 @@ func (r *NoContentResponse) Render() error {
return r.instance.Status(r.code).Send(nil)
}

func (r *NoContentResponse) Abort() error {
return r.Render()
}

type RedirectResponse struct {
code int
location string
Expand All @@ -98,6 +110,10 @@ func (r *RedirectResponse) Render() error {
return r.instance.Redirect(r.location, r.code)
}

func (r *RedirectResponse) Abort() error {
return r.Render()
}

type StringResponse struct {
code int
format string
Expand All @@ -118,6 +134,10 @@ func (r *StringResponse) Render() error {
return r.instance.Status(r.code).SendString(r.values[0].(string))
}

func (r *StringResponse) Abort() error {
return r.Render()
}

type HtmlResponse struct {
data any
instance *fiber.Ctx
Expand Down
Loading