Skip to content

Commit 09f4d60

Browse files
Implemented variants support (#45)
* Implemented variants support * version update * axios package bump * version bump
1 parent 6d84e95 commit 09f4d60

File tree

7 files changed

+110
-9
lines changed

7 files changed

+110
-9
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/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

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+
})

test/unit/entry.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,30 @@ describe('Entry class', () => {
6262
expect(returnedValue).toEqual(entryFetchMock.entry);
6363
});
6464
});
65+
66+
class TestVariants extends Entry {
67+
constructor(client: AxiosInstance) {
68+
super(client, 'xyz', 'abc');
69+
this._client = client;
70+
}
71+
72+
getVariantsHeaders(): string {
73+
return this._client.defaults.headers['x-cs-variant-uid'] || "";
74+
}
75+
}
76+
77+
describe('Variants test', () => {
78+
let client: AxiosInstance;
79+
let mockClient: MockAdapter;
80+
81+
beforeAll(() => {
82+
client = httpClient(MOCK_CLIENT_OPTIONS);
83+
mockClient = new MockAdapter(client as any);
84+
});
85+
it('should get the correct variant headers added to client', async () => {
86+
const testVariantObj = new TestVariants(httpClient(MOCK_CLIENT_OPTIONS))
87+
88+
testVariantObj.variants(['variant1', 'variant2']);
89+
expect(testVariantObj.getVariantsHeaders()).toBe('variant1,variant2');
90+
});
91+
})

0 commit comments

Comments
 (0)