-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
189 lines (181 loc) · 6.67 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import cloneDeep from 'lodash/cloneDeep'
import { create, update, deleteEntity, fetch, query, upload, parseData } from '../../entity'
import error from '../../core/contentstackError'
import FormData from 'form-data'
import { createReadStream } from 'fs'
/**
* Extensions let you create custom fields and custom widgets that lets you customize Contentstack's default UI and behavior. Read more about <a href='https://www.contentstack.com/docs/developers/about-experience-extensions/'>Extensions</a>.
* @namespace Extension
* */
export function Extension (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/extensions`
if (data.extension) {
Object.assign(this, cloneDeep(data.extension))
this.urlPath = `/extensions/${this.uid}`
/**
* @description The Update Extension call lets you update an existing Extension.
* @memberof Extension
* @func update
* @returns {Promise<Extension.Extension>} Promise for Extension instance.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).extension('extension_uid').fetch()
* .then((extension) => {
* extension.title = 'My Extension Type'
* return extension.update()
* })
* .then((extension) => console.log(extension))
*
*/
this.update = update(http, 'extension')
/**
* @description The Delete Extension call is used to delete an existing Extension permanently from your Stack.
* @memberof Extension
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).extension('extension_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
/**
* @description The fetch Extension call fetches Extension details.
* @memberof Extension
* @func fetch
* @returns {Promise<Extension.Extension>} Promise for Extension instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).extension('extension_uid').fetch()
* .then((extension) => console.log(extension))
*
*/
this.fetch = fetch(http, 'extension')
} else {
/**
* @description The Upload is used to upload a new custom widget, custom field, dashboard Widget to a stack.
* @memberof Extension
* @func upload
* @returns {Promise<Extension.Extension>} Promise for Extension instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const extension = {
* upload: 'path/to/file',
* title: 'Title',
* tags: [
* 'tag1',
* 'tag2'
* ],
* data_type: 'text',
* title: 'Old Extension',
* multiple: false,
* config: {},
* type: 'Type of extenstion you want to create widget/dashboard/field'
* }
*
* client.stack({ api_key: 'api_key'}).extension().upload(extension)
* .then((extension) => console.log(extension))
*/
this.upload = async function (data, params) {
try {
const response = await upload({ http: http, urlPath: this.urlPath, stackHeaders: this.stackHeaders, formData: createExtensionFormData(data), params: params })
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders))
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
/**
* @description The Create a extension call creates a new extension in a particular stack of your Contentstack account.
* @memberof Extension
* @func create
* @returns {Promise<Extension.Extension>} Promise for Extension instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const extension = {
* tags: [
* 'tag1',
* 'tag2'
* ],
* data_type: 'text',
* title: 'Old Extension',
* src: "Enter either the source code (use 'srcdoc') or the external hosting link of the extension depending on the hosting method you selected.",
* multiple: false,
* config: {},
* type: 'field'
* }
*
* client.stack().extension().create({ extension })
* .then((extension) => console.log(extension))
*/
this.create = create({ http: http })
/**
* @description The Query on Content Type will allow to fetch details of all or specific Content Type
* @memberof Extension
* @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<Extension>} Array of ContentTyoe.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().extension().query({ query={"type":"field"}})
* .then((extensions) => console.log(extensions))
*/
this.query = query({ http: http, wrapperCollection: ExtensionCollection })
}
}
export function ExtensionCollection (http, data) {
const obj = cloneDeep(data.extensions) || []
const extensionCollection = obj.map((extensiondata) => {
return new Extension(http, { extension: extensiondata, stackHeaders: data.stackHeaders })
})
return extensionCollection
}
export function createExtensionFormData (data) {
return () => {
const formData = new FormData()
if (typeof data.title === 'string') {
formData.append('extension[title]', data.title)
}
if (typeof data.scope === 'object') {
formData.append('extension[scope]', `${data.scope}`)
}
if (typeof data['data_type'] === 'string') {
formData.append('extension[data_type]', data['data_type'])
}
if (typeof data.type === 'string') {
formData.append('extension[type]', data.type)
}
if (data.tags instanceof Array) {
formData.append('extension[tags]', data.tags.join(','))
} else if (typeof data.tags === 'string') {
formData.append('extension[tags]', data.tags)
}
if (typeof data.multiple === 'boolean') {
formData.append('extension[multiple]', `${data.multiple}`)
}
if (typeof data.enable === 'boolean') {
formData.append('extension[enable]', `${data.enable}`)
}
const uploadStream = createReadStream(data.upload)
formData.append('extension[upload]', uploadStream)
return formData
}
}