getContentList
The getContentList
function retrieves a list of content items 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.getContentList({ contentType: 'post' });console.log(postContentList);
Interface
The getContentList
function optionally accepts a QueryParams
object and returns a promise that resolves to a list of content items.
export interface ApiClient { getContentList: (queryParam?: QueryParams) => 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;}
Name | Description |
---|---|
id | The id field of the content to retrieve. Useful when you want to get specific content by their IDs.Example: client.getContentList({ id: { in: ['1', '2', '3'] } }) |
contentType | The content model you want to retrieve. Example: client.getContentList({ contentType: 'post' }); |
language | The language of the content to retrieve. This filter ensures only fields in the selected language are returned. Example: client.getContentList({ language: 'es' }); |
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.getContentList({ contentType: 'post' }) Retrieves content where the model equals post . |
in | Filters content where the parameter matches any of the provided values.Example: client.getContentList({ language: { in: ['es', 'en'] } }) Retrieves content with fields matching es and/or en . |
Output
The getContentList
function returns a list of 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.
Example:
import { createClient, mapContentToModel } from '@content-island/api-client';
// ... client initialization code
const contentList = await client.getContentList({ 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' }, ], }, ]*/
// Your Post model defined in Content Islandinterface Post { id: string; title: string; body: string; order: number; language: 'es' | 'en';}
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.