Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [v1.20.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.20.2) (2025-04-21)
- Fix
- Handle the sanity tests when ENVs are not provided
- 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
Expand Down
6 changes: 6 additions & 0 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
120 changes: 31 additions & 89 deletions lib/stack/globalField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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<GlobalField.GlobalField>} 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)
}
Expand All @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 0 additions & 3 deletions test/sanity-check/api/globalfield-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
)
Expand Down
49 changes: 8 additions & 41 deletions test/unit/globalField-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('Contentstack GlobalField test', () => {
makeGlobalField()
.create()
.then((globalField) => {
checkGlobalField(globalField.global_field)
checkGlobalField(globalField)
done()
})
.catch(done)
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('Contentstack GlobalField test', () => {
})
.update()
.then((globalField) => {
checkGlobalField(globalField.global_field)
checkGlobalField(globalField)
done()
})
.catch(done)
Expand All @@ -141,7 +141,7 @@ describe('Contentstack GlobalField test', () => {
})
.fetch()
.then((globalField) => {
checkGlobalField(globalField.global_field)
checkGlobalField(globalField)
done()
})
.catch(done)
Expand Down Expand Up @@ -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()
})

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -339,7 +339,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => {
})
.update()
.then((globalField) => {
checkGlobalField(globalField.global_field)
checkGlobalField(globalField)
done()
})
.catch(done)
Expand All @@ -361,7 +361,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => {
})
.fetch()
.then((globalField) => {
checkGlobalField(globalField.global_field)
checkGlobalField(globalField)
done()
})
.catch(done)
Expand Down Expand Up @@ -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) {
Expand Down
Loading