Skip to content
Esc
navigateopen⌘Jpreview
On this page

@stetcms/client

The typed content client your codegen writes, the REST client for everything else in the API, and the types every entry carries.

@stetcms/client is two clients in one package.

The content client is what you use day to day. You do not construct it: codegen writes a stet.gen.ts that constructs it for you, typed by your organization’s content model.

The REST client covers everything else in the API — organizations, billing, webhooks — as a fully typed call for every endpoint in the contract.

The content client

stet

The generated module exports a ready stet. Its keys are your collections and maps:

import { stet } from './stet.gen';

const posts = await stet.posts.list(); // PostsEntry[]
const post = await stet.posts.get('hello-world'); // PostsEntry
const landing = await stet.landing.get(); // LandingEntry

A collection holds many entries and gets list() and get(slug). A map holds exactly one and gets get() with no argument — the type system knows which is which, so calling list() on a map is a compile error.

PropType
list()?Promise<E[]>

Every entry in the collection, oldest first. Collections only.

TypePromise<E[]>
get(slug)?Promise<E>

One entry, addressed by its slug. Collections only.

TypePromise<E>
get()?Promise<E>

The map's single entry. Maps only; throws if the map is empty.

TypePromise<E>

Both reject with an Error carrying the status when a request fails, so a missing slug surfaces as a rejected promise rather than undefined.

createContentClient

The function stet.gen.ts calls. Reach for it directly only when you need a second client — a different organization, or a fetch you control:

import { createContentClient } from '@stetcms/client';
import type { StetContentModel } from './stet.gen';

const preview = createContentClient<StetContentModel>({
  origin: 'https://stetcms.com',
  apiKey: process.env.STET_PREVIEW_KEY,
});
PropType
origin?string

The Stet deployment to read from.

Typestring
Defaulthttps://stetcms.com
apiKey?string

Organization API key, sent as x-api-key.

Typestring
fetch?typeof fetch

Override the fetch implementation, e.g. in tests.

Typetypeof fetch

The runtime is one Proxy over the content API, so an unknown key costs nothing and the module stays small.

What an entry holds

Every entry carries the same base, whatever its collection:

PropType
idstring

The entry's stable identifier.

Typestring
slugstring

The addressable name, unique per collection.

Typestring
titlestring

The entry's title.

Typestring
createdAtstring

ISO 8601.

Typestring
updatedAtstring

ISO 8601.

Typestring
fieldsRecord<string, unknown>

The fields your content team defined, typed per collection.

TypeRecord<string, unknown>

Everything under fields is optional and nullable, because an editor may not have filled it in. See the content model for what each field type becomes.

Assets and references

Two field types arrive as objects rather than scalars:

PropType
ContentAsset?{ id, url, name, contentType, size }

An uploaded file. url is fully qualified and public, so it goes straight into an img tag or a download link.

Type{ id, url, name, contentType, size }
ContentReference?{ id, slug, title }

A pointer at an entry of another collection: enough to render a link, plus the slug to fetch the rest. The target collection is named in the generated field's doc comment.

Type{ id, slug, title }

The API returns asset URLs relative to itself, since the same bytes serve whichever origin you reach them through. The client joins them to the origin it was configured with — including the ones an editor dropped inside a rich text body — so what you get is ready to use on your own site.

If you call the REST API directly rather than through the client, do that join yourself:

PropType
assetUrl(url, origin)?string

Joins one asset path to an origin. Anything already absolute is left alone.

Typestring
resolveAssetPaths(text, origin)?string

The same join applied inside a body's markdown: link and image destinations, and the src of any raw HTML.

Typestring

The REST client

createStetClient returns a typed call for every endpoint in the contract, over oRPC:

import { createStetClient, isDefinedError, safe } from '@stetcms/client';

const client = createStetClient({ apiKey: process.env.STET_API_KEY });

const { data, error, isDefined } = await safe(client.org.current());

if (isDefined && error.code === 'UNAUTHORIZED') {
  throw new Error('Invalid or revoked API key.');
}
PropType
origin?string

The Stet deployment.

Typestring
Defaulthttps://stetcms.com
apiKey?string

Organization API key, sent as x-api-key.

Typestring
fetch?typeof fetch

Override the fetch implementation.

Typetypeof fetch

safe and isDefinedError are re-exported from @orpc/client, so error handling needs no second dependency. safe turns a rejection into a result object; isDefined tells you the error came from the contract rather than the network, which is what makes error.code safe to switch on.

Exports

PropType
createContentClient?function

The model-shaped content client. Called for you by stet.gen.ts.

Typefunction
createStetClient?function

The typed REST client for the rest of the API.

Typefunction
assetUrl?function

Join an asset path to an origin.

Typefunction
resolveAssetPaths?function

Join every asset path inside a body's markdown.

Typefunction
safe?function

Re-export from @orpc/client: a rejection as a result object.

Typefunction
isDefinedError?function

Re-export from @orpc/client: whether an error came from the contract.

Typefunction
DEFAULT_ORIGIN?string

https://stetcms.com

Typestring
ApiInputs?type

Inputs of every contract procedure, keyed by path.

Typetype
ApiOutputs?type

Outputs of every contract procedure, keyed by path.

Typetype
StetClient?type

The REST client type.

Typetype
ContentClient?type

The content client, given a model shape.

Typetype
ContentModelShape?type

What stet.gen.ts generates: each key carries a kind and an entry type.

Typetype
ContentEntryBase?type

The fields every entry carries.

Typetype
ContentAsset?type

An asset field's value.

Typetype
ContentReference?type

A reference field's value.

Typetype
CollectionClient?type

list() and get(slug).

Typetype
MapClient?type

get().

Typetype

@stetcms/client/codegen is a second entry point holding fetchContentModel, renderContentModule and entryTypeName. It is documented under codegen.

Was this page helpful?