Skip to content

mapContentToModel

The mapContentToModel function converts content to its corresponding model defined in Content Island.

This function is useful for accessing data in a simpler and more direct way, without having to deal with the internal content structure.

Example:

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

Interface

function mapContentToModel<Model extends BaseModel<Language>, Language = Model['language']>(
content: Content,
language?: Language
): Model;

Parameters

This function accepts the following parameters:

  • content: The content to map to its corresponding model.
interface Content {
id: string;
contentType: { id: string; name: string };
lastUpdate: string;
fields: Field[];
}
  • language: Filters and maps only the fields that match this language. If not provided, the first available language is used.

Output

The mapContentToModel function returns the content mapped to its corresponding model. This object has the same structure as the model defined in Content Island, which makes it easier to use in your application.

Example filtering by language

import { createClient, mapContentToModel } from '@content-island/api-client';
// ... client initialization code
const content = client.getContent('1', { contentType: 'post' }); // Fetches post with id=1 in multiple languages (es, en)
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' },
{ id: '444', name: 'title', value: 'Hello World', type: 'short-text', isArray: false, language: 'en' },
{ id: '555', name: 'body', value: 'This is the body of the post in markdown.', type: 'long-text', isArray: false, language: 'en' },
{ id: '666', name: 'order', value: 1, type: 'number', isArray: false, language: 'en' },
],
}
*/
// Your Post model defined in Content Island
interface Post {
id: string;
title: string;
body: string;
order: number;
language: 'es' | 'en';
}
const post = mapContentToModel<Post>(content); // Returns fields in the first detected language (es)
console.log(post);
const spanishPost = mapContentToModel<Post>(content, 'es'); // Returns fields in Spanish
console.log(spanishPost);
const englishPost = mapContentToModel<Post>(content, 'en'); // Returns fields in English
console.log(englishPost);