Skip to content

Commit 48869fc

Browse files
authoredSep 12, 2024
Merge pull request #213 from contentstack/next
Added variants support
2 parents 14ef35d + 26ab910 commit 48869fc

11 files changed

+184
-50
lines changed
 

‎CHANGELOG.md

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

3+
### Version: 3.21.0
4+
#### Date: September-09-2024
5+
##### Fix:
6+
- Feat Variants support added
7+
8+
### Version: 3.20.4
9+
#### Date: August-14-2024
10+
##### Fix:
11+
- Fix file upload function in sanity report file
312

413
### Version: 3.20.3
514
#### Date: August-02-2024

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ For browsers, we recommend to download the library via npm or yarn to ensure 100
1919
If you'd like to use a standalone built file you can use the following script tag or download it from [jsDelivr](https://www.jsdelivr.com/package/npm/contentstack), under the `dist` directory:
2020

2121
```html
22-
<script src="https://cdn.jsdelivr.net/npm/contentstack@latest/dist/web/contentstack.min.js"></script>
22+
<script src="https://cdn.jsdelivr.net/npm/contentstack@latest/dist/web/contentstack.min.js" integrity="9u29niwIJG3dEc7vPUc1ZA1Dl/uaiR4+s7k55kb/CCkzTzyFuZHNM165BQ10+Hiw%" crossorigin="anonymous"></script>
2323
```
2424
You can also specify a specific version number.
2525
```html
26-
<script src="https://cdn.jsdelivr.net/npm/contentstack@3.16.0/dist/web/contentstack.min.js"></script>
26+
<script src="https://cdn.jsdelivr.net/npm/contentstack@3.20.3/dist/web/contentstack.min.js" integrity="9u29niwIJG3dEc7vPUc1ZA1Dl/uaiR4+s7k55kb/CCkzTzyFuZHNM165BQ10+Hiw%" crossorigin="anonymous"></script>
2727
```
2828

2929
To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.

‎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.3",
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
}

‎sanity-report.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ async function publishMessage (text, report) {
5050
channel: process.env.SLACK_CHANNEL,
5151
text: text
5252
})
53-
await app.client.files.upload({
53+
await app.client.files.uploadV2({
5454
token: process.env.SLACK_BOT_TOKEN,
55-
channels: process.env.SLACK_CHANNEL,
55+
channel_id: process.env.SLACK_CHANNEL_ID,
5656
initial_comment: '*Here is the report generated*',
5757
filetype: 'html',
5858
filename: 'tap-html.html',

‎src/core/lib/request.js

+16-19
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,25 @@ export default function Request(stack, fetchOptions) {
99
let requestParams = stack.requestParams;
1010
return new Promise(function(resolve, reject) {
1111
let queryParams;
12-
let serialize = function(obj, prefix) {
13-
14-
let str = [],
15-
p;
16-
if (typeof obj === "object" && obj.length !== undefined) {
17-
for (var i = 0, _i = obj.length; i < _i; i++) {
18-
str.push(prefix + '[]=' + obj[i]);
19-
}
20-
} else {
21-
for (const p in obj) {
22-
let k = prefix ? prefix + "[" + p + "]" : p,
23-
v = obj[p];
24-
str.push((v !== null && typeof v === "object" && p !== 'query') ?
25-
serialize(v, k) :
26-
k + "=" + encodeURIComponent(p !== 'query' ? v : JSON.stringify(v)));
27-
}
12+
13+
const params = new URLSearchParams();
14+
let serialize = function (obj, prefix) {
15+
if (typeof obj === 'object' && obj.length !== undefined) {
16+
for (let i = 0, _i = obj.length; i < _i; i++) {
17+
params.append(prefix + '[]', obj[i]);
18+
}
19+
} else {
20+
for (const p in obj) {
21+
let k = prefix ? prefix + '[' + p + ']' : p,
22+
v = obj[p];
23+
v !== null && typeof v === 'object' && p !== 'query'
24+
? serialize(v, k)
25+
: params.append(k, p !== 'query' ? v : JSON.stringify(v));
2826
}
29-
return str.join("&");
27+
}
28+
return params.toString();
3029
};
3130

32-
33-
3431
// setting headers
3532
requestParams.headers['Content-Type'] = 'application/json; charset=UTF-8';
3633
requestParams.headers['X-User-Agent'] = 'contentstack-delivery-javascript-{{PLATFORM}}/' + version;

‎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)
Please sign in to comment.