Documentation
¶
Overview ¶
Package chirpc provides helper utilities for handling HTTP responses, including writing typed JSON payloads, setting headers, and basic error handling.
Index ¶
- func AddHandler[R any](r IsRPCRouter, method HttpMethods, path string, handler RequestHandler[R], ...) *rpc.BodyQueryParamType
- func AddMiddlewares(r *RPCRouter, middlewares ...MiddlewareType)
- func GenerateRPCSchema(r *RPCRouter, paths ...string) error
- func Group(r *RPCRouter, fn func(r *RPCRouter), middlewares ...MiddlewareType)
- func MethodNotAllowed(r *RPCRouter, fn http.HandlerFunc)
- func Mount(r *RPCRouter, path string, subRouter *RPCSubRouter)
- func NotFound(r *RPCRouter, fn http.HandlerFunc)
- func RegisterErrorHandler[R any](r *RPCRouter, handler ErrorHandlerType[R])
- func RegisterMethod(method string)
- func Route(r *RPCRouter, path string, fn func(r *RPCRouter), ...)
- type ErrorHandlerType
- type ErrorResponse
- type HttpMethods
- type HttpResponse
- type IsRPCRouter
- type MiddlewareType
- type RPCRouter
- type RPCSubRouter
- type RequestHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddHandler ¶
func AddHandler[R any](r IsRPCRouter, method HttpMethods, path string, handler RequestHandler[R], middlewares ...MiddlewareType) *rpc.BodyQueryParamType
AddHandler registers an RPC handler for the given HTTP method and path, applies optional middlewares, records its schema for TypeScript generation, and returns a BodyQueryParamType to allow parameter configuration.
func AddMiddlewares ¶
func AddMiddlewares(r *RPCRouter, middlewares ...MiddlewareType)
AddMiddlewares attaches the provided middlewares to the root router.
func GenerateRPCSchema ¶
GenerateRPCSchema generates TypeScript types for all registered RPC handlers and writes them to a file. By default, it writes to "apiSchema.ts", but a custom path can be provided.
func Group ¶
func Group(r *RPCRouter, fn func(r *RPCRouter), middlewares ...MiddlewareType)
Group creates an anonymous grouped sub-router, applies middlewares, and invokes the callback for registration.
func MethodNotAllowed ¶
func MethodNotAllowed(r *RPCRouter, fn http.HandlerFunc)
MethodNotAllowed sets a custom handler for HTTP 405 Method Not Allowed responses.
func Mount ¶
func Mount(r *RPCRouter, path string, subRouter *RPCSubRouter)
Mount mounts an existing RPCSubRouter at the specified path.
func NotFound ¶
func NotFound(r *RPCRouter, fn http.HandlerFunc)
NotFound sets a custom handler for HTTP 404 Not Found responses.
func RegisterErrorHandler ¶
func RegisterErrorHandler[R any](r *RPCRouter, handler ErrorHandlerType[R])
RegisterErrorHandler sets a global error handler and registers its type information for generation. This handler must be registered before any handlers are registered.
func RegisterMethod ¶
func RegisterMethod(method string)
RegisterMethod registers a custom HTTP method with chi so it can be used in routing.
Types ¶
type ErrorHandlerType ¶
type ErrorHandlerType[T any] = func(*http.Request, *ErrorResponse) *HttpResponse[T]
ErrorHandlerType is a type alias for a function that handles errors and returns an HttpResponse.
type ErrorResponse ¶
type ErrorResponse struct {
StatusCode int `json:"statusCode,omitempty"`
Errors []string `json:"errors,omitempty"`
ValidationErrors map[string][]string `json:"validationErrors,omitempty"`
}
ErrorResponse represents a structured error response with status code, error messages, and optional field-level validation errors.
type HttpMethods ¶
type HttpMethods = string
type alias for http methods used for defining allowed HTTP methods in RPC routes
const ( MethodGet HttpMethods = http.MethodGet MethodPost HttpMethods = http.MethodPost MethodPut HttpMethods = http.MethodPut MethodDelete HttpMethods = http.MethodDelete MethodPatch HttpMethods = http.MethodPatch MethodOptions HttpMethods = http.MethodOptions MethodHead HttpMethods = http.MethodHead MethodTrace HttpMethods = http.MethodTrace MethodConnect HttpMethods = http.MethodConnect )
type HttpResponse ¶
HttpResponse represents an HTTP response with a generic body type. StatusCode is the HTTP status code. Body contains the response payload. Headers is a map of response headers.
type IsRPCRouter ¶
type IsRPCRouter interface {
// contains filtered or unexported methods
}
IsRPCRouter is an interface used to identify types that act as RPC routers within the chirpc package. It provides a single method isRpcRouter for type assertion and internal routing logic.
type MiddlewareType ¶
MiddlewareType is a type alias for a middleware function that wraps an http.Handler.
type RPCRouter ¶
type RPCRouter struct {
// contains filtered or unexported fields
}
RPCRouter provides a thin wrapper around a chi.Mux router and exposes helper methods for registering RPC-style handlers, mounting sub-routers, and producing an http.Server. It centralizes middleware registration and error handler wiring for the chirpc package.
func NewRPCRouter ¶
func NewRPCRouter() *RPCRouter
NewRPCRouter creates a new RPCRouter with a fresh chi.Mux instance and registers a default error handler for TypeScript type generation.
func (*RPCRouter) GetHttpServer ¶
GetHttpServer returns an *http.Server that uses the underlying chi router as its Handler.
func (*RPCRouter) ListenAndServe ¶
ListenAndServe starts an HTTP server on the provided address using the internal router.
type RPCSubRouter ¶
type RPCSubRouter struct {
// contains filtered or unexported fields
}
RPCSubRouter represents a sub-router within the chirpc routing system. It holds a reference to its parent RPCRouter and a slice of HandlerSchema objects that describe the registered RPC routes for TypeScript type generation and internal routing. This type is used for modular route grouping and mounting within the main router.
func NewRPCSubRouter ¶
func NewRPCSubRouter() *RPCSubRouter
NewRPCSubRouter creates a new RPCSubRouter with an empty route collection.
type RequestHandler ¶
type RequestHandler[T any] func(*http.Request) (*HttpResponse[T], *ErrorResponse)
RequestHandler defines a handler function that processes an HTTP request and returns an HttpResponse or error.
func (RequestHandler[T]) ServeHTTPWithErrorHandler ¶
func (rh RequestHandler[T]) ServeHTTPWithErrorHandler(errorHandler ErrorHandlerType[any]) http.HandlerFunc
ServeHTTPWithErrorHandler wraps the RequestHandler with error handling logic. If an error occurs, it uses the provided errorHandler to generate a response. If errorHandler is nil, it returns a 500 Internal Server Error.