Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added variants support #213

Merged
merged 9 commits into from
Sep 12, 2024
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
## Change log

### Version: 3.21.0
#### Date: September-09-2024
##### Fix:
- Feat Variants support added

### Version: 3.20.4
#### Date: August-14-2024
##### Fix:
- Fix file upload function in sanity report file

### Version: 3.20.3
#### Date: August-02-2024
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export class Entry {
includeOwner(): this;
toJSON(): this;
addParam(key: string, value: any): this;
variants(variant_headers: string | string[]): this;
fetch(fetchOptions?: object): Promise<any>;
}

Expand Down
119 changes: 97 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contentstack",
"version": "3.20.3",
"version": "3.21.0",
"description": "Contentstack Javascript SDK",
"homepage": "https://www.contentstack.com/",
"author": {
Expand Down Expand Up @@ -100,10 +100,10 @@
},
"dependencies": {
"@contentstack/utils": "^1.3.10",
"cheerio": "^1.0.0-rc.12",
"cheerio": "^1.0.0",
"es6-promise": "^4.1.1",
"isomorphic-fetch": "^3.0.0",
"localStorage": "1.0.4",
"qs": "^6.12.1"
"qs": "^6.12.3"
}
}
4 changes: 2 additions & 2 deletions sanity-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ async function publishMessage (text, report) {
channel: process.env.SLACK_CHANNEL,
text: text
})
await app.client.files.upload({
await app.client.files.uploadV2({
token: process.env.SLACK_BOT_TOKEN,
channels: process.env.SLACK_CHANNEL,
channel_id: process.env.SLACK_CHANNEL_ID,
initial_comment: '*Here is the report generated*',
filetype: 'html',
filename: 'tap-html.html',
Expand Down
35 changes: 16 additions & 19 deletions src/core/lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,25 @@ export default function Request(stack, fetchOptions) {
let requestParams = stack.requestParams;
return new Promise(function(resolve, reject) {
let queryParams;
let serialize = function(obj, prefix) {

let str = [],
p;
if (typeof obj === "object" && obj.length !== undefined) {
for (var i = 0, _i = obj.length; i < _i; i++) {
str.push(prefix + '[]=' + obj[i]);
}
} else {
for (const p in obj) {
let k = prefix ? prefix + "[" + p + "]" : p,
v = obj[p];
str.push((v !== null && typeof v === "object" && p !== 'query') ?
serialize(v, k) :
k + "=" + encodeURIComponent(p !== 'query' ? v : JSON.stringify(v)));
}

const params = new URLSearchParams();
let serialize = function (obj, prefix) {
if (typeof obj === 'object' && obj.length !== undefined) {
for (let i = 0, _i = obj.length; i < _i; i++) {
params.append(prefix + '[]', obj[i]);
}
} else {
for (const p in obj) {
let k = prefix ? prefix + '[' + p + ']' : p,
v = obj[p];
v !== null && typeof v === 'object' && p !== 'query'
? serialize(v, k)
: params.append(k, p !== 'query' ? v : JSON.stringify(v));
}
return str.join("&");
}
return params.toString();
};



// setting headers
requestParams.headers['Content-Type'] = 'application/json; charset=UTF-8';
requestParams.headers['X-User-Agent'] = 'contentstack-delivery-javascript-{{PLATFORM}}/' + version;
Expand Down
16 changes: 16 additions & 0 deletions src/core/modules/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,22 @@ export default class Entry {
}
}

/**
* @method Variants
* @memberOf Entry
* @param {String} uid - uid of the variants entry
* @description An initializer is responsible for creating Variants Entry object
* @returns {Variants}
* @instance
*/
variants(variant_headers) {
if (Array.isArray(variant_headers) && variant_headers.length > 0) {
this.headers['x-cs-variant-uid'] = variant_headers.join(',')
}else{
this.headers['x-cs-variant-uid'] = variant_headers;
}
return this;
}

/**
* @method fetch
Expand Down
16 changes: 16 additions & 0 deletions src/core/modules/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,22 @@ export default class Query extends Entry {
var options = Utils.mergeDeep(this.fetchOptions, fetchOptions);
return Utils.sendRequest(Utils.mergeDeep({}, this), options);
}
/**
* @method Variants
* @memberOf Query
* @param {String} uid - uid of the variants entry
* @description An initializer is responsible for creating Variants Entry object
* @returns {Variants}
* @instance
*/
variants(variant_headers) {
if (Array.isArray(variant_headers) && variant_headers.length > 0) {
this.headers['x-cs-variant-uid'] = variant_headers.join(',')
}else{
this.headers['x-cs-variant-uid'] = variant_headers;
}
return this;
}

/**
* @method findOne
Expand Down
17 changes: 16 additions & 1 deletion test/entry/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -1658,4 +1658,19 @@ test('CT Taxonomies Query: Get Entries With Taxonomy Terms Parent and Excluding
assert.fail("CT Taxonomies Query: Get Entries With Taxonomy Terms Parent and Excluding the term itself ($above, level)");
assert.end();
})
})
})
test('Variants in entry', function (t) {
let Query = Stack.ContentType('source').Query();
Query
.variants(['variant_entry_1', 'variant_entry_2'])
.toJSON()
.find()
.then(entries => {
assert.ok(entries[0].length, 'Variant entries present in the resultset');
assert.end();
}, err => {
console.error("error :", err);
assert.fail("Variant Entries are not present in the CT");
assert.end();
})
});
7 changes: 6 additions & 1 deletion test/typescript/entry-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ describe('Entry Query Test', () => {
done();
});

test('Variants Query: Get variant Entries', done => {
makeEntryQuery().variants(['variantEntryUid1', 'variantEntryUid2']).find().then((response) => done()).catch((error) => done());
done();
});

test('Taxonomy find test', done => {
makeTaxonomyQuery().find().then((response) => done()).catch((error) => done());
});
Expand All @@ -466,4 +471,4 @@ function makeEntryQuery() {

function makeTaxonomyQuery() {
return stack.Taxonomies()
}
}
Loading