Skip to content

Commit da154ce

Browse files
authored
Merge pull request #43 from contentstack/next
Next
2 parents fd63ce1 + c27d26a commit da154ce

File tree

11 files changed

+140
-11
lines changed

11 files changed

+140
-11
lines changed

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: Septmber-04-2024
5+
Feat: Variants support added
6+
37
### Version: 4.1.0
48
#### Date: August-07-2024
59
Feat: Live Preview configuration changes

package-lock.json

Lines changed: 6 additions & 6 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",
@@ -23,7 +23,7 @@
2323
"@contentstack/core": "^1.1.0",
2424
"@contentstack/utils": "^1.3.8",
2525
"@types/humps": "^2.0.6",
26-
"axios": "^1.7.2",
26+
"axios": "^1.7.4",
2727
"dotenv": "^16.3.1",
2828
"humps": "^2.0.1"
2929
},

src/lib/contentstack.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { InternalAxiosRequestConfig, AxiosRequestHeaders, AxiosResponse } from '
33
import { handleRequest } from './cache';
44
import { Stack as StackClass } from './stack';
55
import { Policy, StackConfig } from './types';
6-
import { getHost } from './utils';
6+
import * as Utility from './utils';
77
export * as Utils from '@contentstack/utils';
88

99
let version = '{{VERSION}}';
@@ -40,7 +40,7 @@ export function stack(config: StackConfig): StackClass {
4040
live_preview: {} as any
4141
};
4242

43-
defaultConfig.defaultHostname = config.host || getHost(config.region, config.host);
43+
defaultConfig.defaultHostname = config.host || Utility.getHost(config.region, config.host);
4444
config.host = defaultConfig.defaultHostname;
4545

4646
if (config.apiKey) {
@@ -59,6 +59,21 @@ export function stack(config: StackConfig): StackClass {
5959
throw new Error('Environment for Stack is required');
6060
}
6161

62+
if (config.live_preview) {
63+
if (Utility.isBrowser()) {
64+
const params = new URL(document.location.toString()).searchParams;
65+
if (params.has('live_preview')) {
66+
config.live_preview.live_preview = params.get('live_preview') || config.live_preview.live_preview;
67+
}
68+
if (params.has('release_id')) {
69+
defaultConfig.headers['release_id'] = params.get('release_id');
70+
}
71+
if (params.has('preview_timestamp')) {
72+
defaultConfig.headers['preview_timestamp'] = params.get('preview_timestamp');
73+
}
74+
}
75+
}
76+
6277
if (config.branch) {
6378
defaultConfig.headers.branch = config.branch;
6479
}

src/lib/entries.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,25 @@ export class Entries extends EntryQueryable {
184184

185185
return new Query(this._client, this._parameters, this._queryParams, this._contentTypeUid);
186186
}
187+
188+
/**
189+
* @method variants
190+
* @memberof Entry
191+
* @description The variant header will be added to axios client
192+
* @returns {Entry}
193+
* @example
194+
* import contentstack from '@contentstack/delivery-sdk'
195+
*
196+
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
197+
* const result = await stack.contentType('abc').entry().variant('xyz').find();
198+
*/
199+
variants(variants: string | string[]): Entries {
200+
if (Array.isArray(variants) && variants.length > 0) {
201+
this._client.defaults.headers['x-cs-variant-uid'] = variants.join(',');
202+
} else if (typeof variants == 'string' && variants.length > 0) {
203+
this._client.defaults.headers['x-cs-variant-uid'] = variants;
204+
}
205+
206+
return this;
207+
}
187208
}

src/lib/entry.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ interface EntryResponse<T> {
44
entry: T;
55
}
66
export class Entry {
7-
private _client: AxiosInstance;
7+
protected _client: AxiosInstance;
88
private _contentTypeUid: string;
99
private _entryUid: string;
1010
private _urlPath: string;
@@ -34,6 +34,27 @@ export class Entry {
3434
return this;
3535
}
3636

37+
/**
38+
* @method variants
39+
* @memberof Entry
40+
* @description The variant header will be added to axios client
41+
* @returns {Entry}
42+
* @example
43+
* import contentstack from '@contentstack/delivery-sdk'
44+
*
45+
* const stack = contentstack.Stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
46+
* const result = await stack.contentType('abc').entry('entry_uid').variant('xyz').fetch();
47+
*/
48+
variants(variants: string | string[]): Entry {
49+
if (Array.isArray(variants) && variants.length > 0) {
50+
this._client.defaults.headers['x-cs-variant-uid'] = variants.join(',');
51+
} else if (typeof variants == 'string' && variants.length > 0) {
52+
this._client.defaults.headers['x-cs-variant-uid'] = variants;
53+
}
54+
55+
return this;
56+
}
57+
3758
/**
3859
* @method includeMetadata
3960
* @memberof Entry

src/lib/stack.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,12 @@ export class Stack {
153153
}
154154
this._client.stackConfig.live_preview = livePreviewParams;
155155
}
156+
157+
if (query.hasOwnProperty('release_id')) {
158+
this._client.defaults.headers['release_id'] = query.release_id;
159+
}
160+
if (query.hasOwnProperty('preview_timestamp')) {
161+
this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp;
162+
}
156163
}
157164
}

src/lib/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ export interface LivePreviewQuery {
261261
live_preview: string
262262
contentTypeUid: string
263263
entryUid?: any;
264+
preview_timestamp?: string
265+
release_id?: string
264266
}
265267

266268
export type LivePreview = {

src/lib/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ export function getHost(region: Region = Region.US, host?: string) {
1010

1111
return url;
1212
}
13+
14+
export function isBrowser() {
15+
return (typeof window !== "undefined");
16+
}

test/unit/entries.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,31 @@ describe('Entries class', () => {
137137
expect(query._parameters).toEqual({"taxonomies.taxonomy_uid": {"$above": "term_uid", "levels": 4 }});
138138
});
139139
});
140+
141+
class TestVariants extends Entries {
142+
143+
constructor(client: AxiosInstance) {
144+
super(client, 'xyz');
145+
this._client = client;
146+
}
147+
148+
getVariantsHeaders(): string {
149+
return this._client.defaults.headers['x-cs-variant-uid'] || "";
150+
}
151+
}
152+
153+
describe('Variants test', () => {
154+
let client: AxiosInstance;
155+
let mockClient: MockAdapter;
156+
157+
beforeAll(() => {
158+
client = httpClient(MOCK_CLIENT_OPTIONS);
159+
mockClient = new MockAdapter(client as any);
160+
});
161+
it('should get the correct variant headers added to client', async () => {
162+
const testVariantObj = new TestVariants(httpClient(MOCK_CLIENT_OPTIONS))
163+
164+
testVariantObj.variants(['variant1', 'variant2']);
165+
expect(testVariantObj.getVariantsHeaders()).toBe('variant1,variant2');
166+
});
167+
})

0 commit comments

Comments
 (0)