Skip to content

Commit

Permalink
more fixes, add logger config, better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
popestr committed Jul 1, 2024
1 parent 0e0d03f commit 3fc7243
Show file tree
Hide file tree
Showing 22 changed files with 284 additions and 95 deletions.
32 changes: 0 additions & 32 deletions packages/chat-core-aws-connect/THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -283,38 +283,6 @@ SOFTWARE.

-----------

The following NPM package may be included in this product:

- [email protected]

This package contains the following license and notice below:

Copyright (c) 2015, Scott Motte
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-----------

The following NPM package may be included in this product:

- [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

## ChatCoreAwsConnect.emit() method

Emit an event into the AWS Connect chat session. Supported events are: - typing: The customer is typing.

**Signature:**

```typescript
emit(eventName: string, data: any): void;
emit<T extends keyof EventMap>(eventName: T, data: EventMap[T]): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| eventName | string | |
| data | any | |
| eventName | T | The name of the event to emit. |
| data | EventMap\[T\] | The data to be sent with the event. |
**Returns:**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## ChatCoreAwsConnect.getSession() method

Get the current AWS Connect chat session.

**Signature:**

```typescript
Expand All @@ -13,3 +15,7 @@ getSession(): connect.ActiveChatSession | undefined;

connect.ActiveChatSession \| undefined

## Remarks

If the session is not initialized, this method will return `undefined`<!-- -->.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## ChatCoreAwsConnect.init() method

Initialize the AWS Connect chat session using the credentials from the Chat API.

**Signature:**

```typescript
Expand All @@ -14,7 +16,7 @@ init(messageResponse: MessageResponse): Promise<void>;

| Parameter | Type | Description |
| --- | --- | --- |
| messageResponse | MessageResponse | |
| messageResponse | MessageResponse | The response returned from a successful call to the Chat API. |

**Returns:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export interface ChatCoreAwsConnect

| Method | Description |
| --- | --- |
| [emit(eventName, data)](./chat-core-aws-connect.chatcoreawsconnect.emit.md) | |
| [getSession()](./chat-core-aws-connect.chatcoreawsconnect.getsession.md) | |
| [init(messageResponse)](./chat-core-aws-connect.chatcoreawsconnect.init.md) | |
| [on(eventName, cb)](./chat-core-aws-connect.chatcoreawsconnect.on.md) | |
| [processMessage(request)](./chat-core-aws-connect.chatcoreawsconnect.processmessage.md) | |
| [emit(eventName, data)](./chat-core-aws-connect.chatcoreawsconnect.emit.md) | Emit an event into the AWS Connect chat session. Supported events are: - typing: The customer is typing. |
| [getSession()](./chat-core-aws-connect.chatcoreawsconnect.getsession.md) | Get the current AWS Connect chat session. |
| [init(messageResponse)](./chat-core-aws-connect.chatcoreawsconnect.init.md) | Initialize the AWS Connect chat session using the credentials from the Chat API. |
| [on(eventName, cb)](./chat-core-aws-connect.chatcoreawsconnect.on.md) | Register a callback for an event triggered within the AWS Connect chat session. Supported events are: - message: A new message has been received. - typing: The agent is typing. - close: The chat session has been closed. |
| [processMessage(request)](./chat-core-aws-connect.chatcoreawsconnect.processmessage.md) | Process a message sent by the user. |

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

## ChatCoreAwsConnect.on() method

Register a callback for an event triggered within the AWS Connect chat session. Supported events are: - message: A new message has been received. - typing: The agent is typing. - close: The chat session has been closed.

**Signature:**

```typescript
on(eventName: string, cb: EventListener): void;
on<T extends keyof EventMap>(eventName: T, cb: EventCallback<T>): void;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| eventName | string | |
| cb | EventListener | |
| eventName | T | The name of the event to listen for. |
| cb | EventCallback&lt;T&gt; | The callback to be executed when the event is triggered. |
**Returns:**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

## ChatCoreAwsConnect.processMessage() method

Process a message sent by the user.

**Signature:**

```typescript
Expand All @@ -14,7 +16,7 @@ processMessage(request: MessageRequest): Promise<void>;

| Parameter | Type | Description |
| --- | --- | --- |
| request | MessageRequest | |
| request | MessageRequest | The message sent by the user, in the Chat API format. |

**Returns:**

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@yext/chat-core-aws-connect](./chat-core-aws-connect.md) &gt; [LoggerConfig](./chat-core-aws-connect.loggerconfig.md)

## LoggerConfig type

Configuration for the internal logger of the AWS Connect Chat session.

**Signature:**

```typescript
export type LoggerConfig = {
level: keyof typeof connect.LogLevel;
customizedLogger?: connect.Logger;
};
```
8 changes: 7 additions & 1 deletion packages/chat-core-aws-connect/docs/chat-core-aws-connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@

| Function | Description |
| --- | --- |
| [provideChatCoreAwsConnect()](./chat-core-aws-connect.providechatcoreawsconnect.md) | Provider for the ChatCore integration with AWS Connect. |
| [provideChatCoreAwsConnect(loggerConfig)](./chat-core-aws-connect.providechatcoreawsconnect.md) | Provider for the ChatCore integration with AWS Connect. |

## Interfaces

| Interface | Description |
| --- | --- |
| [ChatCoreAwsConnect](./chat-core-aws-connect.chatcoreawsconnect.md) | Provides methods for interacting with Chat's AWS Connect integration. |

## Type Aliases

| Type Alias | Description |
| --- | --- |
| [LoggerConfig](./chat-core-aws-connect.loggerconfig.md) | Configuration for the internal logger of the AWS Connect Chat session. |

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ Provider for the ChatCore integration with AWS Connect.
**Signature:**

```typescript
export declare function provideChatCoreAwsConnect(): ChatCoreAwsConnect;
export declare function provideChatCoreAwsConnect(loggerConfig?: LoggerConfig): ChatCoreAwsConnect;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| loggerConfig | [LoggerConfig](./chat-core-aws-connect.loggerconfig.md) | _(Optional)_ Configuration for the logger. If not provided, the default logger will be used with level ERROR. |

**Returns:**

[ChatCoreAwsConnect](./chat-core-aws-connect.chatcoreawsconnect.md)
Expand Down
19 changes: 11 additions & 8 deletions packages/chat-core-aws-connect/etc/chat-core-aws-connect.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ import { MessageResponse } from '@yext/chat-core';

// @public
export interface ChatCoreAwsConnect {
// (undocumented)
emit(eventName: string, data: any): void;
// (undocumented)
emit<T extends keyof EventMap>(eventName: T, data: EventMap[T]): void;
getSession(): connect.ActiveChatSession | undefined;
// (undocumented)
init(messageResponse: MessageResponse): Promise<void>;
// (undocumented)
on(eventName: string, cb: EventListener): void;
// (undocumented)
// Warning: (ae-forgotten-export) The symbol "EventMap" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "EventCallback" needs to be exported by the entry point index.d.ts
on<T extends keyof EventMap>(eventName: T, cb: EventCallback<T>): void;
processMessage(request: MessageRequest): Promise<void>;
}

// @public
export function provideChatCoreAwsConnect(): ChatCoreAwsConnect;
export type LoggerConfig = {
level: keyof typeof connect.LogLevel;
customizedLogger?: connect.Logger;
};

// @public
export function provideChatCoreAwsConnect(loggerConfig?: LoggerConfig): ChatCoreAwsConnect;

// (No @packageDocumentation comment for this package)

Expand Down
6 changes: 3 additions & 3 deletions packages/chat-core-aws-connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
"homepage": "https://github.com/yext/chat-core#readme",
"dependencies": {
"amazon-connect-chatjs": "^2.3.0",
"cross-fetch": "^3.1.5",
"dotenv": "^16.4.5"
"cross-fetch": "^3.1.5"
},
"devDependencies": {
"@babel/preset-env": "^7.21.5",
Expand All @@ -65,7 +64,8 @@
"prettier": "^2.8.8",
"rollup": "^3.29.0",
"rollup-plugin-typescript2": "^0.35.0",
"typescript": "^5.0.4"
"typescript": "^5.0.4",
"dotenv": "^16.4.5"
},
"peerDependencies": {
"@yext/chat-core": "^0.8.1"
Expand Down
9 changes: 7 additions & 2 deletions packages/chat-core-aws-connect/src/CoreAwsConnectProvider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { ChatCoreAwsConnectImpl } from "./infra/ChatCoreAwsConnectImpl";
import { ChatCoreAwsConnect } from "./models";
import { LoggerConfig } from "./models/LoggerConfig";

/**
* Provider for the ChatCore integration with AWS Connect.
*
* @param loggerConfig - Configuration for the logger. If not provided, the default logger will be used with level ERROR.
*
* @public
*/
export function provideChatCoreAwsConnect(): ChatCoreAwsConnect {
return new ChatCoreAwsConnectImpl();
export function provideChatCoreAwsConnect(
loggerConfig?: LoggerConfig
): ChatCoreAwsConnect {
return new ChatCoreAwsConnectImpl(loggerConfig);
}
32 changes: 21 additions & 11 deletions packages/chat-core-aws-connect/src/infra/ChatCoreAwsConnectImpl.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ChatCoreAwsConnect } from "../models/ChatCoreAwsConnect";
import { MessageRequest, MessageResponse } from "@yext/chat-core";
import { AwsConnectEvent } from "../models/AwsConnectEvent";
import { EventCallback } from "../models/EventCallback";
import { EventMap, EventCallback } from "../models/EventCallback";
import { LoggerConfig } from "../models/LoggerConfig";
import "amazon-connect-chatjs";

// TODO: Remove this type once the integration details are added to the MessageResponse type in chat-core
// TODO: Remove this type once the region is added to the MessageResponse type in chat-core
type MessageResponseWithRegion = MessageResponse & {
integrationDetails: {
awsConnectHandoff: {
Expand All @@ -25,15 +26,24 @@ type MessageResponseWithRegion = MessageResponse & {
*/
export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
private session?: connect.ActiveChatSession;
private eventListeners: Record<string, EventCallback[]> = {};
private eventListeners: { [T in keyof EventMap]?: EventCallback<T>[] } = {};
private loggerConfig: LoggerConfig = {
level: "ERROR",
};

constructor(loggerConfig?: LoggerConfig) {
if (loggerConfig) {
this.loggerConfig = loggerConfig;
}
}

async init(messageRsp: MessageResponse): Promise<void> {
if (this.session) {
console.warn("Chat session already initialized");
return;
}

// TODO: Remove this type assertion once the integration details are added to the MessageResponse type in chat-core
// TODO: Remove this type once the region is added to the MessageResponse type in chat-core
const messageResponse = messageRsp as MessageResponseWithRegion;

const connectionCreds =
Expand All @@ -46,17 +56,17 @@ export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {

connect.ChatSession.setGlobalConfig({
loggerConfig: {
// There are five levels available - DEBUG, INFO, WARN, ERROR, ADVANCED_LOG. Default is INFO
level: connect.LogLevel.ERROR,
useDefaultLogger: true,
level: connect.LogLevel[this.loggerConfig.level],
useDefaultLogger: this.loggerConfig.customizedLogger ? false : true,
customizedLogger: this.loggerConfig.customizedLogger,
},
region: messageResponse.integrationDetails.awsConnectHandoff.region,
});

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

const { connectCalled, connectSuccess } = await this.session.connect(
undefined
Expand Down Expand Up @@ -103,14 +113,14 @@ export class ChatCoreAwsConnectImpl implements ChatCoreAwsConnect {
});
}

on(eventName: string, cb: EventCallback): void {
on<T extends keyof EventMap>(eventName: T, cb: EventCallback<T>): void {
if (!this.eventListeners[eventName]) {
this.eventListeners[eventName] = [];
}
this.eventListeners[eventName].push(cb);
this.eventListeners[eventName]?.push(cb);
}

emit(eventName: string, _: any): void {
emit<T extends keyof EventMap>(eventName: T, _: EventMap[T]): void {
switch (eventName) {
case "typing":
this.session?.sendEvent({
Expand Down
Loading

0 comments on commit 3fc7243

Please sign in to comment.