From e74b079429c8d12c3837891eebcfbd84ec75f86a Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Tue, 8 Apr 2025 19:10:10 +0530 Subject: [PATCH 1/9] fix: handle api_version chaining and ensure backward compatibility --- lib/entity.js | 6 ++ lib/stack/globalField/index.js | 120 ++++++---------------- test/sanity-check/api/globalfield-test.js | 3 - 3 files changed, 37 insertions(+), 92 deletions(-) diff --git a/lib/entity.js b/lib/entity.js index bece8aba..6c9e7f6e 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -45,6 +45,12 @@ export const publishUnpublish = async (http, url, httpBody, headers, locale = nu const response = await http.post(url, httpBody, headers) if (response.data) { const data = response.data || {} + if (http?.httpClientParams?.headers?.api_version) { + delete http.httpClientParams.headers.api_version + } + if (headers?.api_version) { + delete headers.api_version + } if (headers) { data.stackHeaders = headers } diff --git a/lib/stack/globalField/index.js b/lib/stack/globalField/index.js index 3f36cc82..36d1edf4 100644 --- a/lib/stack/globalField/index.js +++ b/lib/stack/globalField/index.js @@ -11,10 +11,8 @@ import { createReadStream } from 'fs' export function GlobalField (http, data = {}) { this.stackHeaders = data.stackHeaders - this.apiVersion = data.api_version || undefined - - if (this.apiVersion) { - this.stackHeaders.api_version = this.apiVersion + if (data.api_version) { + this.apiVersion = data.api_version } this.urlPath = `/global_fields` @@ -41,80 +39,31 @@ export function GlobalField (http, data = {}) { */ 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) - } + headers: { ...cloneDeep(this.stackHeaders) } + } + let payload = config + if (!config) { + const { + stackHeaders, + apiVersion, + update, + delete: deleteFn, + fetch, + ...globalFieldPayload + } = cloneDeep(this) + + payload = { global_field: globalFieldPayload } } - const response = await http.put(`${this.urlPath}`, config, headers) - // Remove `api_version` from headers after fetching data + const response = await http.put(`${this.urlPath}`, payload, headers) if (this.apiVersion) { delete this.stackHeaders.api_version } - const data = response.data - if (data) { - if (this.stackHeaders) { - data.stackHeaders = this.stackHeaders - } - return 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. - * @memberof GlobalField - * @func update - * @returns {Promise} Promise for GlobalField instance - * @example - * import * as contentstack from '@contentstack/management' - * const client = contentstack.client() - * const data = { - * "global_field": { - * "title": "Nested Global Field33", - * "uid": "nested_global_field33", - * "schema": [ - * { - * "data_type": "text", - * "display_name": "Single Line Textbox", - * "uid": "single_line" - * }, - * { - * "data_type": "global_field", - * "display_name": "Global", - * "uid": "global_field", - * "reference_to": "nested_global_field_123" - * } - * ] - * } - * } - * client.stack({ api_key: 'api_key'}).globalField('global_field_uid').updateNestedGlobalField(data, { headers: { api_version: '3.2' }}) - * .then((globalField) => { - console.log(globalField) - * }) - */ - this.updateNestedGlobalField = async (config, headers = {}) => { - const apiVersion = { api_version: '3.2' } - this.stackHeaders = { ...this.stackHeaders, ...apiVersion, ...headers } - try { - const headers = { - headers: { ...cloneDeep(this.stackHeaders) } - } - const response = await http.put(`${this.urlPath}`, config, headers) - const data = response.data - if (data) { - if (this.stackHeaders) { - data.stackHeaders = this.stackHeaders - } - return data + if (response.data) { + return new this.constructor(http, parseData(response, this.stackHeaders)) } else { throw error(response) } @@ -138,7 +87,6 @@ export function GlobalField (http, data = {}) { this.delete = async () => { const param = {} try { - // Add `api_version` to headers if `this.apiVersion` is defined if (this.apiVersion) { this.stackHeaders.api_version = this.apiVersion } @@ -154,12 +102,8 @@ export function GlobalField (http, data = {}) { if (this.apiVersion) { delete this.stackHeaders.api_version } - const data = response.data - if (data) { - if (this.stackHeaders) { - data.stackHeaders = this.stackHeaders - } - return data + if (response.data) { + return response.data } else { throw error(response) } @@ -195,12 +139,11 @@ export function GlobalField (http, data = {}) { } } const response = await http.get(this.urlPath, headers) - const data = response.data - if (data) { - if (this.stackHeaders) { - data.stackHeaders = this.stackHeaders - } - return data + if (this.apiVersion) { + delete this.stackHeaders.api_version + } + if (response.data) { + return new this.constructor(http, parseData(response, this.stackHeaders)) } else { throw error(response) } @@ -241,12 +184,11 @@ export function GlobalField (http, data = {}) { } } const response = await http.post(`${this.urlPath}`, payload, headers) - const data = response.data - if (data) { - if (this.stackHeaders) { - data.stackHeaders = this.stackHeaders - } - return data + if (this.apiVersion) { + delete this.stackHeaders.api_version + } + if (response.data) { + return new this.constructor(http, parseData(response, this.stackHeaders)) } else { throw error(response) } diff --git a/test/sanity-check/api/globalfield-test.js b/test/sanity-check/api/globalfield-test.js index 789224fc..23beeaf5 100644 --- a/test/sanity-check/api/globalfield-test.js +++ b/test/sanity-check/api/globalfield-test.js @@ -19,7 +19,6 @@ 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 @@ -42,7 +41,6 @@ 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 @@ -65,7 +63,6 @@ describe('Global Field api Test', () => { makeGlobalField(createGlobalField.global_field.uid) .update(createGlobalField) .then((updateGlobal) => { - updateGlobal = updateGlobal.global_field expect(updateGlobal.uid).to.be.equal( createGlobalField.global_field.uid ) From efb707d02dcea5866b0ff9bfed6dd21a30e68f55 Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Tue, 8 Apr 2025 19:29:25 +0530 Subject: [PATCH 2/9] Updated unit testcases --- test/unit/globalField-test.js | 47 ++++++----------------------------- 1 file changed, 7 insertions(+), 40 deletions(-) diff --git a/test/unit/globalField-test.js b/test/unit/globalField-test.js index f9c2f912..8419a977 100644 --- a/test/unit/globalField-test.js +++ b/test/unit/globalField-test.js @@ -84,7 +84,7 @@ describe('Contentstack GlobalField test', () => { makeGlobalField() .create() .then((globalField) => { - checkGlobalField(globalField.global_field) + checkGlobalField(globalField) done() }) .catch(done) @@ -120,7 +120,7 @@ describe('Contentstack GlobalField test', () => { }) .update() .then((globalField) => { - checkGlobalField(globalField.global_field) + checkGlobalField(globalField) done() }) .catch(done) @@ -141,7 +141,7 @@ describe('Contentstack GlobalField test', () => { }) .fetch() .then((globalField) => { - checkGlobalField(globalField.global_field) + checkGlobalField(globalField) done() }) .catch(done) @@ -222,7 +222,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { api_version: '3.2' }) expect(globalField.urlPath).to.be.equal('/global_fields') expect(globalField.apiVersion).to.be.equal('3.2') - expect(globalField.stackHeaders).to.deep.equal({ api_key: 'api_key', api_version: '3.2' }) + expect(globalField.stackHeaders).to.deep.equal({ api_key: 'api_key' }) done() }) @@ -300,7 +300,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { api_version: '3.2' }) .create() .then((globalField) => { - checkGlobalField(globalField.global_field) + checkGlobalField(globalField) done() }) .catch(done) @@ -339,7 +339,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { }) .update() .then((globalField) => { - checkGlobalField(globalField.global_field) + checkGlobalField(globalField) done() }) .catch(done) @@ -361,7 +361,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { }) .fetch() .then((globalField) => { - checkGlobalField(globalField.global_field) + checkGlobalField(globalField) done() }) .catch(done) @@ -438,39 +438,6 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { }) .catch(done) }) - - it('should update nested global field', (done) => { - var mock = new MockAdapter(Axios) - const updatedData = { - global_field: { - title: 'Updated Nested Global Field Title', - schema: nestedGlobalFieldPayload - } - } - - mock - .onPut(`/global_fields/${systemUidMock.uid}`) - .reply(200, { - global_field: { - ...nestedGlobalFieldMock, - ...updatedData.global_field - } - }) - - makeGlobalField({ - global_field: { - ...systemUidMock - }, - stackHeaders: stackHeadersMock - }) - .updateNestedGlobalField(updatedData) - .then((response) => { - expect(response.global_field.title).to.be.equal('Updated Nested Global Field Title') - expect(response.global_field.schema).to.deep.equal(nestedGlobalFieldPayload) - done() - }) - .catch(done) - }) }) function makeGlobalField (data) { From 40d0db994fec35e4f1b9d085e024b274791eefc4 Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Tue, 8 Apr 2025 19:32:51 +0530 Subject: [PATCH 3/9] Fixed linting errors --- test/unit/globalField-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/globalField-test.js b/test/unit/globalField-test.js index 8419a977..e357934b 100644 --- a/test/unit/globalField-test.js +++ b/test/unit/globalField-test.js @@ -3,7 +3,7 @@ import Axios from 'axios' import { expect } from 'chai' import { describe, it } from 'mocha' import { GlobalField, GlobalFieldCollection, createFormData } from '../../lib/stack/globalField' -import { systemUidMock, checkSystemFields, globalFieldMock, stackHeadersMock, noticeMock, nestedGlobalFieldMock, nestedGlobalFieldPayload } from './mock/objects' +import { systemUidMock, checkSystemFields, globalFieldMock, stackHeadersMock, noticeMock, nestedGlobalFieldMock } from './mock/objects' import MockAdapter from 'axios-mock-adapter' describe('Contentstack GlobalField test', () => { From 165b9b5c0b60a61d633388b6979e7ea94b80cc52 Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Wed, 9 Apr 2025 13:15:54 +0530 Subject: [PATCH 4/9] Updated changelog file --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51ee6dd7..8d66e9af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ # Changelog + + +## [v1.20.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.20.2) (2025-04-21) + - Fix + - Handle api_version chaining and ensure backward compatibility + ## [v1.20.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.20.1) (2025-04-07) - Fix - Ensure 'api' is replaced with 'app' in uiHostName regardless of position From cddcd1eac718cfc2cbde51a1916847cd31ef274d Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Fri, 11 Apr 2025 13:55:59 +0530 Subject: [PATCH 5/9] Fixed migration rte issue in CLI --- lib/entity.js | 3 + lib/stack/globalField/index.js | 122 ++-------------------- lib/stack/index.js | 23 ++-- test/sanity-check/api/globalfield-test.js | 67 +++++++----- 4 files changed, 58 insertions(+), 157 deletions(-) diff --git a/lib/entity.js b/lib/entity.js index 6c9e7f6e..0c16e46e 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -265,6 +265,9 @@ export const fetchAll = (http, wrapperCollection, params = {}) => { export function parseData (response, stackHeaders, contentTypeUID, taxonomyUid, http) { const data = response.data || {} + if (stackHeaders && 'api_version' in stackHeaders) { + delete stackHeaders.api_version + } if (stackHeaders) { data.stackHeaders = stackHeaders } diff --git a/lib/stack/globalField/index.js b/lib/stack/globalField/index.js index 36d1edf4..8e27224b 100644 --- a/lib/stack/globalField/index.js +++ b/lib/stack/globalField/index.js @@ -1,5 +1,5 @@ import cloneDeep from 'lodash/cloneDeep' -import { query, upload, parseData } from '../../entity' +import { create, update, deleteEntity, fetch, query, upload, parseData } from '../../entity' import error from '../../core/contentstackError' import FormData from 'form-data' import { createReadStream } from 'fs' @@ -12,7 +12,7 @@ import { createReadStream } from 'fs' export function GlobalField (http, data = {}) { this.stackHeaders = data.stackHeaders if (data.api_version) { - this.apiVersion = data.api_version + this.stackHeaders.api_version = data.apiVersion } this.urlPath = `/global_fields` @@ -37,40 +37,7 @@ export function GlobalField (http, data = {}) { * .then((globalField) => console.log(globalField)) * */ - this.update = async (config) => { - try { - if (this.apiVersion) { - this.stackHeaders.api_version = this.apiVersion - } - const headers = { - headers: { ...cloneDeep(this.stackHeaders) } - } - let payload = config - if (!config) { - const { - stackHeaders, - apiVersion, - update, - delete: deleteFn, - fetch, - ...globalFieldPayload - } = cloneDeep(this) - - payload = { global_field: globalFieldPayload } - } - const response = await http.put(`${this.urlPath}`, payload, headers) - if (this.apiVersion) { - delete this.stackHeaders.api_version - } - if (response.data) { - return new this.constructor(http, parseData(response, this.stackHeaders)) - } else { - throw error(response) - } - } catch (err) { - throw error(err) - } - } + this.update = update(http, 'global_field') /** * @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack. @@ -84,33 +51,7 @@ export function GlobalField (http, data = {}) { * client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete() * .then((response) => console.log(response.notice)) */ - this.delete = async () => { - const param = {} - try { - 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) - } - } + this.delete = deleteEntity(http) /** * @description The fetch GlobalField call fetches GlobalField details. @@ -125,32 +66,7 @@ export function GlobalField (http, data = {}) { * .then((globalField) => console.log(globalField)) * */ - 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 (this.apiVersion) { - delete this.stackHeaders.api_version - } - if (response.data) { - return new this.constructor(http, parseData(response, this.stackHeaders)) - } else { - throw error(response) - } - } catch (err) { - throw error(err) - } - } + this.fetch = fetch(http, 'global_field') } else { /** * @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account. @@ -173,29 +89,7 @@ export function GlobalField (http, data = {}) { * client.stack().globalField().create({ global_field }) * .then((globalField) => console.log(globalField)) */ - this.create = async (payload) => { - try { - if (this.apiVersion) { - this.stackHeaders.api_version = this.apiVersion - } - const headers = { - headers: { - ...cloneDeep(this.stackHeaders) - } - } - const response = await http.post(`${this.urlPath}`, payload, headers) - if (this.apiVersion) { - delete this.stackHeaders.api_version - } - if (response.data) { - return new this.constructor(http, parseData(response, this.stackHeaders)) - } else { - throw error(response) - } - } catch (err) { - return error(err) - } - } + this.create = create({ http: http }) /** * @description The Query on GlobalField will allow to fetch details of all or specific GlobalField @@ -210,7 +104,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, apiVersion: this.apiVersion }) + this.query = query({ http: http, wrapperCollection: GlobalFieldCollection, apiVersion: this.apiVersion }) /** * @description The Import a global field call imports a global field into a stack. @@ -266,4 +160,4 @@ export function createFormData (data) { formData.append('global_field', uploadStream) return formData } -} +} \ No newline at end of file diff --git a/lib/stack/index.js b/lib/stack/index.js index 74416687..09127082 100644 --- a/lib/stack/index.js +++ b/lib/stack/index.js @@ -164,25 +164,14 @@ export function Stack (http, data) { * .then((globalField) => console.log(globalField)) * */ - this.globalField = (globalFieldUidOrOptions = null, options = {}) => { - const data = { - stackHeaders: this.stackHeaders + this.globalField = (globalFieldUid = null, api_version = '3.0') => { + const data = { + stackHeaders: this.stackHeaders, + api_version: api_version } - if (typeof globalFieldUidOrOptions === 'object' && globalFieldUidOrOptions !== null) { - options = globalFieldUidOrOptions - } else if (globalFieldUidOrOptions) { - data.global_field = { uid: globalFieldUidOrOptions } + if (globalFieldUid) { + data.global_field = { uid: globalFieldUid } } - - // 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 - } - } - return new GlobalField(http, data) } diff --git a/test/sanity-check/api/globalfield-test.js b/test/sanity-check/api/globalfield-test.js index 23beeaf5..b550d9bc 100644 --- a/test/sanity-check/api/globalfield-test.js +++ b/test/sanity-check/api/globalfield-test.js @@ -1,8 +1,9 @@ import path from 'path' 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, createNestedGlobalField, createNestedGlobalFieldForReference } from '../mock/globalfield' import { contentstackClient } from '../utility/ContentstackClient.js' import dotenv from 'dotenv' @@ -59,25 +60,16 @@ describe('Global Field api Test', () => { .catch(done) }) - it('should update global Field', (done) => { - makeGlobalField(createGlobalField.global_field.uid) - .update(createGlobalField) + it('should update global Field', done => { + const globalField = makeGlobalField(createGlobalField.global_field.uid) + 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 - ) + 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) @@ -142,7 +134,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.global_field.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid); + // expect(globalField.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid); // done(); // }) // .catch(err => { @@ -154,7 +146,7 @@ describe('Global Field api Test', () => { // it('should create nested global field', done => { // makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField) // .then(globalField => { - // expect(globalField.global_field.uid).to.be.equal(createNestedGlobalField.global_field.uid); + // expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid); // done(); // }) // .catch(err => { @@ -166,7 +158,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.global_field.uid).to.be.equal(createNestedGlobalField.global_field.uid); + // expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid); // done(); // }) // .catch(err => { @@ -175,16 +167,39 @@ describe('Global Field api Test', () => { // }); // }); - // it('should update nested global fields without fetch', done => { - // makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }) - // .update(createNestedGlobalField) + // it('should fetch and update nested global Field', done => { + // makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }).fetch() // .then((globalField) => { - // expect(globalField.global_field.schema.length).to.be.equal(2) + // 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 delete nested global field", (done) => { // makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }) // .delete() From 5b39a0c652c4bd02dadb1886943fddd8d027974b Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Fri, 11 Apr 2025 14:18:42 +0530 Subject: [PATCH 6/9] updated variable --- lib/stack/globalField/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stack/globalField/index.js b/lib/stack/globalField/index.js index 8e27224b..1f172498 100644 --- a/lib/stack/globalField/index.js +++ b/lib/stack/globalField/index.js @@ -12,7 +12,7 @@ import { createReadStream } from 'fs' export function GlobalField (http, data = {}) { this.stackHeaders = data.stackHeaders if (data.api_version) { - this.stackHeaders.api_version = data.apiVersion + this.stackHeaders.api_version = data.api_version } this.urlPath = `/global_fields` From 19e73e80ca4628e9826cdb282949d9c3c728c339 Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Fri, 11 Apr 2025 14:26:25 +0530 Subject: [PATCH 7/9] Fixed unit testcases --- lib/entity.js | 1 - test/unit/globalField-test.js | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/entity.js b/lib/entity.js index 0c16e46e..c74200dc 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -89,7 +89,6 @@ export const create = ({ http, params }) => { return async function (data, param) { this.stackHeaders = { ...this.stackHeaders, - ...(http.httpClientParams.headers?.api_version && { api_version: http.httpClientParams.headers.api_version }) } const headers = { headers: { diff --git a/test/unit/globalField-test.js b/test/unit/globalField-test.js index e357934b..273d9800 100644 --- a/test/unit/globalField-test.js +++ b/test/unit/globalField-test.js @@ -221,8 +221,8 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { stackHeaders: stackHeadersMock, api_version: '3.2' }) expect(globalField.urlPath).to.be.equal('/global_fields') - expect(globalField.apiVersion).to.be.equal('3.2') - expect(globalField.stackHeaders).to.deep.equal({ api_key: 'api_key' }) + expect(globalField.stackHeaders.api_version).to.be.equal('3.2') + expect(globalField.stackHeaders).to.deep.equal({ api_key: 'api_key', api_version: '3.2' }) done() }) @@ -237,7 +237,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { expect(globalField.urlPath).to.be.equal( `/global_fields/${systemUidMock.uid}` ) - expect(globalField.apiVersion).to.be.equal('3.2') + expect(globalField.stackHeaders.api_version).to.be.equal('3.2') expect(globalField.update).to.not.equal(undefined) expect(globalField.delete).to.not.equal(undefined) expect(globalField.fetch).to.not.equal(undefined) @@ -257,7 +257,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => { expect(globalField.urlPath).to.be.equal( `/global_fields/${systemUidMock.uid}` ) - expect(globalField.apiVersion).to.be.equal('3.2') + expect(globalField.stackHeaders.api_version).to.be.equal('3.2') expect(globalField.stackHeaders).to.not.equal(undefined) expect(globalField.stackHeaders.api_key).to.be.equal( stackHeadersMock.api_key From 09f01f4ed584dcbeb74a032a700a5ec74648bf6d Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Fri, 11 Apr 2025 15:28:03 +0530 Subject: [PATCH 8/9] Fixed linting errors --- lib/entity.js | 2 +- lib/stack/globalField/index.js | 4 ++-- lib/stack/index.js | 8 +++++--- test/sanity-check/api/globalfield-test.js | 3 +-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/entity.js b/lib/entity.js index c74200dc..6fcb22ff 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -88,7 +88,7 @@ export const upload = async ({ http, urlPath, stackHeaders, formData, params, me export const create = ({ http, params }) => { return async function (data, param) { this.stackHeaders = { - ...this.stackHeaders, + ...this.stackHeaders } const headers = { headers: { diff --git a/lib/stack/globalField/index.js b/lib/stack/globalField/index.js index 1f172498..32c14838 100644 --- a/lib/stack/globalField/index.js +++ b/lib/stack/globalField/index.js @@ -104,7 +104,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, apiVersion: this.apiVersion }) + this.query = query({ http: http, wrapperCollection: GlobalFieldCollection, apiVersion: this.apiVersion }) /** * @description The Import a global field call imports a global field into a stack. @@ -160,4 +160,4 @@ export function createFormData (data) { formData.append('global_field', uploadStream) return formData } -} \ No newline at end of file +} diff --git a/lib/stack/index.js b/lib/stack/index.js index 09127082..3ced19ee 100644 --- a/lib/stack/index.js +++ b/lib/stack/index.js @@ -164,10 +164,12 @@ export function Stack (http, data) { * .then((globalField) => console.log(globalField)) * */ + // eslint-disable-next-line camelcase this.globalField = (globalFieldUid = null, api_version = '3.0') => { - const data = { - stackHeaders: this.stackHeaders, - api_version: api_version + const data = { + stackHeaders: this.stackHeaders, + // eslint-disable-next-line camelcase + api_version: api_version } 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 b550d9bc..eb5ad12a 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, createNestedGlobalField, createNestedGlobalFieldForReference } from '../mock/globalfield' +import { createGlobalField } from '../mock/globalfield' import { contentstackClient } from '../utility/ContentstackClient.js' import dotenv from 'dotenv' @@ -199,7 +199,6 @@ describe('Global Field api Test', () => { // .catch(done) // }) - // it("should delete nested global field", (done) => { // makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }) // .delete() From 7659c614ebb1942140f1afe71d52fd57ebdcaf68 Mon Sep 17 00:00:00 2001 From: sunil-lakshman <104969541+sunil-lakshman@users.noreply.github.com> Date: Fri, 11 Apr 2025 16:22:21 +0530 Subject: [PATCH 9/9] version bump --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0b55535d..91deb3ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/management", - "version": "1.20.1", + "version": "1.20.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/management", - "version": "1.20.1", + "version": "1.20.2", "license": "MIT", "dependencies": { "assert": "^2.1.0", diff --git a/package.json b/package.json index 0e5498ff..55932f62 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/management", - "version": "1.20.1", + "version": "1.20.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",