@@ -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 . apiVersion = data . api_version
18
16
}
19
17
this . urlPath = `/global_fields`
20
18
@@ -41,80 +39,31 @@ export function GlobalField (http, data = {}) {
41
39
*/
42
40
this . update = async ( config ) => {
43
41
try {
44
- // Add `api_version` to headers if `this.apiVersion` is defined
45
42
if ( this . apiVersion ) {
46
43
this . stackHeaders . api_version = this . apiVersion
47
44
}
48
45
const headers = {
49
- headers : {
50
- ...cloneDeep ( this . stackHeaders )
51
- }
46
+ headers : { ...cloneDeep ( this . stackHeaders ) }
47
+ }
48
+ let payload = config
49
+ if ( ! config ) {
50
+ const {
51
+ stackHeaders,
52
+ apiVersion,
53
+ update,
54
+ delete : deleteFn ,
55
+ fetch,
56
+ ...globalFieldPayload
57
+ } = cloneDeep ( this )
58
+
59
+ payload = { global_field : globalFieldPayload }
52
60
}
53
- const response = await http . put ( `${ this . urlPath } ` , config , headers )
54
- // Remove `api_version` from headers after fetching data
61
+ const response = await http . put ( `${ this . urlPath } ` , payload , headers )
55
62
if ( this . apiVersion ) {
56
63
delete this . stackHeaders . api_version
57
64
}
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
65
+ if ( response . data ) {
66
+ return new this . constructor ( http , parseData ( response , this . stackHeaders ) )
118
67
} else {
119
68
throw error ( response )
120
69
}
@@ -138,7 +87,6 @@ export function GlobalField (http, data = {}) {
138
87
this . delete = async ( ) => {
139
88
const param = { }
140
89
try {
141
- // Add `api_version` to headers if `this.apiVersion` is defined
142
90
if ( this . apiVersion ) {
143
91
this . stackHeaders . api_version = this . apiVersion
144
92
}
@@ -154,12 +102,8 @@ export function GlobalField (http, data = {}) {
154
102
if ( this . apiVersion ) {
155
103
delete this . stackHeaders . api_version
156
104
}
157
- const data = response . data
158
- if ( data ) {
159
- if ( this . stackHeaders ) {
160
- data . stackHeaders = this . stackHeaders
161
- }
162
- return data
105
+ if ( response . data ) {
106
+ return response . data
163
107
} else {
164
108
throw error ( response )
165
109
}
@@ -195,12 +139,11 @@ export function GlobalField (http, data = {}) {
195
139
}
196
140
}
197
141
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
142
+ if ( this . apiVersion ) {
143
+ delete this . stackHeaders . api_version
144
+ }
145
+ if ( response . data ) {
146
+ return new this . constructor ( http , parseData ( response , this . stackHeaders ) )
204
147
} else {
205
148
throw error ( response )
206
149
}
@@ -241,12 +184,11 @@ export function GlobalField (http, data = {}) {
241
184
}
242
185
}
243
186
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
187
+ if ( this . apiVersion ) {
188
+ delete this . stackHeaders . api_version
189
+ }
190
+ if ( response . data ) {
191
+ return new this . constructor ( http , parseData ( response , this . stackHeaders ) )
250
192
} else {
251
193
throw error ( response )
252
194
}
0 commit comments