@@ -14,8 +14,7 @@ export function GlobalField (http, data = {}) {
1414 this . apiVersion = data . api_version || undefined ;
1515
1616 if ( this . apiVersion ) {
17- http . defaults . headers . api_version = this . apiVersion ;
18- http . httpClientParams . headers . api_version = this . apiVersion ;
17+ this . stackHeaders . api_version = this . apiVersion ;
1918 }
2019 this . urlPath = `/global_fields`
2120
@@ -40,57 +39,32 @@ export function GlobalField (http, data = {}) {
4039 * .then((globalField) => console.log(globalField))
4140 *
4241 */
43- this . update = update ( http , 'global_field' )
44-
45- /**
46- * @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
47- * @memberof GlobalField
48- * @func update
49- * @returns {Promise<GlobalField.GlobalField> } Promise for GlobalField instance
50- * @example
51- * import * as contentstack from '@contentstack/management'
52- * const client = contentstack.client()
53- * const data = {
54- * "global_field": {
55- * "title": "Nested Global Field33",
56- * "uid": "nested_global_field33",
57- * "schema": [
58- * {
59- * "data_type": "text",
60- * "display_name": "Single Line Textbox",
61- * "uid": "single_line"
62- * },
63- * {
64- * "data_type": "global_field",
65- * "display_name": "Global",
66- * "uid": "global_field",
67- * "reference_to": "nested_global_field_123"
68- * }
69- * ]
70- * }
71- * }
72- * client.stack({ api_key: 'api_key'}).globalField('global_field_uid').updateNestedGlobalField(data, { headers: { api_version: '3.2' }})
73- * .then((globalField) => {
74- console.log(globalField)
75- * })
76- */
77- this . updateNestedGlobalField = async ( config , headers = { } ) => {
78- const apiVersion = { api_version : '3.2' }
79- this . stackHeaders = { ...this . stackHeaders , ...apiVersion , ...headers }
42+ this . update = async ( config ) => {
8043 try {
44+ // Add `api_version` to headers if `this.apiVersion` is defined
45+ if ( this . apiVersion ) {
46+ this . stackHeaders . api_version = this . apiVersion ;
47+ }
8148 const headers = {
82- headers : { ...cloneDeep ( this . stackHeaders ) }
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 ;
8357 }
84- const response = await http . put ( `${ this . urlPath } ` , config , headers )
8558 if ( response . data ) {
86- return response . data
59+ return response . data ;
8760 } else {
88- throw error ( response )
61+ throw error ( response ) ;
8962 }
9063 } catch ( err ) {
91- throw error ( err )
64+ throw error ( err ) ;
9265 }
9366 }
67+
9468
9569 /**
9670 * @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
@@ -104,7 +78,35 @@ export function GlobalField (http, data = {}) {
10478 * client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
10579 * .then((response) => console.log(response.notice))
10680 */
107- this . delete = deleteEntity ( http )
81+ this . delete = async ( ) => {
82+ let param = { } ;
83+ try {
84+ // Add `api_version` to headers if `this.apiVersion` is defined
85+ if ( this . apiVersion ) {
86+ this . stackHeaders . api_version = this . apiVersion ;
87+ }
88+ const headers = {
89+ headers : {
90+ ...cloneDeep ( this . stackHeaders )
91+ } ,
92+ params : {
93+ ...cloneDeep ( param )
94+ }
95+ } ;
96+ const response = await http . delete ( this . urlPath , headers ) ;
97+ if ( this . apiVersion ) {
98+ delete this . stackHeaders . api_version ;
99+ }
100+ if ( response . data ) {
101+ return response . data ;
102+ } else {
103+ throw error ( response ) ;
104+ }
105+ } catch ( err ) {
106+ throw error ( err ) ;
107+ }
108+ } ;
109+
108110
109111 /**
110112 * @description The fetch GlobalField call fetches GlobalField details.
@@ -119,7 +121,30 @@ export function GlobalField (http, data = {}) {
119121 * .then((globalField) => console.log(globalField))
120122 *
121123 */
122- this . fetch = fetch ( http , 'global_field' )
124+ this . fetch = async function ( param = { } ) {
125+ try {
126+ if ( this . apiVersion ) {
127+ this . stackHeaders . api_version = this . apiVersion ;
128+ }
129+ const headers = {
130+ headers : {
131+ ...cloneDeep ( this . stackHeaders )
132+ } ,
133+ params : {
134+ ...cloneDeep ( param )
135+ }
136+ } ;
137+ const response = await http . get ( this . urlPath , headers ) ;
138+ if ( response . data ) {
139+ return response . data ;
140+ } else {
141+ throw error ( response ) ;
142+ }
143+ } catch ( err ) {
144+ throw error ( err ) ;
145+ }
146+ } ;
147+
123148 } else {
124149 /**
125150 * @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
@@ -142,7 +167,27 @@ export function GlobalField (http, data = {}) {
142167 * client.stack().globalField().create({ global_field })
143168 * .then((globalField) => console.log(globalField))
144169 */
145- this . create = create ( { http : http } )
170+ this . create = async ( data ) => {
171+ try {
172+ if ( this . apiVersion ) {
173+ this . stackHeaders . api_version = this . apiVersion ;
174+ }
175+ const headers = {
176+ headers : {
177+ ...cloneDeep ( this . stackHeaders )
178+ }
179+ } ;
180+ const response = await http . post ( `${ this . urlPath } ` , data , headers ) ;
181+ if ( response . data ) {
182+ return response . data ;
183+ } else {
184+ return error ( response ) ;
185+ }
186+ } catch ( err ) {
187+ return error ( err ) ;
188+ }
189+ } ;
190+
146191
147192 /**
148193 * @description The Query on GlobalField will allow to fetch details of all or specific GlobalField
@@ -157,7 +202,7 @@ export function GlobalField (http, data = {}) {
157202 * client.stack().globalField().query({ query: { name: 'Global Field Name' } }).find()
158203 * .then((globalFields) => console.log(globalFields))
159204 */
160- this . query = query ( { http : http , wrapperCollection : GlobalFieldCollection } )
205+ this . query = query ( { http : http , wrapperCollection : GlobalFieldCollection , apiVersion : this . apiVersion } )
161206
162207 /**
163208 * @description The Import a global field call imports a global field into a stack.
0 commit comments