Skip to content

Fixed migration rte issue in CLI #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +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,
...(http.httpClientParams.headers?.api_version && { api_version: http.httpClientParams.headers.api_version })
...this.stackHeaders
}
const headers = {
headers: {
Expand Down Expand Up @@ -265,6 +264,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
}
Expand Down
118 changes: 6 additions & 112 deletions lib/stack/globalField/index.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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.api_version
}
this.urlPath = `/global_fields`

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
23 changes: 7 additions & 16 deletions lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,16 @@ export function Stack (http, data) {
* .then((globalField) => console.log(globalField))
*
*/
this.globalField = (globalFieldUidOrOptions = null, options = {}) => {
// eslint-disable-next-line camelcase
this.globalField = (globalFieldUid = null, api_version = '3.0') => {
const data = {
stackHeaders: this.stackHeaders
stackHeaders: this.stackHeaders,
// eslint-disable-next-line camelcase
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)
}

Expand Down
64 changes: 39 additions & 25 deletions test/sanity-check/api/globalfield-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -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 => {
Expand All @@ -175,11 +167,33 @@ 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)
Expand Down
8 changes: 4 additions & 4 deletions test/unit/globalField-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand All @@ -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)
Expand All @@ -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
Expand Down