Documentation
¶
Overview ¶
Package reference renders an OpenAPI 3.1 document as an API reference in the gorbital design (ADR-0049): an overview, and a page per operation with its parameters, responses, request examples in curl, Go and TypeScript, response examples and "Try it". Generated apps serve it at /docs through openapi.MountDocs, and gorbital.dev renders its example API with it, so the two look the same.
Pages load nothing from other origins: the stylesheet, scripts and fonts come from Assets, and Reference.Handler serves them under ContentSecurityPolicy.
Stability: stable: the Go API follows the compatibility promise; the rendered HTML, CSS and scripts are not API (ADR-0015, ADR-0054).
Index ¶
Constants ¶
const ContentSecurityPolicy = "default-src 'none'; script-src 'self'; style-src 'self'; font-src 'self'; " +
"img-src 'self' data:; connect-src 'self'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'"
ContentSecurityPolicy is the policy Reference.Handler sends with every page: the reference's own scripts, styles and fonts, and requests only to the API serving it.
Variables ¶
This section is empty.
Functions ¶
func LLMs ¶
func LLMs(doc []byte, opts ExportOptions) ([]byte, error)
LLMs returns llms.txt (https://llmstxt.org) for doc, an OpenAPI 3.1 document in JSON: the API's name and summary, how to authenticate, and every endpoint by tag, linked to its Markdown page in the reference the app serves (ADR-0051). The output is deterministic.
func Postman ¶
func Postman(doc []byte, opts ExportOptions) ([]byte, error)
Postman returns a Postman collection (format v2.1) for doc, an OpenAPI 3.1 document in JSON: a folder per tag, {{baseUrl}} and {{token}} variables, bearer authentication on operations that declare security, and example bodies built from the schemas (ADR-0051). The output is deterministic, so a committed collection changes only when the API does.
Types ¶
type AssetFile ¶
type AssetFile struct {
// Name is the file name to serve, relative to the assets directory. The
// stylesheet and scripts carry a content hash, so they can be cached
// forever; fonts keep their names, which the stylesheet refers to.
Name string
// Source is the name layouts ask for, such as "reference.css".
Source string
Data []byte
}
AssetFile is a file reference pages load: the stylesheet, a script, or a font with its licence.
type ExportOptions ¶
type ExportOptions struct {
// BaseURL is the API's address, the collection's baseUrl variable.
// Default: http://localhost:8080.
BaseURL string
// DocsPath is where the app serves this reference, for llms.txt links.
// Default: /docs.
DocsPath string
}
type Options ¶
type Options struct {
// Title names the API in headers and on the overview. Default: the
// document's info.title.
Title string
// Intro is Markdown shown at the top of the overview. Default: the
// document's info.description.
Intro string
// BasePath is where the pages are served. Default "/docs".
BasePath string
// TrailingSlash ends page URLs with a slash, for static hosts that serve
// directories.
TrailingSlash bool
// SameOrigin says the pages are served by the API they document: "Try
// it" sends requests to the page's own origin with the browser's cookies.
// Otherwise readers choose the server, and cookies aren't sent.
SameOrigin bool
// SpecURL, when set, is linked from standalone pages.
SpecURL string
}
Options configure Build.
type Page ¶
type Page struct {
URL string
Title string
// Tag groups operations in navigation; "Overview" for the overview.
Tag string
// Method and Path are empty on the overview.
Method, Path string
// Description is plain text for meta tags and search.
Description string
// Main is the page's content. Panel holds an operation's examples and
// "Try it", shown beside it; it is empty on the overview.
Main, Panel template.HTML
// TOC lists the overview's sections.
TOC []Heading
// Markdown is the page as Markdown, served next to it with .md.
Markdown string
// Text is the page's words, for search.
Text string
}
Page is one page of a reference. Main and Panel are HTML fragments for a layout: the standalone pages of Reference.Handler, or a site's own.
type Reference ¶
type Reference struct {
// Title names the API.
Title string
// Version is the document's info.version.
Version string
// Pages are the overview, then a page per operation grouped by tag.
Pages []*Page
// contains filtered or unexported fields
}
Reference is a rendered API reference.
func (*Reference) Handler ¶
Handler returns a handler serving the reference at its base path as standalone pages: every page, its Markdown at .md, search.json, the assets under assets/, and a 404 page, all under ContentSecurityPolicy. Pages are rendered once, here.
func (*Reference) SearchIndex ¶
SearchIndex returns the index the search dialog loads: a JSON array of pages with their titles, sections, URLs, methods, paths and text.