-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
146 lines (139 loc) · 5.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import cloneDeep from 'lodash/cloneDeep'
import { create, update, deleteEntity, fetch, query } from '../../entity'
import { Variants } from './variants/index'
/**
* Contentstack has a sophisticated multilingual capability. It allows you to create and publish entries in any language. This feature allows you to set up multilingual websites and cater to a wide variety of audience by serving content in their local language(s). Read more about <a href='https://www.contentstack.com/docs/developers/multi-language-content'>VariantGroups</a>.
* @namespace VariantGroup
*/
export function VariantGroup (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/variant_groups`
if (data.variant_group) {
Object.assign(this, cloneDeep(data.variant_group))
this.urlPath += `/${this.uid}`
/**
* @description The Update VariantGroup call lets you update the name and description of an existing VariantGroup.
* @memberof VariantGroup
* @func update
* @returns {Promise<VariantGroup.VariantGroup>} Promise for VariantGroup instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const up_data = {name: 'update name'}
* client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').update(up_data)
* .then((variant_group) => console.log(variant_group))
*
*/
this.update = async (data) => {
try {
const response = await http.put(this.urlPath,
data,
{
headers: {
...cloneDeep(this.stackHeaders)
}
})
if (response.data) {
return response.data
} else {
return error(response)
}
} catch (err) {
return error(err)
}
}
/**
* @description The Delete VariantGroup call is used to delete an existing VariantGroup permanently from your Stack.
* @memberof VariantGroup
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
/**
* @description Content type defines the structure or schema of a page or a section of your web or mobile property.
* @param {String} uid The UID of the ContentType you want to get details.
* @returns {ContenType} Instace of ContentType.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).VariantGroup('variant_group_uid').variants('variant_uid').fetch()
* .then((Variants) => console.log(Variants))
*/
this.variants = (uid = null) => {
const data = { stackHeaders: this.stackHeaders }
data.variant_group_uid = this.uid
if (uid) {
data.variants = { uid: uid }
}
return new Variants(http, data)
}
} else {
/**
* @description The Create a variant group call creates a new variant group in a particular stack of your Contentstack account.
* @memberof VariantGroup
* @func create
* @returns {Promise<VariantGroup.VariantGroup>} Promise for VariantGroup instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const variant_group = {
* "name": "Colors",
* "content_types": [
* "iphone_product_page"
* ],
* "uid": "iphone_color_white", // optional
* }
* client.stack().VariantGroup().create({ variant_group } )
* .then((variant_group) => console.log(variant_group))
*/
this.create = async (data) => {
try {
const response = await http.post(`${this.urlPath}`,
data ,
{
headers: {
...cloneDeep(this.stackHeaders)
}
})
if (response.data) {
return response.data
} else {
return error(response)
}
} catch (err) {
return error(err)
}
}
/**
* @description The Query on Variant Groups will allow to fetch details of all or specific Variant Groups
* @memberof VariantGroup
* @func query
* @param {Boolean} include_count Set this to 'true' to include in response the total count of content types available in your stack.
* @returns {Array<VariantGroup>} Array of ContentTyoe.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack(api_key).VariantGroup().query({ query: { code: 'variant_group-code' } }).find()
* .then((variant_groups) => console.log(variant_groups))
*/
this.query = query({ http: http, wrapperCollection: VariantGroupCollection })
}
return this
}
export function VariantGroupCollection (http, data) {
const obj = cloneDeep(data.variant_groups) || []
const variant_groupCollection = obj.map((userdata) => {
return new VariantGroup(http, { variant_group: userdata, stackHeaders: data.stackHeaders })
})
return variant_groupCollection
}