CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-contentful

Client for Contentful's Content Delivery API

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

entries.mddocs/

Entry Management

Comprehensive entry retrieval and parsing capabilities for accessing Contentful content with flexible querying, type safety, and link resolution.

Capabilities

Get Single Entry

Fetches a single entry by its ID with optional query parameters.

/**
 * Fetches an entry by ID
 * @param id - The entry's ID
 * @param query - Object with search parameters
 * @returns Promise for an entry
 */
getEntry<
  EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
  Locales extends LocaleCode = LocaleCode
>(
  id: string,
  query?: EntryQueries<Modifiers>
): Promise<Entry<EntrySkeleton, Modifiers, Locales>>;

interface EntryQueries<Modifiers> {
  /** Specify the locale for the entry */
  locale?: string;
  /** Select specific fields to return */
  select?: string;
  /** Include linked entries and assets (default: 1) */
  include?: number;
}

Usage Examples:

// Basic entry retrieval
const entry = await client.getEntry("entry-id");
console.log(entry.fields.title);

// Get entry in specific locale
const frenchEntry = await client.getEntry("entry-id", {
  locale: "fr-FR"
});

// Get only specific fields
const entryPartial = await client.getEntry("entry-id", {
  select: "fields.title,fields.description"
});

// Include more levels of linked content
const entryWithLinks = await client.getEntry("entry-id", {
  include: 3
});

Get Multiple Entries

Fetches a collection of entries with extensive query and filtering capabilities.

/**
 * Fetches a collection of entries with optional queries
 * @param query - Object with search parameters
 * @returns Promise for a collection of entries
 */
getEntries<
  EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
  Locales extends LocaleCode = LocaleCode
>(
  query?: EntriesQueries<EntrySkeleton, Modifiers>
): Promise<EntryCollection<EntrySkeleton, Modifiers, Locales>>;

interface EntriesQueries<EntrySkeleton, Modifiers> {
  /** Filter by content type */
  content_type?: string;
  /** Limit number of results (max: 1000) */
  limit?: number;
  /** Skip entries for pagination */
  skip?: number;
  /** Order results by field */
  order?: string;
  /** Specify locale */
  locale?: string;
  /** Select specific fields */
  select?: string;
  /** Include linked entries and assets */
  include?: number;
  /** Search in all text fields */
  query?: string;
  /** Filter by field values */
  [key: `fields.${string}`]: any;
  /** Filter by system properties */
  [key: `sys.${string}`]: any;
  /** Equality filters */
  [key: string]: any;
}

Usage Examples:

// Get all entries
const allEntries = await client.getEntries();

// Get entries of specific content type
const blogPosts = await client.getEntries({
  content_type: "blogPost"
});

// Search and filter entries
const searchResults = await client.getEntries({
  content_type: "blogPost",
  query: "JavaScript",
  "fields.category": "Technology",
  order: "-fields.publishDate",
  limit: 10
});

// Pagination
const page2 = await client.getEntries({
  content_type: "blogPost",
  limit: 10,
  skip: 10
});

// Complex field filtering
const featuredPosts = await client.getEntries({
  content_type: "blogPost",
  "fields.featured": true,
  "fields.publishDate[gte]": "2023-01-01",
  order: "-fields.publishDate"
});

Parse Entries

Parses raw JSON data into a collection of entries with link resolution.

/**
 * Parse raw json data into a collection of entries
 * Links will be resolved based on client configuration
 * @param data - Raw entry collection data
 * @returns Parsed entry collection with resolved links
 */
parseEntries<
  EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
  Locales extends LocaleCode = LocaleCode
>(
  data: EntryCollection<
    EntrySkeleton,
    AddChainModifier<Modifiers, 'WITHOUT_LINK_RESOLUTION'>,
    Locales
  >
): EntryCollection<EntrySkeleton, Modifiers, Locales>;

Usage Example:

// Parse raw data from API response
const rawData = {
  items: [
    {
      sys: { type: 'Entry', id: 'entry1', locale: 'en-US' },
      fields: {
        title: 'My Post',
        author: { sys: { type: 'Link', linkType: 'Entry', id: 'author1' } }
      }
    }
  ],
  includes: {
    Entry: [
      {
        sys: { type: 'Entry', id: 'author1', locale: 'en-US' },
        fields: { name: 'John Doe' }
      }
    ]
  }
};

const parsedEntries = client.parseEntries(rawData);
console.log(parsedEntries.items[0].fields.author.fields.name); // "John Doe"

Entry Types

Entry Structure

interface Entry<
  EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
  Modifiers extends ChainModifiers = ChainModifiers,
  Locales extends LocaleCode = LocaleCode
