response.data.item.elements.headline.value
.
In a more complicated scenario, this approach becomes cumbersome. It forces you to remember the JSON structure of your content items and the codenames of your content elements.
To make this easier, use a Delivery SDK to map the retrieved content items to their strongly typed models.
body_text
becomes property bodyText
.// Tip: Find more about JS/TS SDKs at https://kontent.ai/learn/javascript
import { IContentItem, Elements } from '@kontent-ai/delivery-sdk';
/**
* Generated by '@kontent-ai/model-generator@5.2.0'
* Note: Element comments were omitted for brevity.
*/
export type Homepage = IContentItem<{
headline: Elements.TextElement;
body_text: Elements.RichTextElement;
picture: Elements.AssetsElement;
}>;
npm i @kontent-ai/model-generator -g
kontent-generate --environmentId=<YOUR_ENVIRONMENT_ID> --apiKey=<YOUR_MANAGEMENT_API_KEY>
// Tip: Find more about JS/TS SDKs at https://kontent.ai/learn/javascript
import { createDeliveryClient } from '@kontent-ai/delivery-sdk';
import { Homepage } from './models/homepage';
// Initializes the Delivery client and registers your model in type resolvers
const deliveryClient = createDeliveryClient({
environmentId: '8d20758c-d74c-4f59-ae04-ee928c0816b7',
});
var homepage: Homepage;
// Gets an item by codename from Kontent.ai and maps it to a strongly typed model
const response = await deliveryClient.item<Homepage>('hello_caas_world')
.toPromise();
homepage = response.data.item;