Skip to main content

Response Helpers

Dokumentasi struct dan helper functions untuk standardisasi API response.

Response Structs​

Response (Basic)​

Struct dasar untuk semua API response dengan pagination.

type Response struct {
Code int `json:"code"`
Status bool `json:"status"`
Message string `json:"message"`
Info Info `json:"info"`
Data interface{} `json:"data,omitempty"`
}

Contoh Response:

{
"code": 200,
"status": true,
"message": "Data retrieved successfully",
"info": {
"page": 1,
"perPage": 10,
"totalPages": 5,
"totalData": 50
},
"data": [...]
}

Info​

Struct untuk informasi pagination.

type Info struct {
Page int `json:"page,omitempty"`
PerPage int `json:"perPage,omitempty"`
TotalPages int `json:"totalPages,omitempty"`
TotalData int `json:"totalData,omitempty"`
}

ResponseError​

Struct untuk error response tanpa data dan info.

type ResponseError struct {
Code int `json:"code"`
Status bool `json:"status"`
Message string `json:"message"`
}

Contoh:

{
"code": 400,
"status": false,
"message": "Invalid request body"
}

AuthResponse​

Struct khusus untuk response authentication (tanpa info pagination).

type AuthResponse struct {
Code int `json:"code"`
Status bool `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}

Generic Response Structs​

ResponseData[T]​

Typed response dengan Go generics untuk type safety.

type ResponseData[T any] struct {
Code int `json:"code"`
Status bool `json:"status"`
Message string `json:"message"`
Info Info `json:"info"`
Data T `json:"data,omitempty"`
}

Penggunaan:

// Untuk single data
var response ResponseData[models.Pengguna]

// Untuk array data
var response ResponseData[[]models.Pengguna]

ResponseDataCustomInfo[T, I]​

Response dengan custom info structure.

type ResponseDataCustomInfo[T any, I any] struct {
Code int `json:"code"`
Status bool `json:"status"`
Message string `json:"message"`
Info I `json:"info"`
Data T `json:"data,omitempty"`
}

Penggunaan:

type CustomInfo struct {
Page int `json:"page"`
TotalData int `json:"totalData"`
ExtraField string `json:"extraField"`
}

var response ResponseDataCustomInfo[[]User, CustomInfo]

ResponseDataUpsert[T]​

Response untuk operasi create/update tanpa pagination info.

type ResponseDataUpsert[T any] struct {
Code int `json:"code"`
Status bool `json:"status"`
Message string `json:"message"`
Data T `json:"data,omitempty"`
}

Helper Functions​

SendError​

Mengirim error response ke client.

func SendError(ctx *gin.Context, err error)

Penggunaan:

func GetUser(ctx *gin.Context) {
user, err := repository.GetById(id)
if err != nil {
helpers.SendError(ctx, helpers.NewNotFound("User not found"))
return
}
// ...
}

Behavior:

  • Jika error adalah *ResponseError, gunakan code dari error
  • Jika error biasa, return HTTP 500 Internal Server Error

Best Practices​

  1. Gunakan Generic Types untuk type safety

    // ✅ Good
    var response helpers.ResponseData[models.User]

    // ❌ Avoid
    var response helpers.Response
    response.Data = user // interface{} - no type safety
  2. Konsisten dengan Response Format

    • Success: code: 200/201, status: true
    • Error: code: 4xx/5xx, status: false
  3. Selalu Sertakan Message yang Informatif

    // ✅ Good
    Message: "Pengguna dengan NoPegawai 12345 tidak ditemukan"

    // ❌ Avoid
    Message: "Error"