Skip to content

Commit cc5b7c7

Browse files
Feature nested global field (#259)
* Added support for nested global fields (#230) * Added support for nested global fields * Added mock file * Fixed PR comments * Added released version and removed duplicate code * Update package-lock.json file * Commeneted nested global fields sanity testcases
1 parent aefa26b commit cc5b7c7

File tree

12 files changed

+1067
-460
lines changed

12 files changed

+1067
-460
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## [v1.19.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.2) (2025-01-27)
3+
- Enhancement
4+
- Added support for nested global fields.
25

36
## [v1.19.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.1) (2025-01-27)
47
- Feature

lib/entity.js

+7-2
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),
@@ -101,10 +105,11 @@ export const create = ({ http, params }) => {
101105
}
102106
}
103107

104-
export const query = ({ http, wrapperCollection }) => {
108+
export const query = ({ http, wrapperCollection, apiVersion }) => {
105109
return function (params = {}) {
106110
const headers = {
107-
...cloneDeep(this.stackHeaders)
111+
...cloneDeep(this.stackHeaders),
112+
...(apiVersion != null ? { api_version: apiVersion } : {})
108113
}
109114
if (this.organization_uid) {
110115
headers.organization_uid = this.organization_uid

lib/stack/globalField/index.js

+158-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ 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+
this.stackHeaders.api_version = this.apiVersion;
18+
}
1419
this.urlPath = `/global_fields`
1520

1621
if (data.global_field) {
@@ -34,7 +39,82 @@ export function GlobalField (http, data = {}) {
3439
* .then((globalField) => console.log(globalField))
3540
*
3641
*/
37-
this.update = update(http, 'global_field')
42+
this.update = async (config) => {
43+
try {
44+
// Add `api_version` to headers if `this.apiVersion` is defined
45+
if (this.apiVersion) {
46+
this.stackHeaders.api_version = this.apiVersion;
47+
}
48+
const headers = {
49+
headers: {
50+
...cloneDeep(this.stackHeaders)
51+
}
52+
}
53+
const response = await http.put(`${this.urlPath}`, config, headers);
54+
// Remove `api_version` from headers after fetching data
55+
if (this.apiVersion) {
56+
delete this.stackHeaders.api_version;
57+
}
58+
if (response.data) {
59+
return response.data;
60+
} else {
61+
throw error(response);
62+
}
63+
} catch (err) {
64+
throw error(err);
65+
}
66+
}
67+
68+
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+
}
38118

39119
/**
40120
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
@@ -48,7 +128,35 @@ export function GlobalField (http, data = {}) {
48128
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
49129
* .then((response) => console.log(response.notice))
50130
*/
51-
this.delete = deleteEntity(http)
131+
this.delete = async () => {
132+
let param = {};
133+
try {
134+
// Add `api_version` to headers if `this.apiVersion` is defined
135+
if (this.apiVersion) {
136+
this.stackHeaders.api_version = this.apiVersion;
137+
}
138+
const headers = {
139+
headers: {
140+
...cloneDeep(this.stackHeaders)
141+
},
142+
params: {
143+
...cloneDeep(param)
144+
}
145+
};
146+
const response = await http.delete(this.urlPath, headers);
147+
if (this.apiVersion) {
148+
delete this.stackHeaders.api_version;
149+
}
150+
if (response.data) {
151+
return response.data;
152+
} else {
153+
throw error(response);
154+
}
155+
} catch (err) {
156+
throw error(err);
157+
}
158+
};
159+
52160

53161
/**
54162
* @description The fetch GlobalField call fetches GlobalField details.
@@ -63,7 +171,30 @@ export function GlobalField (http, data = {}) {
63171
* .then((globalField) => console.log(globalField))
64172
*
65173
*/
66-
this.fetch = fetch(http, 'global_field')
174+
this.fetch = async function (param = {}) {
175+
try {
176+
if (this.apiVersion) {
177+
this.stackHeaders.api_version = this.apiVersion;
178+
}
179+
const headers = {
180+
headers: {
181+
...cloneDeep(this.stackHeaders)
182+
},
183+
params: {
184+
...cloneDeep(param)
185+
}
186+
};
187+
const response = await http.get(this.urlPath, headers);
188+
if (response.data) {
189+
return response.data;
190+
} else {
191+
throw error(response);
192+
}
193+
} catch (err) {
194+
throw error(err);
195+
}
196+
};
197+
67198
} else {
68199
/**
69200
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
@@ -86,7 +217,27 @@ export function GlobalField (http, data = {}) {
86217
* client.stack().globalField().create({ global_field })
87218
* .then((globalField) => console.log(globalField))
88219
*/
89-
this.create = create({ http: http })
220+
this.create = async (data) => {
221+
try {
222+
if (this.apiVersion) {
223+
this.stackHeaders.api_version = this.apiVersion;
224+
}
225+
const headers = {
226+
headers: {
227+
...cloneDeep(this.stackHeaders)
228+
}
229+
};
230+
const response = await http.post(`${this.urlPath}`, data, headers);
231+
if (response.data) {
232+
return response.data;
233+
} else {
234+
return error(response);
235+
}
236+
} catch (err) {
237+
return error(err);
238+
}
239+
};
240+
90241

91242
/**
92243
* @description The Query on GlobalField will allow to fetch details of all or specific GlobalField
@@ -101,7 +252,7 @@ export function GlobalField (http, data = {}) {
101252
* client.stack().globalField().query({ query: { name: 'Global Field Name' } }).find()
102253
* .then((globalFields) => console.log(globalFields))
103254
*/
104-
this.query = query({ http: http, wrapperCollection: GlobalFieldCollection })
255+
this.query = query({ http: http, wrapperCollection: GlobalFieldCollection, apiVersion: this.apiVersion })
105256

106257
/**
107258
* @description The Import a global field call imports a global field into a stack.
@@ -119,8 +270,9 @@ export function GlobalField (http, data = {}) {
119270
* .then((globalField) => console.log(globalField))
120271
*
121272
*/
122-
this.import = async function (data, params = {}) {
273+
this.import = async function (data, params = {}, headers = {}) {
123274
try {
275+
this.stackHeaders = { ...this.stackHeaders, ...headers };
124276
const response = await upload({
125277
http: http,
126278
urlPath: `${this.urlPath}/import`,

lib/stack/index.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,30 @@ export function Stack (http, data) {
159159
*
160160
* client.stack({ api_key: 'api_key'}).globalField('globalField_uid').fetch()
161161
* .then((globalField) => console.log(globalField))
162-
*/
163-
this.globalField = (globalFieldUid = null) => {
164-
const data = { stackHeaders: this.stackHeaders }
165-
if (globalFieldUid) {
166-
data.global_field = { uid: globalFieldUid }
162+
*
163+
* client.stack({ api_key: 'api_key'}).globalField('globalField_uid', { api_version: '3.2' }).fetch()
164+
* .then((globalField) => console.log(globalField))
165+
*
166+
*/
167+
this.globalField = (globalFieldUidOrOptions = null, options = {}) => {
168+
let data = {
169+
stackHeaders: this.stackHeaders,
170+
};
171+
if (typeof globalFieldUidOrOptions === 'object' && globalFieldUidOrOptions !== null) {
172+
options = globalFieldUidOrOptions;
173+
} else if (globalFieldUidOrOptions) {
174+
data.global_field = { uid: globalFieldUidOrOptions };
175+
}
176+
177+
// Safely handle `options` and check for `api_version`
178+
options = options || {}; // Ensure `options` is always an object
179+
if (options && typeof options === 'object' && options.api_version) {
180+
data.api_version = options.api_version;
181+
if (options.api_version === '3.2') {
182+
data.nested_global_fields = true;
183+
}
167184
}
185+
168186
return new GlobalField(http, data)
169187
}
170188

0 commit comments

Comments
 (0)