Skip to content

Added branch support for global fields. #357

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 3 commits into from
May 21, 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
8 changes: 7 additions & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ fileignoreconfig:
checksum: 5baabd7d2c391648163f9371f0e5e9484f8fb90fa2284cfc378732ec3192c193
- filename: test/sanity-check/api/stack-test.js
checksum: 198d5cf7ead33b079249dc3ecdee61a9c57453e93f1073ed0341400983e5aa53
version: ""
- filename: lib/stack/index.js
checksum: 6aab5edf85efb17951418b4dc4402889cd24c8d786c671185074aeb4d50f0242
version: ""
fileignoreconfig:
- filename: test/unit/globalField-test.js
checksum: 25185e3400a12e10a043dc47502d8f30b7e1c4f2b6b4d3b8b55cdc19850c48bf
version: "1.0"
12 changes: 8 additions & 4 deletions lib/stack/globalField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import { createReadStream } from 'fs'
*/

export function GlobalField (http, data = {}) {
this.stackHeaders = data.stackHeaders
if (data.api_version) {
this.stackHeaders.api_version = data.api_version
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) {
if (data.global_field && data.global_field.uid) {
Object.assign(this, cloneDeep(data.global_field))
this.urlPath = `/global_fields/${this.uid}`
/**
Expand Down
27 changes: 21 additions & 6 deletions lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 }
Expand Down
202 changes: 101 additions & 101 deletions test/sanity-check/api/globalfield-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand Down
24 changes: 11 additions & 13 deletions test/unit/globalField-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
})
Expand All @@ -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}`
Expand All @@ -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}`
Expand Down
5 changes: 3 additions & 2 deletions types/stack/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;




Expand Down
Loading