Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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
  • Loading branch information
sunil-lakshman authored Jan 31, 2025
commit 594b188add3c06fe37423f9a3b56a173ef669c81
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## [v1.19.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.2) (2025-01-27)
- Enhancement
- Added support for nested global fields.
## [v1.18.4](https://github.com/contentstack/contentstack-management-javascript/tree/v1.18.4) (2024-11-22)
- Enhancement
- Added support for response headers.
Expand Down
5 changes: 3 additions & 2 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ export const create = ({ http, params }) => {
}
}

export const query = ({ http, wrapperCollection }) => {
export const query = ({ http, wrapperCollection, apiVersion }) => {
return function (params = {}) {
const headers = {
...cloneDeep(this.stackHeaders)
...cloneDeep(this.stackHeaders),
...(apiVersion != null ? { api_version: apiVersion } : {})
}
if (this.organization_uid) {
headers.organization_uid = this.organization_uid
Expand Down
109 changes: 102 additions & 7 deletions lib/stack/globalField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export function GlobalField (http, data = {}) {
this.apiVersion = data.api_version || undefined;

if (this.apiVersion) {
http.defaults.headers.api_version = this.apiVersion;
http.httpClientParams.headers.api_version = this.apiVersion;
this.stackHeaders.api_version = this.apiVersion;
}
this.urlPath = `/global_fields`

Expand All @@ -40,7 +39,32 @@ export function GlobalField (http, data = {}) {
* .then((globalField) => console.log(globalField))
*
*/
this.update = update(http, 'global_field')
this.update = async (config) => {
try {
// Add `api_version` to headers if `this.apiVersion` is defined
if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
}
}
const response = await http.put(`${this.urlPath}`, config, headers);
// Remove `api_version` from headers after fetching data
if (this.apiVersion) {
delete this.stackHeaders.api_version;
}
if (response.data) {
return response.data;
} else {
throw error(response);
}
} catch (err) {
throw error(err);
}
}


/**
* @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
Expand Down Expand Up @@ -104,7 +128,35 @@ export function GlobalField (http, data = {}) {
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
this.delete = async () => {
let param = {};
try {
// Add `api_version` to headers if `this.apiVersion` is defined
if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
},
params: {
...cloneDeep(param)
}
};
const response = await http.delete(this.urlPath, headers);
if (this.apiVersion) {
delete this.stackHeaders.api_version;
}
if (response.data) {
return response.data;
} else {
throw error(response);
}
} catch (err) {
throw error(err);
}
};


/**
* @description The fetch GlobalField call fetches GlobalField details.
Expand All @@ -119,7 +171,30 @@ export function GlobalField (http, data = {}) {
* .then((globalField) => console.log(globalField))
*
*/
this.fetch = fetch(http, 'global_field')
this.fetch = async function (param = {}) {
try {
if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
},
params: {
...cloneDeep(param)
}
};
const response = await http.get(this.urlPath, headers);
if (response.data) {
return response.data;
} else {
throw error(response);
}
} catch (err) {
throw error(err);
}
};

} else {
/**
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
Expand All @@ -142,7 +217,27 @@ export function GlobalField (http, data = {}) {
* client.stack().globalField().create({ global_field })
* .then((globalField) => console.log(globalField))
*/
this.create = create({ http: http })
this.create = async (data) => {
try {
if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
}
};
const response = await http.post(`${this.urlPath}`, data, headers);
if (response.data) {
return response.data;
} else {
return error(response);
}
} catch (err) {
return error(err);
}
};


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

