diff --git a/lib/entity.js b/lib/entity.js index a1d55a23..5206e474 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -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), diff --git a/lib/stack/globalField/index.js b/lib/stack/globalField/index.js index 1f4f3119..dba00ef8 100644 --- a/lib/stack/globalField/index.js +++ b/lib/stack/globalField/index.js @@ -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) { @@ -36,6 +42,55 @@ 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} 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.3' }}) + * .then((globalField) => { + console.log(globalField) + * }) + */ + this.updateNestedGlobalField = async (config, headers={}) => { + this.stackHeaders = {api_version: '3.2' } + 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 diff --git a/lib/stack/index.js b/lib/stack/index.js index 2ba65286..4f71ada6 100644 --- a/lib/stack/index.js +++ b/lib/stack/index.js @@ -178,8 +178,9 @@ export function Stack (http, data) { if (options?.api_version) { data.api_version = options.api_version; - http.defaults.headers.api_version = data.api_version; - http.httpClientParams.headers.api_version = data.api_version; + if (options.api_version === '3.2') { + data.nested_global_fields = true; + } } return new GlobalField(http, data) diff --git a/test/sanity-check/api/globalfield-test.js b/test/sanity-check/api/globalfield-test.js index fdb2f9dc..669e3cf4 100644 --- a/test/sanity-check/api/globalfield-test.js +++ b/test/sanity-check/api/globalfield-test.js @@ -3,7 +3,7 @@ import { expect } from 'chai' import { cloneDeep } from 'lodash' import { describe, it, setup } from 'mocha' import { jsonReader } from '../utility/fileOperations/readwrite' -import { createGlobalField } from '../mock/globalfield' +import { createGlobalField, createNestedGlobalField } from '../mock/globalfield' import { contentstackClient } from '../utility/ContentstackClient.js' import dotenv from 'dotenv' @@ -155,8 +155,8 @@ describe("Global Field api Test", () => { .catch(done); }); - it("should get all nested global field from Query", (done) => { - makeGlobalField({ api_version: "3.2" }) + it("should get all nested global fields from Query", (done) => { + makeGlobalField({ api_version: '3.2' }) .query() .find() .then((collection) => { @@ -171,18 +171,7 @@ describe("Global Field api Test", () => { }); it('should create nested global field', done => { - const payload = { - global_field: { - title: 'Nested Global Field', - uid: 'nested_global_field222', - schema: [ - { data_type: 'text', display_name: 'Single Line Textbox', uid: 'single_line' }, - { data_type: 'global_field', display_name: 'Global', uid: 'global_field', reference_to: 'first' }, - ], - }, - }; - - makeGlobalField({ api_version: '3.2' }).create(payload) + makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField) .then(globalField => { console.log('Response:', globalField); expect(globalField.uid).to.be.equal(payload.global_field.uid); @@ -195,7 +184,7 @@ describe("Global Field api Test", () => { }); it('should fetch nested global field', done => { - makeGlobalField('nested_global_field222').fetch() + makeGlobalField('nested_global_field333', { api_version: '3.2' }).fetch() .then(globalField => { console.log('Response:', globalField); expect(globalField.uid).to.be.equal('nested_global_field222'); @@ -207,8 +196,18 @@ describe("Global Field api Test", () => { }); }); + it('should update nested global fields without fetch', done => { + makeGlobalField(createNestedGlobalField.global_field.uid, { headers: { api_version: '3.2' }}) + .updateNestedGlobalField(createNestedGlobalField) + .then((globalField) => { + expect(globalField.global_field.schema.length).to.be.equal(2) + done() + }) + .catch(done) + }) + it("should delete nested global field", (done) => { - makeGlobalField("nested_global_field222") + makeGlobalField("nested_global_field333", { api_version: '3.2' }) .delete() .then((data) => { console.log("Response:", data); @@ -245,16 +244,12 @@ describe("Global Field api Test", () => { function makeGlobalField(globalFieldUid = null, options = {}) { let uid = null; let finalOptions = options; - // If globalFieldUid is an object, treat it as options if (typeof globalFieldUid === "object") { finalOptions = globalFieldUid; } else { uid = globalFieldUid; } - // Ensure finalOptions is always an object with default values finalOptions = finalOptions || {}; - return client - .stack({ api_key: process.env.API_KEY }) - .globalField(uid, finalOptions); + .stack({ api_key: process.env.API_KEY }).globalField(uid, finalOptions); }