Skip to content

Commit 594b188

Browse files
api version conflict issue resolved (#248)
* Added support for nested global fields * Added updated method * Added update method testcases * Added mock file * Fixed PR comments * Fixed api version conflicts * Removed duplicate code * Added released version and removed duplicate code
1 parent 494bb51 commit 594b188

File tree

10 files changed

+440
-341
lines changed

10 files changed

+440
-341
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## [v1.19.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.2) (2025-01-27)
3+
- Enhancement
4+
- Added support for nested global fields.
25
## [v1.18.4](https://github.com/contentstack/contentstack-management-javascript/tree/v1.18.4) (2024-11-22)
36
- Enhancement
47
- Added support for response headers.

lib/entity.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ export const create = ({ http, params }) => {
105105
}
106106
}
107107

108-
export const query = ({ http, wrapperCollection }) => {
108+
export const query = ({ http, wrapperCollection, apiVersion }) => {
109109
return function (params = {}) {
110110
const headers = {
111-
...cloneDeep(this.stackHeaders)
111+
...cloneDeep(this.stackHeaders),
112+
...(apiVersion != null ? { api_version: apiVersion } : {})
112113
}
113114
if (this.organization_uid) {
114115
headers.organization_uid = this.organization_uid

lib/stack/globalField/index.js

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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,7 +39,32 @@ export function GlobalField (http, data = {}) {
4039
* .then((globalField) => console.log(globalField))
4140
*
4241
*/
43-
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+
4468

4569
/**
4670
* @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
@@ -104,7 +128,35 @@ export function GlobalField (http, data = {}) {
104128
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
105129
* .then((response) => console.log(response.notice))
106130
*/
107-
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+
108160

109161
/**
110162
* @description The fetch GlobalField call fetches GlobalField details.
@@ -119,7 +171,30 @@ export function GlobalField (http, data = {}) {
119171
* .then((globalField) => console.log(globalField))
120172
*
121173
*/
122-
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+
123198
} else {
124199
/**
125200
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
@@ -142,7 +217,27 @@ export function GlobalField (http, data = {}) {
142217
* client.stack().globalField().create({ global_field })
143218
* .then((globalField) => console.log(globalField))
144219
*/
145-
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+
146241

147242
/**
148243
* @description The Query on GlobalField will allow to fetch details of all or specific GlobalField
@@ -157,7 +252,7 @@ export function GlobalField (http, data = {}) {
157252
* client.stack().globalField().query({ query: { name: 'Global Field Name' } }).find()
158253
* .then((globalFields) => console.log(globalFields))
159254
*/
160-
this.query = query({ http: http, wrapperCollection: GlobalFieldCollection })
255+
this.query = query({ http: http, wrapperCollection: GlobalFieldCollection, apiVersion: this.apiVersion })
161256

162257
/**
163258
* @description The Import a global field call imports a global field into a stack.

lib/stack/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ export function Stack (http, data) {
173173
} else if (globalFieldUidOrOptions) {
174174
data.global_field = { uid: globalFieldUidOrOptions };
175175
}
176-
177-
if (options?.api_version) {
176+
177+
// Safely handle `options` and check for `api_version`
178+
options = options || {}; // Ensure `options` is always an object
179+
if (options && typeof options === 'object' && options.api_version) {
178180
data.api_version = options.api_version;
179181
if (options.api_version === '3.2') {
180182
data.nested_global_fields = true;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/management",
3-
"version": "1.19.0",
3+
"version": "1.19.2",
44
"description": "The Content Management API is used to manage the content of your Contentstack account",
55
"main": "./dist/node/contentstack-management.js",
66
"browser": "./dist/web/contentstack-management.js",

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

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe("Global Field api Test", () => {
2020
makeGlobalField()
2121
.create(createGlobalField)
2222
.then((globalField) => {
23+
globalField = globalField.global_field;
2324
expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid);
2425
expect(globalField.title).to.be.equal(
2526
createGlobalField.global_field.title
@@ -42,6 +43,7 @@ describe("Global Field api Test", () => {
4243
makeGlobalField(createGlobalField.global_field.uid)
4344
.fetch()
4445
.then((globalField) => {
46+
globalField = globalField.global_field;
4547
expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid);
4648
expect(globalField.title).to.be.equal(
4749
createGlobalField.global_field.title
@@ -60,38 +62,11 @@ describe("Global Field api Test", () => {
6062
.catch(done);
6163
});
6264

63-
it("should fetch and update global Field", (done) => {
64-
makeGlobalField(createGlobalField.global_field.uid)
65-
.fetch()
66-
.then((globalField) => {
67-
globalField.title = "Update title";
68-
return globalField.update();
69-
})
70-
.then((updateGlobal) => {
71-
expect(updateGlobal.uid).to.be.equal(
72-
createGlobalField.global_field.uid
73-
);
74-
expect(updateGlobal.title).to.be.equal("Update title");
75-
expect(updateGlobal.schema[0].uid).to.be.equal(
76-
createGlobalField.global_field.schema[0].uid
77-
);
78-
expect(updateGlobal.schema[0].data_type).to.be.equal(
79-
createGlobalField.global_field.schema[0].data_type
80-
);
81-
expect(updateGlobal.schema[0].display_name).to.be.equal(
82-
createGlobalField.global_field.schema[0].display_name
83-
);
84-
done();
85-
})
86-
.catch(done);
87-
});
88-
8965
it("should update global Field", (done) => {
90-
const globalField = makeGlobalField(createGlobalField.global_field.uid);
91-
Object.assign(globalField, cloneDeep(createGlobalField.global_field));
92-
globalField
93-
.update()
66+
makeGlobalField(createGlobalField.global_field.uid)
67+
.update(createGlobalField)
9468
.then((updateGlobal) => {
69+
updateGlobal = updateGlobal.global_field;
9570
expect(updateGlobal.uid).to.be.equal(
9671
createGlobalField.global_field.uid
9772
);
@@ -140,6 +115,7 @@ describe("Global Field api Test", () => {
140115
.catch(done);
141116
});
142117

118+
143119
it("should get global field title matching Upload", (done) => {
144120
makeGlobalField()
145121
.query({ query: { title: "Upload" } })
@@ -162,7 +138,6 @@ describe("Global Field api Test", () => {
162138
collection.items.forEach((globalField) => {
163139
expect(globalField.uid).to.be.not.equal(null);
164140
expect(globalField.title).to.be.not.equal(null);
165-
expect(globalField.schema).to.be.not.equal(null);
166141
});
167142
done();
168143
})
@@ -173,7 +148,7 @@ describe("Global Field api Test", () => {
173148
it('should create nested global field for reference', done => {
174149
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalFieldForReference)
175150
.then(globalField => {
176-
expect(globalField.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
151+
expect(globalField.global_field.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
177152
done();
178153
})
179154
.catch(err => {
@@ -185,7 +160,7 @@ describe("Global Field api Test", () => {
185160
it('should create nested global field', done => {
186161
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField)
187162
.then(globalField => {
188-
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
163+
expect(globalField.global_field.uid).to.be.equal(createNestedGlobalField.global_field.uid);
189164
done();
190165
})
191166
.catch(err => {
@@ -197,7 +172,7 @@ describe("Global Field api Test", () => {
197172
it('should fetch nested global field', done => {
198173
makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
199174
.then(globalField => {
200-
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
175+
expect(globalField.global_field.uid).to.be.equal(createNestedGlobalField.global_field.uid);
201176
done();
202177
})
203178
.catch(err => {
@@ -207,8 +182,8 @@ describe("Global Field api Test", () => {
207182
});
208183

209184
it('should update nested global fields without fetch', done => {
210-
makeGlobalField(createNestedGlobalField.global_field.uid, { headers: { api_version: '3.2' }})
211-
.updateNestedGlobalField(createNestedGlobalField)
185+
makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' })
186+
.update(createNestedGlobalField)
212187
.then((globalField) => {
213188
expect(globalField.global_field.schema.length).to.be.equal(2)
214189
done()

0 commit comments

Comments
 (0)