From 40ab2c6e61d8fcaddf7daac1cc7919f949c44124 Mon Sep 17 00:00:00 2001 From: jon4hz Date: Tue, 28 Jun 2022 18:55:30 +0200 Subject: feat: add wrapper supporting no request params --- rpc/wrapper.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'rpc/wrapper.go') diff --git a/rpc/wrapper.go b/rpc/wrapper.go index 8aa9556..f16833f 100644 --- a/rpc/wrapper.go +++ b/rpc/wrapper.go @@ -41,4 +41,18 @@ func H[RQ any, RS any](handler func(context.Context, *RQ) (RS, error)) HandlerFu } } +// HS is a simple generic wrapper for rpc handlers without any request params. +func HS[RS any](handler func(context.Context) (RS, error)) HandlerFunc { + return func(ctx context.Context, in json.RawMessage) (json.RawMessage, error) { + resp, err := handler(ctx) + if err != nil { + return nil, Error{ + Code: ErrUser, + Message: err.Error(), + } + } + return json.Marshal(resp) + } +} + type HandlerFunc func(context.Context, json.RawMessage) (json.RawMessage, error) -- cgit v1.2.3