Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cs-43660 live preview 1.0 and 2.0 in ts #144

Merged
merged 6 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 13 additions & 1 deletion src/lib/contentstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ export * as Utils from '@contentstack/utils';
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export function Stack(config: StackConfig): StackClass {
const defaultConfig = {
let defaultConfig = {
defaultHostname: 'cdn.contentstack.io',
headers: {} as AxiosRequestHeaders,
params: {} as any,
live_preview: {} as any
};

if (config.live_preview?.enable === true) {
if (config.live_preview?.management_token != null && config.live_preview?.preview_token == null) {
config.host = 'api.contentstack.io'
config.live_preview.host = config.host
} else if (config.live_preview?.preview_token != null && config.live_preview?.management_token == null) {
config.host = 'rest-preview.contentstack.com'
config.live_preview.host = config.host
}
} else config.host = defaultConfig.defaultHostname
defaultConfig.live_preview = config.live_preview

defaultConfig.defaultHostname = getHost(config.region, config.host);

if (config.apiKey) {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/stack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { StackConfig, SyncStack, SyncType } from './types';
import { StackConfig, SyncStack, SyncType, LivePreviewQuery } from './types';
import { AxiosInstance } from '@contentstack/core';
import { Asset } from './asset';
import { AssetQuery } from './asset-query';
Expand Down Expand Up @@ -141,4 +141,12 @@ export class Stack {
async sync(params: SyncType | SyncStack = {}, recursive = false) {
return await synchronization(this._client, params, recursive);
}

livePreviewQuery(query: LivePreviewQuery) {
if (this.config.live_preview) {
this.config.live_preview.live_preview = query.live_preview || 'init';
this.config.live_preview.contentTypeUid = query.contentTypeUid;
this.config.live_preview.entryUid = query.entryUid
}
}
}
17 changes: 17 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface StackConfig extends HttpClientParams {
plugins?: any[];
logHandler?: (level: string, data: any) => void;
cacheOptions?: CacheOptions;
live_preview?: LivePreview;
}
export interface CacheOptions extends PersistanceStoreOptions {
policy: Policy;
Expand Down Expand Up @@ -254,3 +255,19 @@ export interface FindResponse<T> {
global_fields?: T[];
count?: number
}

export interface LivePreviewQuery {
live_preview: string
contentTypeUid: string
entryUid?: any;
}

export type LivePreview = {
live_preview?: string;
contentTypeUid?: string;
entryUid?: any;
host?: string;
enable: boolean;
management_token?: string;
preview_token?: string;
}
165 changes: 165 additions & 0 deletions test/api/live-preview.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import * as contentstack from '../../src/lib/contentstack';
import { TEntry } from './types';
import dotenv from 'dotenv';

dotenv.config();

const apiKey = process.env.API_KEY as string
const deliveryToken = process.env.DELIVERY_TOKEN as string
const environment = process.env.ENVIRONMENT as string

describe('Live preview tests', () => {
test('should check for values initialized', () => {
const stack = contentstack.Stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
});
const livePreviewObject = stack.config.live_preview;
expect(livePreviewObject).toBeUndefined();
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is enabled and management token is provided', () => {
const stack = contentstack.Stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
live_preview: {
enable: true,
management_token: 'management_token'
}
})
const livePreviewObject = stack.config.live_preview
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('api.contentstack.io');
});

test('should check host when live preview is disabled and management token is provided', () => {
const stack = contentstack.Stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
live_preview: {
enable: false,
management_token: 'management_token'
}
})
const livePreviewObject = stack.config.live_preview
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).not.toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is enabled and preview token is provided', () => {
const stack = contentstack.Stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
live_preview: {
enable: true,
preview_token: 'preview_token'
}
})
const livePreviewObject = stack.config.live_preview
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('rest-preview.contentstack.com');
});

test('should check host when live preview is disabled and preview token is provided', () => {
const stack = contentstack.Stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
live_preview: {
enable: false,
preview_token: 'preview_token'
}
})
const livePreviewObject = stack.config.live_preview
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).not.toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('cdn.contentstack.io');
});
});

