Skip to content

Commit 9b8284c

Browse files
authored
Merge pull request #38 from contentstack/feat/Live-Preview-Implementation
feat: live preview configured
2 parents 068e421 + 0e7eb1d commit 9b8284c

File tree

10 files changed

+93
-35
lines changed

10 files changed

+93
-35
lines changed

.talismanrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ fileignoreconfig:
44
- filename: test/utils/mocks.ts
55
checksum: a1cb4b1890a584f1facd30f2a0974c97a66f91417022be79d00516338e244227
66
- filename: package-lock.json
7-
checksum: 6c9beae17fc270641d855026896e49a1458a6b3a026df30265c51420d364d18a
7+
checksum: 96574fb5fc78856631105fb10913a630466662907e7f8e7bebb5f0d566259b9d
88
- filename: test/typescript/taxonomy.test.ts
99
checksum: e4bdf633e147fd60d929d379f20c814eed5f68b11421d7b53ec8826e9142de37
1010
- filename: src/core/modules/taxonomy.js

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Change log
22

3+
### Version: 4.2.0
4+
#### Date: August-07-2024
5+
Feat: Live Preview configuration changes
6+
37
### Version: 4.1.0
48
#### Date: August-08-2024
59
Feat: fetch asset by any field-uid other that asset-uid

package-lock.json

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

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/delivery-sdk",
3-
"version": "4.1.0",
3+
"version": "4.2.0",
44
"type": "commonjs",
55
"main": "./dist/cjs/src/index.js",
66
"types": "./dist/types/src/index.d.ts",
@@ -20,7 +20,7 @@
2020
"build:types": "node tools/cleanup types && tsc -p config/tsconfig.types.json"
2121
},
2222
"dependencies": {
23-
"@contentstack/core": "^1.0.3",
23+
"@contentstack/core": "^1.1.0",
2424
"@contentstack/utils": "^1.3.8",
2525
"@types/humps": "^2.0.6",
2626
"axios": "^1.7.2",

src/lib/contentstack.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,8 @@ export function stack(config: StackConfig): StackClass {
4040
live_preview: {} as any
4141
};
4242

43-
if (config.live_preview?.enable === true) {
44-
if (config.live_preview?.management_token != null && config.live_preview?.preview_token == null) {
45-
config.host = config.live_preview.host
46-
} else if (config.live_preview?.preview_token != null && config.live_preview?.management_token == null) {
47-
config.host = config.live_preview.host
48-
}
49-
} else {
50-
defaultConfig.defaultHostname = config.host ? config.host : getHost(config.region, config.host);
51-
config.host = config.host || defaultConfig.defaultHostname;
52-
defaultConfig.live_preview = config.live_preview
53-
}
43+
defaultConfig.defaultHostname = config.host || getHost(config.region, config.host);
44+
config.host = defaultConfig.defaultHostname;
5445

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

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

81-
// return new Stack(httpClient(defaultConfig), config);
72+
8273
const client = httpClient(defaultConfig as any);
8374

8475
if (config.logHandler) client.defaults.logHandler = config.logHandler;

src/lib/stack.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class Stack {
1515
constructor(client: AxiosInstance, config: StackConfig) {
1616
this._client = client;
1717
this.config = config;
18+
this._client.stackConfig = this.config;
1819
}
1920

2021
/**
@@ -144,9 +145,13 @@ export class Stack {
144145

145146
livePreviewQuery(query: LivePreviewQuery) {
146147
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
148+
const livePreviewParams: any = {
149+
...this.config.live_preview,
150+
live_preview: query.live_preview || 'init',
151+
contentTypeUid: query.contentTypeUid,
152+
entryUid: query.entryUid
153+
}
154+
this._client.stackConfig.live_preview = livePreviewParams;
150155
}
151156
}
152157
}

test/api/contenttype-query.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// @ts-nocheck
12
import { ContentTypeQuery } from '../../src/lib/contenttype-query';
23
import { stackInstance } from '../utils/stack-instance';
3-
import { TContentTypes } from './types';
4+
import { TContentType, TContentTypes } from './types';
45

56
const stack = stackInstance();
67
describe('ContentTypeQuery API test cases', () => {
7-
it('should check for content_types are defined', async () => {
8-
const result = await makeContentTypeQuery().find<TContentTypes>();
8+
it.only('should check for content_types are defined', async () => {
9+
const result = await makeContentTypeQuery().find<TContentType>();
10+
911
expect(result.content_types).toBeDefined();
1012
expect(result.content_types[0]._version).toBeDefined();
1113
expect(result.content_types[0].uid).toBeDefined();

test/api/live-preview.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Live preview tests', () => {
3838
expect(livePreviewObject).toHaveProperty('enable');
3939
expect(livePreviewObject).toHaveProperty('host');
4040
expect(livePreviewObject).not.toHaveProperty('preview');
41-
expect(stack.config.host).toBe('api.contentstack.io');
41+
expect(stack.config.host).toBe('cdn.contentstack.io');
4242
});
4343

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

8080
test('should check host when live preview is disabled and preview token is provided', () => {

test/unit/live-preview.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('Live preview tests', () => {
2929
expect(livePreviewObject).toHaveProperty('enable');
3030
expect(livePreviewObject).toHaveProperty('host');
3131
expect(livePreviewObject).not.toHaveProperty('preview');
32-
expect(stack.config.host).toBe('api.contentstack.io');
32+
expect(stack.config.host).toBe('cdn.contentstack.io');
3333
});
3434

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

7474
test('should check host when live preview is disabled and preview token is provided', () => {

test/utils/stack-instance.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ function stackInstance() {
99
apiKey: process.env.API_KEY || '',
1010
deliveryToken: process.env.DELIVERY_TOKEN || '',
1111
environment: process.env.ENVIRONMENT || '',
12+
live_preview: {
13+
enable: true,
14+
preview_token: "abcda",
15+
host: "xyz,contentstack.com",
16+
}
1217
};
1318

1419
return contentstack.stack(params);

0 commit comments

Comments
 (0)