ContentType

View as Markdown

ContentType

A content type is the structure, or schema, that the entries of one kind share. ContentType represents one content type of the stack. Use this class to

  • read the schema of one content type
  • reach the entries created from that content type

stack.contentType() takes one optional argument:

NameTypeDescription
uidstringUID of the content type. Omit it to list content types instead.

Get a ContentType with stack.contentType('<CONTENT_TYPE_UID>'). Omit the UID to get a ContentTypeQuery, which lists content types and has its own ContentType Collection class page.

NameTypeDescription
contentTypeUid string

UID of the content type

entry

entry returns an object for one entry of the content type when you pass its UID, or for every entry of the content type when you do not.

NameTypeDescription
uidstring

UID of the entry. Omit it to work with every entry.

Default: undefined

Returns: Entry or Entries. A single entry, or the entries of the content type.

Validation

  • There is no client-side validation. entry hands any non-empty string to the Entry it builds.
  • Known issue: In 5.x, entry('').fetch() compiles and then throws a TypeError at runtime, because an empty UID returns an Entries object that TypeScript still types as Entry. Check that a UID variable is non-empty before you pass it, as the edge-case example below shows.
  • Passing a string | undefined variable is a compile error, because neither overload accepts undefined.
  • Errors that do occur come from the fetch() or find() call that follows, not from entry(). That call throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • Client-side only. entry builds an object and makes no request.
  • Which class you get back decides which methods are available.
    • entry('<ENTRY_UID>') returns an Entry, which reads one entry with fetch and shapes it with methods such as locale and includeReference.
    • entry() returns an Entries object, which lists entries with find, pages them with limit and skip, and opens a filter query with query.
    • The Entry class page documents both.
  • The content type UID is the only state that carries over. Each call returns a new object, so modifiers you chain on one entry object never reach another.

Example

Basic usage: read one entry by UID

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType('<CONTENT_TYPE_UID>').entry('<ENTRY_UID>').fetch();
  console.log(result);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

No UID: list the entries of the content type

import contentstack from '@contentstack/delivery-sdk';
import type { BaseEntry } from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  // Omitting the UID returns an Entries object, so the terminal call is find.
  const result = await stack.contentType('<CONTENT_TYPE_UID>').entry().limit(5).find<BaseEntry>();
  for (const entry of result.entries ?? []) {
    console.log(entry.uid, entry.title);
  }
} catch (error: any) {
  console.error(error);
}

Edge case: an empty UID returns the wrong object

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

const entryUid = '';

try {
  const result = await stack.contentType('<CONTENT_TYPE_UID>').entry(entryUid).fetch();
  console.log(result);
} catch (error: any) {
  // Fires because entry('') returned an Entries object, which has no fetch method
  console.error('Check the entry UID before calling entry():', error.message);
}

fetch

fetch retrieves the schema and details of one content type by its UID.

Returns: Promise<T>. The requested content type.

Validation

  • There is no client-side validation. fetch sends whatever content type UID the object holds.
  • fetch exists only on ContentType. Calling it on stack.contentType() without a UID is a compile error.
  • fetch throws an APIError when the request fails, carrying the HTTP status, the API error_code, and the error_message. An unknown content type UID throws status 422 with error_code 118.
  • Known issue: In 5.x, fetch throws an APIError with status 0 and error_code NETWORK_ERROR while the stack config sets live_preview.enable to true. Read the schema through a second stack created without live_preview.

Additional Resource Refer to Delivery API Errors for the full error code list.

Behavior

  • One call sends one GET /content_types/{content_type_uid} request. It carries only the stack-level environment and locale parameters, because ContentType has no modifier methods.
  • fetch returns the value of the response's content_type key, not the whole body. When the response has no content_type key, fetch returns the whole body.
  • fetch is generic. Pass an interface as T, for example the exported BaseContentType, to type the result. The SDK does not check the response against T.

Limitations

  • Does not include global field schemas. Use the includeGlobalFieldSchema method on stack.contentType() instead, then pick the content type by uid from the list.

Example

Basic usage: read one content type schema

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const result = await stack.contentType('<CONTENT_TYPE_UID>').fetch();
  console.log(result);
} catch (error: any) {
  // Thrown for API rejections and network failures
  console.error('Request failed:', error.status, error.error_code, error.error_message);
}

Typed result: list the fields of the schema

import contentstack from '@contentstack/delivery-sdk';
import type { BaseContentType } from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  const contentType = await stack.contentType('<CONTENT_TYPE_UID>').fetch<BaseContentType>();
  for (const field of contentType.schema) {
    console.log(field.uid, field.data_type, field.mandatory ? 'required' : 'optional');
  }
} catch (error: any) {
  console.error(error);
}

Error handling: an unknown content type UID

import contentstack from '@contentstack/delivery-sdk';

const stack = contentstack.stack({
  apiKey: '<API_KEY>',
  deliveryToken: '<DELIVERY_TOKEN>',
  environment: '<ENVIRONMENT>',
});

try {
  await stack.contentType('no_such_content_type').fetch();
} catch (error: any) {
  // The API rejects a content type UID that does not exist with status 422.
  if (error.status === 422) {
    console.error('No content type with that UID:', error.error_message);
  } else {
    console.error(error);
  }
}