From 40ce51aa1939bdad4e5ce5cac819c3218b5d98ad Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Tue, 20 May 2025 16:58:18 +0530 Subject: [PATCH 1/3] Added branch support for global fields. --- lib/stack/globalField/index.js | 9 +- lib/stack/index.js | 27 ++- test/sanity-check/api/globalfield-test.js | 202 +++++++++++----------- types/stack/index.d.ts | 5 +- 4 files changed, 131 insertions(+), 112 deletions(-) diff --git a/lib/stack/globalField/index.js b/lib/stack/globalField/index.js index 32c14838..95442ca3 100644 --- a/lib/stack/globalField/index.js +++ b/lib/stack/globalField/index.js @@ -11,12 +11,15 @@ import { createReadStream } from 'fs' export function GlobalField (http, data = {}) { this.stackHeaders = data.stackHeaders - if (data.api_version) { - this.stackHeaders.api_version = data.api_version + if (data.apiVersion) { + this.stackHeaders.api_version = data.apiVersion + } + if (data.branch) { + this.stackHeaders.branch = data.branch } this.urlPath = `/global_fields` - if (data.global_field) { + if (data.global_field && data.global_field.uid) { Object.assign(this, cloneDeep(data.global_field)) this.urlPath = `/global_fields/${this.uid}` /** diff --git a/lib/stack/index.js b/lib/stack/index.js index 1a155902..c954e81c 100644 --- a/lib/stack/index.js +++ b/lib/stack/index.js @@ -154,7 +154,7 @@ export function Stack (http, data) { * import * as contentstack from '@contentstack/management' * const client = contentstack.client() * - * client.stack({ api_key: 'api_key'}).globalField().create() + * client.stack({ api_key: 'api_key'}).globalField().create(data) * .then((globalField) => console.log(globalField)) * * client.stack({ api_key: 'api_key'}).globalField('globalField_uid').fetch() @@ -164,12 +164,27 @@ export function Stack (http, data) { * .then((globalField) => console.log(globalField)) * */ - // eslint-disable-next-line camelcase - this.globalField = (globalFieldUid = null, api_version = '3.0') => { + + this.globalField = (uidOrOptions = null, option = {}) => { + let globalFieldUid = null + let apiVersion = '3.0' + let branch = 'main' + const stackHeaders = { ...this.stackHeaders } + if (typeof uidOrOptions === 'object' && uidOrOptions !== null) { + option = uidOrOptions + } else { + globalFieldUid = uidOrOptions + } + if (option?.api_version) { + apiVersion = option.api_version + } + if (option?.branch) { + branch = option.branch + } const data = { - stackHeaders: this.stackHeaders, - // eslint-disable-next-line camelcase - api_version: api_version + stackHeaders, + apiVersion, + branch } if (globalFieldUid) { data.global_field = { uid: globalFieldUid } diff --git a/test/sanity-check/api/globalfield-test.js b/test/sanity-check/api/globalfield-test.js index eb5ad12a..1f369b68 100644 --- a/test/sanity-check/api/globalfield-test.js +++ b/test/sanity-check/api/globalfield-test.js @@ -3,7 +3,7 @@ import { expect } from 'chai' import { cloneDeep } from 'lodash' import { describe, it, setup } from 'mocha' import { jsonReader } from '../utility/fileOperations/readwrite' -import { createGlobalField } from '../mock/globalfield' +import { createGlobalField, createNestedGlobalFieldForReference, createNestedGlobalField } from '../mock/globalfield' import { contentstackClient } from '../utility/ContentstackClient.js' import dotenv from 'dotenv' @@ -117,113 +117,113 @@ describe('Global Field api Test', () => { .catch(done) }) - // it("should get all nested global fields from Query", (done) => { - // makeGlobalField({ api_version: '3.2' }) - // .query() - // .find() - // .then((collection) => { - // collection.items.forEach((globalField) => { - // expect(globalField.uid).to.be.not.equal(null); - // expect(globalField.title).to.be.not.equal(null); - // }); - // done(); - // }) - // .catch(done); - // }); + it('should get all nested global fields from Query', (done) => { + makeGlobalField({ api_version: '3.2' }) + .query() + .find() + .then((collection) => { + collection.items.forEach((globalField) => { + expect(globalField.uid).to.be.not.equal(null) + expect(globalField.title).to.be.not.equal(null) + }) + done() + }) + .catch(done) + }) - // 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); - // done(); - // }) - // .catch(err => { - // console.error('Error:', err.response?.data || err.message); - // done(err); - // }); - // }); + 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) + done() + }) + .catch(err => { + console.error('Error:', err.response?.data || err.message) + done(err) + }) + }) - // 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); - // done(); - // }) - // .catch(err => { - // console.error('Error:', err.response?.data || err.message); - // done(err); - // }); - // }); + 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) + done() + }) + .catch(err => { + console.error('Error:', err.response?.data || err.message) + done(err) + }) + }) - // 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); - // done(); - // }) - // .catch(err => { - // console.error('Error:', err.response?.data || err.message); - // done(err); - // }); - // }); + 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) + done() + }) + .catch(err => { + console.error('Error:', err.response?.data || err.message) + done(err) + }) + }) - // it('should fetch and update nested global Field', done => { - // makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }).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 fetch and update nested global Field', done => { + makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }).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 nested global Field', done => { - // const globalField = makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }) - // Object.assign(globalField, cloneDeep(createGlobalField.global_field)) - // globalField.update() - // .then((updateGlobal) => { - // expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid) - // expect(updateGlobal.title).to.be.equal(createGlobalField.global_field.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 nested global Field', done => { + const globalField = makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }) + Object.assign(globalField, cloneDeep(createGlobalField.global_field)) + globalField.update() + .then((updateGlobal) => { + expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid) + expect(updateGlobal.title).to.be.equal(createGlobalField.global_field.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 delete nested global field", (done) => { - // makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }) - // .delete() - // .then((data) => { - // expect(data.notice).to.be.equal("Global Field deleted successfully."); - // done(); - // }) - // .catch((err) => { - // console.error("Error:", err.response?.data || err.message); - // done(err); - // }); - // }); + it('should delete nested global field', (done) => { + makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Global Field deleted successfully.') + done() + }) + .catch((err) => { + console.error('Error:', err.response?.data || err.message) + done(err) + }) + }) - // it("should delete nested global reference field", (done) => { - // makeGlobalField(createNestedGlobalFieldForReference.global_field.uid, { api_version: '3.2' }) - // .delete() - // .then((data) => { - // expect(data.notice).to.be.equal("Global Field deleted successfully."); - // done(); - // }) - // .catch((err) => { - // console.error("Error:", err.response?.data || err.message); - // done(err); - // }); - // }); + it('should delete nested global reference field', (done) => { + makeGlobalField(createNestedGlobalFieldForReference.global_field.uid, { api_version: '3.2' }) + .delete() + .then((data) => { + expect(data.notice).to.be.equal('Global Field deleted successfully.') + done() + }) + .catch((err) => { + console.error('Error:', err.response?.data || err.message) + done(err) + }) + }) it('should delete global Field', (done) => { makeGlobalField(createGlobalField.global_field.uid) diff --git a/types/stack/index.d.ts b/types/stack/index.d.ts index f0344b38..055ab895 100644 --- a/types/stack/index.d.ts +++ b/types/stack/index.d.ts @@ -63,8 +63,9 @@ export interface Stack extends SystemFields { globalField(): GlobalFields; globalField(uid: string, option?: object): GlobalField; - globalField(options: { api_version: string }): GlobalFields; - globalField(uidOrOptions?: string | { api_version: string }, option?: object): GlobalFields | GlobalField; + globalField(options: object): GlobalFields; + globalField(uidOrOptions?: string | object, option?: object): GlobalFields | GlobalField; + From 8a77ffd84a602427582555a135a12bf27407816f Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Tue, 20 May 2025 18:14:04 +0530 Subject: [PATCH 2/3] Fixed test cases --- .talismanrc | 2 ++ lib/stack/globalField/index.js | 13 +++++++------ test/unit/globalField-test.js | 24 +++++++++++------------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/.talismanrc b/.talismanrc index 9131959e..b5ca0dc5 100644 --- a/.talismanrc +++ b/.talismanrc @@ -8,4 +8,6 @@ fileignoreconfig: checksum: 5baabd7d2c391648163f9371f0e5e9484f8fb90fa2284cfc378732ec3192c193 - filename: test/sanity-check/api/stack-test.js checksum: 198d5cf7ead33b079249dc3ecdee61a9c57453e93f1073ed0341400983e5aa53 +- filename: lib/stack/index.js + checksum: 6aab5edf85efb17951418b4dc4402889cd24c8d786c671185074aeb4d50f0242 version: "" \ No newline at end of file diff --git a/lib/stack/globalField/index.js b/lib/stack/globalField/index.js index 95442ca3..57192ea6 100644 --- a/lib/stack/globalField/index.js +++ b/lib/stack/globalField/index.js @@ -10,13 +10,14 @@ import { createReadStream } from 'fs' */ export function GlobalField (http, data = {}) { - this.stackHeaders = data.stackHeaders - if (data.apiVersion) { - this.stackHeaders.api_version = data.apiVersion - } - if (data.branch) { - this.stackHeaders.branch = data.branch + const rawHeaders = data.stackHeaders || {} + + this.stackHeaders = { + ...cloneDeep(rawHeaders), + ...(data.apiVersion ? { api_version: data.apiVersion } : {}), + ...(data.branch ? { branch: data.branch } : {}) } + this.urlPath = `/global_fields` if (data.global_field && data.global_field.uid) { diff --git a/test/unit/globalField-test.js b/test/unit/globalField-test.js index 8edaefa6..1764d196 100644 --- a/test/unit/globalField-test.js +++ b/test/unit/globalField-test.js @@ -10,7 +10,7 @@ describe('Contentstack GlobalField test', () => { it('GlobalField test without uid', (done) => { const globalField = makeGlobalField() expect(globalField.urlPath).to.be.equal('/global_fields') - expect(globalField.stackHeaders).to.be.equal(undefined) + expect(globalField.stackHeaders).to.deep.equal({}) expect(globalField.update).to.be.equal(undefined) expect(globalField.delete).to.be.equal(undefined) expect(globalField.fetch).to.be.equal(undefined) @@ -25,10 +25,9 @@ describe('Contentstack GlobalField test', () => { ...systemUidMock } }) - expect(globalField.urlPath).to.be.equal( - `/global_fields/${systemUidMock.uid}` - ) - expect(globalField.stackHeaders).to.be.equal(undefined) + + expect(globalField.urlPath).to.be.equal(`/global_fields/${systemUidMock.uid}`) + expect(globalField.stackHeaders).to.deep.equal({}) expect(globalField.update).to.not.equal(undefined) expect(globalField.delete).to.not.equal(undefined) expect(globalField.fetch).to.not.equal(undefined) @@ -218,10 +217,11 @@ describe('Contentstack GlobalField test', () => { describe('Contentstack GlobalField test (API Version 3.2)', () => { it('GlobalField test without uid', (done) => { const globalField = makeGlobalField({ - stackHeaders: stackHeadersMock, - api_version: '3.2' }) - expect(globalField.urlPath).to.be.equal('/global_fields') - expect(globalField.stackHeaders.api_version).to.be.equal('3.2') + stackHeaders: { api_key: 'api_key', api_version: '3.2' } + }) + + expect(globalField.urlPath).to.equal('/global_fields') + expect(globalField.stackHeaders.api_version).to.equal('3.2') expect(globalField.stackHeaders).to.deep.equal({ api_key: 'api_key', api_version: '3.2' }) done() }) @@ -231,8 +231,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { global_field: { ...systemUidMock }, - stackHeaders: stackHeadersMock, - api_version: '3.2' + stackHeaders: { api_key: 'api_key', api_version: '3.2' } }) expect(globalField.urlPath).to.be.equal( `/global_fields/${systemUidMock.uid}` @@ -251,8 +250,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { global_field: { ...systemUidMock }, - stackHeaders: stackHeadersMock, - api_version: '3.2' + stackHeaders: { api_key: 'api_key', api_version: '3.2' } }) expect(globalField.urlPath).to.be.equal( `/global_fields/${systemUidMock.uid}` From 4d828e814d056a0bc20900141f292b16364f96b8 Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Tue, 20 May 2025 18:16:03 +0530 Subject: [PATCH 3/3] chore: Updated talismanrc --- .talismanrc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.talismanrc b/.talismanrc index b5ca0dc5..81ac5d0f 100644 --- a/.talismanrc +++ b/.talismanrc @@ -10,4 +10,8 @@ fileignoreconfig: checksum: 198d5cf7ead33b079249dc3ecdee61a9c57453e93f1073ed0341400983e5aa53 - filename: lib/stack/index.js checksum: 6aab5edf85efb17951418b4dc4402889cd24c8d786c671185074aeb4d50f0242 -version: "" \ No newline at end of file +version: "" +fileignoreconfig: +- filename: test/unit/globalField-test.js + checksum: 25185e3400a12e10a043dc47502d8f30b7e1c4f2b6b4d3b8b55cdc19850c48bf +version: "1.0" \ No newline at end of file