-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontentstack.ts
More file actions
133 lines (115 loc) · 4.29 KB
/
contentstack.ts
File metadata and controls
133 lines (115 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { httpClient, retryRequestHandler, retryResponseErrorHandler, retryResponseHandler } from '@contentstack/core';
import { AxiosRequestHeaders } from 'axios';
import { handleRequest } from './cache';
import { Stack as StackClass } from './stack';
import { Policy, StackConfig } from './types';
import * as Utility from './utils';
let version = '{{VERSION}}';
/**
* @method stack
* @memberof Contentstack
* @description Creates a stack instance
* @param {StackConfig} config - config object for stack with apiKey, deliveryToken and environment as required fields
*
* @example
* import contentstack from '@contentstack/delivery-sdk'
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* @example
* import contentstack from '@contentstack/delivery-sdk'
* const stack = contentstack.stack({
* apiKey: "apiKey",
* deliveryToken: "deliveryToken",
* environment: "environment",
* region:"region",
* locale:"locale",
* cacheOptions: {
* policy: Policy.CACHE_THEN_NETWORK,
* storeType: 'localStorage'
* }
* }
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export function stack(config: StackConfig): StackClass {
let defaultConfig = {
defaultHostname: 'cdn.contentstack.io',
headers: {} as AxiosRequestHeaders,
params: {} as any,
live_preview: {} as any,
port: config.port as number,
};
defaultConfig.defaultHostname = config.host || Utility.getHost(config.region, config.host);
config.host = defaultConfig.defaultHostname;
if (config.apiKey) {
defaultConfig.headers.api_key = config.apiKey;
} else {
throw new Error('API key for Stack is required.');
}
if (config.deliveryToken) {
defaultConfig.headers.access_token = config.deliveryToken;
} else {
throw new Error('Delivery token for Stack is required.');
}
if (config.environment) {
defaultConfig.params.environment = config.environment;
} else {
throw new Error('Environment for Stack is required');
}
if (config.locale) {
defaultConfig.params.locale = config.locale;
}
if (config.live_preview) {
if (Utility.isBrowser()) {
const params = new URL(document.location.toString()).searchParams;
if (params.has('live_preview')) {
config.live_preview.live_preview = params.get('live_preview') || config.live_preview.live_preview;
}
if (params.has('release_id')) {
defaultConfig.headers['release_id'] = params.get('release_id');
}
if (params.has('preview_timestamp')) {
defaultConfig.headers['preview_timestamp'] = params.get('preview_timestamp');
}
}
}
if (config.branch) {
defaultConfig.headers.branch = config.branch;
}
if (config.early_access) {
defaultConfig.headers['x-header-ea'] = config.early_access.join(',');
}
defaultConfig.headers['X-User-Agent'] = 'contentstack-delivery-typescript/' + version;
const client = httpClient(defaultConfig as any);
if (config.logHandler) client.defaults.logHandler = config.logHandler;
if (config.cacheOptions && config.cacheOptions.policy !== Policy.IGNORE_CACHE) {
const defaultAdapter = client.defaults.adapter;
client.defaults.adapter = (adapterConfig: any) => {
return new Promise(async (resolve, reject) => {
if (config.cacheOptions)
await handleRequest(config.cacheOptions, config.apiKey, defaultAdapter, resolve, reject, adapterConfig);
});
};
}
// Retry policy handlers
const errorHandler = (error: any) => {
return retryResponseErrorHandler(error, config, client);
};
client.interceptors.request.use(retryRequestHandler);
client.interceptors.response.use(retryResponseHandler, errorHandler);
if (config.plugins) {
client.interceptors.request.use((reqConfig: any): any => {
if (config && config.plugins)
config.plugins.forEach((pluginInstance) => {
reqConfig = pluginInstance.onRequest(reqConfig);
});
return reqConfig;
});
client.interceptors.response.use((response: any) => {
if (config && config.plugins)
config.plugins.forEach((pluginInstance) => {
response = pluginInstance.onResponse(response.request, response, response.data);
});
return response;
});
}
return new StackClass(client, config);
}