Skip to content

Commit 4d6e017

Browse files
Merge pull request #357 from contentstack/fix/dx-3036
Added branch support for global fields.
2 parents bc29a41 + 4d828e8 commit 4d6e017

File tree

6 files changed

+151
-127
lines changed

6 files changed

+151
-127
lines changed

.talismanrc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ fileignoreconfig:
88
checksum: 5baabd7d2c391648163f9371f0e5e9484f8fb90fa2284cfc378732ec3192c193
99
- filename: test/sanity-check/api/stack-test.js
1010
checksum: 198d5cf7ead33b079249dc3ecdee61a9c57453e93f1073ed0341400983e5aa53
11-
version: ""
11+
- filename: lib/stack/index.js
12+
checksum: 6aab5edf85efb17951418b4dc4402889cd24c8d786c671185074aeb4d50f0242
13+
version: ""
14+
fileignoreconfig:
15+
- filename: test/unit/globalField-test.js
16+
checksum: 25185e3400a12e10a043dc47502d8f30b7e1c4f2b6b4d3b8b55cdc19850c48bf
17+
version: "1.0"

lib/stack/globalField/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ import { createReadStream } from 'fs'
1010
*/
1111

1212
export function GlobalField (http, data = {}) {
13-
this.stackHeaders = data.stackHeaders
14-
if (data.api_version) {
15-
this.stackHeaders.api_version = data.api_version
13+
const rawHeaders = data.stackHeaders || {}
14+
15+
this.stackHeaders = {
16+
...cloneDeep(rawHeaders),
17+
...(data.apiVersion ? { api_version: data.apiVersion } : {}),
18+
...(data.branch ? { branch: data.branch } : {})
1619
}
20+
1721
this.urlPath = `/global_fields`
1822

19-
if (data.global_field) {
23+
if (data.global_field && data.global_field.uid) {
2024
Object.assign(this, cloneDeep(data.global_field))
2125
this.urlPath = `/global_fields/${this.uid}`
2226
/**

lib/stack/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export function Stack (http, data) {
154154
* import * as contentstack from '@contentstack/management'
155155
* const client = contentstack.client()
156156
*
157-
* client.stack({ api_key: 'api_key'}).globalField().create()
157+
* client.stack({ api_key: 'api_key'}).globalField().create(data)
158158
* .then((globalField) => console.log(globalField))
159159
*
160160
* client.stack({ api_key: 'api_key'}).globalField('globalField_uid').fetch()
@@ -164,12 +164,27 @@ export function Stack (http, data) {
164164
* .then((globalField) => console.log(globalField))
165165
*
166166
*/
167-
// eslint-disable-next-line camelcase
168-
this.globalField = (globalFieldUid = null, api_version = '3.0') => {
167+
168+
this.globalField = (uidOrOptions = null, option = {}) => {
169+
let globalFieldUid = null
170+
let apiVersion = '3.0'
171+
let branch = 'main'
172+
const stackHeaders = { ...this.stackHeaders }
173+
if (typeof uidOrOptions === 'object' && uidOrOptions !== null) {
174+
option = uidOrOptions
175+
} else {
176+
globalFieldUid = uidOrOptions
177+
}
178+
if (option?.api_version) {
179+
apiVersion = option.api_version
180+
}
181+
if (option?.branch) {
182+
branch = option.branch
183+
}
169184
const data = {
170-
stackHeaders: this.stackHeaders,
171-
// eslint-disable-next-line camelcase
172-
api_version: api_version
185+
stackHeaders,
186+
apiVersion,
187+
branch
173188
}
174189
if (globalFieldUid) {
175190
data.global_field = { uid: globalFieldUid }

test/sanity-check/api/globalfield-test.js

Lines changed: 101 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { expect } from 'chai'
33
import { cloneDeep } from 'lodash'
44
import { describe, it, setup } from 'mocha'
55
import { jsonReader } from '../utility/fileOperations/readwrite'
6-
import { createGlobalField } from '../mock/globalfield'
6+
import { createGlobalField, createNestedGlobalFieldForReference, createNestedGlobalField } from '../mock/globalfield'
77
import { contentstackClient } from '../utility/ContentstackClient.js'
88
import dotenv from 'dotenv'
99

@@ -117,113 +117,113 @@ describe('Global Field api Test', () => {
117117
.catch(done)
118118
})
119119

120-
// it("should get all nested global fields from Query", (done) => {
121-
// makeGlobalField({ api_version: '3.2' })
122-
// .query()
123-
// .find()
124-
// .then((collection) => {
125-
// collection.items.forEach((globalField) => {
126-
// expect(globalField.uid).to.be.not.equal(null);
127-
// expect(globalField.title).to.be.not.equal(null);
128-
// });
129-
// done();
130-
// })
131-
// .catch(done);
132-
// });
120+
it('should get all nested global fields from Query', (done) => {
121+
makeGlobalField({ api_version: '3.2' })
122+
.query()
123+
.find()
124+
.then((collection) => {
125+
collection.items.forEach((globalField) => {
126+
expect(globalField.uid).to.be.not.equal(null)
127+
expect(globalField.title).to.be.not.equal(null)
128+
})
129+
done()
130+
})
131+
.catch(done)
132+
})
133133

134-
// it('should create nested global field for reference', done => {
135-
// makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalFieldForReference)
136-
// .then(globalField => {
137-
// expect(globalField.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
138-
// done();
139-
// })
140-
// .catch(err => {
141-
// console.error('Error:', err.response?.data || err.message);
142-
// done(err);
143-
// });
144-
// });
134+
it('should create nested global field for reference', done => {
135+
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalFieldForReference)
136+
.then(globalField => {
137+
expect(globalField.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid)
138+
done()
139+
})
140+
.catch(err => {
141+
console.error('Error:', err.response?.data || err.message)
142+
done(err)
143+
})
144+
})
145145

146-
// it('should create nested global field', done => {
147-
// makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField)
148-
// .then(globalField => {
149-
// expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
150-
// done();
151-
// })
152-
// .catch(err => {
153-
// console.error('Error:', err.response?.data || err.message);
154-
// done(err);
155-
// });
156-
// });
146+
it('should create nested global field', done => {
147+
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField)
148+
.then(globalField => {
149+
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid)
150+
done()
151+
})
152+
.catch(err => {
153+
console.error('Error:', err.response?.data || err.message)
154+
done(err)
155+
})
156+
})
157157

158-
// it('should fetch nested global field', done => {
159-
// makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
160-
// .then(globalField => {
161-
// expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
162-
// done();
163-
// })
164-
// .catch(err => {
165-
// console.error('Error:', err.response?.data || err.message);
166-
// done(err);
167-
// });
168-
// });
158+
it('should fetch nested global field', done => {
159+
makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
160+
.then(globalField => {
161+
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid)
162+
done()
163+
})
164+
.catch(err => {
165+
console.error('Error:', err.response?.data || err.message)
166+
done(err)
167+
})
168+
})
169169

170-
// it('should fetch and update nested global Field', done => {
171-
// makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
172-
// .then((globalField) => {
173-
// globalField.title = 'Update title'
174-
// return globalField.update()
175-
// })
176-
// .then((updateGlobal) => {
177-
// expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid)
178-
// expect(updateGlobal.title).to.be.equal('Update title')
179-
// expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid)
180-
// expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type)
181-
// expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name)
182-
// done()
183-
// })
184-
// .catch(done)
185-
// })
170+
it('should fetch and update nested global Field', done => {
171+
makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
172+
.then((globalField) => {
173+
globalField.title = 'Update title'
174+
return globalField.update()
175+
})
176+
.then((updateGlobal) => {
177+
expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid)
178+
expect(updateGlobal.title).to.be.equal('Update title')
179+
expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid)
180+
expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type)
181+
expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name)
182+
done()
183+
})
184+
.catch(done)
185+
})
186186

