Skip to content

Commit cfcad6d

Browse files
Added updated method
1 parent 15dbab3 commit cfcad6d

File tree

4 files changed

+79
-24
lines changed

4 files changed

+79
-24
lines changed

lib/entity.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ export const upload = async ({ http, urlPath, stackHeaders, formData, params, me
7171

7272
export const create = ({ http, params }) => {
7373
return async function (data, param) {
74+
this.stackHeaders = {
75+
...this.stackHeaders,
76+
...(http.httpClientParams.headers?.api_version && { api_version: http.httpClientParams.headers.api_version })
77+
};
7478
const headers = {
7579
headers: {
7680
...cloneDeep(params),

lib/stack/globalField/index.js

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

1212
export function GlobalField (http, data = {}) {
1313
this.stackHeaders = data.stackHeaders
14+
this.apiVersion = data.api_version || undefined;
15+
16+
if (this.apiVersion) {
17+
http.defaults.headers.api_version = this.apiVersion;
18+
http.httpClientParams.headers.api_version = this.apiVersion;
19+
}
1420
this.urlPath = `/global_fields`
1521

1622
if (data.global_field) {
@@ -36,6 +42,55 @@ export function GlobalField (http, data = {}) {
3642
*/
3743
this.update = update(http, 'global_field')
3844

45+
/**
46+
* @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
47+
* @memberof GlobalField
48+
* @func update
49+
* @returns {Promise<GlobalField.GlobalField>} Promise for GlobalField instance
50+
* @example
51+
* import * as contentstack from '@contentstack/management'
52+
* const client = contentstack.client()
53+
* const data = {
54+
* "global_field": {
55+
* "title": "Nested Global Field33",
56+
* "uid": "nested_global_field33",
57+
* "schema": [
58+
* {
59+
* "data_type": "text",
60+
* "display_name": "Single Line Textbox",
61+
* "uid": "single_line"
62+
* },
63+
* {
64+
* "data_type": "global_field",
65+
* "display_name": "Global",
66+
* "uid": "global_field",
67+
* "reference_to": "nested_global_field_123"
68+
* }
69+
* ]
70+
* }
71+
* }
72+
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').updateNestedGlobalField(data, { headers: { api_version: '3.3' }})
73+
* .then((globalField) => {
74+
console.log(globalField)
75+
* })
76+
*/
77+
this.updateNestedGlobalField = async (config, headers={}) => {
78+
this.stackHeaders = {api_version: '3.2' }
79+
try {
80+
const headers = {
81+
headers: { ...cloneDeep(this.stackHeaders) }
82+
}
83+
const response = await http.put(`${this.urlPath}`, config, headers)
84+
if (response.data) {
85+
return response.data
86+
} else {
87+
throw error(response)
88+
}
89+
} catch (err) {
90+
throw error(err)
91+
}
92+
}
93+
3994
/**
4095
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
4196
* @memberof GlobalField

lib/stack/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ export function Stack (http, data) {
178178

179179
if (options?.api_version) {
180180
data.api_version = options.api_version;
181-
http.defaults.headers.api_version = data.api_version;
182-
http.httpClientParams.headers.api_version = data.api_version;
181+
if (options.api_version === '3.2') {
182+
data.nested_global_fields = true;
183+
}
183184
}
184185

185186
return new GlobalField(http, data)

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

Lines changed: 17 additions & 22 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, createNestedGlobalField } from '../mock/globalfield'
77
import { contentstackClient } from '../utility/ContentstackClient.js'
88
import dotenv from 'dotenv'
99

@@ -155,8 +155,8 @@ describe("Global Field api Test", () => {
155155
.catch(done);
156156
});
157157

158-
it("should get all nested global field from Query", (done) => {
159-
makeGlobalField({ api_version: "3.2" })
158+
it("should get all nested global fields from Query", (done) => {
159+
makeGlobalField({ api_version: '3.2' })
160160
.query()
161161
.find()
162162
.then((collection) => {
@@ -171,18 +171,7 @@ describe("Global Field api Test", () => {
171171
});
172172

173173
it('should create nested global field', done => {
174-
const payload = {
175-
global_field: {
176-
title: 'Nested Global Field',
177-
uid: 'nested_global_field222',
178-
schema: [
179-
{ data_type: 'text', display_name: 'Single Line Textbox', uid: 'single_line' },
180-
{ data_type: 'global_field', display_name: 'Global', uid: 'global_field', reference_to: 'first' },
181-
],
182-
},
183-
};
184-
185-
makeGlobalField({ api_version: '3.2' }).create(payload)
174+
makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField)
186175
.then(globalField => {
187176
console.log('Response:', globalField);
188177
expect(globalField.uid).to.be.equal(payload.global_field.uid);
@@ -195,7 +184,7 @@ describe("Global Field api Test", () => {
195184
});
196185

197186
it('should fetch nested global field', done => {
198-
makeGlobalField('nested_global_field222').fetch()
187+
makeGlobalField('nested_global_field333', { api_version: '3.2' }).fetch()
199188
.then(globalField => {
200189
console.log('Response:', globalField);
201190
expect(globalField.uid).to.be.equal('nested_global_field222');
@@ -207,8 +196,18 @@ describe("Global Field api Test", () => {
207196
});
208197
});
209198

199+
it('should update nested global fields without fetch', done => {
200+
makeGlobalField(createNestedGlobalField.global_field.uid, { headers: { api_version: '3.2' }})
201+
.updateNestedGlobalField(createNestedGlobalField)
202+
.then((globalField) => {
203+
expect(globalField.global_field.schema.length).to.be.equal(2)
204+
done()
205+
})
206+
.catch(done)
207+
})
208+
210209
it("should delete nested global field", (done) => {
211-
makeGlobalField("nested_global_field222")
210+
makeGlobalField("nested_global_field333", { api_version: '3.2' })
212211
.delete()
213212
.then((data) => {
214213
console.log("Response:", data);
@@ -245,16 +244,12 @@ describe("Global Field api Test", () => {
245244
function makeGlobalField(globalFieldUid = null, options = {}) {
246245
let uid = null;
247246
let finalOptions = options;
248-
// If globalFieldUid is an object, treat it as options
249247
if (typeof globalFieldUid === "object") {
250248
finalOptions = globalFieldUid;
251249
} else {
252250
uid = globalFieldUid;
253251
}
254-
// Ensure finalOptions is always an object with default values
255252
finalOptions = finalOptions || {};
256-
257253
return client
258-
.stack({ api_key: process.env.API_KEY })
259-
.globalField(uid, finalOptions);
254+
.stack({ api_key: process.env.API_KEY }).globalField(uid, finalOptions);
260255
}

0 commit comments

Comments
 (0)