feat(api): wrap JSON responses in envelope

Add response envelope middleware to standardize JSON responses as
`{code,data,message}` with consistent business codes across endpoints.
Update Swagger annotations and tests to reflect the new response shape.

BREAKING CHANGE: API responses are now wrapped in a response envelope; clients must read payloads from `data` and handle `code`/`message` fields.
This commit is contained in:
zenfun
2026-01-10 00:15:08 +08:00
parent f400ffde95
commit 33838b1e2c
40 changed files with 771 additions and 371 deletions

View File

@@ -9,6 +9,7 @@ import (
"testing"
"github.com/alicebob/miniredis/v2"
"github.com/ez-api/ez-api/internal/middleware"
"github.com/ez-api/ez-api/internal/model"
"github.com/ez-api/ez-api/internal/service"
"github.com/gin-gonic/gin"
@@ -40,6 +41,7 @@ func TestLogWebhookConfigCRUD(t *testing.T) {
h, _ := newTestHandlerWithWebhook(t)
r := gin.New()
r.Use(middleware.ResponseEnvelope())
r.GET("/admin/logs/webhook", h.GetLogWebhookConfig)
r.PUT("/admin/logs/webhook", h.UpdateLogWebhookConfig)
@@ -70,8 +72,9 @@ func TestLogWebhookConfigCRUD(t *testing.T) {
}
var got service.LogWebhookConfig
if err := json.Unmarshal(getRR.Body.Bytes(), &got); err != nil {
t.Fatalf("decode response: %v", err)
env := decodeEnvelope(t, getRR, &got)
if env.Code != "ok" {
t.Fatalf("expected code=ok, got %q", env.Code)
}
if !got.Enabled || got.URL == "" || got.Threshold != 3 {
t.Fatalf("unexpected webhook config: %+v", got)
@@ -85,6 +88,7 @@ func TestLogWebhookConfigDisableClears(t *testing.T) {
h, mr := newTestHandlerWithWebhook(t)
r := gin.New()
r.Use(middleware.ResponseEnvelope())
r.PUT("/admin/logs/webhook", h.UpdateLogWebhookConfig)
seed := service.LogWebhookConfig{Enabled: true, URL: "https://example.com"}