Skip to content

Commit 787ea4d

Browse files
Merge pull request #265 from contentstack/feat/dx-1889-api-version-variants
feat | dx 1889 api version variants
2 parents 5be1096 + ee400ba commit 787ea4d

File tree

15 files changed

+496
-64
lines changed

15 files changed

+496
-64
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Changelog
2-
## [v1.19.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.2) (2025-01-27)
2+
## [v1.19.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.2) (2025-02-11)
33
- Enhancement
44
- Added support for nested global fields.
5+
- Added api_version support for variants
56

67
## [v1.19.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.1) (2025-01-27)
78
- Feature

lib/stack/contentType/entry/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export function Entry (http, data) {
2525
this.urlPath = `/content_types/${this.content_type_uid}/entries`
2626

2727
if (data && data.entry) {
28+
this.apiVersion = data.api_version || undefined;
29+
if (this.apiVersion && !this.stackHeaders.api_version) {
30+
this.stackHeaders.api_version = this.apiVersion;
31+
}
2832
Object.assign(this, cloneDeep(data.entry))
2933
this.urlPath = `/content_types/${this.content_type_uid}/entries/${this.uid}`
3034

lib/stack/contentType/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,16 @@ export function ContentType (http, data = {}) {
147147
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').fetch()
148148
* .then((contentType) => console.log(contentType))
149149
*/
150-
this.entry = (uid = null) => {
150+
this.entry = (uid = null, options = {}) => {
151151
const data = { stackHeaders: this.stackHeaders }
152152
data.content_type_uid = this.uid
153153
if (uid) {
154154
data.entry = { uid: uid }
155155
}
156+
options = options || {}; // Ensure `options` is always an object
157+
if (options && typeof options === 'object' && options.api_version) {
158+
data.api_version = options.api_version;
159+
}
156160
return new Entry(http, data)
157161
}
158162

test/sanity-check/api/entryVariants-test.js

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@ import { describe, it, setup } from "mocha";
33
import { jsonReader } from "../utility/fileOperations/readwrite";
44
import { createVariantGroup } from "../mock/variantGroup.js";
55
import { variant } from "../mock/variants.js";
6-
import { variantEntryFirst } from "../mock/variantEntry.js";
6+
import {
7+
variantEntryFirst,
8+
publishVariantEntryFirst,
9+
unpublishVariantEntryFirst,
10+
} from "../mock/variantEntry.js";
711
import { contentstackClient } from "../utility/ContentstackClient.js";
812

913
var client = {};
1014

1115
var stack = {};
1216
var variantUid = "";
13-
var variantName = "";
1417
var variantGroupUid = "";
1518
var contentTypeUid = "";
1619
var entryUid = "";
1720

18-
const entry = jsonReader("entry.json");
19-
entryUid = entry[2].uid;
20-
contentTypeUid = entry[2].content_type_uid;
21-
2221
describe("Entry Variants api Test", () => {
2322
setup(() => {
2423
const user = jsonReader("loggedinuser.json");
2524
stack = jsonReader("stack.json");
2625
client = contentstackClient(user.authtoken);
26+
const entry = jsonReader("entry.json");
27+
entryUid = entry[2].uid;
28+
contentTypeUid = entry[2].content_type_uid;
2729
});
2830

2931
it("should create a Variant Group", (done) => {
@@ -76,24 +78,8 @@ describe("Entry Variants api Test", () => {
7678
});
7779

7880
it("should publish entry variant", (done) => {
79-
var publishVariantEntryFirst = {
80-
entry: {
81-
environments: ["development"],
82-
locales: ["en-us", "en-at"],
83-
variants: [
84-
{
85-
uid: variantUid,
86-
version: 1,
87-
},
88-
],
89-
variant_rules: {
90-
publish_latest_base: false,
91-
publish_latest_base_conditionally: true,
92-
},
93-
},
94-
locale: "en-us",
95-
version: 1,
96-
};
81+
publishVariantEntryFirst.entry.variants[0].uid = variantUid;
82+
9783
makeEntry()
9884
.entry(entryUid)
9985
.publish({
@@ -111,24 +97,7 @@ describe("Entry Variants api Test", () => {
11197
});
11298

11399
it("should unpublish entry variant", (done) => {
114-
var publishVariantEntryFirst = {
115-
entry: {
116-
environments: ["development"],
117-
locales: ["en-at"],
118-
variants: [
119-
{
120-
uid: variantUid,
121-
version: 1,
122-
},
123-
],
124-
variant_rules: {
125-
publish_latest_base: false,
126-
publish_latest_base_conditionally: true,
127-
},
128-
},
129-
locale: "en-us",
130-
version: 1,
131-
};
100+
unpublishVariantEntryFirst.entry.variants[0].uid = variantUid;
132101
makeEntry()
133102
.entry(entryUid)
134103
.unpublish({
@@ -145,6 +114,41 @@ describe("Entry Variants api Test", () => {
145114
.catch(done);
146115
});
147116

117+
it("should publish entry variant using api_version", (done) => {
118+
publishVariantEntryFirst.entry.variants[0].uid = variantUid;
119+
makeEntry()
120+
.entry(entryUid, { api_version: "3.2" })
121+
.publish({
122+
publishDetails: publishVariantEntryFirst.entry,
123+
locale: publishVariantEntryFirst.locale,
124+
})
125+
.then((data) => {
126+
expect(data.notice).to.be.equal(
127+
"The requested action has been performed."
128+
);
129+
expect(data.job_id).to.be.not.equal(null);
130+
done();
131+
})
132+
.catch(done);
133+
});
134+
135+
it("should unpublish entry variant using api_version", (done) => {
136+
unpublishVariantEntryFirst.entry.variants[0].uid = variantUid;
137+
makeEntry()
138+
.entry(entryUid, { api_version: "3.2" })
139+
.unpublish({
140+
publishDetails: unpublishVariantEntryFirst.entry,
141+
locale: unpublishVariantEntryFirst.locale,
142+
})
143+
.then((data) => {
144+
expect(data.notice).to.be.equal(
145+
"The requested action has been performed."
146+
);
147+
expect(data.job_id).to.be.not.equal(null);
148+
done();
149+
})
150+
.catch(done);
151+
});
148152
it("should get all entry variants", (done) => {
149153
makeEntryVariants()
150154
.query({})

test/sanity-check/mock/variantEntry.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,44 @@ const variantEntryFirst = {
66
_change_set: ["title", "url"],
77
},
88
},
9-
}
9+
};
1010

11-
export { variantEntryFirst };
11+
var publishVariantEntryFirst = {
12+
entry: {
13+
environments: ["development"],
14+
locales: ["en-us", "en-at"],
15+
variants: [
16+
{
17+
uid: "",
18+
version: 1,
19+
},
20+
],
21+
variant_rules: {
22+
publish_latest_base: false,
23+
publish_latest_base_conditionally: true,
24+
},
25+
},
26+
locale: "en-us",
27+
version: 1,
28+
};
29+
30+
const unpublishVariantEntryFirst = {
31+
entry: {
32+
environments: ["development"],
33+
locales: ["en-at"],
34+
variants: [
35+
{
36+
uid: "",
37+
version: 1,
38+
},
39+
],
40+
variant_rules: {
41+
publish_latest_base: false,
42+
publish_latest_base_conditionally: true,
43+
},
44+
},
45+
locale: "en-us",
46+
version: 1,
47+
};
48+
49+
export { variantEntryFirst, publishVariantEntryFirst, unpublishVariantEntryFirst };

test/sanity-check/sanity.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ require('./api/contentType-test')
1515
require('./api/asset-test')
1616
require('./api/extension-test')
1717
require('./api/entry-test')
18+
require('./api/variantGroup-test')
19+
require('./api/variants-test')
20+
require('./api/ungroupedVariants-test')
21+
require('./api/entryVariants-test')
1822
require('./api/bulkOperation-test')
1923
require('./api/webhook-test')
2024
require('./api/workflow-test')
2125
require('./api/globalfield-test')
2226
require('./api/release-test')
2327
require('./api/label-test')
24-
require('./api/variantGroup-test')
25-
require('./api/variants-test')
26-
require('./api/ungroupedVariants-test')
27-
require('./api/entryVariants-test')
2828
require('./api/contentType-delete-test')
2929
require('./api/delete-test')
3030
require('./api/team-test')

test/unit/asset-test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,67 @@ describe('Contentstack Asset test', () => {
315315
})
316316
.catch(done)
317317
})
318+
319+
320+
it('Asset download test', done => {
321+
var mock = new MockAdapter(Axios)
322+
const downloadResponse = new Blob(['file content'], { type: 'text/plain' })
323+
324+
mock.onGet('/assets/download_url').reply(200, downloadResponse)
325+
326+
makeAsset({
327+
asset: {
328+
...systemUidMock
329+
},
330+
stackHeaders: stackHeadersMock
331+
})
332+
.download({ url: '/assets/download_url', responseType: 'blob' })
333+
.then((response) => {
334+
expect(response.data).to.be.instanceOf(Blob)
335+
expect(response.data.type).to.be.equal('text/plain')
336+
done()
337+
})
338+
.catch(done)
339+
})
340+
341+
it('Asset download test without url', done => {
342+
var mock = new MockAdapter(Axios)
343+
const downloadResponse = new Blob(['file content'], { type: 'text/plain' })
344+
345+
mock.onGet(`/assets/${systemUidMock.uid}`).reply(200, downloadResponse)
346+
347+
makeAsset({
348+
asset: {
349+
...systemUidMock
350+
},
351+
stackHeaders: stackHeadersMock
352+
})
353+
.download({ responseType: 'blob' })
354+
.then((response) => {
355+
console.log("🚀 ~ .then ~ response:", response)
356+
expect(response.data).to.be.instanceOf(Blob)
357+
expect(response.data.type).to.be.equal('text/plain')
358+
done()
359+
})
360+
.catch((err) => {
361+
expect(err.message).to.be.equal('Asset URL can not be empty')
362+
done()
363+
})
364+
})
365+
366+
it('Asset download test with missing url', done => {
367+
makeAsset({
368+
asset: {
369+
...systemUidMock
370+
},
371+
stackHeaders: stackHeadersMock
372+
})
373+
.download({ responseType: 'blob' })
374+
.catch((err) => {
375+
expect(err.message).to.be.equal('Asset URL can not be empty')
376+
done()
377+
})
378+
})
318379
})
319380

320381
function makeAsset (data) {

test/unit/contentType-test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,34 @@ describe('Contentstack ContentType test', () => {
280280
done()
281281
})
282282
})
283+
it('should fetch references of a ContentType', done => {
284+
var mock = new MockAdapter(Axios)
285+
const referencesResponse = {
286+
references: [
287+
{
288+
uid: 'entry_uid',
289+
title: 'Entry Title',
290+
content_type: 'UID'
291+
}
292+
]
293+
}
294+
mock.onGet(`/content_types/UID/references`).reply(200, referencesResponse)
295+
makeContentType({
296+
content_type: {
297+
...systemUidMock
298+
},
299+
stackHeaders: stackHeadersMock
300+
})
301+
.references()
302+
.then((response) => {
303+
expect(response.references).to.be.an('array')
304+
expect(response.references[0].uid).to.be.equal('entry_uid')
305+
expect(response.references[0].title).to.be.equal('Entry Title')
306+
expect(response.references[0].content_type).to.be.equal('UID')
307+
done()
308+
})
309+
.catch(done)
310+
})
283311
})
284312

285313
function makeContentType (data) {

test/unit/entry-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,37 @@ describe('Contentstack Entry test', () => {
640640
})
641641
.catch(done)
642642
})
643+
644+
it('should fetch variants of an Entry', done => {
645+
var mock = new MockAdapter(Axios)
646+
const variantsResponse = {
647+
variants: [
648+
{
649+
uid: 'variant_uid',
650+
title: 'Variant Title',
651+
content_type: 'content_type_uid'
652+
}
653+
]
654+
}
655+
656+
mock.onGet(`/content_types/content_type_uid/entries/UID/variants/variantUid`).reply(200, variantsResponse)
657+
658+
makeEntry({
659+
entry: {
660+
...systemUidMock
661+
}
662+
})
663+
.variants('variantUid')
664+
.fetch()
665+
.then((response) => {
666+
expect(response.variants).to.be.an('array')
667+
expect(response.variants[0].uid).to.be.equal('variant_uid')
668+
expect(response.variants[0].title).to.be.equal('Variant Title')
669+
expect(response.variants[0].content_type).to.be.equal('content_type_uid')
670+
done()
671+
})
672+
.catch(done)
673+
})
643674
})
644675

645676
function makeEntry (data) {

0 commit comments

Comments
 (0)