Skip to content

Commit

Permalink
better type guarding
Browse files Browse the repository at this point in the history
  • Loading branch information
popestr committed Jul 9, 2024
1 parent c0209c3 commit d84f53d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AwsConnectEvent } from "../models/AwsConnectEvent";
import { EventMap, EventCallback } from "../models/EventCallback";
import { LoggerConfig } from "../models/LoggerConfig";
import { ChatCoreAwsConnectConfig } from "../models/ChatCoreAwsConnectConfig";
import { isCustomerChatSession } from "../models/ChatSession";
import "amazon-connect-chatjs";

/**
Expand All @@ -12,7 +13,7 @@ import "amazon-connect-chatjs";
* @internal
*/
export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
private session?: connect.ActiveChatSession;
private session?: connect.ActiveCustomerChatSession;
private eventListeners: { [T in keyof EventMap]?: EventCallback<T>[] } = {};
private loggerConfig: LoggerConfig = {
level: "ERROR",
Expand Down Expand Up @@ -51,11 +52,16 @@ export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
region: messageRsp.integrationDetails?.awsConnectHandoff?.region,
});

this.session = connect.ChatSession.create({
const sess = connect.ChatSession.create({
chatDetails: connectionCreds,
type: "CUSTOMER",
});

if (!isCustomerChatSession(sess)) {
throw new Error("Unexpected non-customer chat session type");
}
this.session = sess;

const { connectCalled, connectSuccess } = await this.session.connect({});
if (!connectCalled || !connectSuccess) {
throw new Error("Failed to connect to chat session");
Expand Down Expand Up @@ -136,8 +142,7 @@ export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
return;
}

const customerSession = this.session as connect.ActiveCustomerChatSession;
customerSession.disconnectParticipant();
this.session.disconnectParticipant();
this.session = undefined;
}
}
14 changes: 14 additions & 0 deletions packages/chat-core-aws-connect/src/models/ChatSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Returns true if the Amazon connect chat session is a customer chat session.
* @param session - The Amazon connect chat session.
*
* @internal
*/
export function isCustomerChatSession(
session: connect.ActiveChatSession
): session is connect.ActiveCustomerChatSession {
return (
(session as connect.ActiveCustomerChatSession).disconnectParticipant !==
undefined
);
}
11 changes: 10 additions & 1 deletion packages/chat-core-aws-connect/tests/ChatCoreAwsConnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
AwsConnectEventData,
} from "../src/models/AwsConnectEvent";
import "amazon-connect-chatjs";
import "../src/models/connect";

const createSpy = jest.fn().mockReturnValue(mockChatSession());
const globalConfigSpy = jest.fn();
Expand Down Expand Up @@ -389,3 +388,13 @@ it("noops when clearing undefined session", async () => {
chatCoreAwsConnect.resetSession();
expect(chatCoreAwsConnect.getSession()).toBeUndefined();
});

it("throws error when session is not a customer session", async () => {
const sess = {} as unknown as connect.ActiveChatSession;
createSpy.mockReturnValue(sess);

const chatCoreAwsConnect = provideChatCoreAwsConnect();
expect(() => chatCoreAwsConnect.init(mockMessageResponse())).rejects.toThrow(
"Unexpected non-customer chat session type"
);
});

0 comments on commit d84f53d

Please sign in to comment.