-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
81 lines (66 loc) · 2.88 KB
/
response.go
File metadata and controls
81 lines (66 loc) · 2.88 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package hx
import (
"net/http"
"github.com/struct0x/hx/internal/out"
)
// OK creates a successful HTTP response with a 200 (OK) status code.
// It accepts a body of any type and optional response modifiers.
func OK(body any, opts ...ResponseOpt) Response {
return Respond(http.StatusOK, body, opts...)
}
// NoContent creates an HTTP response with a 204 (No Content) status code.
func NoContent() Response {
return Response{StatusCode: http.StatusNoContent}
}
// Created creates an HTTP response with a 201 (Created) status code.
// It accepts a body of any type and optional response modifiers.
func Created(body any, opts ...ResponseOpt) Response {
return Respond(http.StatusCreated, body, opts...)
}
// BadRequest creates an HTTP response with a 400 (Bad Request) status code.
// It accepts a body of any type and optional response modifiers.
func BadRequest(title string, opts ...ProblemOpt) ProblemDetails {
return Problem(http.StatusBadRequest, title, opts...)
}
// Unauthorized creates an HTTP response with a 401 (Unauthorized) status code.
// It accepts a body of any type and optional response modifiers.
func Unauthorized(title string, opts ...ProblemOpt) ProblemDetails {
return Problem(http.StatusUnauthorized, title, opts...)
}
// Forbidden creates an HTTP response with a 403 (Forbidden) status code.
// It accepts a body of any type and optional response modifiers.
func Forbidden(title string, opts ...ProblemOpt) ProblemDetails {
return Problem(http.StatusForbidden, title, opts...)
}
// NotFound creates an HTTP response with a 404 (Not Found) status code.
// It accepts a body of any type and optional response modifiers.
func NotFound(title string, opts ...ProblemOpt) ProblemDetails {
return Problem(http.StatusNotFound, title, opts...)
}
// MethodNotAllowed creates an HTTP response with a 405 (Method Not Allowed) status code.
// It accepts a body of any type and optional response modifiers.
func MethodNotAllowed(title string, opts ...ProblemOpt) ProblemDetails {
return Problem(http.StatusMethodNotAllowed, title, opts...)
}
// Conflict creates an HTTP response with a 409 (Conflict) status code.
// It accepts a body of any type and optional response modifiers.
func Conflict(title string, opts ...ProblemOpt) ProblemDetails {
return Problem(http.StatusConflict, title, opts...)
}
// Internal creates an HTTP response with a 500 (Internal Server Error) status code.
func Internal(title string, opts ...ProblemOpt) ProblemDetails {
return Problem(http.StatusInternalServerError, title, opts...)
}
// Respond creates an HTTP response with the specified status code and body.
// It accepts a status code, a body of any type, and optional response modifiers.
func Respond(status int, body any, opts ...ResponseOpt) Response {
r := out.Response{
ContentType: "application/json",
StatusCode: status,
Body: body,
}
for _, o := range opts {
o.applyResponseOpt(&r)
}
return r
}