contract

package
v0.71.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package contract renders ContentKit's route catalog (internal/httpapi) and error-code registry as the files other tools read: api/openapi.json, the browser SDK's generated route table, wire types and error codes, and docs/api/routes.md. They are committed: `go generate ./internal/contract` rewrites them and TestGeneratedContractIsFresh fails when one is stale.

Index

Constants

View Source
const (
	OpenAPIFile = "api/openapi.json"
	RoutesDoc   = "docs/api/routes.md"
	SDKDir      = "sdk/ui/src/client/generated/"
)

Where each generated file lives, relative to the repository.

Variables

View Source
var ErrStale = errors.New("generated contract files are stale")

ErrStale is a committed generated file that no longer matches the catalog.

Functions

func Files

func Files(fsys fs.FS) (map[string][]byte, error)

Files renders every generated file by repository path. fsys is the repository: enum values are read from the source that declares them.

func RouteID

func RouteID(s httpapi.Spec) string

RouteID names a route across modules: "module METHOD path".

func Verify

func Verify(fsys fs.FS) error

Verify fails when a generated file in fsys differs from what the catalog renders, and names each one.

func Write

func Write(root string) error

Write renders every generated file into the repository at root.

Types

type Checker

type Checker struct {
	// contains filtered or unexported fields
}

Checker holds real responses to the catalog: tests wrap a handler with it, so every answer of a real request is checked against what its route declares.

func NewChecker

func NewChecker(fsys fs.FS) (*Checker, error)

NewChecker builds a checker over the catalog; fsys is the repository.

func (*Checker) Check

func (c *Checker) Check(method, path string, status int, header http.Header, body []byte) error

Check holds one response to its route's contract: a declared status with its body, or an error body whose code the route may answer with that code's status.

func (*Checker) Served

func (c *Checker) Served() map[string]bool

Served lists the catalog routes a checked response came from, as "module METHOD path".

func (*Checker) Wrap

func (c *Checker) Wrap(strip string, h http.Handler, report func(error)) http.Handler

Wrap serves h and reports each response that breaks its route's contract. path is the request path under the one mount; strip removes the host's mount prefix from it.

type ErrorReply

type ErrorReply struct {
	Error      string              `json:"error"`
	Code       string              `json:"code"`
	RetryAfter int                 `json:"retry_after,omitempty"`
	Action     content.Action      `json:"action,omitempty"`
	Ban        *content.BanNotice  `json:"ban,omitempty"`
	Blobs      []string            `json:"blobs,omitempty"`
	Details    *media.ErrorDetails `json:"details,omitempty"`
}

ErrorReply is the flat error body every module answers. A code's own members appear only with it: retry_after with rate_limited and unavailable, action with an interaction's rate_limited, ban with comment_banned, blobs with not_uploaded, details with an upload refusal.

Directories

Path Synopsis
Command gen writes the files generated from the route catalog (go generate ./internal/contract).
Command gen writes the files generated from the route catalog (go generate ./internal/contract).

Jump to

Keyboard shortcuts

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