Skip to content

Commit d72b676

Browse files
fix: handle api_version chaining and ensure backward compatibility
1 parent f2c7adc commit d72b676

File tree

3 files changed

+37
-92
lines changed

3 files changed

+37
-92
lines changed

lib/entity.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ export const publishUnpublish = async (http, url, httpBody, headers, locale = nu
4545
const response = await http.post(url, httpBody, headers)
4646
if (response.data) {
4747
const data = response.data || {}
48+
if (http?.httpClientParams?.headers?.api_version) {
49+
delete http.httpClientParams.headers.api_version
50+
}
51+
if (headers?.api_version) {
52+
delete headers.api_version
53+
}
4854
if (headers) {
4955
data.stackHeaders = headers
5056
}

lib/stack/globalField/index.js

Lines changed: 31 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ import { createReadStream } from 'fs'
1111

1212
export 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
}

test/sanity-check/api/globalfield-test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ describe('Global Field api Test', () => {
1919
makeGlobalField()
2020
.create(createGlobalField)
2121
.then((globalField) => {
22-
globalField = globalField.global_field
2322
expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid)
2423
expect(globalField.title).to.be.equal(
2524
createGlobalField.global_field.title
@@ -42,7 +41,6 @@ describe('Global Field api Test', () => {
4241
makeGlobalField(createGlobalField.global_field.uid)
4342
.fetch()
4443
.then((globalField) => {
45-
globalField = globalField.global_field
4644
expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid)
4745
expect(globalField.title).to.be.equal(
4846
createGlobalField.global_field.title
@@ -65,7 +63,6 @@ describe('Global Field api Test', () => {
6563
makeGlobalField(createGlobalField.global_field.uid)
6664
.update(createGlobalField)
6765
.then((updateGlobal) => {
68-
updateGlobal = updateGlobal.global_field
6966
expect(updateGlobal.uid).to.be.equal(
7067
createGlobalField.global_field.uid
7168
)

0 commit comments

Comments
 (0)