Skip to content

Commit ca228cc

Browse files
Merge pull request #139 from contentstack/fix/cs-43765-reapply-live-preview1
Fix/cs 43765 reapply live preview1
2 parents 46b0e59 + 427aa04 commit ca228cc

File tree

5 files changed

+37
-177
lines changed

5 files changed

+37
-177
lines changed

config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const config = {
1313
},
1414
live_preview: {
1515
enable: false,
16-
host: 'api.contentstack.io'
16+
host: 'rest-preview.contentstack.com'
1717
}
1818
};
1919

index.d.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,21 @@ export interface ContentTypeCollection {
7272
count?: number
7373
}
7474

75-
export interface LivePreview {
76-
host: string
77-
management_token: string
75+
export type LivePreview = {
76+
host?: string
7877
enable: boolean
78+
} & (LivePreivewConfigWithManagementToken | LivePreviewConfigWithPreviewToken)
79+
80+
export interface LivePreivewConfigWithManagementToken {
81+
/**
82+
* @deprecated Please use `preview_token` instead to enable live preview.
83+
* The `management_token` will be removed in future releases.
84+
*/
85+
management_token: string;
86+
}
87+
88+
export interface LivePreviewConfigWithPreviewToken {
89+
preview_token: string;
7990
}
8091

8192
export interface LivePreviewQuery {

src/core/lib/utils.js

Lines changed: 19 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,21 @@ export function sendRequest(queryObject, options) {
233233
queryObject.requestParams.body = merge(queryObject.requestParams.body, cloneQueryObj);
234234

235235
if (queryObject.live_preview && queryObject.live_preview.enable === true && queryObject.live_preview.live_preview && queryObject.live_preview.live_preview !== "init") {
236-
if(queryObject.live_preview.content_type_uid === queryObject.content_type_uid) {
237-
queryObject.requestParams.body = merge(queryObject.requestParams.body, {live_preview: queryObject.live_preview.live_preview || "init"});
238-
cachePolicy = 2; // network else cache
239-
if(queryObject.requestParams.body['environment']) {
240-
delete queryObject.requestParams.body['environment'];
241-
}
242-
if(queryObject.requestParams.headers['access_token'])
243-
delete queryObject.requestParams.headers['access_token'];
244-
245-
queryObject.requestParams.headers['authorization'] = queryObject.live_preview.management_token
246-
} else if(queryObject.live_preview.live_preview) {
247-
cachePolicy = 1; // cache then network
236+
queryObject.requestParams.body = merge(queryObject.requestParams.body, {live_preview: queryObject.live_preview.live_preview || "init"});
237+
cachePolicy = 2; // network else cache
238+
if(queryObject.requestParams.body['environment']) {
239+
delete queryObject.requestParams.body['environment'];
240+
}
241+
if(queryObject.requestParams.headers['access_token'])
242+
delete queryObject.requestParams.headers['access_token'];
243+
delete queryObject.requestParams.headers['authorization'];
244+
delete queryObject.requestParams.headers['preview_token'];
245+
246+
if (queryObject.live_preview.preview_token) {
247+
queryObject.requestParams.headers['preview_token'] = queryObject.live_preview.preview_token;
248+
queryObject.requestParams.headers['live_preview'] = queryObject.live_preview.live_preview;
249+
} else if (queryObject.live_preview.management_token) {
250+
queryObject.requestParams.headers['authorization'] = queryObject.live_preview.management_token;
248251
}
249252
}
250253
}
@@ -347,56 +350,10 @@ export function sendRequest(queryObject, options) {
347350
if (err || !_data) {
348351
callback(true, resolve, reject);
349352
} else {
350-
try {
351-
352-
const doesQueryRequestForReferences =
353-
queryObject._query &&
354-
Array.isArray(
355-
queryObject._query.include
356-
) &&
357-
queryObject._query.include.length > 0;
358-
359-
if (doesQueryRequestForReferences) {
360-
const referencesToBeResolved =
361-
queryObject._query.include;
362-
363-
const referencesToBeResolvedMap =
364-
generateReferenceMap(
365-
referencesToBeResolved
366-
);
367-
368-
if (isSingle) {
369-
await updateLivePreviewReferenceEntry(
370-
referencesToBeResolvedMap,
371-
_data.entry,
372-
queryObject,
373-
options
374-
);
375-
} else {
376-
await Promise.all(_data.entries.map(async (entry) => {
377-
await updateLivePreviewReferenceEntry(
378-
referencesToBeResolvedMap,
379-
entry,
380-
queryObject,
381-
options
382-
383-
);
384-
}))
385-
}
386-
387-
}
388-
} catch (error) {
389-
}
390-
try {
391-
if (!tojson)
392-
_data =
393-
resultWrapper(_data);
394-
return resolve(
395-
spreadResult(_data)
396-
);
397-
} catch (e) {
398-
return reject(e);
353+
if (!tojson) {
354+
_data = resultWrapper(_data);
399355
}
356+
return resolve(spreadResult(_data));
400357
}
401358
} catch (e) {
402359
return reject(e);
@@ -405,7 +362,7 @@ export function sendRequest(queryObject, options) {
405362
}else {
406363
callback(true, resolve, reject);
407364
}
408-
365+
409366
});
410367
case 2:
411368
case 0:
@@ -448,111 +405,3 @@ export function sendRequest(queryObject, options) {
448405
})
449406
}
450407
}
451-
452-
453-
function generateReferenceMap (references) {
454-
const map = {};
455-
456-
function mapSingleReference(reference) {
457-
reference = reference.replace(/[\[]/gm, ".").replace(/[\]]/gm, ""); //to accept [index]
458-
let keys = reference.split("."),
459-
last = keys.pop();
460-
461-
keys.reduce(function (o, k) {
462-
return (o[k] = o[k] || {});
463-
}, map)[last] = { };
464-
}
465-
466-
references.forEach(function (reference) {
467-
mapSingleReference(reference);
468-
});
469-
470-
return map;
471-
};
472-
473-
async function updateLivePreviewReferenceEntry(referenceMap, entry, stack, options, handlerOptions) {
474-
const {live_preview:livePreview, requestParams} = stack;
475-
const { content_type_uid: livePreviewContentTypeUid, management_token } = livePreview;
476-
477-
478-
async function findReferenceAndFetchEntry(referenceMap, entry, setReference) {
479-
if ( typeof entry === "undefined")
480-
return;
481-
if (Array.isArray(entry)) {
482-
await Promise.all(entry.map((subEntry, i) => {
483-
const setReference = (val) => {
484-
entry[i] = val;
485-
}
486-
return findReferenceAndFetchEntry(referenceMap, subEntry, setReference)
487-
}));
488-
} else {
489-
if (entry._content_type_uid === livePreviewContentTypeUid) {
490-
491-
try {
492-
stack.requestParams = JSON.parse(JSON.stringify(requestParams));
493-
494-
const includeReference = getIncludeParamForReference(referenceMap)
495-
stack.requestParams.body.include = includeReference
496-
stack.requestParams.body.live_preview = livePreview.live_preview
497-
stack.requestParams.body.content_type_uid = livePreviewContentTypeUid
498-
499-
const livePreviewUrl = livePreview.host.match(
500-
/^((http[s]?):(\/\/)?)?(.+)$/
501-
);
502-
503-
const livePreviewHost =
504-
(livePreviewUrl[1] || "https://") + livePreviewUrl[4];
505-
const entryUid = entry.uid;
506-
507-
const url = `${livePreviewHost}/v3/content_types/${entry._content_type_uid}/entries/${entryUid}`;
508-
stack.requestParams.url = url
509-
stack.requestParams.method = "GET"
510-
511-
delete stack.requestParams.headers.access_token
512-
stack.requestParams.headers.authorization = management_token
513-
514-
const data = await Request(stack, options);
515-
data.entry._content_type_uid = livePreviewContentTypeUid;
516-
data.entry.uid = entryUid;
517-
setReference(data.entry);
518-
519-
} catch (err) {
520-
console.log("errror", err)
521-
}
522-
} else {
523-
524-
await Promise.all(Object.entries(referenceMap).map(async function ([
525-
currentRefFieldKey,
526-
subReferenceMap,
527-
]) {
528-
// recurse
529-
const setRef = (val) => {
530-
entry[currentRefFieldKey] = val;
531-
}
532-
await findReferenceAndFetchEntry(subReferenceMap, entry[currentRefFieldKey], () => {});
533-
}));
534-
}
535-
}
536-
}
537-
538-
await findReferenceAndFetchEntry(referenceMap, entry, () => {});
539-
540-
}
541-
542-
function getIncludeParamForReference(referenceMap) {
543-
const newRefences = [];
544-
545-
function buildParamStringRecursively(currentReferenceMap, includeParamTillNow) {
546-
if (Object.keys(currentReferenceMap).length === 0) {
547-
newRefences.push(includeParamTillNow.substring(1));
548-
} else {
549-
550-
Object.entries(currentReferenceMap).forEach(([referenceFieldKey, subReferenceMap]) => {
551-
buildParamStringRecursively(subReferenceMap, [includeParamTillNow, referenceFieldKey].join("."));
552-
});
553-
}
554-
}
555-
556-
buildParamStringRecursively(referenceMap, "");
557-
return newRefences.filter((currentReference) => currentReference !== "");
558-
}

