@@ -11,6 +11,11 @@ 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 ;
18
+ }
14
19
this . urlPath = `/global_fields`
15
20
16
21
if ( data . global_field ) {
@@ -34,7 +39,82 @@ export function GlobalField (http, data = {}) {
34
39
* .then((globalField) => console.log(globalField))
35
40
*
36
41
*/
37
- this . update = update ( http , 'global_field' )
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
+ if ( response . data ) {
59
+ return response . data ;
60
+ } else {
61
+ throw error ( response ) ;
62
+ }
63
+ } catch ( err ) {
64
+ throw error ( err ) ;
65
+ }
66
+ }
67
+
68
+
69
+ /**
70
+ * @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
71
+ * @memberof GlobalField
72
+ * @func update
73
+ * @returns {Promise<GlobalField.GlobalField> } Promise for GlobalField instance
74
+ * @example
75
+ * import * as contentstack from '@contentstack/management'
76
+ * const client = contentstack.client()
77
+ * const data = {
78
+ * "global_field": {
79
+ * "title": "Nested Global Field33",
80
+ * "uid": "nested_global_field33",
81
+ * "schema": [
82
+ * {
83
+ * "data_type": "text",
84
+ * "display_name": "Single Line Textbox",
85
+ * "uid": "single_line"
86
+ * },
87
+ * {
88
+ * "data_type": "global_field",
89
+ * "display_name": "Global",
90
+ * "uid": "global_field",
91
+ * "reference_to": "nested_global_field_123"
92
+ * }
93
+ * ]
94
+ * }
95
+ * }
96
+ * client.stack({ api_key: 'api_key'}).globalField('global_field_uid').updateNestedGlobalField(data, { headers: { api_version: '3.2' }})
97
+ * .then((globalField) => {
98
+ console.log(globalField)
99
+ * })
100
+ */
101
+ this . updateNestedGlobalField = async ( config , headers = { } ) => {
102
+ const apiVersion = { api_version : '3.2' }
103
+ this . stackHeaders = { ...this . stackHeaders , ...apiVersion , ...headers }
104
+ try {
105
+ const headers = {
106
+ headers : { ...cloneDeep ( this . stackHeaders ) }
107
+ }
108
+ const response = await http . put ( `${ this . urlPath } ` , config , headers )
109
+ if ( response . data ) {
110
+ return response . data
111
+ } else {
112
+ throw error ( response )
113
+ }
114
+ } catch ( err ) {
115
+ throw error ( err )
116
+ }
117
+ }
38
118
39
119
/**
40
120
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
@@ -48,7 +128,35 @@ export function GlobalField (http, data = {}) {
48
128
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
49
129
* .then((response) => console.log(response.notice))
50
130
*/
51
- this . delete = deleteEntity ( http )
131
+ this . delete = async ( ) => {
132
+ let param = { } ;
133
+ try {
134
+ // Add `api_version` to headers if `this.apiVersion` is defined
135
+ if ( this . apiVersion ) {
136
+ this . stackHeaders . api_version = this . apiVersion ;
137
+ }
138
+ const headers = {
139
+ headers : {
140
+ ...cloneDeep ( this . stackHeaders )
141
+ } ,
142
+ params : {
143
+ ...cloneDeep ( param )
144
+ }
145
+ } ;
146
+ const response = await http . delete ( this . urlPath , headers ) ;
147
+ if ( this . apiVersion ) {
148
+ delete this . stackHeaders . api_version ;
149
+ }
150
+ if ( response . data ) {
151
+ return response . data ;
152
+ } else {
153
+ throw error ( response ) ;
154
+ }
155
+ } catch ( err ) {
156
+ throw error ( err ) ;
157
+ }
158
+ } ;
159
+
52
160
53
161
/**
54
162
* @description The fetch GlobalField call fetches GlobalField details.
@@ -63,7 +171,30 @@ export function GlobalField (http, data = {}) {
63
171
* .then((globalField) => console.log(globalField))
64
172
*
65
173
*/
66
- this . fetch = fetch ( http , 'global_field' )
174
+ this . fetch = async function ( param = { } ) {
175
+ try {
176
+ if ( this . apiVersion ) {
177
+ this . stackHeaders . api_version = this . apiVersion ;
178
+ }
179
+ const headers = {
180
+ headers : {
181
+ ...cloneDeep ( this . stackHeaders )
182
+ } ,
183
+ params : {
184
+ ...cloneDeep ( param )
185
+ }
186
+ } ;
187
+ const response = await http . get ( this . urlPath , headers ) ;
188
+ if ( response . data ) {
189
+ return response . data ;
190
+ } else {
191
+ throw error ( response ) ;
192
+ }
193
+ } catch ( err ) {
194
+ throw error ( err ) ;
195
+ }
196
+ } ;
197
+
67
198
} else {
68
199
/**
69
200
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
@@ -86,7 +217,27 @@ export function GlobalField (http, data = {}) {
86
217
* client.stack().globalField().create({ global_field })
87
218
* .then((globalField) => console.log(globalField))
88
219
*/
89
- this . create = create ( { http : http } )
220
+ this . create = async ( data ) => {
221
+ try {
222
+ if ( this . apiVersion ) {
223
+ this . stackHeaders . api_version = this . apiVersion ;
224
+ }
225
+ const headers = {
226
+ headers : {
227
+ ...cloneDeep ( this . stackHeaders )
228
+ }
229
+ } ;
230
+ const response = await http . post ( `${ this . urlPath } ` , data , headers ) ;
231
+ if ( response . data ) {
232
+ return response . data ;
233
+ } else {
234
+ return error ( response ) ;
235
+ }
236
+ } catch ( err ) {
237
+ return error ( err ) ;
238
+ }
239
+ } ;
240
+
90
241
91
242
/**
92
243
* @description The Query on GlobalField will allow to fetch details of all or specific GlobalField
@@ -101,7 +252,7 @@ export function GlobalField (http, data = {}) {
101
252
* client.stack().globalField().query({ query: { name: 'Global Field Name' } }).find()
102
253
* .then((globalFields) => console.log(globalFields))
103
254
*/
104
- this . query = query ( { http : http , wrapperCollection : GlobalFieldCollection } )
255
+ this . query = query ( { http : http , wrapperCollection : GlobalFieldCollection , apiVersion : this . apiVersion } )
105
256
106
257
/**
107
258
* @description The Import a global field call imports a global field into a stack.
@@ -119,8 +270,9 @@ export function GlobalField (http, data = {}) {
119
270
* .then((globalField) => console.log(globalField))
120
271
*
121
272
*/
122
- this . import = async function ( data , params = { } ) {
273
+ this . import = async function ( data , params = { } , headers = { } ) {
123
274
try {
275
+ this . stackHeaders = { ...this . stackHeaders , ...headers } ;
124
276
const response = await upload ( {
125
277
http : http ,
126
278
urlPath : `${ this . urlPath } /import` ,
0 commit comments