187-
// it('should update nested global Field', done => {
188-
// const globalField = makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' })
189-
// Object.assign(globalField, cloneDeep(createGlobalField.global_field))
190-
// globalField.update()
191-
// .then((updateGlobal) => {
192-
// expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid)
193-
// expect(updateGlobal.title).to.be.equal(createGlobalField.global_field.title)
194-
// expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid)
195-
// expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type)
196-
// expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name)
197-
// done()
198-
// })
199-
// .catch(done)
200-
// })
187+
it('should update nested global Field', done => {
188+
const globalField = makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' })
189+
Object.assign(globalField, cloneDeep(createGlobalField.global_field))
190+
globalField.update()
191+
.then((updateGlobal) => {
192+
expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid)
193+
expect(updateGlobal.title).to.be.equal(createGlobalField.global_field.title)
194+
expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid)
195+
expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type)
196+
expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name)
197+
done()
198+
})
199+
.catch(done)
200+
})
201201

202-
// it("should delete nested global field", (done) => {
203-
// makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' })
204-
// .delete()
205-
// .then((data) => {
206-
// expect(data.notice).to.be.equal("Global Field deleted successfully.");
207-
// done();
208-
// })
209-
// .catch((err) => {
210-
// console.error("Error:", err.response?.data || err.message);
211-
// done(err);
212-
// });
213-
// });
202+
it('should delete nested global field', (done) => {
203+
makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' })
204+
.delete()
205+
.then((data) => {
206+
expect(data.notice).to.be.equal('Global Field deleted successfully.')
207+
done()
208+
})
209+
.catch((err) => {
210+
console.error('Error:', err.response?.data || err.message)
211+
done(err)
212+
})
213+
})
214214

