Localess
Node.js

Reference

Assets, error handling, caching, and the full type and utility-function reference for @localess/client.

Assets

assetLink(asset)

Generate a fully qualified URL for a content asset.

import { localessClient } from "@localess/client";

const client = localessClient({ origin, spaceId, token });

// From a ContentAsset object
const url = client.assetLink(content.data.image);

// From a URI string
const url = client.assetLink('/spaces/abc/assets/photo.jpg');

Error Handling

getLinks, getContentBySlug, getContentById, and getTranslations throw instead of returning empty data on failure. Network failures reject with the underlying error; non-2xx HTTP responses reject with a LocalessApiError (status, statusText, url). Always wrap calls in try/catch:

import { LocalessApiError } from "@localess/client";

try {
  const content = await client.getContentBySlug('home');
} catch (error) {
  if (error instanceof LocalessApiError) {
    console.error(error.status, error.statusText);
  }
}

Caching

All API responses are cached by default using a TTL (time-to-live) cache. Cache key = full request URL. You can configure caching when initializing the client.

// Default: 5-minute TTL cache
const client = localessClient({ origin, spaceId, token });

// Custom TTL in seconds (e.g., 10 minutes)
const client = localessClient({ origin, spaceId, token, cacheTTL: 600 });

// Disable caching entirely
const client = localessClient({ origin, spaceId, token, cacheTTL: false });

Type Reference

Content<T>

interface Content<T extends ContentData> extends ContentMetadata {
  data?: T;
  links?: Links;            // Populated when resolveLink: true
  references?: References;  // Populated when resolveReference: true
}

ContentMetadata

interface ContentMetadata {
  id: string;
  name: string;
  kind: 'FOLDER' | 'DOCUMENT';
  slug: string;
  fullSlug: string;
  parentSlug: string;
  publishedAt?: string;
  createdAt: string;
  updatedAt: string;
}

ContentData

Base type for all content schema data objects.

interface ContentDataSchema {
  _id: string;
  _schema: string;
}

interface ContentData extends ContentDataSchema {
  [field: string]: ContentDataField | undefined;
}

ContentAsset

interface ContentAsset {
  kind: 'ASSET';
  uri: string;
}
interface ContentLink {
  kind: 'LINK';
  type: 'url' | 'content';
  target: '_blank' | '_self';
  uri: string;
}

ContentReference

interface ContentReference {
  kind: 'REFERENCE';
  uri: string;
}

ContentRichText

interface ContentRichText {
  type?: string;
  content?: ContentRichText[];
}

Other Types

  • Links — A key-value map of content IDs to ContentMetadata objects.
  • References — A key-value map of reference IDs to Content objects.
  • Translations — A key-value map of translation keys to translated string values.

Utility Functions

FunctionReturnsDescription
isBrowser()booleanReturns true if code is running in a browser environment
isServer()booleanReturns true if code is running in a server/Node.js environment
isIframe()booleanReturns true if the page is rendered inside an iframe

On this page