Skip to content

Commit

Permalink
Merge pull request #213 from contentstack/next
Browse files Browse the repository at this point in the history
Added variants support
  • Loading branch information
cs-raj authored Sep 12, 2024
2 parents c314199 + 2549815 commit c5b12ff
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 50 deletions.
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ For browsers, we recommend to download the library via npm or yarn to ensure 100
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:

```html
<script src="https://cdn.jsdelivr.net/npm/contentstack@latest/dist/web/contentstack.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/contentstack@latest/dist/web/contentstack.min.js" integrity="9u29niwIJG3dEc7vPUc1ZA1Dl/uaiR4+s7k55kb/CCkzTzyFuZHNM165BQ10+Hiw%" crossorigin="anonymous"></script>
```
You can also specify a specific version number.
```html
<script src="https://cdn.jsdelivr.net/npm/contentstack@3.16.0/dist/web/contentstack.min.js"></script>
<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>
```

To initialize the SDK, you will need to specify the API Key, Delivery Token, and Environment Name of your stack.
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();
})
});
Loading

0 comments on commit c5b12ff

Please sign in to comment.