Documentation
¶
Index ¶
- type ApiResponse
- func ErrorResponse(errorMessage, message string) ApiResponse[any]
- func SimpleErrorResponse(errorMessage string) ApiResponse[any]
- func SuccessListResponse[T any](items []T) ApiResponse[[]T]
- func SuccessListResponseWithMessage[T any](items []T, message string) ApiResponse[[]T]
- func SuccessPagedResponse[T any](items []T, totalCount int64, page, size int) ApiResponse[Page[T]]
- func SuccessResponse[T any](data T) ApiResponse[T]
- func SuccessResponseWithMessage[T any](data T, message string) ApiResponse[T]
- type Page
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ApiResponse ¶ added in v0.4.7
type ApiResponse[T any] struct { Success bool `json:"success" example:"true"` // Indicates whether the API call was successful Data T `json:"data,omitempty"` // Contains the actual response data (single object, list, or page) Message string `json:"message,omitempty" example:"Operation successful"` // Optional message for additional context Error string `json:"error,omitempty" example:"Error message if failure"` // Error message for failed responses }
ApiResponse represents a standardized API response structure. It uses generics to support different data types for successful responses.
For successful responses:
- Data field contains the result object, array, or paginated result
For error responses:
- Error field contains the error message
func ErrorResponse ¶ added in v0.4.7
func ErrorResponse(errorMessage, message string) ApiResponse[any]
ErrorResponse creates an error API response with a structured error. T is set to 'any' as there is no data to return.
func SimpleErrorResponse ¶ added in v0.4.7
func SimpleErrorResponse(errorMessage string) ApiResponse[any]
SimpleErrorResponse creates an error API response from a simple error message.
func SuccessListResponse ¶ added in v0.4.7
func SuccessListResponse[T any](items []T) ApiResponse[[]T]
SuccessListResponse is a convenience wrapper for list responses. It explicitly takes a slice and returns ApiResponse[[]T]. This ensures that the Data field is always a JSON array.
func SuccessListResponseWithMessage ¶ added in v0.4.7
func SuccessListResponseWithMessage[T any](items []T, message string) ApiResponse[[]T]
SuccessListResponseWithMessage creates a successful list API response with a custom message.
func SuccessPagedResponse ¶ added in v0.4.7
func SuccessPagedResponse[T any](items []T, totalCount int64, page, size int) ApiResponse[Page[T]]
SuccessPagedResponse creates a successful API response with pagination details. It automatically wraps the items and metadata into a Page struct.
func SuccessResponse ¶ added in v0.4.7
func SuccessResponse[T any](data T) ApiResponse[T]
SuccessResponse creates a successful API response for a single object or any generic data. Usage:
- Single Object: SuccessResponse(user) -> Data: User
- Raw List: SuccessResponse(users) -> Data: []User
func SuccessResponseWithMessage ¶ added in v0.4.7
func SuccessResponseWithMessage[T any](data T, message string) ApiResponse[T]
SuccessResponseWithMessage creates a successful API response with a custom message.
type Page ¶ added in v0.4.7
type Page[T any] struct { Items []T `json:"items"` // List of items in the current page TotalCount int64 `json:"total_count" example:"1"` // Total number of items across all pages Page int `json:"page" example:"1"` // Current page number (1-based index) Size int `json:"size" example:"10"` // Number of items per page HasNext bool `json:"has_next" example:"true"` // Indicates if there is a next page }
Page represents a standardized structure for paginated results. This struct is intended to be used within the ApiResponse.Data field.
type Response ¶
type Response struct {
Success bool `json:"success" example:"true"`
Text string `json:"text" example:"Any text"`
Detail string `json:"details,omitempty" example:"Any details"`
Object map[string]interface{} `json:"object,omitempty"`
List []interface{} `json:"list,omitempty"`
}
Response represents a legacy API response structure. @Description **(To be deprecated)** This structure is currently in use but will be replaced by the generic `ApiResponse[T]` in the future.