mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
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:
@@ -2,13 +2,13 @@ package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"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/ez-api/foundation/tokenhash"
|
||||
@@ -43,6 +43,7 @@ func TestAdmin_IssueChildKeyForMaster_IssuedByAdminAndSynced(t *testing.T) {
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.Use(middleware.ResponseEnvelope())
|
||||
r.POST("/admin/masters/:id/keys", adminHandler.IssueChildKeyForMaster)
|
||||
|
||||
body := []byte(`{"scopes":"chat:write"}`)
|
||||
@@ -55,8 +56,12 @@ func TestAdmin_IssueChildKeyForMaster_IssuedByAdminAndSynced(t *testing.T) {
|
||||
}
|
||||
|
||||
var resp map[string]any
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
env := decodeEnvelope(t, rr, &resp)
|
||||
if env.Code != "ok" {
|
||||
t.Fatalf("expected code=ok, got %q", env.Code)
|
||||
}
|
||||
if env.Message != "" {
|
||||
t.Fatalf("expected empty message, got %q", env.Message)
|
||||
}
|
||||
if resp["issued_by"] != "admin" {
|
||||
t.Fatalf("expected issued_by=admin, got=%v", resp["issued_by"])
|
||||
|
||||
Reference in New Issue
Block a user