Documentation
¶
Overview ¶
Package api provides a thin HTTP client for the Sanity.io APIs.
Index ¶
Constants ¶
const ( // QueryGetThreshold is the URL-length threshold (in bytes) above which // GROQ queries are sent via POST instead of GET. Sanity accepts both. QueryGetThreshold = 1024 // ManageBaseURL is the host for the Sanity Manage API. ManageBaseURL = "https://api.sanity.io/v2021-06-07" // DefaultTimeout is the default HTTP timeout. DefaultTimeout = 2 * time.Minute // DefaultMaxRetries is the default maximum number of retries. DefaultMaxRetries = 3 // DefaultInitialBackoff is the default initial backoff duration. DefaultInitialBackoff = 500 * time.Millisecond // DefaultMaxBackoff is the default maximum backoff duration. DefaultMaxBackoff = 10 * time.Second // UserAgent is sent on every request. UserAgent = "sanity-cli (+https://github.com/vstratful/sanity-cli)" )
Variables ¶
var ( ErrForbidden = errors.New("forbidden") ErrRateLimited = errors.New("rate limited") )
Functions ¶
This section is empty.
Types ¶
type AssetKind ¶
type AssetKind string
AssetKind distinguishes image assets from generic file assets.
type AssetResponse ¶
type AssetResponse struct {
Document json.RawMessage `json:"document"`
}
AssetResponse is the envelope returned by the asset upload endpoint.
type AssetUploadOptions ¶
AssetUploadOptions configures an asset upload request.
type Client ¶
type Client interface {
// Query runs a GROQ query and returns the raw `result` field.
Query(ctx context.Context, groq string, params map[string]any) (json.RawMessage, error)
// Mutate applies a list of mutations atomically.
Mutate(ctx context.Context, mutations []json.RawMessage, opts *MutateOptions) (*MutateResponse, error)
// UploadAsset uploads a binary asset.
UploadAsset(ctx context.Context, kind AssetKind, body io.Reader, opts *AssetUploadOptions) (json.RawMessage, error)
// ListProjects lists projects accessible to the token via the Manage API.
ListProjects(ctx context.Context) ([]Project, error)
// ListDatasets lists datasets in a project via the Manage API.
ListDatasets(ctx context.Context, projectID string) ([]Dataset, error)
}
Client is the interface for interacting with the Sanity API.
func DefaultClient ¶
DefaultClient creates a new Client from an instance with default settings.
func NewClient ¶
func NewClient(cfg ClientConfig) Client
NewClient creates a new Client with the given configuration.
type ClientConfig ¶
type ClientConfig struct {
Instance *config.Instance
Timeout time.Duration
HTTPClient *http.Client
Retry *RetryConfig
UserAgent string
}
ClientConfig is the construction-time configuration for a Client.
type Dataset ¶
type Dataset struct {
Name string `json:"name"`
ACLMode string `json:"aclMode,omitempty"`
AddonFor string `json:"addonFor,omitempty"`
Tags []any `json:"tags,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
DatasetID string `json:"datasetId,omitempty"`
ProjectID string `json:"projectId,omitempty"`
}
Dataset represents a dataset within a project.
type MutateOptions ¶
type MutateOptions struct {
TransactionID string
ReturnIDs bool
ReturnDocuments bool
Visibility string // "sync" | "async" | "deferred"
DryRun bool
AutoGenerateKeys bool
}
MutateOptions configures a mutate request.
type MutateResponse ¶
type MutateResponse struct {
TransactionID string `json:"transactionId,omitempty"`
Results json.RawMessage `json:"results,omitempty"`
Documents json.RawMessage `json:"documents,omitempty"`
}
MutateResponse is the envelope returned by the mutate endpoint.
type Project ¶
type Project struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
StudioHost string `json:"studioHost,omitempty"`
Organization string `json:"organizationId,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
Members json.RawMessage `json:"members,omitempty"`
}
Project represents a Sanity project as returned by the Manage API.
type QueryResponse ¶
type QueryResponse struct {
Result json.RawMessage `json:"result"`
Query string `json:"query,omitempty"`
Ms int `json:"ms,omitempty"`
}
QueryResponse is the envelope returned by the GROQ query endpoint.
type RetryConfig ¶
RetryConfig configures retry behavior.
func DefaultRetryConfig ¶
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns the default retry configuration.