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

Added support for nested global fields #230

Draft
wants to merge 5 commits into
base: feature_nested_global_field
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 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
59 changes: 58 additions & 1 deletion lib/stack/globalField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import { createReadStream } from 'fs'

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

if (this.apiVersion) {
http.defaults.headers.api_version = this.apiVersion;
http.httpClientParams.headers.api_version = this.apiVersion;
}
this.urlPath = `/global_fields`

if (data.global_field) {
Expand All @@ -36,6 +42,56 @@ export function GlobalField (http, data = {}) {
*/
this.update = update(http, 'global_field')

/**
* @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.
* @memberof GlobalField
Expand Down Expand Up @@ -119,8 +175,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
26 changes: 21 additions & 5 deletions lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,28 @@ 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 };
}

if (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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/management",
"version": "1.18.4",
"version": "1.19.0",
"description": "The Content Management API is used to manage the content of your Contentstack account",
"main": "./dist/node/contentstack-management.js",
"browser": "./dist/web/contentstack-management.js",
Expand Down
Loading
Loading