Skip to content

Commit

Permalink
feat: Support new REST preview service
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Deepak-Kharah authored and abhinav-from-contentstack committed Feb 1, 2024
1 parent 46b0e59 commit c6b7c1e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const config = {
},
live_preview: {
enable: false,
host: 'api.contentstack.io'
host: 'rest-preview.contentstack.com'
}
};

Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export interface ContentTypeCollection {

export interface LivePreview {
host: string
management_token: string
preview_token?: string
management_token?: string
enable: boolean
}

Expand Down
7 changes: 4 additions & 3 deletions src/core/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c6b7c1e

Please sign in to comment.