From 6c5b2aef005c62fa4378bf8fc319dcfc7c1e85c4 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 8 Feb 2024 15:53:25 +0530 Subject: [PATCH 1/6] feat: support for lp 1.0 and 2.0 --- src/lib/contentstack.ts | 14 +++++++++++++- src/lib/types.ts | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lib/contentstack.ts b/src/lib/contentstack.ts index 18ed959b..366d7e87 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.com' + 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/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 From 1020e9c604e9bf906103ff20aab81630c3606033 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Thu, 8 Feb 2024 15:54:08 +0530 Subject: [PATCH 2/6] test: :white_check_mark: unit and api test cases for lp 1.0 and 2.0 --- test/api/live-preview.spec.ts | 93 ++++++++++++++++++++++++++++++++++ test/unit/live-preview.spec.ts | 87 +++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 test/api/live-preview.spec.ts create mode 100644 test/unit/live-preview.spec.ts diff --git a/test/api/live-preview.spec.ts b/test/api/live-preview.spec.ts new file mode 100644 index 00000000..fad1613f --- /dev/null +++ b/test/api/live-preview.spec.ts @@ -0,0 +1,93 @@ +import * as contentstack from '../../src/lib/contentstack'; +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.com'); + }); + + 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'); + }); +}); diff --git a/test/unit/live-preview.spec.ts b/test/unit/live-preview.spec.ts new file mode 100644 index 00000000..9879c99f --- /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.com'); + }); + + 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'); + }); +}); From eb66a8999101e8dc5d1a8dca7d742d73670d4746 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 12 Feb 2024 14:08:07 +0530 Subject: [PATCH 3/6] feat: added live preview query function to stack --- src/lib/contentstack.ts | 2 +- src/lib/stack.ts | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib/contentstack.ts b/src/lib/contentstack.ts index 366d7e87..060a8822 100644 --- a/src/lib/contentstack.ts +++ b/src/lib/contentstack.ts @@ -40,7 +40,7 @@ export function Stack(config: StackConfig): StackClass { if (config.live_preview?.enable === true) { if (config.live_preview?.management_token != null && config.live_preview?.preview_token == null) { - config.host = 'api.contentstack.com' + 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' diff --git a/src/lib/stack.ts b/src/lib/stack.ts index dbcfd811..d4b93f64 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'; @@ -15,6 +15,7 @@ export class Stack { constructor(client: AxiosInstance, config: StackConfig) { this._client = client; this.config = config; + this.config.live_preview = config.live_preview } /** @@ -141,4 +142,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 + } + } } From e462c4ca490cef2c28c6205cc6314d2c3d5a2f77 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 12 Feb 2024 14:09:01 +0530 Subject: [PATCH 4/6] test: added test case for live preview query function --- test/api/live-preview.spec.ts | 98 +++++++++++++++++++++++++++++----- test/unit/live-preview.spec.ts | 2 +- 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/test/api/live-preview.spec.ts b/test/api/live-preview.spec.ts index fad1613f..88a371a1 100644 --- a/test/api/live-preview.spec.ts +++ b/test/api/live-preview.spec.ts @@ -1,4 +1,5 @@ import * as contentstack from '../../src/lib/contentstack'; +import { TEntry } from './types'; import dotenv from 'dotenv'; dotenv.config(); @@ -25,16 +26,16 @@ describe('Live preview tests', () => { deliveryToken: deliveryToken, environment: environment, live_preview: { - enable: true, - management_token: 'management_token' + 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.com'); + expect(stack.config.host).toBe('api.contentstack.io'); }); test('should check host when live preview is disabled and management token is provided', () => { @@ -43,10 +44,10 @@ describe('Live preview tests', () => { deliveryToken: deliveryToken, environment: environment, live_preview: { - enable: false, - management_token: 'management_token' + enable: false, + management_token: 'management_token' } - }) + }) const livePreviewObject = stack.config.live_preview expect(livePreviewObject).not.toBeUndefined(); expect(livePreviewObject).toHaveProperty('enable'); @@ -61,10 +62,10 @@ describe('Live preview tests', () => { deliveryToken: deliveryToken, environment: environment, live_preview: { - enable: true, - preview_token: 'preview_token' + enable: true, + preview_token: 'preview_token' } - }) + }) const livePreviewObject = stack.config.live_preview expect(livePreviewObject).not.toBeUndefined(); expect(livePreviewObject).toHaveProperty('enable'); @@ -79,10 +80,10 @@ describe('Live preview tests', () => { deliveryToken: deliveryToken, environment: environment, live_preview: { - enable: false, - preview_token: 'preview_token' + enable: false, + preview_token: 'preview_token' } - }) + }) const livePreviewObject = stack.config.live_preview expect(livePreviewObject).not.toBeUndefined(); expect(livePreviewObject).toHaveProperty('enable'); @@ -91,3 +92,74 @@ describe('Live preview tests', () => { 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: 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 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 index 9879c99f..0ac5a436 100644 --- a/test/unit/live-preview.spec.ts +++ b/test/unit/live-preview.spec.ts @@ -28,7 +28,7 @@ describe('Live preview tests', () => { expect(livePreviewObject).toHaveProperty('enable'); expect(livePreviewObject).toHaveProperty('host'); expect(livePreviewObject).not.toHaveProperty('preview'); - expect(stack.config.host).toBe('api.contentstack.com'); + expect(stack.config.host).toBe('api.contentstack.io'); }); test('should check host when live preview is disabled and management token is provided', () => { From 802653076838f88fb1d93c03b4ff7dc48a17e1a0 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 12 Feb 2024 14:58:35 +0530 Subject: [PATCH 5/6] fix: removed redundancy of config.live_preview --- src/lib/stack.ts | 1 - test/api/live-preview.spec.ts | 14 +++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/lib/stack.ts b/src/lib/stack.ts index d4b93f64..b7728522 100644 --- a/src/lib/stack.ts +++ b/src/lib/stack.ts @@ -15,7 +15,6 @@ export class Stack { constructor(client: AxiosInstance, config: StackConfig) { this._client = client; this.config = config; - this.config.live_preview = config.live_preview } /** diff --git a/test/api/live-preview.spec.ts b/test/api/live-preview.spec.ts index 88a371a1..26082c2b 100644 --- a/test/api/live-preview.spec.ts +++ b/test/api/live-preview.spec.ts @@ -105,8 +105,8 @@ describe('Live preview query Entry API tests', () => { } }) stack.livePreviewQuery({ - contentTypeUid: "contentTypeUid", - live_preview: "ser", + contentTypeUid: 'contentTypeUid', + live_preview: 'ser', }) const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch(); expect(result).toBeDefined(); @@ -123,13 +123,13 @@ describe('Live preview query Entry API tests', () => { deliveryToken: process.env.DELIVERY_TOKEN as string, environment: process.env.ENVIRONMENT as string, live_preview: { - enable: true, + enable: false, management_token: 'management_token' } }) stack.livePreviewQuery({ - contentTypeUid: "contentTypeUid", - live_preview: "ser", + contentTypeUid: 'contentTypeUid', + live_preview: 'ser', }) const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch(); expect(result).toBeDefined(); @@ -151,8 +151,8 @@ describe('Live preview query Entry API tests', () => { } }) stack.livePreviewQuery({ - contentTypeUid: "contentTypeUid", - live_preview: "ser", + contentTypeUid: 'contentTypeUid', + live_preview: 'ser', }) const result = await stack.ContentType('contentTypeUid').Entry('entryUid').fetch(); expect(result).toBeDefined(); From 5a7880902041b56593f62402951a1cc5b4668110 Mon Sep 17 00:00:00 2001 From: harshithad0703 Date: Mon, 12 Feb 2024 16:17:09 +0530 Subject: [PATCH 6/6] chore: updated release version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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",