# README
Response mapper v1
This is a response mapper http server for go-helpers.
Structure Response API
Error
{
"status": "status error",
"code": 10, // code internal error
"message": {
"id": "message error language Indonesian",
"en": "message error language English"
}
}
Success response message
{
"status": "Created", // status success
"message": "data created", // response message
}
Success response Multiple message
{
"status": "Created", // status success
"message": {
"id": "Data berhasil dibuat", // response message indonesian
"en": "Data created successfully" // response message english
}
}
Success Single Data
{
"status": "Created", // status success
"data": {} // response data
}
Success Multiple Data
{
"status": "Success", // status success
"meta": {
"page": 1, // current page
"limit": 10, // current limit per page
"total_records": 3 // total records
},
"data": [] // response data
}
Example Usage
package main
import (
"log"
"net/http"
response_mapper "github.com/adamnasrudin03/go-helpers/response-mapper/v1"
)
func main() {
port := "1200"
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
switch {
case r.URL.Path == "/" && r.Method == "GET":
response_mapper.RenderJSON(w, http.StatusOK, "welcome this server") // success response
default:
err := response_mapper.ErrRouteNotFound()
response_mapper.RenderJSON(w, http.StatusNotFound, err) // error response
}
})
log.Printf("Server is running on port %v\n", port)
err := http.ListenAndServe(":"+port, mux)
if err != nil {
log.Fatal(err)
}
}
# Functions
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
FormatValidationError is a function that formats validation errors during user input validation.
NewError creates a new ResponseError from an error code and error.
NewResponseMultiLang creates a new MultiLanguages instance.
RenderJSON renders the response based on the provided data.
RenderStruct renders the response based on the provided data.
StatusErrorMapping returns the HTTP status code for the given error code.
StatusMapping maps HTTP status code to a descriptive string.
WriteJSON writes the JSON representation of v to the response writer w, and sets the status code of the response to statusCode.
# Constants
13, ErrConflict is used when there is a conflict when saving data.
12, ErrDatabase is used when there is an error when interacting with the database.
10, ErrForbidden is used when the user do not have the permission to access the resource.
14, ErrFromUseCase is used when there is an error from the use case.
16, ErrNoFound is used when the data is not found.
11, ErrUnauthorized is used when the user is not logged in.
17, ErrUnknown is used when the error is unknown.
15, ErrValidation is used when there is an error with the validation.
# Structs
Meta struct holds metadata information.
MultiLanguages represents a structure for multi-language support.
Pagination represents the structure for paginating data.
ResponseDefault represents the default structure for response handling.
ResponseError is used to represent an error response to the client.
ResponseErrorHttp represents the structure of an error response from an HTTP request.
# Type aliases
TypeError is a type of custom error.