diff --git a/README.md b/README.md index 4c68556..0fe1d18 100644 --- a/README.md +++ b/README.md @@ -406,6 +406,11 @@ The response `data` is the same as the [API response body](https://docs.aws.amaz ##### `chatSession.sendEvent()` ```js +/** + * @param {Object} args - Event arguments with contentType and optional metadata and content + * @param {string} args.contentType - Content type of the event + * @param {string|Object} [args.content] - Optional payload (stringified if object) + */ const awsSdkResponse = await chatSession.sendEvent({ contentType: "application/vnd.amazonaws.connect.event.typing" }); diff --git a/src/core/chatController.js b/src/core/chatController.js index 1d769cf..4755151 100644 --- a/src/core/chatController.js +++ b/src/core/chatController.js @@ -143,6 +143,11 @@ class ChatController { return this.chatClient.sendEvent(...args); } + /** + * @param {Object} args - Event arguments with contentType and optional metadata and content + * @param {string} args.contentType - Content type of the event + * @param {string|Object} [args.content] - Optional payload (stringified if object) + */ sendEvent(args) { if (!this._validateConnectionStatus('sendEvent')) { return Promise.reject(`Failed to call sendEvent, No active connection`); @@ -154,6 +159,7 @@ class ChatController { const content = args.content || null; var eventType = getEventTypeFromContentType(args.contentType); var parsedContent = typeof content === "string" ? JSON.parse(content) : content; + var stringifiedContent = content && (typeof content === "string" ? content : (typeof content === "object" ? JSON.stringify(content) : null)); if (this.messageReceiptUtil.isMessageReceipt(eventType, args)) { // Ignore all MessageReceipt events if(!GlobalConfig.isFeatureEnabled(FEATURES.MESSAGE_RECEIPTS_ENABLED) || !parsedContent.messageId) { @@ -167,7 +173,7 @@ class ChatController { return this.messageReceiptUtil.prioritizeAndSendMessageReceipt(this.chatClient, this.sendEventIfChatHasNotEnded.bind(this), connectionToken, args.contentType, - content, + stringifiedContent, eventType, GlobalConfig.getMessageReceiptsThrottleTime()) .then(this.handleRequestSuccess(metadata, ACPS_METHODS.SEND_EVENT, startTime, args.contentType)) @@ -177,7 +183,7 @@ class ChatController { .sendEvent( connectionToken, args.contentType, - content + stringifiedContent ) .then(this.handleRequestSuccess(metadata, ACPS_METHODS.SEND_EVENT, startTime, args.contentType)) .catch(this.handleRequestFailure(metadata, ACPS_METHODS.SEND_EVENT, startTime, args.contentType)); diff --git a/src/core/chatController.spec.js b/src/core/chatController.spec.js index 7fb275d..f9282be 100644 --- a/src/core/chatController.spec.js +++ b/src/core/chatController.spec.js @@ -807,6 +807,77 @@ describe("ChatController", () => { }); describe("Test ChatController induvidual methods with mock data", () => { + describe("sendEvent content handling", () => { + let chatController; + + beforeEach(async () => { + chatController = getChatController(); + await chatController.connect(); + chatClient.sendEvent.mockClear(); + }); + + test("handles string content without modification", async () => { + const stringContent = JSON.stringify({ key: "value" }); + const args = { + contentType: CONTENT_TYPE.participantJoined, + content: stringContent + }; + + await chatController.sendEvent(args); + + expect(chatClient.sendEvent).toHaveBeenCalledWith( + "token", + CONTENT_TYPE.participantJoined, + stringContent + ); + }); + + test("stringifies object content", async () => { + const objectContent = { key: "value" }; + const args = { + contentType: CONTENT_TYPE.participantJoined, + content: objectContent + }; + + await chatController.sendEvent(args); + + expect(chatClient.sendEvent).toHaveBeenCalledWith( + "token", + CONTENT_TYPE.participantJoined, + JSON.stringify(objectContent) + ); + }); + + test("handles null content as null", async () => { + const args = { + contentType: CONTENT_TYPE.participantJoined, + content: null + }; + + await chatController.sendEvent(args); + + expect(chatClient.sendEvent).toHaveBeenCalledWith( + "token", + CONTENT_TYPE.participantJoined, + null + ); + }); + + test("handles undefined content as null", async () => { + const args = { + contentType: CONTENT_TYPE.participantJoined + }; + + await chatController.sendEvent(args); + + expect(chatClient.sendEvent).toHaveBeenCalledWith( + "token", + CONTENT_TYPE.participantJoined, + null + ); + }); + }); + test("getEventTypeFromContentType should return default INCOMING_MESSAGE type", () => { const chatController = getChatController(); chatController._forwardChatEvent = jest.fn();