Contentstack
Contentstack
Creates an instance of `Contentstack`.
| Name | Type | Description |
|---|---|---|
| CachePolicy | Contentstack.CachePolicy | CachePolicy contains different cache policies constants. |
Stack
| Name | Type | Description |
|---|---|---|
| api_key (required) | string | Stack API key |
| delivery_token | string | Stack Delivery token. |
| environment | string | Stack Environment name. |
| region | ContentstackRegion | string | DB region for Stack. You can choose from five regions namely, NA, EU, Azure NA, Azure EU, and GCP NA. |
| branch | string | Name of the branch you want to fetch data from |
| live_preview | object | Live preview configuration. |
| fetchOptions.debug | boolean | Optional, to enable debug logs set to true. |
| fetchOptions.logHandler | function | A log handler function to process given log messages & errors. |
| fetchOptions.agent | HttpProxyAgent | |
| fetchOptions.timeout | number | Set timeout for the request. |
| fetchOptions.retryLimit | number | The number of retries before failure. |
| fetchOptions.retryDelay | number | The number of ms to use for operation retries. |
| fetchOptions.retryCondition | function | A function to determine if the error can be retried. |
| fetchOptions.retryDelayOptions.base | number | The base number of milliseconds to use in the exponential backoff for operation retries. |
| fetchOptions.retryDelayOptions.customBackoff | number | 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()
]
});