Skip to content

Commit a06184c

Browse files
Fixed api version conflicts
1 parent ff4a22d commit a06184c

File tree

6 files changed

+424
-379
lines changed

6 files changed

+424
-379
lines changed

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: 93 additions & 48 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,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.

lib/stack/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ export function Stack (http, data) {
174174
data.global_field = { uid: globalFieldUidOrOptions };
175175
}
176176

177-
if (options?.api_version) {
177+
178+
// Safely handle `options` and check for `api_version`
179+
options = options || {}; // Ensure `options` is always an object
180+
if (options && typeof options === 'object' && options.api_version) {
178181
data.api_version = options.api_version;
179182
if (options.api_version === '3.2') {
180183
data.nested_global_fields = true;

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

Lines changed: 10 additions & 35 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
);
@@ -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)