Stack
Stack
A stack is a repository or a container that holds all the entries/assets of your site. It allows multiple users to create, edit, approve, and publish their content within a single space.
The stack function initializes an instance of the Stack. To initialize a stack execute the following code:
import contentstack from '@contentstack/delivery-sdk'
const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });| Name | Type | Description |
|---|---|---|
| apiKey (required) | string | API key of the stack |
| deliveryToken (required) | string | Delivery token to retrieve data from the stack |
| environment (required) | string | Environment name where content is published |
| live_preview | LivePreview | The Live preview configuration for the Contentstack API |
| branch | string | Name of the branch to fetch data from |
| host | string | Sets the host of the API server (example: "dev.contentstack.com") |
| region | string | Region of the stack. You can choose from five regions: NA, EU, Azure NA, Azure EU, GCP NA, and GCP EU. |
| locale | string | Lets you specify which language to use as source content if the entry does not exist in the specified language. |
| cacheOptions.policy | string | Specifies the caching strategy. Accepts a string value from the Policy enum. |
| cacheOptions.storeType | string | Defines where the cache is stored. Accepts localStorage or memoryStorage as string values. |
| cacheOptions.maxAge | number | Sets the maximum age (in milliseconds) before the cache expires. |
| cacheOptions.serializer | function | Function to serialize data before storing it in the cache. |
| cacheOptions.deserializer | function | Function to deserialize data when retrieving it from the cache. |
| early_access | List<string> | Set early access headers |
| logHandler | function | Method to enable custom logging in the SDK |
| plugins | List<any> | Add custom plugins to the SDK |
LivePreviewConfig
Configuration settings to enable live preview functionality and fetch real-time content data.
| Name | Type | Description |
|---|---|---|
| enable | boolean | Specifies whether to enable the live preview feature. |
| host | string | Specifies the host domain used to retrieve live preview content. |
| preview_token | string | Token required to fetch live preview content from the stack. |
Plugins
When creating custom plugins, through this request, you can pass the details of your custom plugins. This facilitates their utilization in subsequent requests when retrieving details.
To initializing a stack with plugins, refer to the code snippet below:
// custom class for plugin
class CrossStackPlugin {
onRequest (request) {
// add request modifications
return request
}
async onResponse (request, response, data) {
// add response modifications here
return response
}
}
const Stack = Contentstack.stack({
api_key,
delivery_token,
environment,
plugins: [
new CrossStackPlugin(),
]
});Asset
The Asset method by default creates an object for all assets of a stack. To retrieve a single asset, specify its UID.
| Name | Type | Description |
|---|---|---|
| assetUid | string | UID of the asset |
Example:
const asset = stack.asset(); // For collection of asset
// OR
const asset = stack.asset('assetUid'); // For a single asset with uid 'assetUid'ContentType
The ContentType method retrieves all the content types of a stack. To retrieve a single contenttype, specify its UID.
| Name | Type | Description |
|---|---|---|
| contentTypeUid | string | UID of the content type |
Example:
const contentType = stack.contentType(); // For collection of contentType
// OR
const contentType = stack.contentType('contentTypeUid'); // For a single contentType with uid 'contentTypeUid'setLocale
The setLocale method sets the locale of the API server.
| Name | Type | Description |
|---|---|---|
| locale (required) | string | Enter the locale code |
Example:
stack.setLocale('en-155');sync
The sync method syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates.
| Name | Type | Description |
|---|---|---|
| params (required) | ISyncType | ISyncStack | An object that supports ‘locale’, ‘start_date’, ‘content_type_uid’, and ‘type’ queries |
| recursive (required) | boolean | Specifies if the sync should be recursive |
Example:
- For initializing sync:
Stack.sync();
- For initializing sync with entries of a specific locale:
Stack.sync({ 'locale': 'en-us'}); - For initializing sync with entries published after a specific date:
Stack.sync({ 'start_date': '2018-10-22'}); - For initializing sync with entries of a specific content type:
Stack.sync({ 'content_type_uid': 'session'}); For initializing sync with a specific type of content:
Stack.sync({ 'type': 'entry_published'}); //Use the type parameter to get a specific type of content. Supports 'asset_published', 'entry_published', 'asset_unpublished', 'entry_unpublished', 'asset_deleted', 'entry_deleted', 'content_type_deleted'- For fetching the next batch of entries using pagination token:
Stack.sync({'pagination_token': '<page_tkn>'}); - For performing subsequent sync after initial sync:
Stack.sync({'sync_token': '<sync_tkn>'});