Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/core/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,15 @@ export function sendRequest(queryObject, options) {
}
if(queryObject.requestParams.headers['access_token'])
delete queryObject.requestParams.headers['access_token'];
delete queryObject.requestParams.headers['authorization'];
delete queryObject.requestParams.headers['preview_token'];

if (queryObject.live_preview.preview_token) {
queryObject.requestParams.headers['preview_token'] = queryObject.live_preview.preview_token;
} else if (queryObject.live_preview.management_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 @@ -510,7 +517,15 @@ async function updateLivePreviewReferenceEntry(referenceMap, entry, stack, optio
stack.requestParams.method = "GET"

delete stack.requestParams.headers.access_token
stack.requestParams.headers.authorization = preview_token || management_token;
delete stack.requestParams.headers.preview_token
delete stack.requestParams.headers.access_token

if (preview_token) {
stack.requestParams.headers.preview_token = preview_token;
} else if (management_token) {
stack.requestParams.headers.authorization =
management_token;
}

const data = await Request(stack, options);
data.entry._content_type_uid = livePreviewContentTypeUid;
Expand Down
66 changes: 58 additions & 8 deletions test/typescript/live-preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ describe("Live preview realtime URL switch", () => {

expect(tester).toBeCalledTimes(1);
expect(tester.mock.calls[0][0].url).toContain("cdn.contentstack.io");
expect(tester.mock.calls[0][0].option.headers.access_token).toBe("delivery_token");

expect(tester.mock.calls[0][0].option.headers.access_token).toBe(
"delivery_token"
);
});

test("should make the call to the Preview server if the live preview matches", async () => {
test("should make the call with preview token to the Preview server if the live preview matches", async () => {
const tester = jest.fn();
const stack = Contentstack.Stack({
api_key: "api_key",
Expand All @@ -130,12 +131,57 @@ describe("Live preview realtime URL switch", () => {
live_preview: {
enable: true,
preview_token: "preview_token",
host: "preview-api.contentstack.io",
},
fetchOptions: {
retryLimit: 0,
retryDelay: 0,
timeout: 0,
},

plugins: [new Plugin(tester)],
});

stack.livePreviewQuery({
content_type_uid: "he",
live_preview: "ser",
});

try {
stack.ContentType("he").Entry("ser").fetch().catch();
} catch (e) {}

expect(tester).toBeCalledTimes(1);
expect(tester.mock.calls[0][0].url).toContain(
"preview-api.contentstack.io"
);

expect(tester.mock.calls[0][0].option.headers.preview_token).toBe(
"preview_token"
);
expect(
tester.mock.calls[0][0].option.headers.authorization
).toBeUndefined();

//@ts-expect-error
delete stack.live_preview.preview_token;
});

test("should make the call with authorization to the Preview server if the live preview matches", async () => {
const tester = jest.fn();
const stack = Contentstack.Stack({
api_key: "api_key",
delivery_token: "delivery_token",
environment: "environment",
live_preview: {
enable: true,
management_token: "management_token",
host: "api.contentstack.io",
},
fetchOptions: {
retryLimit: 0,
retryDelay: 0,
timeout: 0
retryDelay: 0,
timeout: 0,
},

plugins: [new Plugin(tester)],
Expand All @@ -147,13 +193,17 @@ describe("Live preview realtime URL switch", () => {
});

try {
stack.ContentType("he").Entry("ser").fetch().catch();
stack.ContentType("he").Entry("ser").fetch().catch();
} catch (e) {}

expect(tester).toBeCalledTimes(1);
expect(tester.mock.calls[0][0].url).toContain("api.contentstack.io");

expect(tester.mock.calls[0][0].option.headers.authorization).toBe("preview_token");

expect(tester.mock.calls[0][0].option.headers.authorization).toBe(
"management_token"
);
expect(
tester.mock.calls[0][0].option.headers.preview_token
).toBeUndefined();
});
});