chirpc

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package chirpc provides helper utilities for handling HTTP responses, including writing typed JSON payloads, setting headers, and basic error handling.

Index

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

func GenerateRPCSchema(r *RPCRouter, paths ...string) error

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.

func Route

func Route(r *RPCRouter, path string, fn func(r *RPCRouter), middlewares ...MiddlewareType)

Route creates a sub-route at the specified path, applies middlewares to it, and invokes the callback to populate it.

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

type HttpResponse[T any] struct {
	StatusCode int
	Body       T
	Headers    map[string]string
}

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

type MiddlewareType = func(http.Handler) http.Handler

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

func (r *RPCRouter) GetHttpServer() *http.Server

GetHttpServer returns an *http.Server that uses the underlying chi router as its Handler.

func (*RPCRouter) ListenAndServe

func (r *RPCRouter) ListenAndServe(addr string) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL