Skip to content

Commit 7da938d

Browse files
Variants entry getting None response issue fixed (#10)
1 parent b8c98c8 commit 7da938d

File tree

6 files changed

+132
-67
lines changed

6 files changed

+132
-67
lines changed

lib/stack/contentType/entry/variants/index.js

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,39 @@ export function Variants(http, data) {
3030
* @example
3131
* import * as contentstack from '@contentstack/management'
3232
* const client = contentstack.client()
33-
*
34-
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').fetch()
35-
* .then((variants) => {
36-
* variants.title = 'My New Variants'
37-
* variants.description = 'Variants description'
38-
* return variants.update()
39-
* })
33+
* const data = {
34+
* "entry": {
35+
* "title": "example",
36+
* "url": "/example",
37+
* "_variant": {
38+
* "_change_set": [
39+
* "title",
40+
* "url"
41+
* ]
42+
* }
43+
* }
44+
* }
45+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').update(data)
4046
* .then((variants) => console.log(variants))
4147
*/
42-
this.update = update(http, 'variants')
48+
this.update = async (data) => {
49+
try {
50+
const response = await http.put(this.urlPath,
51+
data,
52+
{
53+
headers: {
54+
...cloneDeep(this.stackHeaders)
55+
}
56+
})
57+
if (response.data) {
58+
return response.data
59+
} else {
60+
return error(response)
61+
}
62+
} catch (err) {
63+
return error(err)
64+
}
65+
}
4366

4467
/**
4568
* @description The Delete an variants call is used to delete a specific variants from a content type.
@@ -69,6 +92,37 @@ export function Variants(http, data) {
6992
*
7093
*/
7194
this.fetch = fetch(http, 'variants')
95+
96+
/**
97+
* @description The version Variants call fetches Variants version details.
98+
* @memberof Variants
99+
* @func versions
100+
* @returns {Promise<Variants.Variants>} Promise for Variants instance
101+
* @example
102+
* import * as contentstack from '@contentstack/management'
103+
* const client = contentstack.client()
104+
*
105+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').versions()
106+
* .then((variants) => console.log(variants))
107+
*
108+
*/
109+
this.versions = async () => {
110+
try {
111+
const response = await http.get(`${this.urlPath}/versions`, {
112+
headers: {
113+
...cloneDeep(this.stackHeaders)
114+
}
115+
})
116+
if (response.data) {
117+
return response.data
118+
} else {
119+
return error(response)
120+
}
121+
} catch (err) {
122+
return error(err)
123+
}
124+
}
125+
72126
} else {
73127
/**
74128
* @description The Query on Variants will allow to fetch details of all or specific Variants
@@ -90,11 +144,11 @@ export function Variants(http, data) {
90144
}
91145
export function VariantsCollection(http, data) {
92146
const obj = cloneDeep(data.entries) || []
93-
const variantCollection = obj.map((variants) => {
147+
const variantCollection = obj.map((variant) => {
94148
return new Variants(http, {
95-
content_type_uid: data.content_type_uid,
96-
entry_uid: data.entry_uid,
97-
variants_uid: data.variants_uid,
149+
content_type_uid: variant.content_type_uid,
150+
entry_uid: variant.entry_uid,
151+
variants_uid: variant.uid,
98152
stackHeaders: data.stackHeaders
99153
})
100154
})

lib/stack/variantGroup/index.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,29 @@ export function VariantGroup (http, data = {}) {
2222
* @example
2323
* import * as contentstack from '@contentstack/management'
2424
* const client = contentstack.client()
25-
*
26-
* client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').fetch()
27-
* .then((variant_group) => {
28-
* variant_group.name = 'test'
29-
* variant_group.content_types = ['iphone_product_page']
30-
* return variant_group.update()
31-
* })
25+
* const up_data = {name: 'update name'}
26+
* client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').update(up_data)
3227
* .then((variant_group) => console.log(variant_group))
3328
*
3429
*/
35-
this.update = update(/* The `http` parameter is being used as a dependency injection to make HTTP
36-
requests within the `VariantGroup` and `VariantGroupCollection` functions.
37-
It allows the functions to interact with an external API or server to
38-
perform operations such as creating, updating, deleting, fetching, or
39-
querying variant groups. */
40-
/* The `http` parameter in the `VariantGroup` function is being used to make
41-
HTTP requests to interact with the Contentstack API. It is likely being
42-
used to send requests to create, update, delete, fetch, or query variant
43-
groups and their related entities. */
44-
http, 'variant_group')
45-
30+
this.update = async (data) => {
31+
try {
32+
const response = await http.put(this.urlPath,
33+
data,
34+
{
35+
headers: {
36+
...cloneDeep(this.stackHeaders)
37+
}
38+
})
39+
if (response.data) {
40+
return response.data
41+
} else {
42+
return error(response)
43+
}
44+
} catch (err) {
45+
return error(err)
46+
}
47+
}
4648
/**
4749
* @description The Delete VariantGroup call is used to delete an existing VariantGroup permanently from your Stack.
4850
* @memberof VariantGroup
@@ -71,7 +73,7 @@ export function VariantGroup (http, data = {}) {
7173
* .then((variant_group) => console.log(variant_group))
7274
*
7375
*/
74-
this.fetch = fetch(http, 'variant_group')
76+
// this.fetch = fetch(http, 'variant_group')
7577

7678
/**
7779
* @description Content type defines the structure or schema of a page or a section of your web or mobile property.

lib/stack/variantGroup/variants/index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,29 @@ export function Variants (http, data = {}) {
2222
* @example
2323
* import * as contentstack from '@contentstack/management'
2424
* const client = contentstack.client()
25-
*
26-
* client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').variants('variant_uid').fetch()
27-
* .then((variants) => {
28-
* variants.name = 'test'
29-
* variants.content_types = ['iphone_product_page']
30-
* return variants.update()
31-
* })
25+
* const data = { "name": "Update name" }
26+
* client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').variants('variant_uid').update(data)
3227
* .then((variants) => console.log(variants))
3328
*
3429
*/
35-
this.update = update(http, 'variants')
30+
this.update = async (data) => {
31+
try {
32+
const response = await http.put(this.urlPath,
33+
data,
34+
{
35+
headers: {
36+
...cloneDeep(this.stackHeaders)
37+
}
38+
})
39+
if (response.data) {
40+
return response.data
41+
} else {
42+
return error(response)
43+
}
44+
} catch (err) {
45+
return error(err)
46+
}
47+
}
3648

3749
/**
3850
* @description The fetch Variants call fetches Variants details.

test/unit/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,4 @@ require('./team-stack-role-mapping-test')
4141
require('./managementToken-test')
4242
require('./ungroupedVariants-test')
4343
require('./variantGroup-test')
44-
require('./variantsWithVariantsGroup-test')
45-
44+
require('./variantsWithVariantsGroup-test')

test/unit/variantGroup-test.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ describe('Contentstack VariantGroup test', () => {
1313
expect(variantGroup.stackHeaders).to.be.equal(undefined)
1414
expect(variantGroup.update).to.be.equal(undefined)
1515
expect(variantGroup.delete).to.be.equal(undefined)
16-
expect(variantGroup.fetch).to.be.equal(undefined)
1716
expect(variantGroup.create).to.not.equal(undefined)
1817
expect(variantGroup.query).to.not.equal(undefined)
1918
done()
@@ -25,7 +24,6 @@ describe('Contentstack VariantGroup test', () => {
2524
expect(variantGroup.stackHeaders).to.be.equal(undefined)
2625
expect(variantGroup.update).to.not.equal(undefined)
2726
expect(variantGroup.delete).to.not.equal(undefined)
28-
expect(variantGroup.fetch).to.not.equal(undefined)
2927
expect(variantGroup.create).to.be.equal(undefined)
3028
expect(variantGroup.query).to.be.equal(undefined)
3129
done()
@@ -42,7 +40,6 @@ describe('Contentstack VariantGroup test', () => {
4240
expect(variantGroup.stackHeaders.api_key).to.be.equal(stackHeadersMock.api_key)
4341
expect(variantGroup.update).to.not.equal(undefined)
4442
expect(variantGroup.delete).to.not.equal(undefined)
45-
expect(variantGroup.fetch).to.not.equal(undefined)
4643
expect(variantGroup.create).to.be.equal(undefined)
4744
expect(variantGroup.query).to.be.equal(undefined)
4845
done()
@@ -107,34 +104,35 @@ describe('Contentstack VariantGroup test', () => {
107104
stackHeaders: stackHeadersMock
108105
}
109106
})
110-
.update()
107+
.update({name: 'test'})
111108
.then((variantGroup) => {
112-
checkVariantGroup(variantGroup)
109+
checkVariantGroup(variantGroup.variant_group)
113110
done()
114111
})
115112
.catch(done)
116113
})
117114

118-
it('VariantGroup fetch test', done => {
119-
var mock = new MockAdapter(Axios)
120-
mock.onGet('/variant_groups/UID').reply(200, {
121-
variant_group: {
122-
...variantGroupMock
123-
}
124-
})
125-
makeVariantGroup({
126-
variant_group: {
127-
...systemUidMock
128-
},
129-
stackHeaders: stackHeadersMock
130-
})
131-
.fetch()
132-
.then((variantGroup) => {
133-
checkVariantGroup(variantGroup)
134-
done()
135-
})
136-
.catch(done)
137-
})
115+
// Fetch API not present in the variant group
116+
// it('VariantGroup fetch test', done => {
117+
// var mock = new MockAdapter(Axios)
118+
// mock.onGet('/variant_groups/UID').reply(200, {
119+
// variant_group: {
120+
// ...variantGroupMock
121+
// }
122+
// })
123+
// makeVariantGroup({
124+
// variant_group: {
125+
// ...systemUidMock
126+
// },
127+
// stackHeaders: stackHeadersMock
128+
// })
129+
// .fetch()
130+
// .then((variantGroup) => {
131+
// checkVariantGroup(variantGroup)
132+
// done()
133+
// })
134+
// .catch(done)
135+
// })
138136

139137
it('VariantGroup delete test', done => {
140138
var mock = new MockAdapter(Axios)

test/unit/variantsWithVariantsGroup-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('Contentstack Variants test', () => {
101101
})
102102
.update()
103103
.then((variant) => {
104-
checkVariants(variant)
104+
checkVariants(variant.variants)
105105
done()
106106
})
107107
.catch(done)

0 commit comments

Comments
 (0)