Documentation
¶
Index ¶
- type APIKeyLocation
- type Config
- type MediaType
- type OAuthFlow
- type OAuthFlows
- type Operation
- type Parameter
- type RequestBody
- type Response
- type Responses
- type RouteConfig
- type ScalarOptions
- type Schema
- type SecurityRequirement
- type SecurityScheme
- type Server
- type Spec
- func (s *Spec) AddSecurityScheme(name string, scheme SecurityScheme)
- func (s *Spec) JSONHandler() http.HandlerFunc
- func (s *Spec) Route(cfg RouteConfig) kori.Option
- func (s *Spec) ScalarHandler(specURL string, opts ...ScalarOptions) http.HandlerFunc
- func (s *Spec) SetGlobalSecurity(reqs ...SecurityRequirement)
- func (s *Spec) YAMLHandler() http.HandlerFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIKeyLocation ¶ added in v0.3.0
type APIKeyLocation string
APIKeyLocation specifies where an API key is sent (header, query, or cookie).
const ( InHeader APIKeyLocation = "header" InQuery APIKeyLocation = "query" InCookie APIKeyLocation = "cookie" )
type MediaType ¶
type MediaType struct {
Schema *Schema `json:"schema,omitempty"`
Example any `json:"example,omitempty"`
}
MediaType describes the schema and example for a request/response body.
type OAuthFlow ¶ added in v0.3.0
type OAuthFlow struct {
AuthorizationURL string `json:"authorizationUrl,omitempty"`
TokenURL string `json:"tokenUrl,omitempty"`
RefreshURL string `json:"refreshUrl,omitempty"`
Scopes map[string]string `json:"scopes"`
}
OAuthFlow represents a single OAuth 2.0 flow.
type OAuthFlows ¶ added in v0.3.0
type OAuthFlows struct {
Implicit *OAuthFlow `json:"implicit,omitempty"`
Password *OAuthFlow `json:"password,omitempty"`
ClientCredentials *OAuthFlow `json:"clientCredentials,omitempty"`
AuthorizationCode *OAuthFlow `json:"authorizationCode,omitempty"`
}
OAuthFlows holds the four OAuth 2.0 flow types.
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 Responses `json:"responses"`
Security []SecurityRequirement `json:"security,omitempty"`
}
Operation represents an OpenAPI path operation (GET, POST, etc.).
type Parameter ¶
type Parameter struct {
Name string `json:"name"`
In string `json:"in"` // "path", "query", "header"
Required bool `json:"required,omitempty"`
Description string `json:"description,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Schema *Schema `json:"schema,omitempty"`
Example any `json:"example,omitempty"`
}
Parameter represents an OpenAPI parameter (path, query, or header).
type RequestBody ¶
type RequestBody struct {
Required bool `json:"required,omitempty"`
Description string `json:"description,omitempty"`
Content map[string]MediaType `json:"content"`
}
RequestBody represents an OpenAPI request body.
type Response ¶
type Response struct {
Description string `json:"description"`
Content map[string]MediaType `json:"content,omitempty"`
}
Response represents an OpenAPI response.
type RouteConfig ¶
type RouteConfig struct {
OperationID string
Summary string
Description string
Tags []string
Deprecated bool
Params any
Body any
Responses map[int]any
Security []SecurityRequirement
NoSecurity bool
}
RouteConfig configures the OpenAPI operation for a route.
type ScalarOptions ¶
type ScalarOptions struct {
Theme string
DarkMode bool
HideModels bool
HideDownloadButton bool
DefaultOpenAllTags bool
CustomCSS string
}
ScalarOptions customizes the Scalar API reference UI.
type Schema ¶
type Schema struct {
Ref string `json:"$ref,omitempty"`
Type any `json:"type,omitempty"` // string or []string (OpenAPI 3.1 nullable)
Format string `json:"format,omitempty"` // "int32","int64","float","double","email","uuid","uri","date-time"
Description string `json:"description,omitempty"`
Example any `json:"example,omitempty"`
Deprecated bool `json:"deprecated,omitempty"`
Properties map[string]*Schema `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
AdditionalProperties any `json:"additionalProperties,omitempty"`
Items *Schema `json:"items,omitempty"`
MinItems *int `json:"minItems,omitempty"`
MaxItems *int `json:"maxItems,omitempty"`
MinLength *int `json:"minLength,omitempty"`
MaxLength *int `json:"maxLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
Enum []any `json:"enum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
ExclusiveMinimum *float64 `json:"exclusiveMinimum,omitempty"`
ExclusiveMaximum *float64 `json:"exclusiveMaximum,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty"`
}
Schema represents an OpenAPI schema object. Generated automatically from Go types via the Spec builder.
type SecurityRequirement ¶ added in v0.3.0
SecurityRequirement maps scheme names to required scopes. Use Require or RequireScopes to build one.
func Require ¶ added in v0.3.0
func Require(schemes ...string) SecurityRequirement
Require creates a security requirement for the given scheme names (no scopes).
func RequireScopes ¶ added in v0.3.0
func RequireScopes(scheme string, scopes ...string) SecurityRequirement
RequireScopes creates a security requirement with specific OAuth scopes.
type SecurityScheme ¶ added in v0.3.0
type SecurityScheme struct {
Type string `json:"type"`
Description string `json:"description,omitempty"`
Scheme string `json:"scheme,omitempty"`
BearerFormat string `json:"bearerFormat,omitempty"`
In string `json:"in,omitempty"`
Name string `json:"name,omitempty"`
Flows *OAuthFlows `json:"flows,omitempty"`
OpenIDConnectURL string `json:"openIdConnectUrl,omitempty"`
}
SecurityScheme represents an OpenAPI security scheme. Use the constructor functions (BearerAuth, BasicAuth, etc.) to create one.
func APIKeyAuth ¶ added in v0.3.0
func APIKeyAuth(name string, in APIKeyLocation) SecurityScheme
APIKeyAuth creates an API key security scheme.
name := openapi.APIKeyAuth("X-API-Key", openapi.InHeader)
func BasicAuth ¶ added in v0.3.0
func BasicAuth() SecurityScheme
BasicAuth creates an HTTP Basic authentication security scheme.
func BearerAuth ¶ added in v0.3.0
func BearerAuth(bearerFormat ...string) SecurityScheme
BearerAuth creates an HTTP Bearer token security scheme. Optionally specify the bearer format (e.g. "JWT").
func OAuth2 ¶ added in v0.3.0
func OAuth2(flows OAuthFlows) SecurityScheme
OAuth2 creates an OAuth 2.0 security scheme with the given flows.
func OpenIDConnect ¶ added in v0.3.0
func OpenIDConnect(discoveryURL string) SecurityScheme
OpenIDConnect creates an OpenID Connect security scheme.
type Spec ¶
type Spec struct {
// contains filtered or unexported fields
}
Spec builds an OpenAPI 3.1 specification from registered routes and types.
func NewSpec ¶
NewSpec creates a new OpenAPI spec builder.
doc := openapi.NewSpec(openapi.Config{
Title: "My API",
Version: "1.0.0",
})
func (*Spec) AddSecurityScheme ¶ added in v0.3.0
func (s *Spec) AddSecurityScheme(name string, scheme SecurityScheme)
AddSecurityScheme registers a security scheme for use by routes.
func (*Spec) JSONHandler ¶
func (s *Spec) JSONHandler() http.HandlerFunc
JSONHandler returns an HTTP handler that serves the OpenAPI spec as JSON.
func (*Spec) Route ¶
func (s *Spec) Route(cfg RouteConfig) kori.Option
Route returns an Option that registers the route in the OpenAPI spec.
kori.GET(r, "/todos", listTodos, doc.Route(openapi.RouteConfig{
Summary: "List all todos",
Params: &ListParams{},
Responses: map[int]any{200: &TodoList{}},
}))
func (*Spec) ScalarHandler ¶
func (s *Spec) ScalarHandler(specURL string, opts ...ScalarOptions) http.HandlerFunc
ScalarHandler returns an HTTP handler that serves the Scalar API reference UI. specURL is the URL where the spec JSON is served (e.g. "/openapi.json").
func (*Spec) SetGlobalSecurity ¶ added in v0.3.0
func (s *Spec) SetGlobalSecurity(reqs ...SecurityRequirement)
SetGlobalSecurity sets security requirements that apply to all routes. Individual routes can override this via RouteConfig.Security.
func (*Spec) YAMLHandler ¶
func (s *Spec) YAMLHandler() http.HandlerFunc
YAMLHandler returns an HTTP handler that serves the OpenAPI spec as YAML.