Skip to content

Commit e09c48a

Browse files
Merge pull request #144 from contentstack/feat/cs-43660-live-preview-ts
cs-43660 live preview 1.0 and 2.0 in ts
2 parents 67096a3 + a198795 commit e09c48a

File tree

7 files changed

+294
-5
lines changed

7 files changed

+294
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.0.0-beta.2",
3+
"version": "4.0.0-beta.3",
44
"type": "commonjs",
55
"main": "./dist/cjs/src/index.js",
66
"types": "./dist/types/src/index.d.ts",

src/lib/contentstack.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,24 @@ export * as Utils from '@contentstack/utils';
3131
*/
3232
// eslint-disable-next-line @typescript-eslint/naming-convention
3333
export function Stack(config: StackConfig): StackClass {
34-
const defaultConfig = {
34+
let defaultConfig = {
3535
defaultHostname: 'cdn.contentstack.io',
3636
headers: {} as AxiosRequestHeaders,
3737
params: {} as any,
38+
live_preview: {} as any
3839
};
3940

41+
if (config.live_preview?.enable === true) {
42+
if (config.live_preview?.management_token != null && config.live_preview?.preview_token == null) {
43+
config.host = 'api.contentstack.io'
44+
config.live_preview.host = config.host
45+
} else if (config.live_preview?.preview_token != null && config.live_preview?.management_token == null) {
46+
config.host = 'rest-preview.contentstack.com'
47+
config.live_preview.host = config.host
48+
}
49+
} else config.host = defaultConfig.defaultHostname
50+
defaultConfig.live_preview = config.live_preview
51+
4052
defaultConfig.defaultHostname = getHost(config.region, config.host);
4153

4254
if (config.apiKey) {

src/lib/stack.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { StackConfig, SyncStack, SyncType } from './types';
1+
import { StackConfig, SyncStack, SyncType, LivePreviewQuery } from './types';
22
import { AxiosInstance } from '@contentstack/core';
33
import { Asset } from './asset';
44
import { AssetQuery } from './asset-query';
@@ -141,4 +141,12 @@ export class Stack {
141141
async sync(params: SyncType | SyncStack = {}, recursive = false) {
142142
return await synchronization(this._client, params, recursive);
143143
}
144+
145+
livePreviewQuery(query: LivePreviewQuery) {
146+
if (this.config.live_preview) {
147+
this.config.live_preview.live_preview = query.live_preview || 'init';
148+
this.config.live_preview.contentTypeUid = query.contentTypeUid;
149+
this.config.live_preview.entryUid = query.entryUid
150+
}
151+
}
144152
}

src/lib/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface StackConfig extends HttpClientParams {
2020
plugins?: any[];
2121
logHandler?: (level: string, data: any) => void;
2222
cacheOptions?: CacheOptions;
23+
live_preview?: LivePreview;
2324
}
2425
export interface CacheOptions extends PersistanceStoreOptions {
2526
policy: Policy;
@@ -254,3 +255,19 @@ export interface FindResponse<T> {
254255
global_fields?: T[];
255256
count?: number
256257
}
258+
259+
export interface LivePreviewQuery {
260+
live_preview: string
261+
contentTypeUid: string
262+
entryUid?: any;
263+
}
264+
265+
export type LivePreview = {
266+
live_preview?: string;
267+
contentTypeUid?: string;
268+
entryUid?: any;
269+
host?: string;
270+
enable: boolean;
271+
management_token?: string;
272+
preview_token?: string;
273+
}

test/api/live-preview.spec.ts

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import * as contentstack from '../../src/lib/contentstack';
2+
import { TEntry } from './types';
3+
import dotenv from 'dotenv';
4+
5+
dotenv.config();
6+
7+
const apiKey = process.env.API_KEY as string
8+
const deliveryToken = process.env.DELIVERY_TOKEN as string
9+
const environment = process.env.ENVIRONMENT as string
10+
11+
describe('Live preview tests', () => {
12+
test('should check for values initialized', () => {
13+
const stack = contentstack.Stack({
14+
apiKey: apiKey,
15+
deliveryToken: deliveryToken,
16+
environment: environment,
17+
});
18+
const livePreviewObject = stack.config.live_preview;
19+
expect(livePreviewObject).toBeUndefined();
20+
expect(stack.config.host).toBe('cdn.contentstack.io');
21+
});
22+
23+
test('should check host when live preview is enabled and management token is provided', () => {
24+
const stack = contentstack.Stack({
25+
apiKey: apiKey,
26+
deliveryToken: deliveryToken,
27+
environment: environment,
28+
live_preview: {
29+
enable: true,
30+
management_token: 'management_token'
31+
}
32+
})
33+
const livePreviewObject = stack.config.live_preview
34+
expect(livePreviewObject).not.toBeUndefined();
35+
expect(livePreviewObject).toHaveProperty('enable');
36+
expect(livePreviewObject).toHaveProperty('host');
37+
expect(livePreviewObject).not.toHaveProperty('preview');
38+
expect(stack.config.host).toBe('api.contentstack.io');
39+
});
40+
41+
test('should check host when live preview is disabled and management token is provided', () => {
42+
const stack = contentstack.Stack({
43+
apiKey: apiKey,
44+
deliveryToken: deliveryToken,
45+
environment: environment,
46+
live_preview: {
47+
enable: false,
48+
management_token: 'management_token'
49+
}
50+
})
51+
const livePreviewObject = stack.config.live_preview
52+
expect(livePreviewObject).not.toBeUndefined();
53+
expect(livePreviewObject).toHaveProperty('enable');
54+
expect(livePreviewObject).not.toHaveProperty('host');
55+
expect(livePreviewObject).not.toHaveProperty('preview');
56+
expect(stack.config.host).toBe('cdn.contentstack.io');
57+
});
58+
59+
test('should check host when live preview is enabled and preview token is provided', () => {
60+
const stack = contentstack.Stack({
61+
apiKey: apiKey,
62+
deliveryToken: deliveryToken,
63+
environment: environment,
64+
live_preview: {
65+
enable: true,
66+
preview_token: 'preview_token'
67+
}
68+
})
69+
const livePreviewObject = stack.config.live_preview
70+
expect(livePreviewObject).not.toBeUndefined();
71+
expect(livePreviewObject).toHaveProperty('enable');
72+
expect(livePreviewObject).toHaveProperty('host');
73+
expect(livePreviewObject).not.toHaveProperty('preview');
74+
expect(stack.config.host).toBe('rest-preview.contentstack.com');
75+
});
76+
77+
test('should check host when live preview is disabled and preview token is provided', () => {
78+
const stack = contentstack.Stack({
79+
apiKey: apiKey,
80+
deliveryToken: deliveryToken,
81+
environment: environment,
82+
live_preview: {
83+
enable: false,
84+
preview_token: 'preview_token'
85+
}
86+
})
87+
const livePreviewObject = stack.config.live_preview
88+
expect(livePreviewObject).not.toBeUndefined();
89+
expect(livePreviewObject).toHaveProperty('enable');
90+
expect(livePreviewObject).not.toHaveProperty('host');
91+
expect(livePreviewObject).not.toHaveProperty('preview');
92+
expect(stack.config.host).toBe('cdn.contentstack.io');
93+
});
94+
});
95+
96+
describe('Live preview query Entry API tests', () => {
97+
it('should check for entry is when live preview is enabled with managemenet token', async () => {
98+
const stack = contentstack.Stack({
99+
apiKey: process.env.API_KEY as string,
100+
deliveryToken: process.env.DELIVERY_TOKEN as string,
101+
environment: process.env.ENVIRONMENT as string,
102+
live_preview: {
103+
enable: true,
104+
management_token: 'management_token'
105+
}
106+
})
107+
stack.livePreviewQuery({
108+
contentTypeUid: 'contentTypeUid',
109+
live_preview: 'ser',
110+
})
111+
const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch<TEntry>();
112+
expect(result).toBeDefined();
113+
expect(result._version).toBeDefined();
114+
expect(result.locale).toEqual('en-us');
115+
expect(result.uid).toBeDefined();
116+
expect(result.created_by).toBeDefined();
117+
expect(result.updated_by).toBeDefined();
118+
});
119+
120+
it('should check for entry is when live preview is disabled with managemenet token', async () => {
121+
const stack = contentstack.Stack({
122+
apiKey: process.env.API_KEY as string,
123+
deliveryToken: process.env.DELIVERY_TOKEN as string,
124+
environment: process.env.ENVIRONMENT as string,
125+
live_preview: {
126+
enable: false,
127+
management_token: 'management_token'
128+
}
129+
})
130+
stack.livePreviewQuery({
131+
contentTypeUid: 'contentTypeUid',
132+
live_preview: 'ser',
133+
})
134+
const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch<TEntry>();
135+
expect(result).toBeDefined();
136+
expect(result._version).toBeDefined();
137+
expect(result.locale).toEqual('en-us');
138+
expect(result.uid).toBeDefined();
139+
expect(result.created_by).toBeDefined();
140+
expect(result.updated_by).toBeDefined();
141+
});
142+
143+
it('should check for entry is when live preview is disabled with preview token', async () => {
144+
const stack = contentstack.Stack({
145+
apiKey: process.env.API_KEY as string,
146+
deliveryToken: process.env.DELIVERY_TOKEN as string,
147+
environment: process.env.ENVIRONMENT as string,
148+
live_preview: {
149+
enable: false,
150+
preview_token: 'preview_token'
151+
}
152+
})
153+
stack.livePreviewQuery({
154+
contentTypeUid: 'contentTypeUid',
155+
live_preview: 'ser',
156+
})
157+
const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch<TEntry>();
158+
expect(result).toBeDefined();
159+
expect(result._version).toBeDefined();
160+
expect(result.locale).toEqual('en-us');
161+
expect(result.uid).toBeDefined();
162+
expect(result.created_by).toBeDefined();
163+
expect(result.updated_by).toBeDefined();
164+
});
165+
})

test/unit/live-preview.spec.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import * as Contentstack from '../../src/lib/contentstack';
2+
3+
describe('Live preview tests', () => {
4+
5+
test('should check for values initialized', () => {
6+
const stack = Contentstack.Stack({
7+
apiKey: 'apiKey',
8+
deliveryToken: 'deliveryToken',
9+
environment: 'environment',
10+
});
11+
const livePreviewObject = stack.config.live_preview;
12+
expect(livePreviewObject).toBeUndefined();
13+
expect(stack.config.host).toBe('cdn.contentstack.io');
14+
});
15+
16+
test('should check host when live preview is enabled and management token is provided', () => {
17+
const stack = Contentstack.Stack({
18+
apiKey: 'apiKey',
19+
deliveryToken: 'deliveryToken',
20+
environment: 'environment',
21+
live_preview: {
22+
enable: true,
23+
management_token: 'management_token'
24+
}
25+
})
26+
const livePreviewObject = stack.config.live_preview
27+
expect(livePreviewObject).not.toBeUndefined();
28+
expect(livePreviewObject).toHaveProperty('enable');
29+
expect(livePreviewObject).toHaveProperty('host');
30+
expect(livePreviewObject).not.toHaveProperty('preview');
31+
expect(stack.config.host).toBe('api.contentstack.io');
32+
});
33+
34+
test('should check host when live preview is disabled and management token is provided', () => {
35+
const stack = Contentstack.Stack({
36+
apiKey: 'apiKey',
37+
deliveryToken: 'deliveryToken',
38+
environment: 'environment',
39+
live_preview: {
40+
enable: false,
41+
management_token: 'management_token'
42+
}
43+
})
44+
const livePreviewObject = stack.config.live_preview
45+
expect(livePreviewObject).not.toBeUndefined();
46+
expect(livePreviewObject).toHaveProperty('enable');
47+
expect(livePreviewObject).not.toHaveProperty('host');
48+
expect(livePreviewObject).not.toHaveProperty('preview');
49+
expect(stack.config.host).toBe('cdn.contentstack.io');
50+
});
51+
52+
test('should check host when live preview is enabled and preview token is provided', () => {
53+
const stack = Contentstack.Stack({
54+
apiKey: 'apiKey',
55+
deliveryToken: 'deliveryToken',
56+
environment: 'environment',
57+
live_preview: {
58+
enable: true,
59+
preview_token: 'preview_token'
60+
}
61+
})
62+
const livePreviewObject = stack.config.live_preview
63+
expect(livePreviewObject).not.toBeUndefined();
64+
expect(livePreviewObject).toHaveProperty('enable');
65+
expect(livePreviewObject).toHaveProperty('host');
66+
expect(livePreviewObject).not.toHaveProperty('preview');
67+
expect(stack.config.host).toBe('rest-preview.contentstack.com');
68+
});
69+
70+
test('should check host when live preview is disabled and preview token is provided', () => {
71+
const stack = Contentstack.Stack({
72+
apiKey: 'apiKey',
73+
deliveryToken: 'deliveryToken',
74+
environment: 'environment',
75+
live_preview: {
76+
enable: false,
77+
preview_token: 'preview_token'
78+
}
79+
})
80+
const livePreviewObject = stack.config.live_preview
81+
expect(livePreviewObject).not.toBeUndefined();
82+
expect(livePreviewObject).toHaveProperty('enable');
83+
expect(livePreviewObject).not.toHaveProperty('host');
84+
expect(livePreviewObject).not.toHaveProperty('preview');
85+
expect(stack.config.host).toBe('cdn.contentstack.io');
86+
});
87+
});

0 commit comments

Comments
 (0)