Skip to content

Commit 40ce51a

Browse files
Added branch support for global fields.
1 parent bc29a41 commit 40ce51a

File tree

4 files changed

+131
-112
lines changed

4 files changed

+131
-112
lines changed

lib/stack/globalField/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ import { createReadStream } from 'fs'
1111

1212
export function GlobalField (http, data = {}) {
1313
this.stackHeaders = data.stackHeaders
14-
if (data.api_version) {
15-
this.stackHeaders.api_version = data.api_version
14+
if (data.apiVersion) {
15+
this.stackHeaders.api_version = data.apiVersion
16+
}
17+
if (data.branch) {
18+
this.stackHeaders.branch = data.branch
1619
}
1720
this.urlPath = `/global_fields`
1821

19-
if (data.global_field) {
22+
if (data.global_field && data.global_field.uid) {
2023
Object.assign(this, cloneDeep(data.global_field))
2124
this.urlPath = `/global_fields/${this.uid}`
2225
/**

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)

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)