Skip to content

Commit

Permalink
🔥 feat: add Req and Res interfaces
Browse files Browse the repository at this point in the history
Split the existing Ctx API into two separate APIs for Requests and
Responses. There are two goals to this change:

1. Reduce cognitive load by making it more obvious whether a Ctx method
   interacts with the request or the response.
2. Increase API parity with Express.
  • Loading branch information
nickajacks1 committed Dec 20, 2024
1 parent ec48579 commit fc00225
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type DefaultCtx struct {
fasthttp *fasthttp.RequestCtx // Reference to *fasthttp.RequestCtx
bind *Bind // Default bind reference
redirect *Redirect // Default redirect reference
req *DefaultReq // Default request api reference
res *DefaultRes // Default response api reference
values [maxParams]string // Route parameter values
viewBindMap sync.Map // Default view map to bind template engine
method string // HTTP method
Expand Down Expand Up @@ -1463,6 +1465,18 @@ func (c *DefaultCtx) renderExtensions(bind any) {
}
}

// Res returns a convenience type whose API is limited to operations
// on the incoming request.
func (c *DefaultCtx) Req() Req {
return c.req

Check warning on line 1471 in ctx.go

View check run for this annotation

Codecov / codecov/patch

ctx.go#L1470-L1471

Added lines #L1470 - L1471 were not covered by tests
}

// Res returns a convenience type whose API is limited to operations
// on the outgoing response.
func (c *DefaultCtx) Res() Res {
return c.res

Check warning on line 1477 in ctx.go

View check run for this annotation

Codecov / codecov/patch

ctx.go#L1476-L1477

Added lines #L1476 - L1477 were not covered by tests
}

