Skip to content

Commit dfd7166

Browse files
Added variants support (#211)
* Added variants support * version bump * updated changelog file * type support added * fixed PR comments * Fixed testcases * Added typescript testcases * uncommented code
1 parent 6a11a21 commit dfd7166

File tree

8 files changed

+160
-27
lines changed

8 files changed

+160
-27
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## Change log
22

3+
### Version: 3.21.0
4+
#### Date: September-09-2024
5+
##### Fix:
6+
- Feat Variants support added
7+
38
### Version: 3.20.4
49
#### Date: August-14-2024
510
##### Fix:

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export class Entry {
236236
includeOwner(): this;
237237
toJSON(): this;
238238
addParam(key: string, value: any): this;
239+
variants(variant_headers: string | string[]): this;
239240
fetch(fetchOptions?: object): Promise<any>;
240241
}
241242

package-lock.json

+97-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "contentstack",
3-
"version": "3.20.5",
3+
"version": "3.21.0",
44
"description": "Contentstack Javascript SDK",
55
"homepage": "https://www.contentstack.com/",
66
"author": {
@@ -100,10 +100,10 @@
100100
},
101101
"dependencies": {
102102
"@contentstack/utils": "^1.3.10",
103-
"cheerio": "^1.0.0-rc.12",
103+
"cheerio": "^1.0.0",
104104
"es6-promise": "^4.1.1",
105105
"isomorphic-fetch": "^3.0.0",
106106
"localStorage": "1.0.4",
107-
"qs": "^6.12.1"
107+
"qs": "^6.12.3"
108108
}
109109
}

src/core/modules/entry.js

+16
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,22 @@ export default class Entry {
342342
}
343343
}
344344

345+
/**
346+
* @method Variants
347+
* @memberOf Entry
348+
* @param {String} uid - uid of the variants entry
349+
* @description An initializer is responsible for creating Variants Entry object
350+
* @returns {Variants}
351+
* @instance
352+
*/
353+
variants(variant_headers) {
354+
if (Array.isArray(variant_headers) && variant_headers.length > 0) {
355+
this.headers['x-cs-variant-uid'] = variant_headers.join(',')
356+
}else{
357+
this.headers['x-cs-variant-uid'] = variant_headers;
358+
}
359+
return this;
360+
}
345361

346362
/**
347363
* @method fetch

src/core/modules/query.js

+16
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,22 @@ export default class Query extends Entry {
790790
var options = Utils.mergeDeep(this.fetchOptions, fetchOptions);
791791
return Utils.sendRequest(Utils.mergeDeep({}, this), options);
792792
}
793+
/**
794+
* @method Variants
795+
* @memberOf Query
796+
* @param {String} uid - uid of the variants entry
797+
* @description An initializer is responsible for creating Variants Entry object
798+
* @returns {Variants}
799+
* @instance
800+
*/
801+
variants(variant_headers) {
802+
if (Array.isArray(variant_headers) && variant_headers.length > 0) {
803+
this.headers['x-cs-variant-uid'] = variant_headers.join(',')
804+
}else{
805+
this.headers['x-cs-variant-uid'] = variant_headers;
806+
}
807+
return this;
808+
}
793809

794810
/**
795811
* @method findOne

test/entry/find.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1658,4 +1658,19 @@ test('CT Taxonomies Query: Get Entries With Taxonomy Terms Parent and Excluding
16581658
assert.fail("CT Taxonomies Query: Get Entries With Taxonomy Terms Parent and Excluding the term itself ($above, level)");
16591659
assert.end();
16601660
})
1661-
})
1661+
})
1662+
test('Variants in entry', function (t) {
1663+
let Query = Stack.ContentType('source').Query();
1664+
Query
1665+
.variants(['variant_entry_1', 'variant_entry_2'])
1666+
.toJSON()
1667+
.find()
1668+
.then(entries => {
1669+
assert.ok(entries[0].length, 'Variant entries present in the resultset');
1670+
assert.end();
1671+
}, err => {
1672+
console.error("error :", err);
1673+
assert.fail("Variant Entries are not present in the CT");
1674+
assert.end();
1675+
})
1676+
});

test/typescript/entry-query.test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ describe('Entry Query Test', () => {
455455
done();
456456
});
457457

458+
test('Variants Query: Get variant Entries', done => {
459+
makeEntryQuery().variants(['variantEntryUid1', 'variantEntryUid2']).find().then((response) => done()).catch((error) => done());
460+
done();
461+
});
462+
458463
test('Taxonomy find test', done => {
459464
makeTaxonomyQuery().find().then((response) => done()).catch((error) => done());
460465
});
@@ -466,4 +471,4 @@ function makeEntryQuery() {
466471

467472
function makeTaxonomyQuery() {
468473
return stack.Taxonomies()
469-
}
474+
}

0 commit comments

Comments
 (0)