-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
134 lines (127 loc) · 4.48 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
import cloneDeep from 'lodash/cloneDeep'
import {
deleteEntity,
fetch,
query,
create
} from '../../entity'
/**
* Variantss allow you to group a collection of content within a stack. Using variants you can group content types that need to work together. Read more about <a href='https://www.contentstack.com/docs/developers/create-content-types/manage-variants'>Variantss</a>.
* @namespace Variants
*/
export function Variants (http, data) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/variants`
if (data.variants) {
Object.assign(this, cloneDeep(data.variants))
this.urlPath += `/${this.uid}`
/**
* @description The Delete variants call is used to delete a specific variants.
* @memberof Variants
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).variants('variants_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
/**
* @description The fetch Variants returns information about a particular variants of a stack.
* @memberof Variants
* @func fetch
* @returns {Promise<Variants.Variants>} Promise for Variants instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).variants('variants_uid').fetch()
* .then((variants) => console.log(variants))
*
*/
this.fetch = fetch(http, 'variants')
} else {
/**
* @description The Create an variants call creates a new variants.
* @memberof Variants
* @func create
* @returns {Promise<Variants.Variants>} Promise for Variants instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const variants = {
* "uid": "iphone_color_white", // optional
* "name": "White",
* "personalize_metadata": {
* "experience_uid": "exp1",
* "experience_short_uid": "expShortUid1",
* "project_uid": "project_uid1",
* "variant_short_uid": "variantShort_uid1"
* },
* }
* client.stack().variants().create({ variants })
* .then((variants) => console.log(variants))
*/
this.create = create({ http })
/**
* @description The Query on Variants will allow to fetch details of all or specific Variants.
* @memberof Variants
* @param {Object} params - URI parameters
* @prop {Object} params.query - Queries that you can use to fetch filtered results.
* @func query
* @returns {Array<Variants>} Array of Variants.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().variants().query({ query: { name: 'Variants Name' } }).find()
* .then((variants) => console.log(variants))
*/
this.query = query({ http: http, wrapperCollection: VariantsCollection })
/**
* @description The fetchByUIDs on Variants will allow to fetch details of specific Variants UID.
* @memberof Variants
* @param {Object} params - URI parameters
* @prop {Object} params.query - fetchByUIDs that you can use to fetch filtered results.
* @func query
* @returns {Array<Variants>} Array of Variants.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().variants().fetchByUIDs(['uid1','uid2']).find()
* .then((variants) => console.log(variants))
*/
this.fetchByUIDs = async (variantUids) => {
try {
const response = await http.get(this.urlPath, {
params: {
uids: variantUids
},
headers: {
...cloneDeep(this.stackHeaders)
}
})
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
}
}
export function VariantsCollection (http, data) {
const obj = cloneDeep(data.variants) || []
const VariantsCollection = obj.map((userdata) => {
return new Variants(http, { variants: userdata, stackHeaders: data.stackHeaders })
})
return VariantsCollection
}