Migrate to TypeScript Delivery SDK v5
Note This guide explains how to migrate from v4 to v5 of the TypeScript Delivery SDK.
To migrate your project from v4 to v5 of the TypeScript Delivery SDK, follow the steps below.
-
Upgrade the SDK to the Latest Version
To ensure your project is using the latest version of the SDK, run the following command:
npm i @contentstack/delivery-sdk@latest
-
Install the Utils SDK
The Utils package is no longer bundled with the Delivery SDK by default. To use any utilities, install the Utils SDK separately:
npm i @contentstack/utils@latest
Once installed, you can use it as before. For example:
import contentstack, { StackConfig } from '@contentstack/delivery-sdk'; import * as ContentstackUtils from '@contentstack/utils'; const credentials: StackConfig = { apiKey: '<CONTENTSTACK_API_KEY>', deliveryToken: '<CONTENTSTACK_DELIVERY_TOKEN>', environment: '<CONTENTSTACK_ENVIRONMENT>', }; const Stack = contentstack.stack(credentials); const result = await Stack .contentType('<CONTENT_TYPE_UID>') .entry('<ENTRY_UID>') .includeEmbeddedItems() .fetch<BlogPostEntry>(); ContentstackUtils.jsonToHTML({ entry: result, paths: ['rte_fieldUid', 'group.rteFieldUID'], renderOption, }); -
Configure Caching (optional)
To use caching, install the Persistence plugin separately:
npm i @contentstack/persistence-plugin@latest
Then, configure caching by passing cacheOptions when initializing the SDK:
import contentstack, { StackConfig, Policy } from '@contentstack/delivery-sdk'; import PersistenceStore from '@contentstack/persistence-plugin'; const Stack = contentstack.stack({ apiKey: '<CONTENTSTACK_API_KEY>', deliveryToken: '<CONTENTSTACK_DELIVERY_TOKEN>', environment: '<CONTENTSTACK_ENVIRONMENT>', cacheOptions: { policy: Policy.CACHE_THEN_NETWORK, persistenceStore: new PersistenceStore({ storeType: 'localStorage', maxAge: <age>, // Default is 24 hours serializer: <serializer_fn>, // Default is JSON.stringify deserializer: <deserializer_fn>, // Default is JSON.parse }), }, });Note If you do not use caching or set the policy to Policy.IGNORE_CACHE, you can skip installing the Persistence plugin and omitting the cacheOptions configuration.