Documentation
¶
Overview ¶
Package apicontract implements the shared HTTP API contract for Workspace apps.
It parses list and search query parameters into typed structs, writes the list envelope and the error body, signs opaque cursors, and describes the same parameters as JSON Schema and OpenAPI so specs and tool schemas match the parser.
Index ¶
- Constants
- func BindQuery(r *http.Request, dst any) error
- func BindValues(values url.Values, dst any) error
- func InputSchema(v any) (map[string]any, error)
- func OpenAPIParameters(v any) ([]map[string]any, error)
- func OpenAPIParametersOf(fields []Field) ([]map[string]any, error)
- func QueryBinding(values url.Values, principal ...string) string
- func WriteError(w http.ResponseWriter, status int, code, message string, fields ...FieldError) error
- func WriteJSON(w http.ResponseWriter, status int, value any) error
- func WriteList[T any](w http.ResponseWriter, items []T, next string) error
- func WriteProblem(w http.ResponseWriter, err error) error
- type Cursors
- type ErrorBody
- type Field
- type FieldError
- type FieldType
- type List
- type Page
- type Problem
- type Range
- type Search
- type SortKey
- type Zone
Constants ¶
const ( CodeMalformedQuery = "malformed_query" CodeInvalidQuery = "invalid_query" CodeInternal = "internal_error" )
Error codes written in ErrorBody.Code.
const ( FieldInvalid = "invalid" FieldRequired = "required" FieldOutOfRange = "out_of_range" FieldTooLong = "too_long" FieldUnknownValue = "unknown_value" FieldConflict = "conflict" FieldInvalidCursor = "invalid_cursor" )
Field error codes written in FieldError.Code.
const ( DefaultLimit = 50 MaxLimit = 200 MaxCursorLength = 2048 )
const Version = "0.1"
Version is the contract version this module implements. An OpenAPI document states the version it meets in info.x-whilesmart-contract.
const VersionExtension = "x-whilesmart-contract"
VersionExtension is the OpenAPI info extension that carries Version.
Variables ¶
This section is empty.
Functions ¶
func BindQuery ¶
BindQuery parses the request's query string into dst, a pointer to a struct whose fields carry query tags. It returns a *Problem with status 400 when a value cannot be parsed and 422 when a parsed value breaks a rule. Any other error means dst is not a valid parameter struct.
func BindValues ¶
BindValues parses values into dst. See BindQuery.
func InputSchema ¶
InputSchema describes the parameters of the struct value v as a JSON Schema object. Use it for tool input schemas so tools accept the same names, formats and bounds as the HTTP route.
func OpenAPIParameters ¶
OpenAPIParameters describes the parameters of the struct value v as OpenAPI 3.1 query parameter objects, for specs that are built in code.
func OpenAPIParametersOf ¶
OpenAPIParametersOf is OpenAPIParameters for a struct described by fields instead of a value.
func QueryBinding ¶
QueryBinding returns a binding for a cursor from the query values that shape the result and from the principal parts, such as user and owner IDs. cursor and limit are left out so a client can change the page size between pages.
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, code, message string, fields ...FieldError) error
WriteError writes an error body with the given status.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, status int, value any) error
WriteJSON writes value as JSON with the given status.
func WriteList ¶
func WriteList[T any](w http.ResponseWriter, items []T, next string) error
WriteList writes a 200 response with the list envelope.
func WriteProblem ¶
func WriteProblem(w http.ResponseWriter, err error) error
WriteProblem writes err as an error body. Errors that are not a *Problem are written as a 500 without exposing their text.
Types ¶
type Cursors ¶
type Cursors struct {
// contains filtered or unexported fields
}
Cursors signs and verifies opaque cursors. A cursor carries a JSON position and a MAC over the position and a binding, so it is rejected when replayed against a different query or principal. Positions are readable by anyone who decodes the cursor; do not put secrets in them.
func NewCursors ¶
NewCursors returns a Cursors signing with key, which must be at least 32 bytes.
type ErrorBody ¶
type ErrorBody struct {
Error string `json:"error"`
Code string `json:"code"`
Fields []FieldError `json:"fields,omitempty"`
}
ErrorBody is the JSON body of every error response. Error stays a plain string so clients that read it as a message keep working.
type Field ¶
type Field struct {
Name string
Exported bool
Anonymous bool
Type FieldType
Tag reflect.StructTag
// Range is true when the field's type is apicontract.Range.
Range bool
// Fields holds the nested fields when Type is FieldStruct.
Fields []Field
}
Field describes one struct field. Static tools that read Go source, such as OpenAPI generators, build Fields from the source types and pass them to OpenAPIParametersOf, so they describe a parameter struct exactly as BindQuery reads it. Fields lists every field of the struct in order.
type FieldError ¶
type List ¶
type List[T any] struct { Items []T `json:"items"` NextCursor string `json:"next_cursor,omitempty"` Total *int `json:"total,omitempty"` }
List is the response envelope for collection routes. NextCursor is empty on the last page. Total is set only when it is exact and cheap to compute.
type Page ¶
type Page struct {
Limit int `` /* 135-byte string literal not displayed */
Cursor string `` /* 141-byte string literal not displayed */
}
Page holds cursor paging parameters. Limit defaults to DefaultLimit and is clamped to MaxLimit.
type Problem ¶
type Problem struct {
Status int
Code string
Message string
Fields []FieldError
}
Problem is an error that knows its HTTP status and body.
type Range ¶
type Range struct {
From time.Time `` /* 132-byte string literal not displayed */
To time.Time `query:"to" bound:"end" doc:"End of the range. A timestamp is exclusive. A YYYY-MM-DD day includes that whole day."`
}
Range is a half-open time range [From, To). A day value (YYYY-MM-DD) in From means the start of that day; in To it means the end of that day. Days are resolved in the request Zone, or UTC when the struct has no Zone.
Embedded without a query tag the parameters are from and to. As a named field with query:"updated" they are updated_from and updated_to.
type Search ¶
type Search struct {
Q string `query:"q" maxLength:"500" doc:"Free-text search."`
}
Search holds the free-text search parameter.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
apicontract
command
Command apicontract checks OpenAPI documents against the API contract.
|
Command apicontract checks OpenAPI documents against the API contract. |
|
Package lint checks an OpenAPI document against the API contract.
|
Package lint checks an OpenAPI document against the API contract. |