Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature nested global field #259

Merged
merged 5 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## [v1.19.2](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.2) (2025-01-27)
- Enhancement
- Added support for nested global fields.

## [v1.19.1](https://github.com/contentstack/contentstack-management-javascript/tree/v1.19.1) (2025-01-27)
- Feature
Expand Down
9 changes: 7 additions & 2 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export const upload = async ({ http, urlPath, stackHeaders, formData, params, me

export const create = ({ http, params }) => {
return async function (data, param) {
this.stackHeaders = {
...this.stackHeaders,
...(http.httpClientParams.headers?.api_version && { api_version: http.httpClientParams.headers.api_version })
};
const headers = {
headers: {
...cloneDeep(params),
Expand Down Expand Up @@ -101,10 +105,11 @@ export const create = ({ http, params }) => {
}
}

export const query = ({ http, wrapperCollection }) => {
export const query = ({ http, wrapperCollection, apiVersion }) => {
return function (params = {}) {
const headers = {
...cloneDeep(this.stackHeaders)
...cloneDeep(this.stackHeaders),
...(apiVersion != null ? { api_version: apiVersion } : {})
}
if (this.organization_uid) {
headers.organization_uid = this.organization_uid
Expand Down
164 changes: 158 additions & 6 deletions lib/stack/globalField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import { createReadStream } from 'fs'

export function GlobalField (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.apiVersion = data.api_version || undefined;

if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
this.urlPath = `/global_fields`

if (data.global_field) {
Expand All @@ -34,7 +39,82 @@ export function GlobalField (http, data = {}) {
* .then((globalField) => console.log(globalField))
*
*/
this.update = update(http, 'global_field')
this.update = async (config) => {
try {
// Add `api_version` to headers if `this.apiVersion` is defined
if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
}
}
const response = await http.put(`${this.urlPath}`, config, headers);
// Remove `api_version` from headers after fetching data
if (this.apiVersion) {
delete this.stackHeaders.api_version;
}
if (response.data) {
return response.data;
} else {
throw error(response);
}
} catch (err) {
throw error(err);
}
}


/**
* @description The Update GlobalField call lets you update the name and description of an existing GlobalField.
* @memberof GlobalField
* @func update
* @returns {Promise<GlobalField.GlobalField>} Promise for GlobalField instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const data = {
* "global_field": {
* "title": "Nested Global Field33",
* "uid": "nested_global_field33",
* "schema": [
* {
* "data_type": "text",
* "display_name": "Single Line Textbox",
* "uid": "single_line"
* },
* {
* "data_type": "global_field",
* "display_name": "Global",
* "uid": "global_field",
* "reference_to": "nested_global_field_123"
* }
* ]
* }
* }
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').updateNestedGlobalField(data, { headers: { api_version: '3.2' }})
* .then((globalField) => {
console.log(globalField)
* })
*/
this.updateNestedGlobalField = async (config, headers={}) => {
const apiVersion = {api_version: '3.2' }
this.stackHeaders = {...this.stackHeaders, ...apiVersion, ...headers}
try {
const headers = {
headers: { ...cloneDeep(this.stackHeaders) }
}
const response = await http.put(`${this.urlPath}`, config, headers)
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}

/**
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
Expand All @@ -48,7 +128,35 @@ export function GlobalField (http, data = {}) {
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
this.delete = async () => {
let param = {};
try {
// Add `api_version` to headers if `this.apiVersion` is defined
if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
},
params: {
...cloneDeep(param)
}
};
const response = await http.delete(this.urlPath, headers);
if (this.apiVersion) {
delete this.stackHeaders.api_version;
}
if (response.data) {
return response.data;
} else {
throw error(response);
}
} catch (err) {
throw error(err);
}
};


/**
* @description The fetch GlobalField call fetches GlobalField details.
Expand All @@ -63,7 +171,30 @@ export function GlobalField (http, data = {}) {
* .then((globalField) => console.log(globalField))
*
*/
this.fetch = fetch(http, 'global_field')
this.fetch = async function (param = {}) {
try {
if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
},
params: {
...cloneDeep(param)
}
};
const response = await http.get(this.urlPath, headers);
if (response.data) {
return response.data;
} else {
throw error(response);
}
} catch (err) {
throw error(err);
}
};

} else {
/**
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
Expand All @@ -86,7 +217,27 @@ export function GlobalField (http, data = {}) {
* client.stack().globalField().create({ global_field })
* .then((globalField) => console.log(globalField))
*/
this.create = create({ http: http })
this.create = async (data) => {
try {
if (this.apiVersion) {
this.stackHeaders.api_version = this.apiVersion;
}
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
}
};
const response = await http.post(`${this.urlPath}`, data, headers);
if (response.data) {
return response.data;
} else {
return error(response);
}
} catch (err) {
return error(err);
}
};


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

/**
* @description The Import a global field call imports a global field into a stack.
Expand All @@ -119,8 +270,9 @@ export function GlobalField (http, data = {}) {
* .then((globalField) => console.log(globalField))
*
*/
this.import = async function (data, params = {}) {
this.import = async function (data, params = {}, headers = {}) {
try {
this.stackHeaders = { ...this.stackHeaders, ...headers };
const response = await upload({
http: http,
urlPath: `${this.urlPath}/import`,
Expand Down
28 changes: 23 additions & 5 deletions lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,30 @@ export function Stack (http, data) {
*
* client.stack({ api_key: 'api_key'}).globalField('globalField_uid').fetch()
* .then((globalField) => console.log(globalField))
*/
this.globalField = (globalFieldUid = null) => {
const data = { stackHeaders: this.stackHeaders }
if (globalFieldUid) {
data.global_field = { uid: globalFieldUid }
*
* client.stack({ api_key: 'api_key'}).globalField('globalField_uid', { api_version: '3.2' }).fetch()
* .then((globalField) => console.log(globalField))
*
*/
this.globalField = (globalFieldUidOrOptions = null, options = {}) => {
let data = {
stackHeaders: this.stackHeaders,
};
if (typeof globalFieldUidOrOptions === 'object' && globalFieldUidOrOptions !== null) {
options = globalFieldUidOrOptions;
} else if (globalFieldUidOrOptions) {
data.global_field = { uid: globalFieldUidOrOptions };
}

// Safely handle `options` and check for `api_version`
options = options || {}; // Ensure `options` is always an object
if (options && typeof options === 'object' && options.api_version) {
data.api_version = options.api_version;
if (options.api_version === '3.2') {
data.nested_global_fields = true;
}
}

return new GlobalField(http, data)
}

Expand Down
Loading
Loading