Skip to content

Commit 9f1c636

Browse files
committed
SDK plugin support added
1 parent 9cc13f1 commit 9f1c636

File tree

4 files changed

+51
-22
lines changed

4 files changed

+51
-22
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export class Stack {
100100
/**
101101
* @deprecated since version 3.15.0
102102
*/
103-
constructor(api_key: string, delivery_token: string, environment_name: string, region?: Region, fetchOptions?: FetchOptions, live_preview?: LivePreview);
103+
constructor(api_key: string, delivery_token: string, environment_name: string, region?: Region, fetchOptions?: FetchOptions, live_preview?: LivePreview, plugins?: Plugins[]);
104104

105105
environment: string;
106106
cachePolicy: CachePolicy;

src/core/lib/request.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import fetch from "runtime/http.js";
55
let version = '{{VERSION}}';
66
let environment,
77
api_key;
8-
export default function Request(options, fetchOptions) {
8+
export default function Request(stack, fetchOptions) {
9+
let requestParams = stack.requestParams;
910
return new Promise(function(resolve, reject) {
1011
let queryParams;
1112
let serialize = function(obj, prefix) {
@@ -28,21 +29,19 @@ export default function Request(options, fetchOptions) {
2829
return str.join("&");
2930
};
3031

31-
let url = options.url,
32-
headers = options.headers;
32+
3333

3434
// setting headers
35-
headers['Content-Type'] = 'application/json; charset=UTF-8';
36-
headers['X-User-Agent'] = 'contentstack-{{PLATFORM}}/' + version;
35+
requestParams.headers['Content-Type'] = 'application/json; charset=UTF-8';
36+
requestParams.headers['X-User-Agent'] = 'contentstack-{{PLATFORM}}/' + version;
3737

38-
if (options.body && typeof options.body === 'object') {
39-
delete options.body._method;
40-
if (typeof options.body.query === "object" && Object.keys(options.body.query).length === 0) delete options.body.query;
41-
queryParams = serialize(options.body);
38+
if (requestParams.body && typeof requestParams.body === 'object') {
39+
delete requestParams.body._method;
40+
if (typeof requestParams.body.query === "object" && Object.keys(requestParams.body.query).length === 0) delete requestParams.body.query;
41+
queryParams = serialize(requestParams.body);
4242
}
4343

44-
return fetchRetry(url + '?' + queryParams,
45-
headers,
44+
return fetchRetry(stack, queryParams,
4645
fetchOptions,
4746
resolve,
4847
reject,
@@ -58,7 +57,10 @@ function wait(retryDelay) {
5857
});
5958
}
6059

61-
function fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay = 300, retryLimit = 5) {
60+
function fetchRetry(stack, queryParams, fetchOptions, resolve, reject, retryDelay = 300, retryLimit = 5) {
61+
let requestParams = stack.requestParams,
62+
url = requestParams.url + '?' + queryParams,
63+
headers = requestParams.headers
6264
var option = Utils.mergeDeep({
6365
method: 'GET',
6466
headers: headers,
@@ -83,20 +85,38 @@ function fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay = 30
8385
}
8486
wait(msDelay)
8587
.then(() => {
86-
return fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay, retryLimit)
88+
return fetchRetry(stack, queryParams, fetchOptions, resolve, reject, retryDelay, retryLimit)
8789
})
8890
.catch(() => {
89-
return fetchRetry(url, headers, fetchOptions, resolve, reject, retryDelay, retryLimit)
91+
return fetchRetry(stack, queryParams, fetchOptions, resolve, reject, retryDelay, retryLimit)
9092
})
9193
}
9294
}
95+
9396
if (fetchOptions.debug) fetchOptions.logHandler('info', { url: url, option: option});
97+
98+
var plugins = stack.plugins;
99+
if (plugins && plugins != undefined)
100+
plugins.forEach(pluginObj => {
101+
pluginObj.onRequest(stack, {url, option})
102+
});
103+
94104
fetch(url, option)
95-
.then(function(response) {
105+
.then( function(response) {
106+
107+
96108
if (fetchOptions.debug) fetchOptions.logHandler('info', response);
97-
let data = response.json();
109+
let data = response.json();
110+
98111
if (response.ok && response.status === 200) {
99-
resolve(data);
112+
data.then(json => {
113+
if (plugins) json.plugins = [];
114+
for (let index = 0; index < plugins.length; index++) {
115+
json = plugins[index].onResponse(stack, {url, option}, response, json)
116+
}
117+
resolve(json);
118+
})
119+
100120
} else {
101121
data.then((json) => {
102122
if (fetchOptions.retryCondition && fetchOptions.retryCondition(response)) {

src/core/lib/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export function sendRequest(queryObject, options) {
265265

266266
let callback = function(continueFlag, resolve, reject) {
267267
if (continueFlag) {
268-
Request(queryObject.requestParams, options)
268+
Request(queryObject, options)
269269
.then(function(data) {
270270
try {
271271
self.entry_uid = self.asset_uid = self.tojson = self.queryCachePolicy = undefined;

src/core/stack.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ let errorRetry = [408, 429]
1818
* @param param.region - DB region for Stack.
1919
* @param param.branch - Name of the branch you want to fetch data from
2020
* @param param.live_preview - Live preview configuration.
21+
* @param param.plugins - List of plugins objects.
2122
* @param param.fetchOptions - Custom setting for the request.
2223
* @param param.fetchOptions.debug - This will enable debug log. Default is false
2324
* @param param.fetchOptions.timeout - Set timeout for the request.
@@ -65,7 +66,7 @@ export default class Stack {
6566
return
6667
}
6768
}
68-
};
69+
};
6970
this.config = Utils.mergeDeep({}, config)
7071

7172
if(stack_arguments[0].region && stack_arguments[0].region !== undefined && stack_arguments[0].region !== "us") {
@@ -75,6 +76,13 @@ export default class Stack {
7576
if (stack_arguments[0].fetchOptions && stack_arguments[0].fetchOptions !== undefined) {
7677
this.fetchOptions = Utils.mergeDeep(this.fetchOptions, stack_arguments[0].fetchOptions);
7778
}
79+
80+
if (stack_arguments[0].plugins && stack_arguments[0].plugins !== undefined) {
81+
this.plugins = []
82+
stack_arguments[0].plugins.forEach(pluginObj => {
83+
this.plugins.push(pluginObj)
84+
});
85+
}
7886

7987
this.cachePolicy = CacheProvider.policies.IGNORE_CACHE;
8088
this.provider = CacheProvider.providers('localstorage');
@@ -371,17 +379,18 @@ export default class Stack {
371379
* @instance
372380
*/
373381
fetch(fetchOptions) {
374-
let result = {
382+
this.requestParams = {
375383
method: 'POST',
376384
headers: this.headers,
385+
plugins: this.plugins,
377386
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types + this.content_type_uid,
378387
body: {
379388
_method: 'GET',
380389
environment: this.environment
381390
}
382391
};
383392
var options = Utils.mergeDeep(this.fetchOptions, fetchOptions);
384-
return Request(result, options);
393+
return Request(this, options);
385394
}
386395

387396
/**

0 commit comments

Comments
 (0)