Skip to content

Commit ec6cc80

Browse files
committed
add E function for empty handler
1 parent 25a3a72 commit ec6cc80

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

handler.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ func R[Request any](h TypedHandlerFunc[Request, httpx.ResponseRender]) HandlerFu
6767
return Render(h)
6868
}
6969

70+
// E is a convenience function for creating a handler that doesn't require any request data.
71+
// It takes a function that accepts a context and returns a response of type Response.
72+
func E[Response any](h func(ctx context.Context) (Response, error)) TypedHandlerFunc[httpx.Empty, Response] {
73+
return func(ctx context.Context, req httpx.Empty) (Response, error) {
74+
return h(ctx)
75+
}
76+
}
77+
7078
// TypedHandlerFunc is a generic handler function that processes requests of type Request
7179
// and returns responses of type Response. It operates within a context and may return an error.
7280
type TypedHandlerFunc[Request, Response any] func(context.Context, Request) (Response, error)

0 commit comments

Comments
 (0)