Content and forms
The cms group reads the shop's content entries, such as pages, blog posts and lookbooks. The forms group reads a site form, posts a visitor's answers and finishes newsletter sign-ups.
Every call takes an optional request context last ({ headers, signal, orgId }). Without generated types, a CMS read returns unknown; name the type at the call site or see Generated types.
cms.list
Lists the published entries of one content type, one page at a time. Use it for a blog index, a list of stockists or a page's child pages.
eldra.cms.list<Response>(schemaApiId, options?, context?): Promise<Response>| Name | Type | Default | What it does |
|---|---|---|---|
schemaApiId | string | — | The content type's API id (or its id), such as page or blogPost. |
page | number | — | Page number, starting at 1. |
pageSize | number | — | Entries per page. limit is an alias. |
sort | string[] | — | Sort fields. Prefix with - for descending. |
filter | string[] | — | Filter tokens as [group:]field:op:value, such as slug:eq:um-okkur. Tokens in one group are OR'd; the rest are AND'd. |
fields | string[] | all | Only these fields. |
depth | number | — | Levels of referenced entries to embed. 0 embeds none; at most 8. |
locale | string | — | Language of localized fields. |
siteId | string | client's siteId | The website to read, when the organisation has several. |
Returns { data, meta }. Each item in data is an entry: { id, data }, where data holds the fields your content type defines. meta is { page, pageSize, total, totalPages, rows, hasNext, hasPrev }.
Errors worth handling
CMS_SITE_REQUIRED(400): the organisation runs several websites and the call named none. SetsiteIdon the client. See Several websites.
import type { EldraPaginated } from '@eldrajs/sdk';
type Post = { id: string; data: { title: string; slug: string } };
const posts = await eldra.cms.list<EldraPaginated<Post>>('blogPost', {
pageSize: 10,
locale: 'is-IS',
});
for (const post of posts.data) console.log(post.data.title);cms.get
Reads one entry by its id. Use it when you already hold the id, for example from a reference in another entry.
eldra.cms.get<Response>(schemaApiId, entryId, options?, context?): Promise<Response>| Name | Type | Default | What it does |
|---|---|---|---|
schemaApiId | string | — | The content type's API id (or its id). |
entryId | string | — | The entry's id. |
depth | number | — | Levels of referenced entries to embed. 0 embeds none; at most 8. |
locale | string | — | Language of localized fields. |
siteId | string | client's siteId | The website to read. |
Returns the entry: { id, data }. Rich text fields hold a document; render it as shown in Rich text.
Errors worth handling
CMS_SITE_REQUIRED(400): as forcms.list.
const lookbook = await eldra.cms.get<{ id: string; data: { title: string } }>(
'lookbook',
lookbookId,
{ depth: 1, locale: 'is-IS' }
);cms.getEntryByUniqueField
Reads one entry by the value of a unique field, such as its slug. Use it to serve a page from its URL.
eldra.cms.getEntryByUniqueField<Response>(schemaApiId, fieldId, value, options?, context?): Promise<Response>| Name | Type | Default | What it does |
|---|---|---|---|
schemaApiId | string | — | The content type's API id (or its id). |
fieldId | string | — | The unique field to look up, such as slug. |
value | string | — | The value to find. |
depth | number | — | Levels of referenced entries to embed. 0 embeds none; at most 8. |
locale | string | — | Language of localized fields. |
siteId | string | client's siteId | The website to read. |
Returns the entry: { id, data }, as cms.get does.
Errors worth handling
CMS_SITE_REQUIRED(400): as forcms.list.
import { EldraHttpError } from '@eldrajs/sdk';
import { toHtml, type RichTextDocument } from '@eldrajs/rich-text';
type Page = { id: string; data: { title: string; body?: RichTextDocument } };
try {
const page = await eldra.cms.getEntryByUniqueField<Page>('page', 'slug', 'um-okkur', {
locale: 'is-IS',
});
const html = page.data.body ? toHtml(page.data.body) : '';
} catch (error) {
if (error instanceof EldraHttpError && error.status === 404) return notFound();
throw error;
}TIP
Serve a page at /<slug>, after its parents' slugs when it has a parent, such as /um-okkur/teymid. Eldra's emails link there.
cms.resolveEntryList
Turns the value of an entry-list field into the entries it selects. Use it when a page holds a block like "latest three knitting patterns", stored as a rule rather than as fixed entries.
eldra.cms.resolveEntryList<Response, List>(entryList, options?, context?)| Name | Type | Default | What it does |
|---|---|---|---|
entryList | EldraCmsEntryList | — | The field's value: schemas, filters, orderBy, pageSize and displayAs. The SDK sends only these. |
page | number | — | Page of the selected entries, starting at 1. |
pageSize | number | the list's own | Entries per page. limit is an alias. |
depth | number | 2 | Levels of referenced entries to embed. 0 embeds none; at most 8. |
locale | string | — | Language of localized fields. |
Returns the list you passed, with entries filled in as a page: { data, meta }. Pass Response to name a different shape.
// `page.data.patterns` is an entry-list field on the page.
const patterns = await eldra.cms.resolveEntryList(page.data.patterns, { locale: 'is-IS' });
for (const pattern of patterns.entries.data) console.log(pattern);forms.get
Reads a site form's definition, so your app can draw it. Use it on any page that shows a contact form or newsletter sign-up.
eldra.forms.get(key, context?): Promise<EldraFormDefinition>| Name | Type | Default | What it does |
|---|---|---|---|
key | string | — | The form's key, as set in Studio. |
Returns the definition. Every label is a record per locale, such as { 'is-IS': 'Netfang', 'en-US': 'Email' }; pick your locale and fall back.
| Field | What it is |
|---|---|
key, name | Identify the form. |
fields | Each field: { id, type, required, label, placeholder?, options?, validation? }. type is text, email, phone, textarea, select, checkbox, consent, number or url. |
honeypotField | Name of a field to render hidden from people. Send its value as hp. |
turnstileSiteKey | A Cloudflare Turnstile site key, or null. When set, render the widget yourself. |
success | { message, redirect? } to show or follow after a submission. |
kinds | What a submission does: store, notify, confirm, subscribe. |
consentText | The sign-up wording per locale. Present only when kinds contains subscribe. |
Errors worth handling
FEATURE_DISABLED(403): the organisation does not have theFORMSfeature.- A 404 means there is no form with that key.
const signup = await eldra.forms.get('frettabref');
const emailField = signup.fields.find((field) => field.type === 'email');SiteForm from @eldrajs/ui draws any definition and handles everything on this page for you. See Forms without a theme.
forms.submit
Posts a visitor's answers to a form. Use it from your form's submit handler.
eldra.forms.submit(key, input, context?): Promise<EldraFormSubmitResult>| Name | Type | Default | What it does |
|---|---|---|---|
key | string | — | The form's key. |
input.values | Record<string, string | boolean> | — | One entry per field id. A checkbox is a boolean; everything else a string. |
input.locale | string | — | The visitor's locale, such as is-IS. |
input.pagePath | string | — | The page the form was on. Send location.pathname. |
input.turnstileToken | string | — | The Turnstile widget's token, when the form has a site key. |
input.hp | string | — | The honeypot field's value. A person leaves it empty. |
Returns one of two answers. A rejected submission (422) is an answer, not an error.
{ status: 'ok', pending }: saved.pendingistruewhen a confirmation email is on its way.{ status: 'invalid', errors, form }:errorsmaps field ids to messages;formholds messages about the whole form. Show them inline.
Errors worth handling: a 404 (unknown form), 413 (too large) or 429 (rate limited) rejects with EldraHttpError. FEATURE_DISABLED (403) as for forms.get. From a browser, ORIGIN_NOT_REGISTERED (403) when the site's address is not registered: see Browsers and origins.
const result = await eldra.forms.submit('frettabref', {
values: { email: 'gudrun@example.is', name: 'Guðrún' },
locale: 'is-IS',
pagePath: location.pathname,
turnstileToken,
hp: honeypotValue,
});
if (result.status === 'invalid') showErrors(result.errors, result.form);
else if (result.pending) showMessage('Staðfestu netfangið þitt í póstinum sem við sendum.');WARNING
For a form that subscribes, show consentText next to the submit button as plain text. Submitting is the visitor's consent to exactly that wording. Add no extra checkbox and send no extra field.
forms.confirm
Confirms a newsletter sign-up from the token in the emailed link. Use it only if you host the confirm step in your own app.
eldra.forms.confirm(token, context?): Promise<EldraFormTokenResult>| Name | Type | Default | What it does |
|---|---|---|---|
token | string | — | The token from the link. |
Returns { status: 'confirmed' }. Confirming twice gives the same answer.
const token = new URL(location.href).searchParams.get('token');
if (token) await eldra.forms.confirm(token);TIP
You do not need this. The emailed links open a page Eldra hosts, which asks the subscriber to press a button. A mail scanner that opens the link changes nothing.
forms.unsubscribe
Unsubscribes a subscriber from the token in the emailed link. Use it only if you host the unsubscribe step in your own app.
eldra.forms.unsubscribe(token, context?): Promise<EldraFormTokenResult>| Name | Type | Default | What it does |
|---|---|---|---|
token | string | — | The token from the link. |
Returns { status: 'unsubscribed' }. Unsubscribing twice gives the same answer.
const token = new URL(location.href).searchParams.get('token');
if (token) await eldra.forms.unsubscribe(token);