Stack

Stack

new Stack(param) → {Stack}

Initialize an instance of ‘Stack’
Parameters:
Name Description
param Stack configuration.
Properties
Name Description
api_key Stack API Key.
delivery_token Stack Delivery token.
environment Stack Environment name.
region DB region for Stack.
branch Name of the branch you want to fetch data from
live_preview Live preview configuration.
fetchOptions Custom setting for the request.
Properties
Name Description
timeout Set timeout for the request.
retryLimit The number of retries before failure. Default is 5
retryDelay The number of ms to use for operation retries. Default is 300ms
retryCondition A function to determine if the error can be retried. Default retry is on status codes 408, 429.
retryDelayOptions.base The base number of milliseconds to use in the exponential backoff for operation retries.
retryDelayOptions.customBackoff A custom function that accepts a retry count and error and returns the amount of time to delay in milliseconds.
Example
var Stack = Contentstack.Stack({
     'api_key':'api_key',
     'delivery_token':'delivery_token',
     'environment':'environment_name',
     'region': 'us',
     'fetchOptions': {
      
     }
});

Methods

setPort(port) → {Stack}

Sets the port of the host
Parameters:
Name Description
port Port Number

setProtocol(protocol) → {Stack}

Sets the protocol for the host
Parameters:
Name Description
protocol http/https protocol

setHost(host) → {Stack}

Sets the host of the API server
Parameters:
Name Description
host valid ip or host

setCachePolicy(keyopt) → {Stack}

Allows you to set cache policies
Parameters:
Name Attributes Default Description
key <optional>
ONLY_NETWORK Cache policy to be applied on Stack or Query.
Example
Stack.setCachePolicy(Contentstack.CachePolicy.IGNORE_CACHE)
Stack.setCachePolicy(Contentstack.CachePolicy.ONLY_NETWORK)
Stack.setCachePolicy(Contentstack.CachePolicy.CACHE_ELSE_NETWORK)
Stack.setCachePolicy(Contentstack.CachePolicy.NETWORK_ELSE_CACHE)
Stack.setCachePolicy(Contentstack.CachePolicy.CACHE_THEN_NETWORK)

setCacheProvider() → {Stack}

Allows you to set an object of the cache provider
Example
Stack
     .setCacheProvider({
         get: function (key, callback) {
             // custom logic
         },
         set: function (key, value, callback) {
             // custom logic
         }
     });

clearByQuery() → {Stack}

'clearByQuery' function to clear the query from the cache.
Example
Stack.clearQuery(query, callback);

clearByContentType() → {Stack}

'clearByContentType' function to clear the query from the cache by specified content type.
Example
Stack.clearByContentType(content_type_uid, callback);
Stack.clearByContentType(content_type_uid, language_uid, callback);

clearAll() → {Stack}

'clearAll' function to clear all the queries from cache.
Example
Stack.clearAll(callback);

getCacheProvider() → {object}

Returns the currently set object of 'CacheProvider'
Example
Stack.getCacheProvider();

ContentType(content_type_uidopt) → {Stack}

Set the content type of which you want to retrieve the entries
Parameters:
Name Attributes Description
content_type_uid <optional>
uid of the existing content type
Example
let data = Stack.ContentType('blog').Query().toJSON().find()
     data
     .then(function(result) {
          // 'result' content the list of entries of particular content type blog.       
     }, function(error) {
          // error function
     })

Assets(uid) → {Assets}

Retrieves all assets of a stack by default. To retrieve a single asset, specify its UID.
Parameters:
Name Description
uid uid of the asset
Examples
// Retrieves all assets
let data = Stack.Assets().Query().toJSON().find()
     data
     .then(function(result) {
         // All the asset with limit of 100
         // Use skip and limit functions to paginate
         // ‘result’ will display all assets present in stack       
     }, function(error) {
          // error function
     })
let data = Stack.Assets('asset_uid').toJSON().fetch()
     data
       .then(function(result) {
          // ‘result’ is a single asset object of specified uid       
     }, function(error) {
          // error function
     })

Query() → {Query}

An initializer is responsible for creating Query object.Provides support for all search queries

getLastActivities() → {promise}

getLastActivities get all the ContentTypes whose last activity updated.
Examples
Stack.getLastActivities()
let data = Stack.getLastActivities().toJSON().fetch()
     data
     .then(function(result) {
          // 'result' is list of contentTypes whose last activity updated.       
     }, function(error) {
          // error function
     })

getContentTypes(param) → {promise}

This method returns comprehensive information of all the content types of a particular stack in your account.
Parameters:
Name Description
param Query on contentTypes
Example
let data = Stack.getContentTypes({"include_global_field_schema": true})
     data
     .then(function(result) {
          // 'result' is list of contentTypes.       
     }, function(error) {
          // error function
     })

sync(params) → {promise}

Syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates
Parameters:
Name Description
params params is an object that supports ‘locale’, ‘start_date’, ‘content_type_uid’, and ‘type’ queries.
Examples
Stack.sync({'init': true})        // For initializing sync
Stack.sync({'init': true, 'locale': 'en-us'})     //For initializing sync with entries of a specific locale
Stack.sync({'init': true, 'start_date': '2018-10-22'})    //For initializing sync with entries published after a specific date
Stack.sync({'init': true, 'content_type_uid': 'session'})   //For initializing sync with entries of a specific content type
Stack.sync({'init': true, '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'.
Stack.sync({'pagination_token': '<page_tkn>'})    // For fetching the next batch of entries using pagination token
Stack.sync({'sync_token': '<sync_tkn>'})    // For performing subsequent sync after initial sync

imageTransform(url, params) → {string}

Performs transformations on images of mentioned url based on transformation parameters
Parameters:
Name Description
url Image url on which transformations need to be applied.
params Object with transformation parameters
Examples
Stack.imageTransform(imageURL, {height: 100, width: 200, disable: "upscale"});
Stack.imageTransform(imageURL, {crop: "150,100"});
Stack.imageTransform(imageURL, {format: "png", crop: "150,100"});