Documentation
¶
Overview ¶
Package typed registers typed handlers on a chi router and generates an OpenAPI 3.1 document from their input and output types. See the README and examples/users for usage. For non-pointer fields the zero value counts as "not sent" and skips validation rules; use a pointer to enforce rules on zero values or to require presence.
Index ¶
- func Delete[In, Out any](api *API, path string, h Handler[In, Out], opts ...RouteOption)
- func Get[In, Out any](api *API, path string, h Handler[In, Out], opts ...RouteOption)
- func Patch[In, Out any](api *API, path string, h Handler[In, Out], opts ...RouteOption)
- func Post[In, Out any](api *API, path string, h Handler[In, Out], opts ...RouteOption)
- func Put[In, Out any](api *API, path string, h Handler[In, Out], opts ...RouteOption)
- type API
- type Components
- type Config
- type Error
- func BadRequest(detail string) *Error
- func Conflict(detail string) *Error
- func FieldError(location, message string) *Error
- func Forbidden(detail string) *Error
- func InternalServerError(detail string) *Error
- func NewError(status int, detail string) *Error
- func NotFound(detail string) *Error
- func Unauthorized(detail string) *Error
- func UnprocessableEntity(detail string) *Error
- type ErrorDetail
- type Handler
- type Info
- type MediaType
- type OpenAPI
- type Operation
- type Parameter
- type PathItem
- type RequestBody
- type Response
- type RouteOption
- type Schema
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Delete ¶
func Delete[In, Out any](api *API, path string, h Handler[In, Out], opts ...RouteOption)
Delete registers a DELETE route.
func Get ¶
func Get[In, Out any](api *API, path string, h Handler[In, Out], opts ...RouteOption)
Get registers a GET route.
func Patch ¶
func Patch[In, Out any](api *API, path string, h Handler[In, Out], opts ...RouteOption)
Patch registers a PATCH route.
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
API registers typed routes on a chi router and builds the OpenAPI document.
func New ¶
New returns an API that registers its routes on r. It panics when the configuration is invalid.
type Components ¶
Components holds reusable objects of the document.
type Config ¶
type Config struct {
// Title and Version are required and appear in the OpenAPI info object.
Title string
Version string
Description string
// SpecPath serves the OpenAPI document (default "/openapi.json").
// DocsPath serves the documentation UI (default "/docs"). "-" disables
// either.
SpecPath string
DocsPath string
// MaxBodyBytes limits request bodies (default 1 MiB).
MaxBodyBytes int64
// ErrorHandler replaces the default problem+json error writer.
ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
}
Config configures an API.
type Error ¶
type Error struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail,omitempty"`
Errors []ErrorDetail `json:"errors,omitempty"`
}
Error is an RFC 9457 problem details response.
func FieldError ¶
FieldError returns a 422 validation error for a single field.
func InternalServerError ¶
InternalServerError returns a 500 error.
func UnprocessableEntity ¶
UnprocessableEntity returns a 422 error.
type ErrorDetail ¶
type ErrorDetail struct {
Location string `json:"location"`
Message string `json:"message"`
Value any `json:"value,omitempty"`
}
ErrorDetail describes a single problem, such as one invalid field.
type Info ¶
type Info struct {
Title string `json:"title,omitempty"`
Version string `json:"version,omitempty"`
Description string `json:"description,omitempty"`
}
Info describes the API.
type MediaType ¶
type MediaType struct {
Schema *Schema `json:"schema,omitempty"`
}
MediaType holds the schema for one content type.
type OpenAPI ¶
type OpenAPI struct {
OpenAPI string `json:"openapi"`
Info Info `json:"info"`
Paths map[string]*PathItem `json:"paths"`
Components *Components `json:"components,omitempty"`
}
OpenAPI is the root of an OpenAPI 3.1 document.
type Operation ¶
type Operation struct {
OperationID string `json:"operationId,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Tags []string `json:"tags,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Parameters []*Parameter `json:"parameters,omitempty"`
RequestBody *RequestBody `json:"requestBody,omitempty"`
Responses map[string]*Response `json:"responses"`
}
Operation describes one HTTP operation.
type Parameter ¶
type Parameter struct {
Name string `json:"name,omitempty"`
In string `json:"in,omitempty"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
Schema *Schema `json:"schema,omitempty"`
}
Parameter describes a path, query, header or cookie parameter.
type PathItem ¶
type PathItem struct {
Get *Operation `json:"get,omitempty"`
Put *Operation `json:"put,omitempty"`
Post *Operation `json:"post,omitempty"`
Delete *Operation `json:"delete,omitempty"`
Patch *Operation `json:"patch,omitempty"`
}
PathItem groups the operations available on one path.
type RequestBody ¶
type RequestBody struct {
Required bool `json:"required,omitempty"`
Content map[string]*MediaType `json:"content,omitempty"`
}
RequestBody describes the body an operation accepts.
type Response ¶
type Response struct {
Description string `json:"description"`
Content map[string]*MediaType `json:"content,omitempty"`
}
Response describes one response of an operation.
type RouteOption ¶
type RouteOption func(*routeConfig)
RouteOption customizes a route and its operation.
func Description ¶
func Description(s string) RouteOption
Description sets the operation description.
func OperationID ¶
func OperationID(id string) RouteOption
OperationID overrides the generated operation ID.
type Schema ¶
type Schema struct {
Ref string `json:"$ref,omitempty"`
Type any `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Description string `json:"description,omitempty"`
Pattern string `json:"pattern,omitempty"`
ContentEncoding string `json:"contentEncoding,omitempty"`
Items *Schema `json:"items,omitempty"`
AdditionalProperties *Schema `json:"additionalProperties,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
AnyOf []*Schema `json:"anyOf,omitempty"`
Enum []any `json:"enum,omitempty"`
Examples []any `json:"examples,omitempty"`
Default any `json:"default,omitempty"`
MinLength *int `json:"minLength,omitempty"`
MaxLength *int `json:"maxLength,omitempty"`
MinItems *int `json:"minItems,omitempty"`
MaxItems *int `json:"maxItems,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
}
Schema is a JSON Schema (2020-12) object as used by OpenAPI 3.1. Type is a string, or a []string when the value may also be null.