215-
// it("should delete nested global reference field", (done) => {
216-
// makeGlobalField(createNestedGlobalFieldForReference.global_field.uid, { api_version: '3.2' })
217-
// .delete()
218-
// .then((data) => {
219-
// expect(data.notice).to.be.equal("Global Field deleted successfully.");
220-
// done();
221-
// })
222-
// .catch((err) => {
223-
// console.error("Error:", err.response?.data || err.message);
224-
// done(err);
225-
// });
226-
// });
215+
it('should delete nested global reference field', (done) => {
216+
makeGlobalField(createNestedGlobalFieldForReference.global_field.uid, { api_version: '3.2' })
217+
.delete()
218+
.then((data) => {
219+
expect(data.notice).to.be.equal('Global Field deleted successfully.')
220+
done()
221+
})
222+
.catch((err) => {
223+
console.error('Error:', err.response?.data || err.message)
224+
done(err)
225+
})
226+
})
227227

228228
it('should delete global Field', (done) => {
229229
makeGlobalField(createGlobalField.global_field.uid)

test/unit/globalField-test.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Contentstack GlobalField test', () => {
1010
it('GlobalField test without uid', (done) => {
1111
const globalField = makeGlobalField()
1212
expect(globalField.urlPath).to.be.equal('/global_fields')
13-
expect(globalField.stackHeaders).to.be.equal(undefined)
13+
expect(globalField.stackHeaders).to.deep.equal({})
1414
expect(globalField.update).to.be.equal(undefined)
1515
expect(globalField.delete).to.be.equal(undefined)
1616
expect(globalField.fetch).to.be.equal(undefined)
@@ -25,10 +25,9 @@ describe('Contentstack GlobalField test', () => {
2525
...systemUidMock
2626
}
2727
})
28-
expect(globalField.urlPath).to.be.equal(
29-
`/global_fields/${systemUidMock.uid}`
30-
)
31-
expect(globalField.stackHeaders).to.be.equal(undefined)
28+
29+
expect(globalField.urlPath).to.be.equal(`/global_fields/${systemUidMock.uid}`)
30+
expect(globalField.stackHeaders).to.deep.equal({})
3231
expect(globalField.update).to.not.equal(undefined)
3332
expect(globalField.delete).to.not.equal(undefined)
3433
expect(globalField.fetch).to.not.equal(undefined)
@@ -218,10 +217,11 @@ describe('Contentstack GlobalField test', () => {
218217
describe('Contentstack GlobalField test (API Version 3.2)', () => {
219218
it('GlobalField test without uid', (done) => {
220219
const globalField = makeGlobalField({
221-
stackHeaders: stackHeadersMock,
222-
api_version: '3.2' })
223-
expect(globalField.urlPath).to.be.equal('/global_fields')
224-
expect(globalField.stackHeaders.api_version).to.be.equal('3.2')
220+
stackHeaders: { api_key: 'api_key', api_version: '3.2' }
221+
})
222+
223+
expect(globalField.urlPath).to.equal('/global_fields')
224+
expect(globalField.stackHeaders.api_version).to.equal('3.2')
225225
expect(globalField.stackHeaders).to.deep.equal({ api_key: 'api_key', api_version: '3.2' })
226226
done()
227227
})
@@ -231,8 +231,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => {
231231
global_field: {
232232
...systemUidMock
233233
},
234-
stackHeaders: stackHeadersMock,
235-
api_version: '3.2'
234+
stackHeaders: { api_key: 'api_key', api_version: '3.2' }
236235
})
237236
expect(globalField.urlPath).to.be.equal(
238237
`/global_fields/${systemUidMock.uid}`
@@ -251,8 +250,7 @@ describe('Contentstack GlobalField test (API Version 3.2)', () => {
251250
global_field: {
252251
...systemUidMock
253252
},
254-
stackHeaders: stackHeadersMock,
255-
api_version: '3.2'
253+
stackHeaders: { api_key: 'api_key', api_version: '3.2' }
256254
})
257255
expect(globalField.urlPath).to.be.equal(
258256
`/global_fields/${systemUidMock.uid}`

types/stack/index.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ export interface Stack extends SystemFields {
6363

6464
globalField(): GlobalFields;
6565
globalField(uid: string, option?: object): GlobalField;
66-
globalField(options: { api_version: string }): GlobalFields;
67-
globalField(uidOrOptions?: string | { api_version: string }, option?: object): GlobalFields | GlobalField;
66+
globalField(options: object): GlobalFields;
67+
globalField(uidOrOptions?: string | object, option?: object): GlobalFields | GlobalField;
68+
6869

6970

7071

0 commit comments

Comments
 (0)