Skip to content

Commit 55dfb1a

Browse files
Merge branch 'feature_nested_global_field' into fix/api_version_conflict
2 parents a06184c + 494bb51 commit 55dfb1a

File tree

4 files changed

+291
-4
lines changed

4 files changed

+291
-4
lines changed

lib/stack/globalField/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,56 @@ export function GlobalField (http, data = {}) {
6666
}
6767

6868

69+
/**
70+
* @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
71+
* @memberof GlobalField
72+
* @func update
73+
* @returns {Promise<GlobalField.GlobalField>} Promise for GlobalField instance
74+
* @example
75+
* import * as contentstack from '@contentstack/management'
76+
* const client = contentstack.client()
77+
* const data = {
78+
* "global_field": {
79+
* "title": "Nested Global Field33",
80+
* "uid": "nested_global_field33",
81+
* "schema": [
82+
* {
83+
* "data_type": "text",
84+
* "display_name": "Single Line Textbox",
85+
* "uid": "single_line"
86+
* },
87+
* {
88+
* "data_type": "global_field",
89+
* "display_name": "Global",
90+
* "uid": "global_field",
91+
* "reference_to": "nested_global_field_123"
92+
* }
93+
* ]
94+
* }
95+
* }
96+
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').updateNestedGlobalField(data, { headers: { api_version: '3.2' }})
97+
* .then((globalField) => {
98+
console.log(globalField)
99+
* })
100+
*/
101+
this.updateNestedGlobalField = async (config, headers={}) => {
102+
const apiVersion = {api_version: '3.2' }
103+
this.stackHeaders = {...this.stackHeaders, ...apiVersion, ...headers}
104+
try {
105+
const headers = {
106+
headers: { ...cloneDeep(this.stackHeaders) }
107+
}
108+
const response = await http.put(`${this.urlPath}`, config, headers)
109+
if (response.data) {
110+
return response.data
111+
} else {
112+
throw error(response)
113+
}
114+
} catch (err) {
115+
throw error(err)
116+
}
117+
}
118+
69119
/**
70120
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
71121
* @memberof GlobalField

lib/stack/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ export function Stack (http, data) {
173173
} else if (globalFieldUidOrOptions) {
174174
data.global_field = { uid: globalFieldUidOrOptions };
175175
}
176-
177176

178177
// Safely handle `options` and check for `api_version`
179178
options = options || {}; // Ensure `options` is always an object

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ describe("Global Field api Test", () => {
115115
.catch(done);
116116
});
117117

118+
118119
it("should get global field title matching Upload", (done) => {
119120
makeGlobalField()
120121
.query({ query: { title: "Upload" } })
@@ -129,6 +130,20 @@ describe("Global Field api Test", () => {
129130
.catch(done);
130131
});
131132

133+
it("should get all nested global fields from Query", (done) => {
134+
makeGlobalField({ api_version: '3.2' })
135+
.query()
136+
.find()
137+
.then((collection) => {
138+
collection.items.forEach((globalField) => {
139+
expect(globalField.uid).to.be.not.equal(null);
140+
expect(globalField.title).to.be.equal("Upload");
141+
});
142+
done();
143+
})
144+
.catch(done);
145+
});
146+
132147
it("should get all nested global fields from Query", (done) => {
133148
makeGlobalField({ api_version: '3.2' })
134149
.query()
@@ -148,7 +163,19 @@ describe("Global Field api Test", () => {
148163
it('should create nested global field for reference', done => {
149164
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalFieldForReference)
150165
.then(globalField => {
151-
expect(globalField.global_field.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
166+
expect(globalField.global_field.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
167+
done();
168+
})
169+
.catch(err => {
170+
console.error('Error:', err.response?.data || err.message);
171+
done(err);
172+
});
173+
});
174+
175+
it('should create nested global field', done => {
176+
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField)
177+
.then(globalField => {
178+
expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
152179
done();
153180
})
154181
.catch(err => {

test/unit/globalField-test.js

Lines changed: 213 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,219 @@ describe("Contentstack GlobalField test (API Version 3.2)", () => {
440440
});
441441
});
442442

443-
function makeGlobalField(data) {
444-
return new GlobalField(Axios, data);
443+
describe('Contentstack GlobalField test (API Version 3.2)', () => {
444+
it('GlobalField test without uid', done => {
445+
const globalField = makeGlobalField({ api_version: '3.2' })
446+
expect(globalField.urlPath).to.be.equal('/global_fields')
447+
expect(globalField.apiVersion).to.be.equal('3.2')
448+
expect(globalField.stackHeaders).to.be.equal(undefined)
449+
expect(globalField.update).to.be.equal(undefined)
450+
expect(globalField.delete).to.be.equal(undefined)
451+
expect(globalField.fetch).to.be.equal(undefined)
452+
expect(globalField.create).to.not.equal(undefined)
453+
expect(globalField.query).to.not.equal(undefined)
454+
done()
455+
})
456+
457+
it('GlobalField test with uid', done => {
458+
const globalField = makeGlobalField({
459+
global_field: {
460+
...systemUidMock
461+
},
462+
api_version: '3.2'
463+
})
464+
expect(globalField.urlPath).to.be.equal(`/global_fields/${systemUidMock.uid}`)
465+
expect(globalField.apiVersion).to.be.equal('3.2')
466+
expect(globalField.update).to.not.equal(undefined)
467+
expect(globalField.delete).to.not.equal(undefined)
468+
expect(globalField.fetch).to.not.equal(undefined)
469+
expect(globalField.create).to.be.equal(undefined)
470+
expect(globalField.query).to.be.equal(undefined)
471+
done()
472+
})
473+
474+
it('GlobalField test with Stack Headers', done => {
475+
const globalField = makeGlobalField({
476+
global_field: {
477+
...systemUidMock
478+
},
479+
stackHeaders: stackHeadersMock,
480+
api_version: '3.2'
481+
})
482+
expect(globalField.urlPath).to.be.equal(`/global_fields/${systemUidMock.uid}`)
483+
expect(globalField.apiVersion).to.be.equal('3.2')
484+
expect(globalField.stackHeaders).to.not.equal(undefined)
485+
expect(globalField.stackHeaders.api_key).to.be.equal(stackHeadersMock.api_key)
486+
expect(globalField.update).to.not.equal(undefined)
487+
expect(globalField.delete).to.not.equal(undefined)
488+
expect(globalField.fetch).to.not.equal(undefined)
489+
expect(globalField.create).to.be.equal(undefined)
490+
expect(globalField.query).to.be.equal(undefined)
491+
done()
492+
})
493+
494+
it('GlobalField Collection test with blank data', done => {
495+
const globalFields = new GlobalFieldCollection(Axios, { api_version: '3.2' })
496+
expect(globalFields.length).to.be.equal(0)
497+
done()
498+
})
499+
500+
it('GlobalField Collection test with data', done => {
501+
const globalFields = new GlobalFieldCollection(Axios, {
502+
global_fields: [
503+
nestedGlobalFieldMock
504+
],
505+
api_version: '3.2'
506+
})
507+
expect(globalFields.length).to.be.equal(1)
508+
checkGlobalField(globalFields[0])
509+
done()
510+
})
511+
512+
it('GlobalField create test', done => {
513+
var mock = new MockAdapter(Axios)
514+
mock.onPost('/global_fields').reply(200, {
515+
global_field: {
516+
...nestedGlobalFieldMock
517+
}
518+
})
519+
makeGlobalField({ api_version: '3.2' })
520+
.create()
521+
.then((globalField) => {
522+
checkGlobalField(globalField)
523+
done()
524+
})
525+
.catch(done)
526+
})
527+
528+
it('GlobalField Query test', done => {
529+
var mock = new MockAdapter(Axios)
530+
mock.onGet('/global_fields').reply(200, {
531+
global_fields: [
532+
nestedGlobalFieldMock
533+
]
534+
})
535+
makeGlobalField({ api_version: '3.2' })
536+
.query()
537+
.find()
538+
.then((globalField) => {
539+
checkGlobalField(globalField.items[0])
540+
done()
541+
})
542+
.catch(done)
543+
})
544+
545+
it('GlobalField update test', done => {
546+
var mock = new MockAdapter(Axios)
547+
mock.onPut('/global_fields/UID').reply(200, {
548+
global_field: {
549+
...nestedGlobalFieldMock
550+
}
551+
})
552+
makeGlobalField({
553+
global_field: {
554+
...systemUidMock
555+
},
556+
stackHeaders: stackHeadersMock,
557+
api_version: '3.2'
558+
})
559+
.update()
560+
.then((globalField) => {
561+
checkGlobalField(globalField)
562+
done()
563+
})
564+
.catch(done)
565+
})
566+
567+
it('GlobalField fetch test', done => {
568+
var mock = new MockAdapter(Axios)
569+
mock.onGet('/global_fields/UID').reply(200, {
570+
global_field: {
571+
...nestedGlobalFieldMock
572+
}
573+
})
574+
makeGlobalField({
575+
global_field: {
576+
...systemUidMock
577+
},
578+
stackHeaders: stackHeadersMock,
579+
api_version: '3.2'
580+
})
581+
.fetch()
582+
.then((globalField) => {
583+
checkGlobalField(globalField)
584+
done()
585+
})
586+
.catch(done)
587+
})
588+
589+
it('GlobalField delete test', done => {
590+
var mock = new MockAdapter(Axios)
591+
mock.onDelete('/global_fields/UID').reply(200, {
592+
...noticeMock
593+
})
594+
makeGlobalField({
595+
global_field: {
596+
...systemUidMock
597+
},
598+
stackHeaders: stackHeadersMock,
599+
api_version: '3.2'
600+
})
601+
.delete()
602+
.then((response) => {
603+
expect(response.notice).to.be.equal(noticeMock.notice)
604+
done()
605+
})
606+
.catch(done)
607+
})
608+
609+
it('GlobalField import test', done => {
610+
var mock = new MockAdapter(Axios)
611+
mock.onPost('/global_fields/import').reply(200, {
612+
global_field: {
613+
...globalFieldMock
614+
}
615+
})
616+
const gfUpload = { global_field: path.join(__dirname, '../api/mock/globalfield.json') }
617+
const form = createFormData(gfUpload)()
618+
var boundary = form.getBoundary()
619+
620+
expect(boundary).to.be.equal(form.getBoundary())
621+
expect(boundary.length).to.be.equal(50)
622+
makeGlobalField({ api_version: '3.2' })
623+
.import()
624+
.then((webhook) => {
625+
checkGlobalField(webhook)
626+
done()
627+
})
628+
.catch(done)
629+
})
630+
631+
it('GlobalField import test with overwrite flag', done => {
632+
var mock = new MockAdapter(Axios)
633+
mock.onPost('/global_fields/import').reply(200, {
634+
global_field: {
635+
...nestedGlobalFieldMock
636+
}
637+
})
638+
const gfUpload = { global_field: path.join(__dirname, '../api/mock/globalfield.json') }
639+
const form = createFormData(gfUpload)()
640+
var boundary = form.getBoundary()
641+
642+
expect(boundary).to.be.equal(form.getBoundary())
643+
expect(boundary.length).to.be.equal(50)
644+
makeGlobalField({ api_version: '3.2' })
645+
.import(gfUpload, { overwrite: true })
646+
.then((webhook) => {
647+
checkGlobalField(webhook)
648+
done()
649+
})
650+
.catch(done)
651+
})
652+
})
653+
654+
function makeGlobalField (data) {
655+
return new GlobalField(Axios, data)
445656
}
446657

447658
function checkGlobalField(globalField) {

0 commit comments

Comments
 (0)