@@ -11,6 +11,11 @@ import { createReadStream } from 'fs'
1111
1212export function GlobalField ( http , data = { } ) {
1313 this . stackHeaders = data . stackHeaders
14+ this . apiVersion = data . api_version || undefined ;
15+
16+ if ( this . apiVersion ) {
17+ this . stackHeaders . api_version = this . apiVersion ;
18+ }
1419 this . urlPath = `/global_fields`
1520
1621 if ( data . global_field ) {
@@ -34,7 +39,82 @@ export function GlobalField (http, data = {}) {
3439 * .then((globalField) => console.log(globalField))
3540 *
3641 */
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+ }
38118
39119 /**
40120 * @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 = {}) {
48128 * client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
49129 * .then((response) => console.log(response.notice))
50130 */
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+
52160
53161 /**
54162 * @description The fetch GlobalField call fetches GlobalField details.
@@ -63,7 +171,30 @@ export function GlobalField (http, data = {}) {
63171 * .then((globalField) => console.log(globalField))
64172 *
65173 */
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+
67198 } else {
68199 /**
69200 * @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 = {}) {
86217 * client.stack().globalField().create({ global_field })
87218 * .then((globalField) => console.log(globalField))
88219 */
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+
90241
91242 /**
92243 * @description The Query on GlobalField will allow to fetch details of all or specific GlobalField
@@ -101,7 +252,7 @@ export function GlobalField (http, data = {}) {
101252 * client.stack().globalField().query({ query: { name: 'Global Field Name' } }).find()
102253 * .then((globalFields) => console.log(globalFields))
103254 */
104- this . query = query ( { http : http , wrapperCollection : GlobalFieldCollection } )
255+ this . query = query ( { http : http , wrapperCollection : GlobalFieldCollection , apiVersion : this . apiVersion } )
105256
106257 /**
107258 * @description The Import a global field call imports a global field into a stack.
@@ -119,8 +270,9 @@ export function GlobalField (http, data = {}) {
119270 * .then((globalField) => console.log(globalField))
120271 *
121272 */
122- this . import = async function ( data , params = { } ) {
273+ this . import = async function ( data , params = { } , headers = { } ) {
123274 try {
275+ this . stackHeaders = { ...this . stackHeaders , ...headers } ;
124276 const response = await upload ( {
125277 http : http ,
126278 urlPath : `${ this . urlPath } /import` ,
0 commit comments