getContentList
The getContentList
function retrieves a list of contents 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 postList = await client.getContentList({ contentType: 'post' });console.log(postList);
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: <Model>(queryParam?: QueryParams<Model>) => Promise<Model[]>; // ... 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'}
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' }); |
fields.${string} | Allows filtering by specific field values. Examples: client.getContentList({ 'fields.slug': 'my-post' } }) client.getContentList({ '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.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 contents (filtered or not). This list is an array of objects representing the content values that an users define in their project in Content Island.
Example:
import { createClient } 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 postList = await client.getContentList<Post>({ contentType: 'post' });console.log(postList);/* [ { id: '1', title: 'Hola Mundo', body: 'Este es el cuerpo del post en markdown.', order: 1, language: 'es', }, { id: '2', title: 'Segundo Post', body: 'Este es el segundo post en markdown.', order: 2, language: 'es', } ]*/
As you can see, the content is already mapped to its corresponding model and you can access the fields directly without manually mapping them.
getContentList
uses the mapContentToModel function under the hood to map the content to its model, visit that section for more information on how content mapping works.