From 2c8cfec83553a36140728162f42568ed8f1e2360 Mon Sep 17 00:00:00 2001 From: MaitriGurey1 Date: Wed, 14 May 2025 13:22:31 +0530 Subject: [PATCH 1/5] feat: added getDraftData method --- src/entry.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/entry.ts b/src/entry.ts index b8bb4d6..279016a 100755 --- a/src/entry.ts +++ b/src/entry.ts @@ -91,6 +91,19 @@ class Entry { return this._data; } + /** + * Gets the draft data of the current entry. + * If no changes are available, returns an empty object. + * @return {Object} Returns the draft entry data (_changedData) if available; otherwise, returns an empty object. + */ + getDraftData() { + if (this._changedData && Object.keys(this._changedData).length > 0) { + return this._changedData; + } else { + return {}; + } + } + /** * * From fb887ed49dc74e529d2bda617e24097dd78513a8 Mon Sep 17 00:00:00 2001 From: MaitriGurey1 Date: Wed, 14 May 2025 14:34:42 +0530 Subject: [PATCH 2/5] fix: added test case --- __test__/entry.test.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/__test__/entry.test.ts b/__test__/entry.test.ts index 5fdfff9..a7e506b 100644 --- a/__test__/entry.test.ts +++ b/__test__/entry.test.ts @@ -57,6 +57,17 @@ describe("Entry", () => { expect(testData.entry).toEqual(entry.getData()); }); + it("getDraftData", () => { + entry._changedData = { title: "Draft Title", content: "Draft Content" }; + expect(entry.getDraftData()).toEqual({ + title: "Draft Title", + content: "Draft Content", + }); + + entry._changedData = {}; + expect(entry.getDraftData()).toEqual({}); + }); + describe("getField", () => { it("getField undefined", function () { const uid = "group1.group"; @@ -133,7 +144,7 @@ describe("Entry", () => { }); it("should use custom Field instance if internal flag is set", () => { const fieldInstance: any = jest.fn(); - entry = new Entry(testData as any, connection as any, emitter ,{ + entry = new Entry(testData as any, connection as any, emitter, { _internalFlags: { FieldInstance: fieldInstance, }, @@ -182,4 +193,4 @@ describe("Entry", () => { "Callback must be a function" ); }); -}); \ No newline at end of file +}); From 6f590ebc88075dbf834ca320de1c4a7f7efa3098 Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Wed, 11 Jun 2025 17:17:10 +0530 Subject: [PATCH 3/5] test: getDraftData --- src/entry.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/entry.ts b/src/entry.ts index 279016a..bcbcb68 100755 --- a/src/entry.ts +++ b/src/entry.ts @@ -17,6 +17,7 @@ import { import { ContentType, PublishDetails, Schema } from "./types/stack.types"; import { GenericObjectType } from "./types/common.types"; import EventRegistry from "./EventRegistry"; +import { onData, onError } from "./utils/utils"; /** Class representing an entry from Contentstack UI. Not available for Dashboard UI Location. */ @@ -94,14 +95,15 @@ class Entry { /** * Gets the draft data of the current entry. * If no changes are available, returns an empty object. - * @return {Object} Returns the draft entry data (_changedData) if available; otherwise, returns an empty object. + * @return {Promise} Returns a promise that resolves to the draft entry data (_changedData) if available; otherwise, returns an empty object. */ - getDraftData() { - if (this._changedData && Object.keys(this._changedData).length > 0) { - return this._changedData; - } else { - return {}; - } + async getDraftData(): Promise { + const changedData = this._changedData || {}; + console.log("changedData", changedData); + return this._connection + .sendToParent("getDraftData", { changedData }) + .then(onData) + .catch(onError); } /** From 22dd5cab212bd09337b5616d5ebd8f3a5f77464d Mon Sep 17 00:00:00 2001 From: Abhishek Ezhava Date: Thu, 12 Jun 2025 18:11:47 +0530 Subject: [PATCH 4/5] fix: test-cases & version update --- __test__/entry.test.ts | 53 ++++++++++++++++++++++++++++++++++++------ package-lock.json | 4 ++-- package.json | 2 +- src/entry.ts | 22 +++++++++++------- 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/__test__/entry.test.ts b/__test__/entry.test.ts index a7e506b..2548935 100644 --- a/__test__/entry.test.ts +++ b/__test__/entry.test.ts @@ -57,15 +57,54 @@ describe("Entry", () => { expect(testData.entry).toEqual(entry.getData()); }); - it("getDraftData", () => { - entry._changedData = { title: "Draft Title", content: "Draft Content" }; - expect(entry.getDraftData()).toEqual({ - title: "Draft Title", - content: "Draft Content", + describe("getDraftData", () => { + it("should return draft data successfully", async () => { + const mockDraftData = { + title: "Draft Title", + description: "Draft Description", + }; + const sendToParentSpy = jest + .spyOn(connection, "sendToParent") + .mockResolvedValue({ data: mockDraftData }); + + const result = await entry.getDraftData(); + + expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData"); + expect(result).toEqual(mockDraftData); }); - entry._changedData = {}; - expect(entry.getDraftData()).toEqual({}); + it("should return empty object when response data is null", async () => { + const sendToParentSpy = jest + .spyOn(connection, "sendToParent") + .mockResolvedValue({ data: null }); + + const result = await entry.getDraftData(); + + expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData"); + expect(result).toEqual({}); + }); + + it("should return empty object when response data is undefined", async () => { + const sendToParentSpy = jest + .spyOn(connection, "sendToParent") + .mockResolvedValue({ data: undefined }); + + const result = await entry.getDraftData(); + + expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData"); + expect(result).toEqual({}); + }); + + it("should throw error when sendToParent fails", async () => { + const sendToParentSpy = jest + .spyOn(connection, "sendToParent") + .mockRejectedValue(new Error("Connection failed")); + + await expect(entry.getDraftData()).rejects.toThrow( + "Failed to retrieve draft data." + ); + expect(sendToParentSpy).toHaveBeenCalledWith("getDraftData"); + }); }); describe("getField", () => { diff --git a/package-lock.json b/package-lock.json index 99a0c72..6b31b37 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/app-sdk", - "version": "2.3.1", + "version": "2.3.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/app-sdk", - "version": "2.3.1", + "version": "2.3.2", "license": "MIT", "dependencies": { "axios": "^1.7.9", diff --git a/package.json b/package.json index 30f8cff..5fa7d7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@contentstack/app-sdk", - "version": "2.3.1", + "version": "2.3.2", "types": "dist/src/index.d.ts", "description": "The Contentstack App SDK allows you to customize your Contentstack applications.", "main": "dist/index.js", diff --git a/src/entry.ts b/src/entry.ts index bcbcb68..14ecd76 100755 --- a/src/entry.ts +++ b/src/entry.ts @@ -93,17 +93,21 @@ class Entry { } /** - * Gets the draft data of the current entry. - * If no changes are available, returns an empty object. - * @return {Promise} Returns a promise that resolves to the draft entry data (_changedData) if available; otherwise, returns an empty object. + * Retrieves the draft data of the current unsaved entry. + * Returns an empty object if there are no changes. + * + * @returns {Promise} The draft entry data or an empty object. */ async getDraftData(): Promise { - const changedData = this._changedData || {}; - console.log("changedData", changedData); - return this._connection - .sendToParent("getDraftData", { changedData }) - .then(onData) - .catch(onError); + try { + const response = + await this._connection.sendToParent( + "getDraftData" + ); + return response?.data ?? {}; + } catch (error) { + throw new Error("Failed to retrieve draft data."); + } } /** From 7e94ebaad68bd29764718c5df567a601cd37bc19 Mon Sep 17 00:00:00 2001 From: Amit Kanswal <41462986+Amitkanswal@users.noreply.github.com> Date: Wed, 9 Jul 2025 12:23:23 +0530 Subject: [PATCH 5/5] feat:updated region support (#151) * fix:region support --- src/types.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index c351baa..bee397c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -254,6 +254,7 @@ export enum Region { UNKNOWN = "UNKNOWN", NA = "NA", EU = "EU", + AU = "AU", AZURE_NA = "AZURE_NA", AZURE_EU = "AZURE_EU", GCP_NA = "GCP_NA", @@ -264,7 +265,9 @@ export type RegionType = | "UNKNOWN" | "NA" | "EU" + | "AU" | "AZURE_NA" | "AZURE_EU" | "GCP_NA" - | string; + | "GCP_EU" + | (string & {});