1
1
import cloneDeep from 'lodash/cloneDeep'
2
- import { query , upload , parseData } from '../../entity'
2
+ import { create , update , deleteEntity , fetch , query , upload , parseData } from '../../entity'
3
3
import error from '../../core/contentstackError'
4
4
import FormData from 'form-data'
5
5
import { createReadStream } from 'fs'
@@ -11,10 +11,8 @@ import { createReadStream } from 'fs'
11
11
12
12
export function GlobalField ( http , data = { } ) {
13
13
this . stackHeaders = data . stackHeaders
14
- this . apiVersion = data . api_version || undefined
15
-
16
- if ( this . apiVersion ) {
17
- this . stackHeaders . api_version = this . apiVersion
14
+ if ( data . api_version ) {
15
+ this . stackHeaders . api_version = data . api_version
18
16
}
19
17
this . urlPath = `/global_fields`
20
18
@@ -39,89 +37,7 @@ export function GlobalField (http, data = {}) {
39
37
* .then((globalField) => console.log(globalField))
40
38
*
41
39
*/
42
- this . update = async ( config ) => {
43
- try {
44
- // Add `api_version` to headers if `this.apiVersion` is defined
45
- if ( this . apiVersion ) {
46
- this . stackHeaders . api_version = this . apiVersion
47
- }
48
- const headers = {
49
- headers : {
50
- ...cloneDeep ( this . stackHeaders )
51
- }
52
- }
53
- const response = await http . put ( `${ this . urlPath } ` , config , headers )
54
- // Remove `api_version` from headers after fetching data
55
- if ( this . apiVersion ) {
56
- delete this . stackHeaders . api_version
57
- }
58
- const data = response . data
59
- if ( data ) {
60
- if ( this . stackHeaders ) {
61
- data . stackHeaders = this . stackHeaders
62
- }
63
- return data
64
- } else {
65
- throw error ( response )
66
- }
67
- } catch ( err ) {
68
- throw error ( err )
69
- }
70
- }
71
-
72
- /**
73
- * @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
74
- * @memberof GlobalField
75
- * @func update
76
- * @returns {Promise<GlobalField.GlobalField> } Promise for GlobalField instance
77
- * @example
78
- * import * as contentstack from '@contentstack/management'
79
- * const client = contentstack.client()
80
- * const data = {
81
- * "global_field": {
82
- * "title": "Nested Global Field33",
83
- * "uid": "nested_global_field33",
84
- * "schema": [
85
- * {
86
- * "data_type": "text",
87
- * "display_name": "Single Line Textbox",
88
- * "uid": "single_line"
89
- * },
90
- * {
91
- * "data_type": "global_field",
92
- * "display_name": "Global",
93
- * "uid": "global_field",
94
- * "reference_to": "nested_global_field_123"
95
- * }
96
- * ]
97
- * }
98
- * }
99
- * client.stack({ api_key: 'api_key'}).globalField('global_field_uid').updateNestedGlobalField(data, { headers: { api_version: '3.2' }})
100
- * .then((globalField) => {
101
- console.log(globalField)
102
- * })
103
- */
104
- this . updateNestedGlobalField = async ( config , headers = { } ) => {
105
- const apiVersion = { api_version : '3.2' }
106
- this . stackHeaders = { ...this . stackHeaders , ...apiVersion , ...headers }
107
- try {
108
- const headers = {
109
- headers : { ...cloneDeep ( this . stackHeaders ) }
110
- }
111
- const response = await http . put ( `${ this . urlPath } ` , config , headers )
112
- const data = response . data
113
- if ( data ) {
114
- if ( this . stackHeaders ) {
115
- data . stackHeaders = this . stackHeaders
116
- }
117
- return data
118
- } else {
119
- throw error ( response )
120
- }
121
- } catch ( err ) {
122
- throw error ( err )
123
- }
124
- }
40
+ this . update = update ( http , 'global_field' )
125
41
126
42
/**
127
43
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
@@ -135,38 +51,7 @@ export function GlobalField (http, data = {}) {
135
51
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
136
52
* .then((response) => console.log(response.notice))
137
53
*/
138
- this . delete = async ( ) => {
139
- const param = { }
140
- try {
141
- // Add `api_version` to headers if `this.apiVersion` is defined
142
- if ( this . apiVersion ) {
143
- this . stackHeaders . api_version = this . apiVersion
144
- }
145
- const headers = {
146
- headers : {
147
- ...cloneDeep ( this . stackHeaders )
148
- } ,
149
- params : {
150
- ...cloneDeep ( param )
151
- }
152
- }
153
- const response = await http . delete ( this . urlPath , headers )
154
- if ( this . apiVersion ) {
155
- delete this . stackHeaders . api_version
156
- }
157
- const data = response . data
158
- if ( data ) {
159
- if ( this . stackHeaders ) {
160
- data . stackHeaders = this . stackHeaders
161
- }
162
- return data
163
- } else {
164
- throw error ( response )
165
- }
166
- } catch ( err ) {
167
- throw error ( err )
168
- }
169
- }
54
+ this . delete = deleteEntity ( http )
170
55
171
56
/**
172
57
* @description The fetch GlobalField call fetches GlobalField details.
@@ -181,33 +66,7 @@ export function GlobalField (http, data = {}) {
181
66
* .then((globalField) => console.log(globalField))
182
67
*
183
68
*/
184
- this . fetch = async function ( param = { } ) {
185
- try {
186
- if ( this . apiVersion ) {
187
- this . stackHeaders . api_version = this . apiVersion
188
- }
189
- const headers = {
190
- headers : {
191
- ...cloneDeep ( this . stackHeaders )
192
- } ,
193
- params : {
194
- ...cloneDeep ( param )
195
- }
196
- }
197
- const response = await http . get ( this . urlPath , headers )
198
- const data = response . data
199
- if ( data ) {
200
- if ( this . stackHeaders ) {
201
- data . stackHeaders = this . stackHeaders
202
- }
203
- return data
204
- } else {
205
- throw error ( response )
206
- }
207
- } catch ( err ) {
208
- throw error ( err )
209
- }
210
- }
69
+ this . fetch = fetch ( http , 'global_field' )
211
70
} else {
212
71
/**
213
72
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
@@ -230,30 +89,7 @@ export function GlobalField (http, data = {}) {
230
89
* client.stack().globalField().create({ global_field })
231
90
* .then((globalField) => console.log(globalField))
232
91
*/
233
- this . create = async ( payload ) => {
234
- try {
235
- if ( this . apiVersion ) {
236
- this . stackHeaders . api_version = this . apiVersion
237
- }
238
- const headers = {
239
- headers : {
240
- ...cloneDeep ( this . stackHeaders )
241
- }
242
- }
243
- const response = await http . post ( `${ this . urlPath } ` , payload , headers )
244
- const data = response . data
245
- if ( data ) {
246
- if ( this . stackHeaders ) {
247
- data . stackHeaders = this . stackHeaders
248
- }
249
- return data
250
- } else {
251
- throw error ( response )
252
- }
253
- } catch ( err ) {
254
- return error ( err )
255
- }
256
- }
92
+ this . create = create ( { http : http } )
257
93
258
94
/**
259
95
* @description The Query on GlobalField will allow to fetch details of all or specific GlobalField
0 commit comments