From 277c693dedcffe527ae02e059106ce1d04999870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Benouarets?= Date: Thu, 25 Sep 2025 23:25:07 +0200 Subject: [PATCH] feat: add API response utilities - Add standardized API response structures - Include success and error response helpers - Add request ID tracking for better debugging - Support pagination metadata and links - Provide consistent API response format across all endpoints --- api/response.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 api/response.go diff --git a/api/response.go b/api/response.go new file mode 100644 index 0000000..b33c7e8 --- /dev/null +++ b/api/response.go @@ -0,0 +1,47 @@ +package api + +import "github.com/gofiber/fiber/v2" + +type Response struct { + Data interface{} `json:"data"` + Meta fiber.Map `json:"_meta"` + Links *fiber.Map `json:"_links"` +} + +func Success(c *fiber.Ctx, data interface{}, status int, paginationInformation *fiber.Map, paginationLinks *fiber.Map) error { + requestID := string(c.Response().Header.Peek("X-Request-ID")) + if requestID == "" { + requestID = "unknown" + } + + response := Response{ + Data: data, + Meta: fiber.Map{ + "request_id": requestID, + }, + } + + if paginationInformation != nil { + response.Meta["pagination"] = paginationInformation + } + + if paginationLinks != nil { + response.Links = paginationLinks + } + + return c.Status(status).JSON(response) +} + +func Error(c *fiber.Ctx, message string, status int, data interface{}) error { + requestID := string(c.Response().Header.Peek("X-Request-ID")) + if requestID == "" { + requestID = "unknown" + } + return c.Status(status).JSON(Response{ + Data: data, + Meta: fiber.Map{ + "message": message, + "request_id": requestID, + }, + }) +}