getRawContentList
The getRawContentList
function retrieves a list of raw content items (content values and metadata) from a project.
This function is useful for fetching all content or filtering by a specific model.
For example, if you have a model called post
, you can use this function to fetch all content of that model.
Example:
// Using the previously created clientconst postContentList = await client.getRawContentList({ contentType: 'post' });console.log(postContentList);
Interface
The getRawContentList
function optionally accepts a QueryParams
object and returns a promise that resolves to a list of content items.
export interface ApiClient { getRawContentList: <Model>(queryParam?: QueryParams<Model>) => Promise<Content[]>; // ... other methods}
Parameters
queryParam
: An object containing query parameters to filter the content list. This parameter is optional. If not provided, it returns a list of all content in the project.
interface QueryParams { id?: Filter; contentType?: Filter; language?: Filter; `fields.${string}`?: Filter; // Allows filtering by specific field values // For example: 'fields.slug': 'my-post'`fields.${string}`?: Filter; // Allows filtering by specific field values}
Name | Description |
---|---|
id | The id field of the content to retrieve. Useful when you want to get specific content by their IDs.Example: client.getRawContentList({ id: { in: ['1', '2', '3'] } }) |
contentType | The content model you want to retrieve. Example: client.getRawContentList({ contentType: 'post' }); |
language | The language of the content to retrieve. This filter ensures only fields in the selected language are returned. Example: client.getRawContentList({ language: 'es' }); |
fields.${string} | Allows filtering by specific field values. Examples: client.getRawContentList({ 'fields.slug': 'my-post' } }) client.getRawContentList({ 'fields.title': { in: ['Hello World', 'Hola Mundo'] } }) Retrieves content with fields matching the specified values. |
type Filter = | string | { in?: string[]; // More filters to be added in the future };
Name | Description |
---|---|
string | Filters content where the parameter equals this value.Example: client.getRawContentList({ contentType: 'post' }) Retrieves content where the model equals post . |
in | Filters content where the parameter matches any of the provided values.Example: client.getRawContentList({ language: { in: ['es', 'en'] } }) Retrieves content with fields matching es and/or en . |
Output
The getRawContentList
function returns a list of raw content (filtered or not). This list is an array of objects representing 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;}
As you can see, each content item has an id
, a contentType
(representing the model it belongs to), a lastUpdate
date, and an array of fields
representing the content fields.
Consuming the fields as defined in this interface is useful when integrating with external systems, like a GraphQL server, or when mapping fields to a different content format.
However, the simplest and most recommended way to consume content is through the mapContentToModel
function after retrieving the data. It returns the content already mapped to its corresponding model. Alternatively, you can directly use the getContentList 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 Islandinterface Post { id: string; title: string; body: string; order: number; language: 'es' | 'en';}
const contentList = await client.getRawContentList<Post>({ contentType: 'post' });console.log(contentList);/* [ { 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 postList = contentList.map(content => mapContentToModel<Post>(content));console.log(postList);
/* [ { 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 the fields directly without needing to map manually. For more information on this function, check the mapContentToModel section.