-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard.go
41 lines (36 loc) · 1.36 KB
/
standard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package stdresp
import (
"github.com/goravel/framework/contracts/http"
"github.com/spotlibs/go-lib/stderr"
)
// Std holds standard response structure for brispot microservice.
type Std struct {
httpCode int // httpCode hold http code that shall be controlled by stderr
ResponseCode string `json:"responseCode"`
ResponseDesc string `json:"responseDesc"`
ResponseData any `json:"responseData,omitempty"`
ResponseValidation []string `json:"responseValidation,omitempty"`
}
// Resp construct standard response after applying the response code and description
// along with any given options.
func Resp(c http.Context, code, desc string, opts ...StdOpt) http.Response {
res := Std{
httpCode: http.StatusOK,
ResponseCode: code,
ResponseDesc: desc,
}
for _, opt := range opts {
opt(&res)
}
return c.Response().Json(res.httpCode, res)
}
// Success construct standard response with default success response code and
// description after applying any given options.
func Success(c http.Context, opts ...StdOpt) http.Response {
return Resp(c, "00", "Success", opts...)
}
// Error construct standard response with default error response code and
// description after applying any given options.
func Error(c http.Context, opts ...StdOpt) http.Response {
return Resp(c, stderr.ERROR_CODE_SYSTEM, stderr.ERROR_DESC_SYSTEM, opts...)
}