Skip to content

Commit 494bb51

Browse files
Added support for nested global fields (#230)
* Added support for nested global fields * Added updated method * Added update method testcases * Added mock file * Fixed PR comments
1 parent 0e5105b commit 494bb51

File tree

9 files changed

+677
-120
lines changed

9 files changed

+677
-120
lines changed

lib/entity.js

+4
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

+58-1
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,56 @@ 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.2' }})
73+
* .then((globalField) => {
74+
console.log(globalField)
75+
* })
76+
*/
77+
this.updateNestedGlobalField = async (config, headers={}) => {
78+
const apiVersion = {api_version: '3.2' }
79+
this.stackHeaders = {...this.stackHeaders, ...apiVersion, ...headers}
80+
try {
81+
const headers = {
82+
headers: { ...cloneDeep(this.stackHeaders) }
83+
}
84+
const response = await http.put(`${this.urlPath}`, config, headers)
85+
if (response.data) {
86+
return response.data
87+
} else {
88+
throw error(response)
89+
}
90+
} catch (err) {
91+
throw error(err)
92+
}
93+
}
94+
3995
/**
4096
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
4197
* @memberof GlobalField
@@ -119,8 +175,9 @@ export function GlobalField (http, data = {}) {
119175
* .then((globalField) => console.log(globalField))
120176
*
121177
*/
122-
this.import = async function (data, params = {}) {
178+
this.import = async function (data, params = {}, headers = {}) {
123179
try {
180+
this.stackHeaders = { ...this.stackHeaders, ...headers };
124181
const response = await upload({
125182
http: http,
126183
urlPath: `${this.urlPath}/import`,

lib/stack/index.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,28 @@ 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+
if (options?.api_version) {
178+
data.api_version = options.api_version;
179+
if (options.api_version === '3.2') {
180+
data.nested_global_fields = true;
181+
}
167182
}
183+
168184
return new GlobalField(http, data)
169185
}
170186

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/management",
3-
"version": "1.18.4",
3+
"version": "1.19.0",
44
"description": "The Content Management API is used to manage the content of your Contentstack account",
55
"main": "./dist/node/contentstack-management.js",
66
"browser": "./dist/web/contentstack-management.js",

0 commit comments

Comments
 (0)