src/core/modules/entry.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export default class Entry {
359359
*/
360360
fetch(fetchOptions) {
361361
var host = this.config.host + ':' + this.config.port
362-
if(this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
362+
if(this.live_preview && this.live_preview.enable === true && this.live_preview.live_preview && this.live_preview.live_preview !== "init" ) {
363363
host = this.live_preview.host
364364
}
365365
if (this.entry_uid) {

src/core/modules/query.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ export default class Query extends Entry {
771771
*/
772772
find(fetchOptions) {
773773
var host = this.config.host + ':' + this.config.port
774-
if (this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
774+
if (this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.live_preview && this.live_preview.live_preview !== "init") {
775775
host = this.live_preview.host;
776776
}
777777
const baseURL = this.config.protocol + "://" + host + '/' + this.config.version
@@ -808,7 +808,7 @@ export default class Query extends Entry {
808808
*/
809809
findOne() {
810810
let host = this.config.protocol + "://" + this.config.host + ':' + this.config.port + '/' + this.config.version
811-
if(this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.content_type_uid === this.content_type_uid ) {
811+
if(this.type && this.type !== 'asset' && this.live_preview && this.live_preview.enable === true && this.live_preview.live_preview && this.live_preview.live_preview !== "init" ) {
812812
host = this.config.protocol + "://" + this.live_preview.host + '/' + this.config.version
813813
}
814814
const url = getRequestUrl(this.type, this.config, this.content_type_uid, host)

0 commit comments

Comments
 (0)