Documentation
¶
Index ¶
- func NewClient(config Client) func(adapter adapter.Adapter) *Reply
- type Client
- type ErrorPayload
- type Meta
- type OptErrorPayload
- type Pagination
- type Reply
- func (r *Reply) CreatedBinary()
- func (r *Reply) CreatedHTML()
- func (r *Reply) CreatedJSON()
- func (r *Reply) CreatedStream()
- func (r *Reply) CreatedText()
- func (r *Reply) CreatedXML()
- func (r *Reply) Error(code, message string, optional ...OptErrorPayload) *Reply
- func (r *Reply) FailJSON(code ...int)
- func (r *Reply) FailXML(code ...int)
- func (r *Reply) NoContent()
- func (r *Reply) OkBinary()
- func (r *Reply) OkHTML()
- func (r *Reply) OkJSON()
- func (r *Reply) OkStream()
- func (r *Reply) OkText()
- func (r *Reply) OkXML()
- func (r *Reply) Paginate(limit, offset, total int) *Reply
- func (r *Reply) ReplyBinary(code int)
- func (r *Reply) ReplyHTML(code int)
- func (r *Reply) ReplyJSON(code int)
- func (r *Reply) ReplyStream(code int)
- func (r *Reply) ReplyText(code int)
- func (r *Reply) ReplyXML(code int)
- func (r *Reply) Success(data any) *Reply
- type ReplyEnvelope
- type Stream
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewClient ¶
Create a new client with given configuration.
Example:
var Client = reply.NewClient(reply.Client{
Finalizer: finalizer,
Transformer: transformer,
CodeAliases map[string]int{
"SERVER_ERROR": 500,
"BAD_REQUEST": 400,
},
DefaultHeaders map[string]string{
"Content-Type": "application/json",
},
})
Types ¶
type Client ¶
type Client struct {
Finalizer func(data any, meta Meta) // Runs before sending
Transformer func(data any, meta Meta) any // Transforms payload
CodeAliases map[string]int // Maps error codes to HTTP status
DefaultHeaders map[string]string // Default response headers
}
Client holds global config for Reply instances.
type ErrorPayload ¶
type ErrorPayload struct {
Code string `json:"code" xml:"code"` // Machine-readable error code
Message string `json:"message" xml:"message"` // Human-readable message
Details string `json:"details,omitempty" xml:"details,omitempty"` // Optional debug details
Field string `json:"field,omitempty" xml:"field,omitempty"` // Field causing the error (if any)
}
ErrorPayload defines the error response body.
Example:
ErrorPayload{Code: "NOT_FOUND", Message: "User not found"}
type Meta ¶
type Meta struct {
Status string `json:"status" xml:"status"` // "SUCCESS" or "ERROR"
Info string `json:"information,omitempty" xml:"information,omitempty"` // Optional info message
Pagination *Pagination `json:"pagination,omitempty" xml:"pagination,omitempty"` // Pagination info if applicable
}
Meta contains reply metadata.
Example:
Meta{Status: "SUCCESS", Info: "Fetched 10 items"}
type OptErrorPayload ¶
type OptErrorPayload struct {
Details string `json:"details,omitempty" xml:"details,omitempty"` // Optional debug details
Field string `json:"field,omitempty" xml:"field,omitempty"` // Field causing the error (if any)
}
OptErrorPayload holds optional error fields for partial errors.
Example:
OptErrorPayload{Details: "Invalid email format", Field: "email"}
type Pagination ¶
type Pagination struct {
NextOffset int `json:"nextOffset,omitempty" xml:"nextOffset,omitempty"` // Offset for the next page
HasNext bool `json:"hasNext,omitempty" xml:"hasNext,omitempty"` // True if more results exist
}
Pagination holds pagination metadata, embedded in Meta when needed.
Example:
Pagination{NextOffset: 20, HasNext: true}
type Reply ¶
type Reply struct {
*ReplyEnvelope
Payload any // Internal transformed payload
// contains filtered or unexported fields
}
Reply is the main HTTP response helper with chained methods.
func (*Reply) CreatedBinary ¶
func (r *Reply) CreatedBinary()
CreatedBinary is a shortcut for ReplyBinary with http.StatusCreated (201).
func (*Reply) CreatedHTML ¶
func (r *Reply) CreatedHTML()
CreatedHTML sends status 201 Created with HTML body.
func (*Reply) CreatedJSON ¶
func (r *Reply) CreatedJSON()
CreatedJSON sends status 201 Created with JSON body.
func (*Reply) CreatedStream ¶
func (r *Reply) CreatedStream()
CreatedStream sends status 201 Created with stream body.
func (*Reply) CreatedText ¶
func (r *Reply) CreatedText()
CreatedText sends status 201 Created with plain text body.
func (*Reply) CreatedXML ¶
func (r *Reply) CreatedXML()
CreatedXML sends status 201 Created with XML body.
func (*Reply) Error ¶
func (r *Reply) Error(code, message string, optional ...OptErrorPayload) *Reply
Error sets reply status to "ERROR" and attaches an error payload.
func (*Reply) FailJSON ¶
FailJSON sends a JSON response with an error status. If code is provided, use it; otherwise, retrieve from CodeAliases or default to 500.
func (*Reply) FailXML ¶
FailXML sends a XML response with an error status. If code is provided, use it; otherwise, retrieve from CodeAliases or default to 500.
func (*Reply) OkBinary ¶
func (r *Reply) OkBinary()
OkBinary is a shortcut for ReplyBinary with http.StatusOK (200).
func (*Reply) OkHTML ¶
func (r *Reply) OkHTML()
OkHTML is a shortcut for ReplyHTML with status 200 OK.
func (*Reply) OkJSON ¶
func (r *Reply) OkJSON()
OkJSON is a shortcut for ReplyJSON with status 200 OK.
func (*Reply) OkStream ¶
func (r *Reply) OkStream()
OkStream is a shortcut for ReplyStream with status 200 OK.
func (*Reply) OkText ¶
func (r *Reply) OkText()
OkText is a shortcut for ReplyText with status 200 OK.
func (*Reply) Paginate ¶ added in v1.0.0
Create pagianate information in meta data.
Example:
rp.Success(datas).Paginate(limit, offset, len(datas)).OkJSON()
func (*Reply) ReplyBinary ¶
ReplyBinary sends a binary response using the adapter's BinarySender. If Data is not []byte, it logs an error and sends an empty body.
func (*Reply) ReplyHTML ¶
ReplyHTML sends an HTML string response with the specified status code. The Data in *Reply must be a string; if not, it will be treated as an empty string and an error will be logged. The string is automatically escaped before sending.
Example:
rp.Success("<p>Hello & welcome!</p>").ReplyHTML(http.StatusOK) // -> <p>Hello & welcome!</p>
func (*Reply) ReplyJSON ¶
ReplyJSON sends a JSON-formatted response with the specified status code. The Payload in *Reply will be automatically encoded.
Example:
rp.Success(Data{Msg: "ok"}).ReplyJSON(http.StatusOK) // -> {"msg":"ok"}
func (*Reply) ReplyStream ¶
ReplyStream sends a streaming response with the specified status code. Data must be of type Stream; otherwise, logs an error and sends an empty stream.
Example:
rp.Success(reply.Stream{Data: file, ContentType: "image/png"}).
ReplyStream(http.StatusOK) // streams the file as PNG
func (*Reply) ReplyText ¶
ReplyText sends a plain text response with the specified status code. Data must be a string; if not, logs an error and sends empty string.
Example:
rp.Success("Hello world!").ReplyText(http.StatusOK) // -> Hello world!
type ReplyEnvelope ¶
type ReplyEnvelope struct {
Meta Meta `json:"meta" xml:"meta"` // Metadata section
Data any `json:"data" xml:"data"` // Payload data
}
ReplyEnvelope is the standard API response envelope.
Example:
ReplyEnvelope{Meta: Meta{Status: "SUCCESS"}, Data: user}