-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
159 lines (150 loc) · 5.58 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
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'
/**
* GlobalField defines the structure or schema of a page or a section of your web or mobile property. To create global Fields for your application, you are required to first create a gloabl field. Read more about <a href='https://www.contentstack.com/docs/guide/global-fields'>Global Fields</a>.
* @namespace GlobalField
*/
export function GlobalField (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/global_fields`
if (data.global_field) {
Object.assign(this, cloneDeep(data.global_field))
this.urlPath = `/global_fields/${this.uid}`
/**
* @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
* @memberof GlobalField
* @func update
* @returns {Promise<GlobalField.GlobalField>} Promise for GlobalField instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').fetch()
* .then((globalField) => {
* globalField.title = 'My New Content Type'
* globalField.description = 'Content Type description'
* return globalField.update()
* })
* .then((globalField) => console.log(globalField))
*
*/
this.update = update(http, 'global_field')
/**
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
* @memberof GlobalField
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
/**
* @description The fetch GlobalField call fetches GlobalField details.
* @memberof GlobalField
* @func fetch
* @returns {Promise<GlobalField.GlobalField>} Promise for GlobalField instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').fetch()
* .then((globalField) => console.log(globalField))
*
*/
this.fetch = fetch(http, 'global_field')
} else {
/**
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
* @memberof GlobalField
* @func create
* @returns {Promise<GlobalField.GlobalField>} Promise for GlobalField instance
*
* @example
* import * as contentstac k from '@contentstack/management'
* const client = contentstack.client()
* const global_field = {
* title: 'First',
* uid: 'first',
* schema: [{
* display_name: 'Name',
* uid: 'name',
* data_type: 'text'
* }]
* }
* client.stack().globalField().create({ global_field })
* .then((globalField) => console.log(globalField))
*/
this.create = create({ http: http })
/**
* @description The Query on GlobalField will allow to fetch details of all or specific GlobalField
* @memberof GlobalField
* @func query
* @returns {Array<GlobalField>} Array of GlobalField.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().globalField().query({ query: { name: 'Global Field Name' } }).find()
* .then((globalFields) => console.log(globalFields))
*/
this.query = query({ http: http, wrapperCollection: GlobalFieldCollection })
/**
* @description The Import a global field call imports a global field into a stack.
* @memberof GlobalField
* @func import
* @param {String} data.global_field path to file
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const data = {
* global_field: 'path/to/file.json',
* }
* client.stack({ api_key: 'api_key'}).globalField().import(data, { overwrite: true })
* .then((globalField) => console.log(globalField))
*
*/
this.import = async function (data, params = {}) {
try {
const response = await upload({
http: http,
urlPath: `${this.urlPath}/import`,
stackHeaders: this.stackHeaders,
formData: createFormData(data),
params: params
})
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders))
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
}
return this
}
export function GlobalFieldCollection (http, data) {
const obj = cloneDeep(data.global_fields) || []
const globalFieldCollection = obj.map((userdata) => {
return new GlobalField(http, { global_field: userdata, stackHeaders: data.stackHeaders })
})
return globalFieldCollection
}
export function createFormData (data) {
return () => {
const formData = new FormData()
const uploadStream = createReadStream(data.global_field)
formData.append('global_field', uploadStream)
return formData
}
}