package response import ( "encoding/json" "net/http" ) // Envelope wraps API responses in a consistent structure. type Envelope struct { Success bool `json:"success"` Data interface{} `json:"data,omitempty"` Error *ErrorBody `json:"error,omitempty"` Meta *Meta `json:"meta,omitempty"` } // ErrorBody represents an error response. type ErrorBody struct { Code int `json:"code"` Message string `json:"message"` } // Meta holds pagination and other metadata. type Meta struct { Page int `json:"page,omitempty"` Limit int `json:"limit,omitempty"` Total int64 `json:"total,omitempty"` TotalPages int `json:"total_pages,omitempty"` } // JSON sends a success response with the given data. func JSON(w http.ResponseWriter, status int, data interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) json.NewEncoder(w).Encode(Envelope{ Success: true, Data: data, }) } // JSONWithMeta sends a success response with data and pagination metadata. func JSONWithMeta(w http.ResponseWriter, status int, data interface{}, meta *Meta) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) json.NewEncoder(w).Encode(Envelope{ Success: true, Data: data, Meta: meta, }) } // Error sends an error response. func Error(w http.ResponseWriter, status int, message string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) json.NewEncoder(w).Encode(Envelope{ Success: false, Error: &ErrorBody{ Code: status, Message: message, }, }) } // NoContent sends a 204 No Content response. func NoContent(w http.ResponseWriter) { w.WriteHeader(http.StatusNoContent) }