Skip to content

Commit

Permalink
adding metadata in response
Browse files Browse the repository at this point in the history
  • Loading branch information
coolwednesday committed Dec 20, 2024
1 parent 90de6c2 commit 0cf0eb0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
2 changes: 0 additions & 2 deletions pkg/gofr/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Handle custom headers if 'result' is a 'Response'.
if resp, ok := result.(response.Response); ok {
resp.SetCustomHeaders(w)

result = resp.Data
}

// Handler function completed
Expand Down
12 changes: 9 additions & 3 deletions pkg/gofr/http/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func (r Responder) Respond(data interface{}, err error) {
data = nil
}

resp = response{Data: data, Error: errorObj}
res, ok := data.(resTypes.Response)
if !ok {
resp = response{Data: data, Error: errorObj}
} else {
resp = res
}
}

r.w.Header().Set("Content-Type", "application/json")
Expand Down Expand Up @@ -91,8 +96,9 @@ func createErrorResponse(err error) map[string]interface{} {

// response represents an HTTP response.
type response struct {
Error interface{} `json:"error,omitempty"`
Data interface{} `json:"data,omitempty"`
Error interface{} `json:"error,omitempty"`
Metadata interface{} `json:"metadata,omitempty"`
Data interface{} `json:"data,omitempty"`
}

type statusCodeResponder interface {
Expand Down
5 changes: 3 additions & 2 deletions pkg/gofr/http/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

type Response struct {
Data any `json:"data"`
Headers map[string]string `json:"-"`
Data any `json:"data"`
Metadata map[string]any `json:"metadata,omitempty"`
Headers map[string]string `json:"-"`
}

func (resp Response) SetCustomHeaders(w http.ResponseWriter) {
Expand Down

0 comments on commit 0cf0eb0

Please sign in to comment.