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 SuccessResponse[T any](item T) ApiResponse[T]
- func SuccessResponseWithMessage[T any](item T, message string) ApiResponse[T]
- type MyUser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ApiResponse ¶ added in v0.4.6
type ApiResponse[T any] struct { Success bool `json:"success"` // Indicates whether the API call was successful Item *T `json:"item,omitempty"` // Contains a single object for successful responses Items []T `json:"items,omitempty"` // Contains an array of objects for successful list responses Message string `json:"message,omitempty"` // Optional message for additional context Error string `json:"error,omitempty"` // 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:
- Item field contains a single object
- Items field contains an array of objects
For error responses:
- Error field contains the error message
The Message field is optional and can provide additional context when needed.
func ErrorResponse ¶ added in v0.4.6
func ErrorResponse(errorMessage, Message string) ApiResponse[any]
ErrorResponse creates an error API response with a structured error. When an error occurs, T cannot be a specific type, so we use 'any'. It returns an ApiResponse with success=false and the error message.
func SimpleErrorResponse ¶ added in v0.4.6
func SimpleErrorResponse(errorMessage string) ApiResponse[any]
SimpleErrorResponse creates an error API response from a simple error message. This is a convenience function for quick error responses. It returns an ApiResponse with success=false and the error message.
func SuccessListResponse ¶ added in v0.4.6
func SuccessListResponse[T any](items []T) ApiResponse[T]
SuccessListResponse creates a successful API response for a list of objects. It returns an ApiResponse with success=true and the provided items array.
func SuccessListResponseWithMessage ¶ added in v0.4.6
func SuccessListResponseWithMessage[T any](items []T, message string) ApiResponse[T]
SuccessListResponseWithMessage creates a successful list API response with a custom message. The message field can provide additional context about the successful operation.
func SuccessResponse ¶ added in v0.4.6
func SuccessResponse[T any](item T) ApiResponse[T]
SuccessResponse creates a successful API response for a single object. It returns an ApiResponse with success=true and the provided item.
func SuccessResponseWithMessage ¶ added in v0.4.6
func SuccessResponseWithMessage[T any](item T, message string) ApiResponse[T]
SuccessResponseWithMessage creates a successful API response with a custom message. The message field can provide additional context about the successful operation.