diff --git a/package-lock.json b/package-lock.json index 61b67bda..9806c357 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/delivery-sdk", - "version": "4.0.0-beta.2", + "version": "4.0.0-beta.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@contentstack/delivery-sdk", - "version": "4.0.0-beta.2", + "version": "4.0.0-beta.3", "dependencies": { "@contentstack/core": "^1.0.1", "@contentstack/utils": "^1.3.1", diff --git a/package.json b/package.json index d2e9f09c..4a11640d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/delivery-sdk", - "version": "4.0.0-beta.2", + "version": "4.0.0-beta.3", "type": "commonjs", "main": "./dist/cjs/src/index.js", "types": "./dist/types/src/index.d.ts", diff --git a/src/lib/contentstack.ts b/src/lib/contentstack.ts index 18ed959b..060a8822 100644 --- a/src/lib/contentstack.ts +++ b/src/lib/contentstack.ts @@ -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) { diff --git a/src/lib/stack.ts b/src/lib/stack.ts index dbcfd811..b7728522 100644 --- a/src/lib/stack.ts +++ b/src/lib/stack.ts @@ -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'; @@ -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 + } + } } diff --git a/src/lib/types.ts b/src/lib/types.ts index c6b7d2d1..5edbd766 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -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; @@ -254,3 +255,19 @@ export interface FindResponse { 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; +} \ No newline at end of file diff --git a/test/api/live-preview.spec.ts b/test/api/live-preview.spec.ts new file mode 100644 index 00000000..26082c2b --- /dev/null +++ b/test/api/live-preview.spec.ts @@ -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(); + 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(); + 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(); + 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(); + }); +}) \ No newline at end of file diff --git a/test/unit/live-preview.spec.ts b/test/unit/live-preview.spec.ts new file mode 100644 index 00000000..0ac5a436 --- /dev/null +++ b/test/unit/live-preview.spec.ts @@ -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'); + }); +});