// Route returns the matched Route struct.
func (c *DefaultCtx) Route() *Route {
if c.route == nil {
Expand Down
2 changes: 2 additions & 0 deletions ctx_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func NewDefaultCtx(app *App) *DefaultCtx {
return &DefaultCtx{
// Set app reference
app: app,
req: &DefaultReq{},
res: &DefaultRes{},
}
}

Expand Down
6 changes: 6 additions & 0 deletions ctx_interface_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions req.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package fiber

//go:generate ifacemaker --file req.go --struct DefaultReq --iface Req --pkg fiber --output req_interface_gen.go --not-exported true --iface-comment "Req"
type DefaultReq struct {
ctx *DefaultCtx
}

func (r *DefaultReq) Accepts(offers ...string) string {
return r.ctx.Accepts(offers...)

Check warning on line 9 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L8-L9

Added lines #L8 - L9 were not covered by tests
}

func (r *DefaultReq) AcceptsCharsets(offers ...string) string {
return r.ctx.AcceptsCharsets(offers...)

Check warning on line 13 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L12-L13

Added lines #L12 - L13 were not covered by tests
}

func (r *DefaultReq) AcceptsEncodings(offers ...string) string {
return r.ctx.AcceptsEncodings(offers...)

Check warning on line 17 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L16-L17

Added lines #L16 - L17 were not covered by tests
}

func (r *DefaultReq) AcceptsLanguages(offers ...string) string {
return r.ctx.AcceptsLanguages(offers...)

Check warning on line 21 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L20-L21

Added lines #L20 - L21 were not covered by tests
}

func (r *DefaultReq) BaseURL() string {
return r.ctx.BaseURL()

Check warning on line 25 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L24-L25

Added lines #L24 - L25 were not covered by tests
}

func (r *DefaultReq) Body() []byte {
return r.ctx.Body()

Check warning on line 29 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L28-L29

Added lines #L28 - L29 were not covered by tests
}

func (r *DefaultReq) Cookies(key string, defaultValue ...string) string {
return r.ctx.Cookies(key, defaultValue...)

Check warning on line 33 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L32-L33

Added lines #L32 - L33 were not covered by tests
}

func (r *DefaultReq) Fresh() bool {
return r.ctx.Fresh()

Check warning on line 37 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L36-L37

Added lines #L36 - L37 were not covered by tests
}

func (r *DefaultReq) Get(key string, defaultValue ...string) string {
return r.ctx.Get(key, defaultValue...)

Check warning on line 41 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L40-L41

Added lines #L40 - L41 were not covered by tests
}

func (r *DefaultReq) Host() string {
return r.ctx.Host()

Check warning on line 45 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}

func (r *DefaultReq) Hostname() string {
return r.ctx.Hostname()

Check warning on line 49 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L48-L49

Added lines #L48 - L49 were not covered by tests
}

func (r *DefaultReq) IP() string {
return r.ctx.IP()

Check warning on line 53 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L52-L53

Added lines #L52 - L53 were not covered by tests
}

func (r *DefaultReq) Is(extension string) bool {
return r.ctx.Is(extension)

Check warning on line 57 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}

func (r *DefaultReq) IPs() []string {
return r.ctx.IPs()

Check warning on line 61 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}

func (r *DefaultReq) Method() string {
return r.ctx.Method()

Check warning on line 65 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

func (r *DefaultReq) OriginalURL() string {
return r.ctx.OriginalURL()

Check warning on line 69 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}

func (r *DefaultReq) Params(key string, defaultValue ...string) string {
return r.ctx.Params(key, defaultValue...)

Check warning on line 73 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L72-L73

Added lines #L72 - L73 were not covered by tests
}

func (r *DefaultReq) Path() string {
return r.ctx.Path()

Check warning on line 77 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}

func (r *DefaultReq) Protocol() string {
return r.ctx.Protocol()

Check warning on line 81 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L80-L81

Added lines #L80 - L81 were not covered by tests
}

func (r *DefaultReq) Query(key string, defaultValue ...string) string {
return r.ctx.Query(key, defaultValue...)

Check warning on line 85 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L84-L85

Added lines #L84 - L85 were not covered by tests
}

func (r *DefaultReq) Range(size int) (Range, error) {
return r.ctx.Range(size)

Check warning on line 89 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}

func (r *DefaultReq) Route() *Route {
return r.ctx.Route()

Check warning on line 93 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L92-L93

Added lines #L92 - L93 were not covered by tests
}

func (r *DefaultReq) Secure() bool {
return r.ctx.Secure()

Check warning on line 97 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L96-L97

Added lines #L96 - L97 were not covered by tests
}

func (r *DefaultReq) Stale() bool {
return r.ctx.Stale()

Check warning on line 101 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L100-L101

Added lines #L100 - L101 were not covered by tests
}

func (r *DefaultReq) Subdomains(offset ...int) []string {
return r.ctx.Subdomains(offset...)

Check warning on line 105 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L104-L105

Added lines #L104 - L105 were not covered by tests
}

func (r *DefaultReq) XHR() bool {
return r.ctx.XHR()

Check warning on line 109 in req.go

View check run for this annotation

Codecov / codecov/patch

req.go#L108-L109

Added lines #L108 - L109 were not covered by tests
}
33 changes: 33 additions & 0 deletions req_interface_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions res.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fiber

//go:generate ifacemaker --file res.go --struct DefaultRes --iface Res --pkg fiber --output res_interface_gen.go --not-exported true --iface-comment "Res"
type DefaultRes struct {
ctx *DefaultCtx
}

func (r *DefaultRes) Locals(key any, value ...any) any {
return r.ctx.Locals(key, value...)

Check warning on line 9 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L8-L9

Added lines #L8 - L9 were not covered by tests
}

func (r *DefaultRes) Append(field string, values ...string) {
r.ctx.Append(field, values...)

Check warning on line 13 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L12-L13

Added lines #L12 - L13 were not covered by tests
}

func (r *DefaultRes) Attachment(filename ...string) {
r.ctx.Attachment(filename...)

Check warning on line 17 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L16-L17

Added lines #L16 - L17 were not covered by tests
}

func (r *DefaultRes) AutoFormat(body any) error {
return r.ctx.AutoFormat(body)

Check warning on line 21 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L20-L21

Added lines #L20 - L21 were not covered by tests
}

func (r *DefaultRes) Cookie(cookie *Cookie) {
r.ctx.Cookie(cookie)

Check warning on line 25 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L24-L25

Added lines #L24 - L25 were not covered by tests
}

func (r *DefaultRes) ClearCookie(key ...string) {
r.ctx.ClearCookie(key...)

Check warning on line 29 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L28-L29

Added lines #L28 - L29 were not covered by tests
}

func (r *DefaultRes) Download(file string, filename ...string) error {
return r.ctx.Download(file, filename...)

Check warning on line 33 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L32-L33

Added lines #L32 - L33 were not covered by tests
}

func (r *DefaultRes) Format(handlers ...ResFmt) error {
return r.ctx.Format(handlers...)

Check warning on line 37 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L36-L37

Added lines #L36 - L37 were not covered by tests
}

func (r *DefaultRes) Get(key string, defaultValue ...string) string {
return r.ctx.GetRespHeader(key, defaultValue...)

Check warning on line 41 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L40-L41

Added lines #L40 - L41 were not covered by tests
}

func (r *DefaultRes) JSON(body any) error {
return r.ctx.JSON(body)

Check warning on line 45 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}

func (r *DefaultRes) JSONP(data any, callback ...string) error {
return r.ctx.JSONP(data, callback...)

Check warning on line 49 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L48-L49

Added lines #L48 - L49 were not covered by tests
}

func (r *DefaultRes) Links(link ...string) {
r.ctx.Links(link...)

Check warning on line 53 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L52-L53

Added lines #L52 - L53 were not covered by tests
}

func (r *DefaultRes) Location(path string) {
r.ctx.Location(path)

Check warning on line 57 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}

func (r *DefaultRes) Render(name string, bind Map, layouts ...string) error {
return r.ctx.Render(name, bind, layouts...)

Check warning on line 61 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L60-L61

Added lines #L60 - L61 were not covered by tests
}

func (r *DefaultRes) Send(body []byte) error {
return r.ctx.Send(body)

Check warning on line 65 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

func (r *DefaultRes) SendFile(file string, config ...SendFile) error {
return r.ctx.SendFile(file, config...)

Check warning on line 69 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}

func (r *DefaultRes) SendStatus(status int) error {
return r.ctx.SendStatus(status)

Check warning on line 73 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L72-L73

Added lines #L72 - L73 were not covered by tests
}

func (r *DefaultRes) Set(key, val string) {
r.ctx.Set(key, val)

Check warning on line 77 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L76-L77

Added lines #L76 - L77 were not covered by tests
}

func (r *DefaultRes) Status(status int) Res {
r.ctx.Status(status)
return r

Check warning on line 82 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L80-L82

Added lines #L80 - L82 were not covered by tests
}

func (r *DefaultRes) Type(extension string, charset ...string) Res {
r.ctx.Type(extension, charset...)
return r

Check warning on line 87 in res.go

View check run for this annotation

Codecov / codecov/patch

res.go#L85-L87

Added lines #L85 - L87 were not covered by tests
}
27 changes: 27 additions & 0 deletions res_interface_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fc00225

Please sign in to comment.