Contentstack

View as Markdown

Contentstack

Creates an instance of `Contentstack`.

NameTypeDescription
CachePolicy Contentstack.CachePolicy

CachePolicy contains different cache policies constants.

Stack

NameTypeDescription
api_key (required)string

Stack API key

delivery_tokenstring

Stack Delivery token.

environmentstring

Stack Environment name.

regionContentstackRegion | string

DB region for Stack. You can choose from five regions namely, NA, EU, Azure NA, Azure EU, and GCP NA.

branchstring

Name of the branch you want to fetch data from

live_previewobject

Live preview configuration.

fetchOptions.debugboolean

Optional, to enable debug logs set to true.

Default: false
fetchOptions.logHandlerfunction

A log handler function to process given log messages & errors.

fetchOptions.agentHttpProxyAgent
fetchOptions.timeoutnumber

Set timeout for the request.

fetchOptions.retryLimitnumber

The number of retries before failure.

Default: 5
fetchOptions.retryDelaynumber

The number of ms to use for operation retries.

Default: 300ms
fetchOptions.retryConditionfunction

A function to determine if the error can be retried.

Default: Retry is on status codes 408, 429
fetchOptions.retryDelayOptions.basenumber

The base number of milliseconds to use in the exponential backoff for operation retries.

fetchOptions.retryDelayOptions.customBackoffnumber

A custom function that accepts a retry count and error and returns the amount of time to delay in milliseconds.

Initialize Stack:

import Contentstack from 'contentstack';

const Stack = Contentstack.Stack({ api_key: 'api_key', delivery_token: 'delivery_token', environment: 'environment' });

To set the European, Azure North American, Azure European, or GCP North America region, refer to the code below:

import Contentstack from 'contentstack';

const Stack = new Contentstack({ 'api_key': "api_key", 'delivery_token': "delivery_token", 'environment': "environment", "region": Contentstack.Region.<<add_your_region>>})

For Setting the Branch for a Region.

import Contentstack from 'contentstack';

const Stack = Contentstack.Stack({ api_key: 'api_key', delivery_token: 'delivery_token', environment: 'environment', region: Contentstack.Region.<<add_your_region>>, host: '<<add_your_host_URL>>', branch: 'branch')

Proxy Configuration

const Stack = Contentstack.Stack({ 
    api_key: 'api_key', 
    delivery_token: 'delivery_token', 
    environment: 'environment',
    URL: URL,
    fetchOptions: {
       fetch: `proxy-url`
    }
});

Here are a few examples of how you can add a username and password to HttpProxyAgent.

  • You can pass it in the URI:
var proxyAgent = new HttpsProxyAgent('https://username:[email protected]');
  • You can set it in the auth option
var proxyOpts = url.parse('https://your-proxy.com');

proxyOpts.auth = 'username:password';

var proxyAgent = new HttpsProxyAgent(proxyOpts);
  • You can even set the HTTP header manually:
var proxyOpts = url.parse('https://your-proxy.com');

proxyOpts.headers = {

  'Proxy-Authentication': 'Basic ' + new Buffer('username:password').toString('base64')

};

var proxyAgent = new HttpsProxyAgent(proxyOpts);

Plugins

When creating custom plugins, through this request, you can pass the detailss of your custom plugins. This facilitates their utilization in subsequent requests when retrieving details.

// custom class for plugin

class CrossStackPlugin {

  onRequest (stack, request) {

    // request modifications

    return request

  }

  async onResponse (stack, request, response, data) {

    // response modifications here

    return response

  }

}


const Stack = Contentstack.Stack({

  api_key,

  delivery_token,

  environment,

  plugins: [

    new CrossStackPlugin(),

    new Livepreview()

  ]

});