init push
This commit is contained in:
43
internal/transport/http/jsonapi/jsonapi.go
Normal file
43
internal/transport/http/jsonapi/jsonapi.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package jsonapi
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
|
||||
type Resource struct {
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Attributes map[string]any `json:"attributes,omitempty"`
|
||||
}
|
||||
|
||||
type Document struct {
|
||||
Data any `json:"data"`
|
||||
Meta any `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
type ErrorSource struct {
|
||||
Pointer string `json:"pointer,omitempty"`
|
||||
}
|
||||
|
||||
type ErrorObject struct {
|
||||
Status string `json:"status"`
|
||||
Code string `json:"code,omitempty"`
|
||||
ErrorCode string `json:"error_code,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
Source *ErrorSource `json:"source,omitempty"`
|
||||
}
|
||||
|
||||
type ErrorDocument struct {
|
||||
Errors []ErrorObject `json:"errors"`
|
||||
}
|
||||
|
||||
func Write(c *fiber.Ctx, status int, data any) error {
|
||||
return c.Status(status).JSON(Document{Data: data})
|
||||
}
|
||||
|
||||
func WriteWithMeta(c *fiber.Ctx, status int, data any, meta any) error {
|
||||
return c.Status(status).JSON(Document{Data: data, Meta: meta})
|
||||
}
|
||||
|
||||
func WriteErrors(c *fiber.Ctx, status int, errs []ErrorObject) error {
|
||||
return c.Status(status).JSON(ErrorDocument{Errors: errs})
|
||||
}
|
||||
75
internal/transport/http/jsonapi/jsonapi_test.go
Normal file
75
internal/transport/http/jsonapi/jsonapi_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package jsonapi
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
app := fiber.New()
|
||||
app.Get("/write", func(c *fiber.Ctx) error {
|
||||
return Write(c, http.StatusCreated, Resource{Type: "sample", ID: "1", Attributes: map[string]any{"name": "john"}})
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, "/write", nil)
|
||||
resp, err := app.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("app.Test: %v", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusCreated {
|
||||
t.Fatalf("expected 201, got %d", resp.StatusCode)
|
||||
}
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
if !strings.Contains(string(body), `"data"`) || !strings.Contains(string(body), `"type":"sample"`) {
|
||||
t.Fatalf("unexpected body: %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteWithMeta(t *testing.T) {
|
||||
app := fiber.New()
|
||||
app.Get("/write-meta", func(c *fiber.Ctx) error {
|
||||
return WriteWithMeta(c, http.StatusOK, Resource{Type: "sample"}, map[string]any{"total": 1})
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, "/write-meta", nil)
|
||||
resp, err := app.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("app.Test: %v", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", resp.StatusCode)
|
||||
}
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
if !strings.Contains(string(body), `"meta"`) || !strings.Contains(string(body), `"total":1`) {
|
||||
t.Fatalf("unexpected body: %s", string(body))
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteErrors(t *testing.T) {
|
||||
app := fiber.New()
|
||||
app.Get("/write-errors", func(c *fiber.Ctx) error {
|
||||
return WriteErrors(c, http.StatusUnprocessableEntity, []ErrorObject{{
|
||||
Status: "422",
|
||||
Title: "Validation error",
|
||||
Detail: "email is required",
|
||||
Source: &ErrorSource{Pointer: "/data/attributes/email"},
|
||||
}})
|
||||
})
|
||||
|
||||
req, _ := http.NewRequest(http.MethodGet, "/write-errors", nil)
|
||||
resp, err := app.Test(req)
|
||||
if err != nil {
|
||||
t.Fatalf("app.Test: %v", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusUnprocessableEntity {
|
||||
t.Fatalf("expected 422, got %d", resp.StatusCode)
|
||||
}
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
if !strings.Contains(string(body), `"errors"`) || !strings.Contains(string(body), `"email is required"`) {
|
||||
t.Fatalf("unexpected body: %s", string(body))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user