api

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package api provides a thin HTTP client for the Sanity.io APIs.

Index

Constants

View Source
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

View Source
var (
	ErrUnauthorized       = errors.New("unauthorized")
	ErrForbidden          = errors.New("forbidden")
	ErrRateLimited        = errors.New("rate limited")
	ErrServiceUnavailable = errors.New("service unavailable")
)

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	Message    string
	Body       string
}

APIError represents an error returned by the Sanity API.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap maps known status codes to sentinel errors.

type AssetKind

type AssetKind string

AssetKind distinguishes image assets from generic file assets.

const (
	AssetKindImage AssetKind = "image"
	AssetKindFile  AssetKind = "file"
)

type AssetResponse

type AssetResponse struct {
	Document json.RawMessage `json:"document"`
}

AssetResponse is the envelope returned by the asset upload endpoint.

type AssetUploadOptions

type AssetUploadOptions struct {
	ContentType string
	Filename    string
	Label       string
	Title       string
}

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

func DefaultClient(inst *config.Instance, timeout time.Duration) Client

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

type RetryConfig struct {
	MaxRetries     int
	InitialBackoff time.Duration
	MaxBackoff     time.Duration
}

RetryConfig configures retry behavior.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns the default retry configuration.

Jump to

Keyboard shortcuts

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