From c6b7c1e9e04af8db207fcadc20f8964a483dbc94 Mon Sep 17 00:00:00 2001 From: Deepak Kharah <42672761+Deepak-Kharah@users.noreply.github.com> Date: Mon, 23 Oct 2023 12:47:12 +0530 Subject: [PATCH] feat: Support new REST preview service The REST preview service introduces its own preview token and API endpoint for Live preview. This values will be used to fetch entry data for live preview. Replaced api.contentstack.io to rest-preview.contentstack.com and replaced to fixes: https://contentstack.atlassian.net/browse/VC-311 --- config.js | 2 +- index.d.ts | 3 ++- src/core/lib/utils.js | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/config.js b/config.js index edabbd85..f223550f 100755 --- a/config.js +++ b/config.js @@ -13,7 +13,7 @@ const config = { }, live_preview: { enable: false, - host: 'api.contentstack.io' + host: 'rest-preview.contentstack.com' } }; diff --git a/index.d.ts b/index.d.ts index 4e709118..c93dca39 100644 --- a/index.d.ts +++ b/index.d.ts @@ -74,7 +74,8 @@ export interface ContentTypeCollection { export interface LivePreview { host: string - management_token: string + preview_token?: string + management_token?: string enable: boolean } diff --git a/src/core/lib/utils.js b/src/core/lib/utils.js index 7714e358..5fe22827 100755 --- a/src/core/lib/utils.js +++ b/src/core/lib/utils.js @@ -242,7 +242,7 @@ export function sendRequest(queryObject, options) { if(queryObject.requestParams.headers['access_token']) delete queryObject.requestParams.headers['access_token']; - queryObject.requestParams.headers['authorization'] = queryObject.live_preview.management_token + queryObject.requestParams.headers['authorization'] = queryObject.live_preview.preview_token || queryObject.live_preview.management_token; } else if(queryObject.live_preview.live_preview) { cachePolicy = 1; // cache then network } @@ -472,7 +472,8 @@ function generateReferenceMap (references) { async function updateLivePreviewReferenceEntry(referenceMap, entry, stack, options, handlerOptions) { const {live_preview:livePreview, requestParams} = stack; - const { content_type_uid: livePreviewContentTypeUid, management_token } = livePreview; + const { content_type_uid: livePreviewContentTypeUid, preview_token } = + livePreview; async function findReferenceAndFetchEntry(referenceMap, entry, setReference) { @@ -509,7 +510,7 @@ async function updateLivePreviewReferenceEntry(referenceMap, entry, stack, optio stack.requestParams.method = "GET" delete stack.requestParams.headers.access_token - stack.requestParams.headers.authorization = management_token + stack.requestParams.headers.authorization = preview_token; const data = await Request(stack, options); data.entry._content_type_uid = livePreviewContentTypeUid;