describe('Live preview query Entry API tests', () => {
it('should check for entry is when live preview is enabled with managemenet token', async () => {
const stack = contentstack.Stack({
apiKey: process.env.API_KEY as string,
deliveryToken: process.env.DELIVERY_TOKEN as string,
environment: process.env.ENVIRONMENT as string,
live_preview: {
enable: true,
management_token: 'management_token'
}
})
stack.livePreviewQuery({
contentTypeUid: 'contentTypeUid',
live_preview: 'ser',
})
const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch<TEntry>();
expect(result).toBeDefined();
expect(result._version).toBeDefined();
expect(result.locale).toEqual('en-us');
expect(result.uid).toBeDefined();
expect(result.created_by).toBeDefined();
expect(result.updated_by).toBeDefined();
});

it('should check for entry is when live preview is disabled with managemenet token', async () => {
const stack = contentstack.Stack({
apiKey: process.env.API_KEY as string,
deliveryToken: process.env.DELIVERY_TOKEN as string,
environment: process.env.ENVIRONMENT as string,
live_preview: {
enable: false,
management_token: 'management_token'
}
})
stack.livePreviewQuery({
contentTypeUid: 'contentTypeUid',
live_preview: 'ser',
})
const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch<TEntry>();
expect(result).toBeDefined();
expect(result._version).toBeDefined();
expect(result.locale).toEqual('en-us');
expect(result.uid).toBeDefined();
expect(result.created_by).toBeDefined();
expect(result.updated_by).toBeDefined();
});

it('should check for entry is when live preview is disabled with preview token', async () => {
const stack = contentstack.Stack({
apiKey: process.env.API_KEY as string,
deliveryToken: process.env.DELIVERY_TOKEN as string,
environment: process.env.ENVIRONMENT as string,
live_preview: {
enable: false,
preview_token: 'preview_token'
}
})
stack.livePreviewQuery({
contentTypeUid: 'contentTypeUid',
live_preview: 'ser',
})
const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch<TEntry>();
expect(result).toBeDefined();
expect(result._version).toBeDefined();
expect(result.locale).toEqual('en-us');
expect(result.uid).toBeDefined();
expect(result.created_by).toBeDefined();
expect(result.updated_by).toBeDefined();
});
})
87 changes: 87 additions & 0 deletions test/unit/live-preview.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import * as Contentstack from '../../src/lib/contentstack';

describe('Live preview tests', () => {

test('should check for values initialized', () => {
const stack = Contentstack.Stack({
apiKey: 'apiKey',
deliveryToken: 'deliveryToken',
environment: 'environment',
});
const livePreviewObject = stack.config.live_preview;
expect(livePreviewObject).toBeUndefined();
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is enabled and management token is provided', () => {
const stack = Contentstack.Stack({
apiKey: 'apiKey',
deliveryToken: 'deliveryToken',
environment: 'environment',
live_preview: {
enable: true,
management_token: 'management_token'
}
})
const livePreviewObject = stack.config.live_preview
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('api.contentstack.io');
});

test('should check host when live preview is disabled and management token is provided', () => {
const stack = Contentstack.Stack({
apiKey: 'apiKey',
deliveryToken: 'deliveryToken',
environment: 'environment',
live_preview: {
enable: false,
management_token: 'management_token'
}
})
const livePreviewObject = stack.config.live_preview
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).not.toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is enabled and preview token is provided', () => {
const stack = Contentstack.Stack({
apiKey: 'apiKey',
deliveryToken: 'deliveryToken',
environment: 'environment',
live_preview: {
enable: true,
preview_token: 'preview_token'
}
})
const livePreviewObject = stack.config.live_preview
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('rest-preview.contentstack.com');
});

test('should check host when live preview is disabled and preview token is provided', () => {
const stack = Contentstack.Stack({
apiKey: 'apiKey',
deliveryToken: 'deliveryToken',
environment: 'environment',
live_preview: {
enable: false,
preview_token: 'preview_token'
}
})
const livePreviewObject = stack.config.live_preview
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).not.toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('cdn.contentstack.io');
});
});