Skip to content

Commit

Permalink
Merge pull request #38 from contentstack/feat/Live-Preview-Implementa…
Browse files Browse the repository at this point in the history
…tion

feat: live preview configured
  • Loading branch information
nadeem-cs authored Aug 7, 2024
2 parents 12230c6 + c5a46df commit 2c66941
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fileignoreconfig:
- filename: test/utils/mocks.ts
checksum: a1cb4b1890a584f1facd30f2a0974c97a66f91417022be79d00516338e244227
- filename: package-lock.json
checksum: 6c9beae17fc270641d855026896e49a1458a6b3a026df30265c51420d364d18a
checksum: 96574fb5fc78856631105fb10913a630466662907e7f8e7bebb5f0d566259b9d
- filename: test/typescript/taxonomy.test.ts
checksum: e4bdf633e147fd60d929d379f20c814eed5f68b11421d7b53ec8826e9142de37
- filename: src/core/modules/taxonomy.js
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Change log

### Version: 4.2.0
#### Date: August-07-2024
Feat: Live Preview configuration changes

### Version: 4.1.0
#### Date: August-08-2024
Feat: fetch asset by any field-uid other that asset-uid
Expand Down
71 changes: 61 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/delivery-sdk",
"version": "4.1.0",
"version": "4.2.0",
"type": "commonjs",
"main": "./dist/cjs/src/index.js",
"types": "./dist/types/src/index.d.ts",
Expand All @@ -20,7 +20,7 @@
"build:types": "node tools/cleanup types && tsc -p config/tsconfig.types.json"
},
"dependencies": {
"@contentstack/core": "^1.0.3",
"@contentstack/core": "^1.1.0",
"@contentstack/utils": "^1.3.8",
"@types/humps": "^2.0.6",
"axios": "^1.7.2",
Expand Down
15 changes: 3 additions & 12 deletions src/lib/contentstack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,8 @@ export function stack(config: StackConfig): StackClass {
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 = config.live_preview.host
} else if (config.live_preview?.preview_token != null && config.live_preview?.management_token == null) {
config.host = config.live_preview.host
}
} else {
defaultConfig.defaultHostname = config.host ? config.host : getHost(config.region, config.host);
config.host = config.host || defaultConfig.defaultHostname;
defaultConfig.live_preview = config.live_preview
}
defaultConfig.defaultHostname = config.host || getHost(config.region, config.host);
config.host = defaultConfig.defaultHostname;

if (config.apiKey) {
defaultConfig.headers.api_key = config.apiKey;
Expand Down Expand Up @@ -78,7 +69,7 @@ export function stack(config: StackConfig): StackClass {

defaultConfig.headers['X-User-Agent'] = 'contentstack-delivery-typescript-{{PLATFORM}}/' + version;

// return new Stack(httpClient(defaultConfig), config);

const client = httpClient(defaultConfig as any);

if (config.logHandler) client.defaults.logHandler = config.logHandler;
Expand Down
11 changes: 8 additions & 3 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class Stack {
constructor(client: AxiosInstance, config: StackConfig) {
this._client = client;
this.config = config;
this._client.stackConfig = this.config;
}

/**
Expand Down Expand Up @@ -144,9 +145,13 @@ export class Stack {

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
const livePreviewParams: any = {
...this.config.live_preview,
live_preview: query.live_preview || 'init',
contentTypeUid: query.contentTypeUid,
entryUid: query.entryUid
}
this._client.stackConfig.live_preview = livePreviewParams;
}
}
}
8 changes: 5 additions & 3 deletions test/api/contenttype-query.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// @ts-nocheck
import { ContentTypeQuery } from '../../src/lib/contenttype-query';
import { stackInstance } from '../utils/stack-instance';
import { TContentTypes } from './types';
import { TContentType, TContentTypes } from './types';

const stack = stackInstance();
describe('ContentTypeQuery API test cases', () => {
it('should check for content_types are defined', async () => {
const result = await makeContentTypeQuery().find<TContentTypes>();
it.only('should check for content_types are defined', async () => {
const result = await makeContentTypeQuery().find<TContentType>();

expect(result.content_types).toBeDefined();
expect(result.content_types[0]._version).toBeDefined();
expect(result.content_types[0].uid).toBeDefined();
Expand Down
4 changes: 2 additions & 2 deletions test/api/live-preview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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.io');
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is disabled and management token is provided', () => {
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('Live preview tests', () => {
expect(livePreviewObject).toHaveProperty('enable');
expect(livePreviewObject).toHaveProperty('host');
expect(livePreviewObject).not.toHaveProperty('preview');
expect(stack.config.host).toBe('rest-preview.contentstack.com');
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is disabled and preview token is provided', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/live-preview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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.io');
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is disabled and management token is provided', () => {
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('Live preview tests', () => {
expect(livePreviewObject).toHaveProperty('host');
expect(livePreviewObject).toHaveProperty('preview_token');
expect(livePreviewObject).not.toHaveProperty('management_token');
expect(stack.config.host).toBe('rest-preview.contentstack.com');
expect(stack.config.host).toBe('cdn.contentstack.io');
});

test('should check host when live preview is disabled and preview token is provided', () => {
Expand Down
5 changes: 5 additions & 0 deletions test/utils/stack-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ function stackInstance() {
apiKey: process.env.API_KEY || '',
deliveryToken: process.env.DELIVERY_TOKEN || '',
environment: process.env.ENVIRONMENT || '',
live_preview: {
enable: true,
preview_token: "abcda",
host: "xyz,contentstack.com",
}
};

return contentstack.stack(params);
Expand Down

0 comments on commit 2c66941

Please sign in to comment.