reply

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(config Client) func(adapter adapter.Adapter) *Reply

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.

func (*Client) New

func (c *Client) New(adapter adapter.Adapter) *Reply

Create a new reply.

Example:

rp := replylib.Client(nethttpadapter.Adapt(w))
// ...
rp.Success(datas).Paginate(limit, offset, len(datas)).OkJSON()

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

func (r *Reply) FailJSON(code ...int)

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

func (r *Reply) FailXML(code ...int)

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) NoContentBinary added in v1.0.0

func (r *Reply) NoContentBinary()

NoContentBinary is a shortcut for ReplyBinary with http.StatusNoContent (204). Typically used when there's no body to send.

func (*Reply) NoContentHTML added in v1.0.0

func (r *Reply) NoContentHTML()

NoContentHTML sends status 204 No Content (no body).

func (*Reply) NoContentJSON added in v1.0.0

func (r *Reply) NoContentJSON()

NoContentJSON sends status 204 No Content (no body).

func (*Reply) NoContentStream added in v1.0.0

func (r *Reply) NoContentStream()

NoContentStream sends status 204 No Content (no body).

func (*Reply) NoContentText added in v1.0.0

func (r *Reply) NoContentText()

NoContentText sends status 204 No Content (no body).

func (*Reply) NoContentXML added in v1.0.0

func (r *Reply) NoContentXML()

NoContentXML sends status 204 No Content (no body).

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) OkXML

func (r *Reply) OkXML()

OkXML is a shortcut for ReplyXML with status 200 OK.

func (*Reply) Paginate added in v1.0.0

func (r *Reply) Paginate(limit, offset, total int) *Reply

Create pagianate information in meta data.

Example:

rp.Success(datas).Paginate(limit, offset, len(datas)).OkJSON()

func (*Reply) ReplyBinary

func (r *Reply) ReplyBinary(code int)

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

func (r *Reply) ReplyHTML(code int)

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 &amp; welcome!</p>

func (*Reply) ReplyJSON

func (r *Reply) ReplyJSON(code int)

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

func (r *Reply) ReplyStream(code int)

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

func (r *Reply) ReplyText(code int)

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!

func (*Reply) ReplyXML

func (r *Reply) ReplyXML(code int)

ReplyXML sends an XML-formatted response with the specified status code. Payload will be marshaled to XML automatically.

Example:

rp.Success(User{ID: 1, Name: "Chesta"}).ReplyXML(http.StatusOK)
// -> <User><ID>1</ID><Name>Chesta</Name></User>

func (*Reply) Success

func (r *Reply) Success(data any) *Reply

Success marks the reply as successful and attaches data.

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}

type Stream

type Stream struct {
	Data        io.Reader // Stream source
	ContentType string    // MIME type of the stream
}

Stream enables streaming responses (files, SSE, etc.).

Example:

Stream{Data: fileReader, ContentType: "video/mp4"}

Directories

Path Synopsis
Package adapter provides a unified interface for sending HTTP responses across different web frameworks (Gin, Echo, Fiber) and standard net/http.
Package adapter provides a unified interface for sending HTTP responses across different web frameworks (Gin, Echo, Fiber) and standard net/http.

Jump to

Keyboard shortcuts

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