/**
* @description The Import a global field call imports a global field into a stack.
Expand Down
6 changes: 4 additions & 2 deletions lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ export function Stack (http, data) {
} else if (globalFieldUidOrOptions) {
data.global_field = { uid: globalFieldUidOrOptions };
}

if (options?.api_version) {

// Safely handle `options` and check for `api_version`
options = options || {}; // Ensure `options` is always an object
if (options && typeof options === 'object' && options.api_version) {
data.api_version = options.api_version;
if (options.api_version === '3.2') {
data.nested_global_fields = true;
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/management",
"version": "1.19.0",
"version": "1.19.2",
"description": "The Content Management API is used to manage the content of your Contentstack account",
"main": "./dist/node/contentstack-management.js",
"browser": "./dist/web/contentstack-management.js",
Expand Down
47 changes: 11 additions & 36 deletions test/sanity-check/api/globalfield-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("Global Field api Test", () => {
makeGlobalField()
.create(createGlobalField)
.then((globalField) => {
globalField = globalField.global_field;
expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid);
expect(globalField.title).to.be.equal(
createGlobalField.global_field.title
Expand All @@ -42,6 +43,7 @@ describe("Global Field api Test", () => {
makeGlobalField(createGlobalField.global_field.uid)
.fetch()
.then((globalField) => {
globalField = globalField.global_field;
expect(globalField.uid).to.be.equal(createGlobalField.global_field.uid);
expect(globalField.title).to.be.equal(
createGlobalField.global_field.title
Expand All @@ -60,38 +62,11 @@ describe("Global Field api Test", () => {
.catch(done);
});

it("should fetch and update global Field", (done) => {
makeGlobalField(createGlobalField.global_field.uid)
.fetch()
.then((globalField) => {
globalField.title = "Update title";
return globalField.update();
})
.then((updateGlobal) => {
expect(updateGlobal.uid).to.be.equal(
createGlobalField.global_field.uid
);
expect(updateGlobal.title).to.be.equal("Update title");
expect(updateGlobal.schema[0].uid).to.be.equal(
createGlobalField.global_field.schema[0].uid
);
expect(updateGlobal.schema[0].data_type).to.be.equal(
createGlobalField.global_field.schema[0].data_type
);
expect(updateGlobal.schema[0].display_name).to.be.equal(
createGlobalField.global_field.schema[0].display_name
);
done();
})
.catch(done);
});

it("should update global Field", (done) => {
const globalField = makeGlobalField(createGlobalField.global_field.uid);
Object.assign(globalField, cloneDeep(createGlobalField.global_field));
globalField
.update()
makeGlobalField(createGlobalField.global_field.uid)
.update(createGlobalField)
.then((updateGlobal) => {
updateGlobal = updateGlobal.global_field;
expect(updateGlobal.uid).to.be.equal(
createGlobalField.global_field.uid
);
Expand Down Expand Up @@ -140,6 +115,7 @@ describe("Global Field api Test", () => {
.catch(done);
});


it("should get global field title matching Upload", (done) => {
makeGlobalField()
.query({ query: { title: "Upload" } })
Expand All @@ -162,7 +138,6 @@ describe("Global Field api Test", () => {
collection.items.forEach((globalField) => {
expect(globalField.uid).to.be.not.equal(null);
expect(globalField.title).to.be.not.equal(null);
expect(globalField.schema).to.be.not.equal(null);
});
done();
})
Expand All @@ -173,7 +148,7 @@ describe("Global Field api Test", () => {
it('should create nested global field for reference', done => {
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalFieldForReference)
.then(globalField => {
expect(globalField.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
expect(globalField.global_field.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
done();
})
.catch(err => {
Expand All @@ -185,7 +160,7 @@ describe("Global Field api Test", () => {
it('should create nested global field', done => {
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField)
.then(globalField => {
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
expect(globalField.global_field.uid).to.be.equal(createNestedGlobalField.global_field.uid);
done();
})
.catch(err => {
Expand All @@ -197,7 +172,7 @@ describe("Global Field api Test", () => {
it('should fetch nested global field', done => {
makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
.then(globalField => {
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
expect(globalField.global_field.uid).to.be.equal(createNestedGlobalField.global_field.uid);
done();
})
.catch(err => {
Expand All @@ -207,8 +182,8 @@ describe("Global Field api Test", () => {
});

it('should update nested global fields without fetch', done => {
makeGlobalField(createNestedGlobalField.global_field.uid, { headers: { api_version: '3.2' }})
.updateNestedGlobalField(createNestedGlobalField)
makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' })
.update(createNestedGlobalField)
.then((globalField) => {
expect(globalField.global_field.schema.length).to.be.equal(2)
done()
Expand Down
Loading