getContentListSize
The getContentListSize
function retrieves the total number of content items in a project. You can use it to get the overall count of content items or apply specific filters to narrow down the results. This is particularly useful for counting items of a specific model (e.g., post) or for performing more detailed queries based on parameters like language, IDs, or custom field values.
For instance, if you want to know how many content items exist in a model called post, this function will provide the exact count.
Example:
// Using the previously created clientconst size = await client.getContentListSize({ contentType: 'post' });console.log(size);
Interface
The getContentListSize
function optionally accepts a ContentListSizeQueryParams
object and returns a promise that resolves to the size of the content list.
export interface ApiClient { getContentListSize: <Model>(queryParam?: ContentListSizeQueryParams<Model>) => Promise<number>; // ... 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 ContentListSizeQueryParams { 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.getContentListSize({ id: { in: ['1', '2', '3'] } }) |
contentType | The content model you want to retrieve. Example: client.getContentListSize({ contentType: 'post' }); |
language | The language of the content to retrieve. This filter ensures only fields in the selected language are returned. Example: client.getContentListSize({ language: 'es' }); |
fields.${string} | Allows filtering by specific field values. Examples: client.getContentListSize({ 'fields.slug': 'my-post' } }) client.getContentListSize({ '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.getContentListSize({ contentType: 'post' }) Retrieves content where the model equals post . |
in | Filters content where the parameter matches any of the provided values.Example: client.getContentListSize({ language: { in: ['es', 'en'] } }) Retrieves content with fields matching es and/or en . |
Output
The getContentListSize
function returns a promise that resolves to a number representing the size of the content list that matches the provided query parameters.
Example:
const size = await client.getContenListSize({ contentType: 'post' });console.log(size); // 42 Example output: total number of posts in the project
As you can see, the output is a number representing the total count of content items that match the specified filters.