Documentation
¶
Index ¶
- Constants
- func BindPathParams[T any](request T, r *http.Request, paramExtractor ParamExtractor, ...) (T, error)
- func ClearValidator()
- func Created[T any](w http.ResponseWriter, data T, location string)
- func DecodeRequestBody[T any](r *http.Request, maxBodyBytes int64) (T, error)
- func GetProblemTypeURL(errorType string) string
- func OK[T any](w http.ResponseWriter, data T)
- func OKWithMeta[T any](w http.ResponseWriter, data T, meta any)
- func ParseRequest[T any](w http.ResponseWriter, r *http.Request, paramExtractor ParamExtractor, ...) (T, error)
- func ProblemResponse(w http.ResponseWriter, problem *ProblemDetails)
- func SendResponse[T any](w http.ResponseWriter, code int, data T, problem *ProblemDetails, meta any)
- func SetValidator(v Validator)
- type BodyDecodeError
- type BodyDecodeErrorKind
- type CursorMeta
- type Meta
- type PageMeta
- type ParamExtractor
- type ParseOptions
- type PathParamError
- type ProblemBuilder
- func (b *ProblemBuilder) Build() *ProblemDetails
- func (b *ProblemBuilder) Detail(detail string) *ProblemBuilder
- func (b *ProblemBuilder) Extension(key string, value any) *ProblemBuilder
- func (b *ProblemBuilder) Extensions(values map[string]any) *ProblemBuilder
- func (b *ProblemBuilder) Instance(instance string) *ProblemBuilder
- func (b *ProblemBuilder) Title(title string) *ProblemBuilder
- func (b *ProblemBuilder) Type(problemType string) *ProblemBuilder
- type ProblemConfig
- type ProblemDetails
- type ReplyBuilder
- func (b *ReplyBuilder) Created(w http.ResponseWriter, data any, location string)
- func (b *ReplyBuilder) Header(key, value string) *ReplyBuilder
- func (b *ReplyBuilder) Headers(headers http.Header) *ReplyBuilder
- func (b *ReplyBuilder) Meta(meta any) *ReplyBuilder
- func (b *ReplyBuilder) OK(w http.ResponseWriter, data any)
- func (b *ReplyBuilder) Problem(w http.ResponseWriter, problem *ProblemDetails)
- type RequestParamSetter
- type Response
- type ResponseBuilder
- func (b *ResponseBuilder[T]) Header(key, value string) *ResponseBuilder[T]
- func (b *ResponseBuilder[T]) Headers(headers http.Header) *ResponseBuilder[T]
- func (b *ResponseBuilder[T]) Meta(meta any) *ResponseBuilder[T]
- func (b *ResponseBuilder[T]) Status(code int) *ResponseBuilder[T]
- func (b *ResponseBuilder[T]) Write(w http.ResponseWriter)
- type ValidationErrorDetail
- type Validator
Constants ¶
const BlankURL = "about:blank"
Variables ¶
This section is empty.
Functions ¶
func BindPathParams ¶
func BindPathParams[T any](request T, r *http.Request, paramExtractor ParamExtractor, pathParams ...string) (T, error)
BindPathParams applies extracted path params to a request object without writing HTTP responses.
func ClearValidator ¶
func ClearValidator()
ClearValidator removes the package-level default validator.
func Created ¶
func Created[T any](w http.ResponseWriter, data T, location string)
Created writes a 201 JSON response and optionally sets the Location header.
func DecodeRequestBody ¶
DecodeRequestBody decodes a JSON request body into T without writing HTTP responses.
func GetProblemTypeURL ¶
GetProblemTypeURL returns the default problem type URL for a known error type.
func OK ¶
func OK[T any](w http.ResponseWriter, data T)
OK writes a 200 JSON response without metadata.
func OKWithMeta ¶
func OKWithMeta[T any](w http.ResponseWriter, data T, meta any)
OKWithMeta writes a 200 JSON response with metadata.
func ParseRequest ¶
func ParseRequest[T any](w http.ResponseWriter, r *http.Request, paramExtractor ParamExtractor, opts *ParseOptions, pathParams ...string) (T, error)
ParseRequest parses the incoming HTTP request into a specified struct type, handling JSON decoding, request body limits, path parameter binding, and optional validation. Invalid inputs return regular errors instead of panicking.
func ProblemResponse ¶
func ProblemResponse(w http.ResponseWriter, problem *ProblemDetails)
ProblemResponse writes a problem response using the problem's status.
func SendResponse ¶
func SendResponse[T any](w http.ResponseWriter, code int, data T, problem *ProblemDetails, meta any)
SendResponse sends a JSON response to the client, supporting both success and error scenarios.
func SetValidator ¶
func SetValidator(v Validator)
SetValidator configures the package-level default validator used by ParseRequest.
Types ¶
type BodyDecodeError ¶
type BodyDecodeError struct {
Kind BodyDecodeErrorKind
Err error
Limit int64
}
BodyDecodeError represents a request body parsing error.
func (*BodyDecodeError) Error ¶
func (e *BodyDecodeError) Error() string
func (*BodyDecodeError) Unwrap ¶
func (e *BodyDecodeError) Unwrap() error
type BodyDecodeErrorKind ¶
type BodyDecodeErrorKind string
BodyDecodeErrorKind identifies the decode failure category.
const ( BodyDecodeErrorInvalidJSON BodyDecodeErrorKind = "invalid_json" BodyDecodeErrorBodyTooLarge BodyDecodeErrorKind = "body_too_large" BodyDecodeErrorMultipleDocuments BodyDecodeErrorKind = "multiple_documents" )
type CursorMeta ¶
type CursorMeta struct {
NextCursor string `json:"next_cursor,omitempty"`
PrevCursor string `json:"prev_cursor,omitempty"`
HasNext bool `json:"has_next"`
HasPrev bool `json:"has_prev"`
}
CursorMeta provides cursor-based pagination details.
func NewCursorMeta ¶
func NewCursorMeta(nextCursor, prevCursor string, hasNext, hasPrev bool) *CursorMeta
NewCursorMeta builds cursor-based metadata.
type Meta ¶
type Meta = PageMeta
Meta is kept as a compatibility alias for page-based pagination metadata.
type PageMeta ¶
type PageMeta struct {
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
TotalPages int `json:"total_pages,omitempty"`
TotalItems int `json:"total_items,omitempty"`
}
PageMeta provides page-based pagination details.
func NewPageMeta ¶
NewPageMeta builds page-based metadata and derives total pages when possible.
type ParamExtractor ¶
ParamExtractor extracts a path parameter from a request.
type ParseOptions ¶
type ParseOptions struct {
MaxBodyBytes int64
Problems *ProblemConfig
Validator Validator
SkipValidation bool
}
ParseOptions configures request parsing behavior.
type PathParamError ¶
PathParamError represents a path parameter binding error.
func (*PathParamError) Error ¶
func (e *PathParamError) Error() string
func (*PathParamError) Unwrap ¶
func (e *PathParamError) Unwrap() error
type ProblemBuilder ¶
type ProblemBuilder struct {
// contains filtered or unexported fields
}
ProblemBuilder builds ProblemDetails declaratively.
func Problem ¶
func Problem(status int) *ProblemBuilder
Problem starts a declarative ProblemDetails builder.
func ProblemBadRequest ¶
func ProblemBadRequest(detail string) *ProblemBuilder
ProblemBadRequest returns a bad request problem builder.
func ProblemNotFound ¶
func ProblemNotFound(detail string) *ProblemBuilder
ProblemNotFound returns a not found problem builder.
func (*ProblemBuilder) Build ¶
func (b *ProblemBuilder) Build() *ProblemDetails
Build returns the configured ProblemDetails.
func (*ProblemBuilder) Detail ¶
func (b *ProblemBuilder) Detail(detail string) *ProblemBuilder
Detail sets the problem detail.
func (*ProblemBuilder) Extension ¶
func (b *ProblemBuilder) Extension(key string, value any) *ProblemBuilder
Extension sets a single problem extension.
func (*ProblemBuilder) Extensions ¶
func (b *ProblemBuilder) Extensions(values map[string]any) *ProblemBuilder
Extensions merges multiple problem extensions.
func (*ProblemBuilder) Instance ¶
func (b *ProblemBuilder) Instance(instance string) *ProblemBuilder
Instance sets the problem instance.
func (*ProblemBuilder) Title ¶
func (b *ProblemBuilder) Title(title string) *ProblemBuilder
Title sets the problem title.
func (*ProblemBuilder) Type ¶
func (b *ProblemBuilder) Type(problemType string) *ProblemBuilder
Type sets the problem type URL.
type ProblemConfig ¶
ProblemConfig controls how problem type URLs are generated.
func DefaultProblemConfig ¶
func DefaultProblemConfig() ProblemConfig
DefaultProblemConfig returns a copy of the package default config.
func NewProblemConfig ¶
func NewProblemConfig() ProblemConfig
NewProblemConfig returns a config preloaded with the default problem type paths.
func (ProblemConfig) Clone ¶
func (c ProblemConfig) Clone() ProblemConfig
Clone returns a deep copy of the config.
func (ProblemConfig) TypeURL ¶
func (c ProblemConfig) TypeURL(errorType string) string
TypeURL builds the full type URL for a known error type.
type ProblemDetails ¶
type ProblemDetails struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail,omitempty"`
Instance string `json:"instance,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
ProblemDetails conforms to RFC 9457, providing a standard format for describing errors in HTTP APIs.
func NewBadRequestProblem ¶
func NewBadRequestProblem(detail string) *ProblemDetails
NewBadRequestProblem returns a ready-to-use bad request problem.
func NewNotFoundProblem ¶
func NewNotFoundProblem(detail string) *ProblemDetails
NewNotFoundProblem returns a ready-to-use not found problem.
func NewProblemDetails ¶
func NewProblemDetails(status int, problemType, title, detail string) *ProblemDetails
NewProblemDetails creates a ProblemDetails instance with standard fields.
func ValidateRequest ¶
func ValidateRequest(request any, validator Validator) *ProblemDetails
ValidateRequest applies a validator without writing HTTP responses.
func (ProblemDetails) MarshalJSON ¶
func (p ProblemDetails) MarshalJSON() ([]byte, error)
MarshalJSON serializes RFC 9457 extension members at the top level.
type ReplyBuilder ¶
type ReplyBuilder struct {
// contains filtered or unexported fields
}
ReplyBuilder configures metadata and headers before writing a response.
func (*ReplyBuilder) Created ¶
func (b *ReplyBuilder) Created(w http.ResponseWriter, data any, location string)
Created writes a 201 JSON response using the fluent helper configuration.
func (*ReplyBuilder) Header ¶
func (b *ReplyBuilder) Header(key, value string) *ReplyBuilder
Header sets a single response header for a fluent helper chain.
func (*ReplyBuilder) Headers ¶
func (b *ReplyBuilder) Headers(headers http.Header) *ReplyBuilder
Headers merges multiple response headers for a fluent helper chain.
func (*ReplyBuilder) Meta ¶
func (b *ReplyBuilder) Meta(meta any) *ReplyBuilder
Meta sets response metadata for a fluent helper chain.
func (*ReplyBuilder) OK ¶
func (b *ReplyBuilder) OK(w http.ResponseWriter, data any)
OK writes a 200 JSON response using the fluent helper configuration.
func (*ReplyBuilder) Problem ¶
func (b *ReplyBuilder) Problem(w http.ResponseWriter, problem *ProblemDetails)
Problem writes a problem response using the fluent helper configuration.
type RequestParamSetter ¶
RequestParamSetter defines custom path parameter binding for request structs.
type Response ¶
Response represents the structure of an HTTP response, including an optional body and metadata.
type ResponseBuilder ¶
type ResponseBuilder[T any] struct { // contains filtered or unexported fields }
ResponseBuilder builds and writes HTTP responses declaratively.
func Respond ¶
func Respond[T any](data T) *ResponseBuilder[T]
Respond starts a success response builder.
func RespondProblem ¶
func RespondProblem(problem *ProblemDetails) *ResponseBuilder[any]
RespondProblem starts a problem response builder.
func (*ResponseBuilder[T]) Header ¶
func (b *ResponseBuilder[T]) Header(key, value string) *ResponseBuilder[T]
Header sets a single response header.
func (*ResponseBuilder[T]) Headers ¶
func (b *ResponseBuilder[T]) Headers(headers http.Header) *ResponseBuilder[T]
Headers merges multiple response headers.
func (*ResponseBuilder[T]) Meta ¶
func (b *ResponseBuilder[T]) Meta(meta any) *ResponseBuilder[T]
Meta sets response metadata.
func (*ResponseBuilder[T]) Status ¶
func (b *ResponseBuilder[T]) Status(code int) *ResponseBuilder[T]
Status overrides the response status code.
func (*ResponseBuilder[T]) Write ¶
func (b *ResponseBuilder[T]) Write(w http.ResponseWriter)
Write writes the configured response.
type ValidationErrorDetail ¶
ValidationErrorDetail provides structured details about a single validation error.
type Validator ¶
type Validator interface {
Validate(any) *ProblemDetails
}
Validator validates request payloads without coupling the core package to a validation library.
func DefaultValidator ¶
func DefaultValidator() Validator
DefaultValidator returns the current package-level default validator.