> {
  sys: EntrySys;
  fields: ChainModifiers extends Modifiers
    ? EntryFields<EntrySkeleton> | LocalizedEntryFields<EntrySkeleton, Locales>
    : 'WITH_ALL_LOCALES' extends Modifiers
      ? LocalizedEntryFields<EntrySkeleton, Locales>
      : EntryFields<EntrySkeleton>;
  metadata: Metadata;
}

interface EntrySys extends EntitySys {
  contentType: { sys: ContentTypeLink };
  type: 'Entry';
}

interface EntitySys {
  id: string;
  type: string;
  space: { sys: { type: 'Link'; linkType: 'Space'; id: string } };
  environment: { sys: { type: 'Link'; linkType: 'Environment'; id: string } };
  createdAt: string;
  updatedAt: string;
  revision: number;
  locale?: string;
}

type EntryCollection<
  EntrySkeleton extends EntrySkeletonType = EntrySkeletonType,
  Modifiers extends ChainModifiers = ChainModifiers,
  Locales extends LocaleCode = LocaleCode
> = ContentfulCollection<Entry<EntrySkeleton, Modifiers, Locales>>;

interface ContentfulCollection<T> {
  items: T[];
  total: number;
  skip: number;
  limit: number;
}

Entry Field Types

declare namespace EntryFieldTypes {
  type Symbol<Values extends string = string> = { type: 'Symbol'; values: Values };
  type Text<Values extends string = string> = { type: 'Text'; values: Values };
  type Integer<Values extends number = number> = { type: 'Integer'; values: Values };
  type Number<Values extends number = number> = { type: 'Number'; values: Values };
  type Date = { type: 'Date' };
  type Boolean = { type: 'Boolean' };
  type Location = { type: 'Location' };
  type RichText = { type: 'RichText' };
  type EntryLink<EntrySkeleton extends EntrySkeletonType> = {
    type: 'EntryLink';
    entry: EntrySkeleton;
  };
  type EntryResourceLink<EntrySkeleton extends EntrySkeletonType> = {
    type: 'EntryResourceLink';
    entry: EntrySkeleton;
  };
  type ExternalResourceLink = { type: 'ExternalResourceLink' };
  type AssetLink = { type: 'AssetLink' };
  type Array<Item extends EntryFieldTypes[keyof EntryFieldTypes]> = {
    type: 'Array';
    items: Item;
  };
}

interface EntrySkeletonType {
  [fieldName: string]: any;
}

type LocaleCode = string;

Metadata

interface Metadata {
  tags: Array<{
    sys: {
      type: 'Link';
      linkType: 'Tag';
      id: string;
    };
  }>;
  concepts?: Array<{
    sys: {
      type: 'Link';
      linkType: 'TaxonomyConcept';
      id: string;
    };
  }>;
}

Working with Entry Fields

Field Access Patterns

// Standard field access
const entry = await client.getEntry("blog-post-id");
console.log(entry.fields.title); // string
console.log(entry.fields.publishDate); // string (ISO date)
console.log(entry.fields.featured); // boolean

// Working with rich text fields
const richTextField = entry.fields.body; // RichTextDocument
console.log(richTextField.nodeType); // 'document'
console.log(richTextField.content); // array of rich text nodes

// Working with linked entries
const authorEntry = entry.fields.author; // Entry object
console.log(authorEntry.fields.name);

// Working with arrays of links
const relatedPosts = entry.fields.relatedPosts; // Entry[]
relatedPosts.forEach(post => {
  console.log(post.fields.title);
});

Localized Content

// Using withAllLocales modifier
const allLocalesClient = client.withAllLocales;
const localizedEntry = await allLocalesClient.getEntry("entry-id");

// Access localized fields
console.log(localizedEntry.fields.title['en-US']); // English title
console.log(localizedEntry.fields.title['fr-FR']); // French title

// Check available locales
Object.keys(localizedEntry.fields.title).forEach(locale => {
  console.log(`${locale}: ${localizedEntry.fields.title[locale]}`);
});

Link Resolution Control

// Disable link resolution for performance
const noLinksClient = client.withoutLinkResolution;
const rawEntry = await noLinksClient.getEntry("entry-id");

// Fields contain raw link objects
console.log(rawEntry.fields.author); // { sys: { type: 'Link', linkType: 'Entry', id: 'author-id' } }

// Manually resolve if needed
const resolvedEntries = client.parseEntries({ items: [rawEntry], includes: {} });

docs

assets.md

client.md

entries.md

index.md

querying.md

space-content-types.md

sync.md

tags-taxonomy.md

tile.json