Skip to content

Commit b381706

Browse files
committed
live preview update after plugin support added
1 parent 9f1c636 commit b381706

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

src/core/lib/request.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ function fetchRetry(stack, queryParams, fetchOptions, resolve, reject, retryDela
110110

111111
if (response.ok && response.status === 200) {
112112
data.then(json => {
113-
if (plugins) json.plugins = [];
114113
for (let index = 0; index < plugins.length; index++) {
115114
json = plugins[index].onResponse(stack, {url, option}, response, json)
116115
}

src/core/lib/utils.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -369,17 +369,15 @@ export function sendRequest(queryObject, options) {
369369
await updateLivePreviewReferenceEntry(
370370
referencesToBeResolvedMap,
371371
_data.entry,
372-
queryObject.live_preview,
373-
queryObject.requestParams,
372+
queryObject,
374373
options
375374
);
376375
} else {
377376
await Promise.all(_data.entries.map(async (entry) => {
378377
await updateLivePreviewReferenceEntry(
379378
referencesToBeResolvedMap,
380379
entry,
381-
queryObject.live_preview,
382-
queryObject.requestParams,
380+
queryObject,
383381
options
384382

385383
);
@@ -481,7 +479,8 @@ function generateReferenceMap (references) {
481479
return map;
482480
};
483481

484-
async function updateLivePreviewReferenceEntry(referenceMap, entry, livePreview, requestParams, options, handlerOptions) {
482+
async function updateLivePreviewReferenceEntry(referenceMap, entry, stack, options, handlerOptions) {
483+
const {livePreview, requestParams} = stack;
485484
const { content_type_uid: livePreviewContentTypeUid, management_token } = livePreview;
486485

487486

@@ -499,12 +498,12 @@ async function updateLivePreviewReferenceEntry(referenceMap, entry, livePreview,
499498
if (entry._content_type_uid === livePreviewContentTypeUid) {
500499

501500
try {
502-
const referenceRequestParam = JSON.parse(JSON.stringify(requestParams));
501+
stack.requestParams = JSON.parse(JSON.stringify(requestParams));
503502

504503
const includeReference = getIncludeParamForReference(referenceMap)
505-
referenceRequestParam.body.include = includeReference
506-
referenceRequestParam.body.live_preview = livePreview.live_preview
507-
referenceRequestParam.body.content_type_uid = livePreviewContentTypeUid
504+
stack.requestParams.body.include = includeReference
505+
stack.requestParams.body.live_preview = livePreview.live_preview
506+
stack.requestParams.body.content_type_uid = livePreviewContentTypeUid
508507

509508
const livePreviewUrl = livePreview.host.match(
510509
/^((http[s]?):(\/\/)?)?(.+)$/
@@ -515,13 +514,13 @@ async function updateLivePreviewReferenceEntry(referenceMap, entry, livePreview,
515514
const entryUid = entry.uid;
516515

517516
const url = `${livePreviewHost}/v3/content_types/${entry._content_type_uid}/entries/${entryUid}`;
518-
referenceRequestParam.url = url
519-
referenceRequestParam.method = "GET"
517+
stack.requestParams.url = url
518+
stack.requestParams.method = "GET"
520519

521-
delete referenceRequestParam.headers.access_token
522-
referenceRequestParam.headers.authorization = management_token
520+
delete stack.requestParams.headers.access_token
521+
stack.requestParams.headers.authorization = management_token
523522

524-
const data = await Request(referenceRequestParam, options);
523+
const data = await Request(stack, options);
525524
data.entry._content_type_uid = livePreviewContentTypeUid;
526525
data.entry.uid = entryUid;
527526
setReference(data.entry);

src/core/stack.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export default class Stack {
6868
}
6969
};
7070
this.config = Utils.mergeDeep({}, config)
71+
this.plugins = []
7172

7273
if(stack_arguments[0].region && stack_arguments[0].region !== undefined && stack_arguments[0].region !== "us") {
7374
this.config['host'] = stack_arguments[0].region+"-"+"cdn.contentstack.com";
@@ -78,7 +79,7 @@ export default class Stack {
7879
}
7980

8081
if (stack_arguments[0].plugins && stack_arguments[0].plugins !== undefined) {
81-
this.plugins = []
82+
8283
stack_arguments[0].plugins.forEach(pluginObj => {
8384
this.plugins.push(pluginObj)
8485
});
@@ -96,6 +97,7 @@ export default class Stack {
9697
};
9798
if (typeof stack_arguments[0].live_preview == "object") {
9899
this.live_preview = Utils.mergeDeep(this.config.live_preview , stack_arguments[0].live_preview)
100+
console.log(this.live_preview);
99101
}
100102
if (typeof stack_arguments[0].branch === "string" && stack_arguments[0].branch !== undefined) {
101103
this.headers.branch = stack_arguments[0].branch
@@ -461,7 +463,7 @@ export default class Stack {
461463
* @instance
462464
*/
463465
getLastActivities() {
464-
let query = {
466+
this.requestParams = {
465467
method: 'POST',
466468
headers: this.headers,
467469
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types,
@@ -471,7 +473,7 @@ export default class Stack {
471473
environment: this.environment
472474
}
473475
};
474-
return Request(query, this.fetchOptions);
476+
return Request(this, this.fetchOptions);
475477
}
476478

477479
/**
@@ -491,7 +493,7 @@ export default class Stack {
491493
* @instance
492494
*/
493495
getContentTypes(param = {}) {
494-
let query = {
496+
this.requestParams = {
495497
method: 'POST',
496498
headers: this.headers,
497499
url: this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version + this.config.urls.content_types,
@@ -502,10 +504,10 @@ export default class Stack {
502504
};
503505
if(param) {
504506
for( var key in param) {
505-
query.body[key] = param[key]
507+
this.requestParams.body[key] = param[key]
506508
}
507509
}
508-
return Request(query, this.fetchOptions);
510+
return Request(this, this.fetchOptions);
509511
}
510512

511513
/**

0 commit comments

Comments
 (0)