Skip to content

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.

ts
eldra.cms.list<Response>(schemaApiId, options?, context?): Promise<Response>
NameTypeDefaultWhat it does
schemaApiIdstring—The content type's API id (or its id), such as page or blogPost.
pagenumber—Page number, starting at 1.
pageSizenumber—Entries per page. limit is an alias.
sortstring[]—Sort fields. Prefix with - for descending.
filterstring[]—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.
fieldsstring[]allOnly these fields.
depthnumber—Levels of referenced entries to embed. 0 embeds none; at most 8.
localestring—Language of localized fields.
siteIdstringclient's siteIdThe 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. Set siteId on the client. See Several websites.
ts
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.

ts
eldra.cms.get<Response>(schemaApiId, entryId, options?, context?): Promise<Response>
NameTypeDefaultWhat it does
schemaApiIdstring—The content type's API id (or its id).
entryIdstring—The entry's id.
depthnumber—Levels of referenced entries to embed. 0 embeds none; at most 8.
localestring—Language of localized fields.
siteIdstringclient's siteIdThe 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 for cms.list.
ts
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.

ts
eldra.cms.getEntryByUniqueField<Response>(schemaApiId, fieldId, value, options?, context?): Promise<Response>
NameTypeDefaultWhat it does
schemaApiIdstring—The content type's API id (or its id).
fieldIdstring—The unique field to look up, such as slug.
valuestring—The value to find.
depthnumber—Levels of referenced entries to embed. 0 embeds none; at most 8.
localestring—Language of localized fields.
siteIdstringclient's siteIdThe website to read.

Returns the entry: { id, data }, as cms.get does.

Errors worth handling

  • CMS_SITE_REQUIRED (400): as for cms.list.
ts
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.

ts
eldra.cms.resolveEntryList<Response, List>(entryList, options?, context?)
NameTypeDefaultWhat it does
entryListEldraCmsEntryList—The field's value: schemas, filters, orderBy, pageSize and displayAs. The SDK sends only these.
pagenumber—Page of the selected entries, starting at 1.
pageSizenumberthe list's ownEntries per page. limit is an alias.
depthnumber2Levels of referenced entries to embed. 0 embeds none; at most 8.
localestring—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.

ts
// `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.

ts
eldra.forms.get(key, context?): Promise<EldraFormDefinition>
NameTypeDefaultWhat it does
keystring—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.

FieldWhat it is
key, nameIdentify the form.
fieldsEach field: { id, type, required, label, placeholder?, options?, validation? }. type is text, email, phone, textarea, select, checkbox, consent, number or url.
honeypotFieldName of a field to render hidden from people. Send its value as hp.
turnstileSiteKeyA Cloudflare Turnstile site key, or null. When set, render the widget yourself.
success{ message, redirect? } to show or follow after a submission.
kindsWhat a submission does: store, notify, confirm, subscribe.
consentTextThe sign-up wording per locale. Present only when kinds contains subscribe.

Errors worth handling

  • FEATURE_DISABLED (403): the organisation does not have the FORMS feature.
  • A 404 means there is no form with that key.
ts
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.

ts
eldra.forms.submit(key, input, context?): Promise<EldraFormSubmitResult>
NameTypeDefaultWhat it does
keystring—The form's key.
input.valuesRecord<string, string | boolean>—One entry per field id. A checkbox is a boolean; everything else a string.
input.localestring—The visitor's locale, such as is-IS.
input.pagePathstring—The page the form was on. Send location.pathname.
input.turnstileTokenstring—The Turnstile widget's token, when the form has a site key.
input.hpstring—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. pending is true when a confirmation email is on its way.
  • { status: 'invalid', errors, form }: errors maps field ids to messages; form holds 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.

ts
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.

ts
eldra.forms.confirm(token, context?): Promise<EldraFormTokenResult>
NameTypeDefaultWhat it does
tokenstring—The token from the link.

Returns { status: 'confirmed' }. Confirming twice gives the same answer.

ts
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.

ts
eldra.forms.unsubscribe(token, context?): Promise<EldraFormTokenResult>
NameTypeDefaultWhat it does
tokenstring—The token from the link.

Returns { status: 'unsubscribed' }. Unsubscribing twice gives the same answer.

ts
const token = new URL(location.href).searchParams.get('token');
if (token) await eldra.forms.unsubscribe(token);

Eldra