@@ -11,10 +11,8 @@ 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
14+ if ( data . api_version ) {
15+ this . apiVersion = data . api_version
1816 }
1917 this . urlPath = `/global_fields`
2018
@@ -41,80 +39,31 @@ export function GlobalField (http, data = {}) {
4139 */
4240 this . update = async ( config ) => {
4341 try {
44- // Add `api_version` to headers if `this.apiVersion` is defined
4542 if ( this . apiVersion ) {
4643 this . stackHeaders . api_version = this . apiVersion
4744 }
4845 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 }
5260 }
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 )
5562 if ( this . apiVersion ) {
5663 delete this . stackHeaders . api_version
5764 }
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 ) )
11867 } else {
11968 throw error ( response )
12069 }
@@ -138,7 +87,6 @@ export function GlobalField (http, data = {}) {
13887 this . delete = async ( ) => {
13988 const param = { }
14089 try {
141- // Add `api_version` to headers if `this.apiVersion` is defined
14290 if ( this . apiVersion ) {
14391 this . stackHeaders . api_version = this . apiVersion
14492 }
@@ -154,12 +102,8 @@ export function GlobalField (http, data = {}) {
154102 if ( this . apiVersion ) {
155103 delete this . stackHeaders . api_version
156104 }
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
163107 } else {
164108 throw error ( response )
165109 }
@@ -195,12 +139,11 @@ export function GlobalField (http, data = {}) {
195139 }
196140 }
197141 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 ) )
204147 } else {
205148 throw error ( response )
206149 }
@@ -241,12 +184,11 @@ export function GlobalField (http, data = {}) {
241184 }
242185 }
243186 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 ) )
250192 } else {
251193 throw error ( response )
252194 }
0 commit comments