api

package
v0.0.0-...-4eeea1e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RenderType

type RenderType uint8

RenderType defines the response content rendering type.

const (
	// JSON render full JSON response with code, message and data.
	JSON RenderType = iota

	// DataOnlyJSON render only data portion as JSON.
	DataOnlyJSON

	// String render response data as String content.
	String
)

type Response

type Response struct {
	Code    ecodes.Code `json:"code"`
	Message string      `json:"message"`
	Data    any         `json:"data,omitempty"`
	// contains filtered or unexported fields
}

Response represents a standardized API response structure.

func FromError

func FromError(ctx context.Context, err error) Response

FromError constructs a structured Response from an error.

func NewError

func NewError(ctx context.Context, code ecodes.Code, err error) Response

NewError creates an error API response with stack trace.

func NewSuccess

func NewSuccess(ctx context.Context, data any) Response

NewSuccess creates a successful API response.

func (Response) GetCallers

func (r Response) GetCallers() string

GetCallers return the callers of the response.

func (Response) GetContext

func (r Response) GetContext() context.Context

GetContext return the HTTP status code of the response.

func (Response) GetError

func (r Response) GetError() error

GetError return the error of the response.

func (Response) GetHTTPCode

func (r Response) GetHTTPCode() int

GetHTTPCode return the HTTP status code of the response.

func (Response) GetLevel

func (r Response) GetLevel() log.Level

GetLevel return the level of the response.

func (Response) GetRenderType

func (r Response) GetRenderType() RenderType

GetRenderType return the render type of the response.

func (Response) RenderString

func (r Response) RenderString() string

RenderString return the string based on the render type.

func (Response) WithData

func (r Response) WithData(data any) Response

WithData attaches payload data to the response.

func (Response) WithError

func (r Response) WithError(err error) Response

WithError attaches original error to the response.

func (Response) WithHTTPCode

func (r Response) WithHTTPCode(code int) Response

WithHTTPCode overrides default HTTP status code.

func (Response) WithLevel

func (r Response) WithLevel(level log.Level) Response

WithLevel sets logging level for the response.

func (Response) WithMessage

func (r Response) WithMessage(message string) Response

WithMessage sets custom message for the response.

func (Response) WithRenderType

func (r Response) WithRenderType(renderType RenderType) Response

WithRenderType specifies response content rendering format.

func (Response) WrapCallers

func (r Response) WrapCallers(depth ...int) Response

WrapCallers appends current caller information to the stack trace.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL