settingsv1
import settingsv1 "pocketone.dev/ext/contracts/settings/v1"
Package settingsv1 is the settings/v1 contract: the settings an app's admins change for its extensions (pitr's S3 bucket, bulkmail's sending limits), one API for all of them. Each extension registers its section with Add; the contract's provider (ext/settings) lists them, reads, saves and tests them.
A wire contract at /api/_c/settings/v1. Settings an operator sets for a whole host stay in pocketone.yaml; these are the ones the app's own superusers change.
Source: ext/contracts/settings/v1.
Constants
const (
FieldText = "text"
FieldSecret = "secret" // Get returns it empty, with "<key>Set": true when one is saved; saving it empty keeps it
FieldBool = "bool"
FieldNumber = "number"
FieldSelect = "select"
FieldURL = "url"
FieldReadOnly = "readonly" // shown, never saved (a webhook URL to copy)
)
Field types.
Variables
var Contract = extkit.NewContract[Settings]("settings/v1", extkit.One).HostProvidable().JS(extkit.JSHooks, nil).JSTypes(jsTypes)
Contract is settings/v1.
var ErrNotTestable = &extkit.Error{Code: "not_testable", Message: "these settings have no test", Status: 400}
ErrNotTestable: the extension's settings have no test.
var ErrReadOnly = &extkit.Error{Code: "read_only", Message: "these settings are set by the host", Status: 400}
ErrReadOnly: the extension's settings are shown here but set elsewhere (by the host).
var ErrUnknown = &extkit.Error{Code: "unknown_extension", Message: "no settings for that extension on this app", Status: 404}
ErrUnknown: no extension with that name has settings on this app.
var W = extkit.NewWire(Contract, "settings", func(inner Settings, g *extkit.Guard[Settings]) Settings {
return &guardSettings{inner, g}
},
extkit.Op[Settings]{
Name: "list",
Desc: "Names the extensions whose settings this app's admins can change.",
Method: "GET",
Path: "",
Scope: "settings:read",
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Settings, a extkit.Auth, req extkit.OpRequest) (any, error) {
return impl.List(ctx, a)
},
},
extkit.Op[Settings]{
Name: "get",
Desc: "Returns one extension's settings. Secrets come back empty, with a flag saying they're set.",
Method: "GET",
Path: "get",
Scope: "settings:read",
In: reflect.TypeOf(Ref{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Settings, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in Ref
if err := extkit.DecodeQuery(req.Query, &in); err != nil {
return nil, err
}
return impl.Get(ctx, a, in)
},
},
extkit.Op[Settings]{
Name: "save",
Desc: "Replaces one extension's settings and applies them. A secret left empty keeps its saved value. Settings a host sets are read-only here.",
Method: "POST",
Path: "save",
Scope: "settings",
In: reflect.TypeOf(Change{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Settings, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in Change
if err := extkit.DecodeInput(req.JSON, &in); err != nil {
return nil, err
}
return impl.Save(ctx, a, in)
},
},
extkit.Op[Settings]{
Name: "test",
Desc: "Checks one extension's settings without saving them (a test write to a bucket, a test message).",
Method: "POST",
Path: "test",
Scope: "settings",
In: reflect.TypeOf(Change{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Settings, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in Change
if err := extkit.DecodeInput(req.JSON, &in); err != nil {
return nil, err
}
return impl.Test(ctx, a, in)
},
},
)
W is settings/v1's wire side: routes at /api/_c/settings/v1/<op>, action hooks, MCP tools and the client.
Functions
Add
func Add(app core.App, s Section)
Add registers an extension's settings on app (call it in Register). The provider of settings/v1 serves them.
OnGet
func OnGet(owner any, fn func(e *extkit.CallEvent, in *Ref) error)
OnGet binds fn to get calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnList
func OnList(owner any, fn func(e *extkit.CallEvent) error)
OnList binds fn to list calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse.
OnSave
func OnSave(owner any, fn func(e *extkit.CallEvent, in *Change) error)
OnSave binds fn to save calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnTest
func OnTest(owner any, fn func(e *extkit.CallEvent, in *Change) error)
OnTest binds fn to test calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
Types
Change
type Change struct {
Extension string `json:"extension"`
Values json.RawMessage `json:"values"`
}
Change is settings to save or test.
Client
type Client struct {
// contains filtered or unexported fields
}
Client is settings/v1 over HTTP, for the CLI and other apps. It implements Settings; the token it was made with is the credential, so the extkit.Auth argument is ignored.
NewClient
func NewClient(baseURL, token string, hc *http.Client) *Client
NewClient calls settings/v1 on the app at baseURL with token (a PocketBase auth token or an API token).
Client.Get
func (c *Client) Get(ctx context.Context, _ extkit.Auth, in Ref) (Values, error)
Client.List
func (c *Client) List(ctx context.Context, _ extkit.Auth) (List, error)
Client.Save
func (c *Client) Save(ctx context.Context, _ extkit.Auth, in Change) (Values, error)
Client.Test
func (c *Client) Test(ctx context.Context, _ extkit.Auth, in Change) (TestResult, error)
Field
type Field struct {
Key string `json:"key"`
Label string `json:"label"`
Type string `json:"type"` // FieldText ...
Help string `json:"help,omitempty"`
Placeholder string `json:"placeholder,omitempty"`
Options []Option `json:"options,omitempty"` // FieldSelect
Required bool `json:"required,omitempty"`
}
Field is one value in a section's form.
Info
type Info struct {
Extension string `json:"extension"`
Title string `json:"title"`
Description string `json:"description,omitempty"`
Testable bool `json:"testable"`
ReadOnly bool `json:"readOnly"` // shown, not changed here (a host sets them)
// Group is where the dashboard shows the section: another extension's page that renders its group ("billing"
// shows payment processors' sections on the Billing page), or "" for Settings → Extensions.
Group string `json:"group,omitempty"`
// Fields describe the section's values as a form, so the dashboard renders it with no JS of the extension's own.
// Each field's Key is a top-level key of Values. A section without fields needs its own UI.
Fields []Field `json:"fields,omitempty"`
}
Info describes one extension's settings.
List
type List struct {
Sections []Info `json:"sections"`
}
List is the extensions with settings.
Option
type Option struct {
Value string `json:"value"`
Label string `json:"label"`
}
Option is one choice of a select field.
Ref
type Ref struct {
Extension string `json:"extension"`
}
Ref names an extension.
Saver
type Saver interface {
Save(ctx context.Context, values json.RawMessage) (json.RawMessage, error)
}
Saver is a Section whose settings the admins change. Return an *extkit.Error for a value they should fix.
Section
type Section interface {
Info() Info
Get(ctx context.Context) (json.RawMessage, error)
}
Section is one extension's settings, as its extension keeps them. A section the admins can change is also a Saver; one they can only see (set by the host) isn't.
SectionOf
func SectionOf(app core.App, extension string) (Section, bool)
SectionOf returns one extension's section on app.
Sections
func Sections(app core.App) []Section
Sections returns app's registered sections, sorted by extension name (for providers).
Settings
type Settings interface {
// List names the extensions whose settings this app's admins can change.
//
//op: GET / scope=settings:read
List(ctx context.Context, a extkit.Auth) (List, error)
// Get returns one extension's settings. Secrets come back empty, with a flag saying they're set.
//
//op: GET get scope=settings:read
Get(ctx context.Context, a extkit.Auth, in Ref) (Values, error)
// Save replaces one extension's settings and applies them. A secret left empty keeps its saved value. Settings a
// host sets are read-only here.
//
//op: POST save scope=settings
Save(ctx context.Context, a extkit.Auth, in Change) (Values, error)
// Test checks one extension's settings without saving them (a test write to a bucket, a test message).
//
//op: POST test scope=settings
Test(ctx context.Context, a extkit.Auth, in Change) (TestResult, error)
}
Settings reads and changes the app's extension settings.
TestResult
type TestResult struct {
OK bool `json:"ok"`
Message string `json:"message"`
}
TestResult says how a test went.
Tester
type Tester interface {
Test(ctx context.Context, values json.RawMessage) error
}
Tester is a Section that can check settings without saving them.
Values
type Values struct {
Extension string `json:"extension"`
Values json.RawMessage `json:"values"`
}
Values are one extension's settings, in its own shape.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.