diff --git a/__test__/entry.test.ts b/__test__/entry.test.ts index 5fdfff9..2548935 100644 --- a/__test__/entry.test.ts +++ b/__test__/entry.test.ts @@ -57,6 +57,56 @@ describe("Entry", () => { expect(testData.entry).toEqual(entry.getData()); }); + 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); + }); + + 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", () => { it("getField undefined", function () { const uid = "group1.group"; @@ -133,7 +183,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 +232,4 @@ describe("Entry", () => { "Callback must be a function" ); }); -}); \ No newline at end of file +}); 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 b8bb4d6..14ecd76 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. */ @@ -91,6 +92,24 @@ class Entry { return this._data; } + /** + * 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 { + try { + const response = + await this._connection.sendToParent( + "getDraftData" + ); + return response?.data ?? {}; + } catch (error) { + throw new Error("Failed to retrieve draft data."); + } + } + /** * * 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 & {});