Skip to content

getRawContent

The getRawContent function retrieves a single raw content (content values and metadata) from a project. This function is useful for fetching a specific piece of content, such as an article or blog post.

Example:

// Using the previously created client
const content = await client.getRawContent({ id: '1' });
console.log(content);

Interface

The getRawContent function accepts a required QueryParams object. It returns a promise that resolves to the content.

export interface ApiClient {
getRawContent: <Model>(queryParam: QueryParams<Model>) => Promise<Content>;
// ... other methods
}

Parameters

  • queryParam: An object containing query parameters to filter the content. This parameter is optional. If not provided, the full content will be returned.
interface QueryParams {
id?: Filter;
contentType?: Filter;
language?: Filter;
`fields.${string}`?: Filter; // Allows filtering by specific field values
// For example: 'fields.slug': 'my-post'
}
NameDescription
idThe ID of the content you want to retrieve.
This parameter must be a string.
Example: client.getRawContent({ id: '1' })
You can find the content id in your project’s Content tab.
contentTypeThe model of the content you want to retrieve.
Example: client.getRawContent({ id: '1', contentType: 'post' })
languageThe language of the content you want to retrieve.
This filter ensures only the model fields in the selected language are returned.
Example: client.getRawContent({ id: '1', language: 'es' })
fields.${string}Allows filtering by specific field values.
Examples:
client.getRawContent({ 'fields.slug': 'my-post' } })
client.getRawContent({ 'fields.title': { in: ['Hello World', 'Hola Mundo'] } })
Retrieves content with fields matching the specified values.
type Filter =
| string
| {
in?: string[];
};
NameDescription
stringFilters the content where the parameter equals this value.
Example: client.getRawContent({ id: '1', contentType: 'post' })
Retrieves a content item whose model equals post.
inFilters the content where the parameter matches any of the provided values.
Example: client.getRawContent({ id: '1', language: { in: ['es', 'en'] } })
Retrieves a content item with fields matching es and/or en.

Output

The getRawContent function returns an object representing the raw content (filtered or not). This object contains the content values and metadata:

export interface Content {
id: string;
contentType: { id: string; name: string };
lastUpdate: Date;
fields: Field[];
}
export interface Field {
id: string;
name: string;
value: any;
type: FieldType;
isArray: boolean;
language: string;
}

Each content item has an id, a contentType (which represents the model it belongs to), a lastUpdate timestamp, and an array of fields representing the content fields.

Consuming the fields as defined in this interface is useful when integrating with external systems, such as a GraphQL server, or when mapping the fields to another type of content.

However, the easiest and recommended way to consume content is by using the mapContentToModel function after retrieving the data. It returns the content already mapped to its corresponding model. Alternatively, you can directly use the getContent method, which already performs this mapping automatically.

Example:

import { createClient, mapContentToModel } from '@content-island/api-client';
// ... client initialization code
// Your Post model defined in Content Island
interface Post {
id: string;
title: string;
body: string;
order: number;
language: 'es' | 'en';
}
const content = await client.getRawContent<Post>({ id: '1' });
console.log(content);
/*
{
id: '1',
contentType: { id: '100', name: 'post' },
lastUpdate: '2023-10-01T12:00:00Z',
fields: [
{ id: '111', name: 'title', value: 'Hola Mundo', type: 'short-text', isArray: false, language: 'es' },
{ id: '222', name: 'body', value: 'Este es el cuerpo del post en markdown.', type: 'long-text', isArray: false, language: 'es' },
{ id: '333', name: 'order', value: 1, type: 'number', isArray: false, language: 'es' },
],
}
*/
const post = mapContentToModel<Post>(content);
console.log(post);
/*
{
id: '1',
title: 'Hola Mundo',
body: 'Este es el cuerpo del post en markdown.',
order: 1,
language: 'es',
}
*/

As you can see, the content is already mapped to its corresponding model and you can access fields directly without manually mapping them. For more information on this function, check the mapContentToModel section.