Skip to content

Commit

Permalink
Explicitly list error targets in chat stateful client (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
prprabhu-ms authored Aug 4, 2021
1 parent 370cb99 commit 3329ea7
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 46 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Add explicit string literals for error targets",
"packageName": "@internal/chat-stateful-client",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { ChatClientOptions } from '@azure/communication-chat';
import { ChatMessage } from '@azure/communication-chat';
import { ChatMessageReadReceipt } from '@azure/communication-chat';
import { ChatParticipant } from '@azure/communication-chat';
import { ChatThreadClient } from '@azure/communication-chat';
import { CommunicationIdentifierKind } from '@azure/communication-common';
import { CommunicationTokenCredential } from '@azure/communication-common';
import { MessageStatus } from '@internal/acs-ui-common';
Expand Down Expand Up @@ -38,20 +37,14 @@ export type ChatErrors = {
};

// @public
export type ChatErrorTargets = ChatObjectMethodNames<'ChatClient', ChatClient> | ChatObjectMethodNames<'ChatThreadClient', ChatThreadClient>;
export type ChatErrorTargets = 'ChatClient.createChatThread' | 'ChatClient.deleteChatThread' | 'ChatClient.getChatThreadClient' | 'ChatClient.listChatThreads' | 'ChatClient.off' | 'ChatClient.on' | 'ChatClient.startRealtimeNotifications' | 'ChatClient.stopRealtimeNotifications' | 'ChatThreadClient.addParticipants' | 'ChatThreadClient.deleteMessage' | 'ChatThreadClient.getMessage' | 'ChatThreadClient.getProperties' | 'ChatThreadClient.listMessages' | 'ChatThreadClient.listParticipants' | 'ChatThreadClient.listReadReceipts' | 'ChatThreadClient.removeParticipant' | 'ChatThreadClient.sendMessage' | 'ChatThreadClient.sendReadReceipt' | 'ChatThreadClient.sendTypingNotification' | 'ChatThreadClient.updateMessage' | 'ChatThreadClient.updateTopic';

// @public (undocumented)
export type ChatMessageWithStatus = ChatMessage & {
clientMessageId?: string;
status: MessageStatus;
};

// @public
export type ChatMethodName<T, K extends keyof T> = T[K] extends Function ? (K extends string ? K : never) : never;

// @public
export type ChatObjectMethodNames<TName extends string, T> = any;

// @public (undocumented)
export type ChatThreadClientState = {
chatMessages: {
Expand Down
43 changes: 22 additions & 21 deletions packages/chat-stateful-client/src/ChatClientState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { ChatClient, ChatMessageReadReceipt, ChatParticipant, ChatThreadClient } from '@azure/communication-chat';
import { ChatMessageReadReceipt, ChatParticipant } from '@azure/communication-chat';
import { CommunicationIdentifierKind } from '@azure/communication-common';
import { ChatMessageWithStatus } from './types/ChatMessageWithStatus';
import { TypingIndicatorReceivedEvent } from '@azure/communication-signaling';
Expand Down Expand Up @@ -93,23 +93,24 @@ export class ChatError extends Error {
* String literal type for all permissible keys in {@Link ChatErrors}.
*/
export type ChatErrorTargets =
| ChatObjectMethodNames<'ChatClient', ChatClient>
| ChatObjectMethodNames<'ChatThreadClient', ChatThreadClient>;

/**
* Helper type to build a string literal type containing methods of an object.
* @experimental
*/
// TODO: this was set to `any` from:
// { [K in keyof T]: `${TName}.${ChatMethodName<T, K>}`; }[keyof T];
// However this notation is only supported in typescript 4.1+ and is blocking some customers.
// GitHub issue: https://github.com/Azure/communication-ui-library/issues/619
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export declare type ChatObjectMethodNames<TName extends string, T> = any;

/**
* Helper type to build a string literal type containing methods of an object.
*/
// eslint complains on all uses of `Function`. Using it as a type constraint is legitimate.
// eslint-disable-next-line @typescript-eslint/ban-types
export type ChatMethodName<T, K extends keyof T> = T[K] extends Function ? (K extends string ? K : never) : never;
| 'ChatClient.createChatThread'
| 'ChatClient.deleteChatThread'
| 'ChatClient.getChatThreadClient'
| 'ChatClient.listChatThreads'
| 'ChatClient.off'
| 'ChatClient.on'
| 'ChatClient.startRealtimeNotifications'
| 'ChatClient.stopRealtimeNotifications'
| 'ChatThreadClient.addParticipants'
| 'ChatThreadClient.deleteMessage'
| 'ChatThreadClient.getMessage'
| 'ChatThreadClient.getProperties'
| 'ChatThreadClient.listMessages'
| 'ChatThreadClient.listParticipants'
| 'ChatThreadClient.listReadReceipts'
| 'ChatThreadClient.removeParticipant'
| 'ChatThreadClient.sendMessage'
| 'ChatThreadClient.sendReadReceipt'
| 'ChatThreadClient.sendTypingNotification'
| 'ChatThreadClient.updateMessage'
| 'ChatThreadClient.updateTopic';
38 changes: 38 additions & 0 deletions packages/chat-stateful-client/src/TypeAssertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { ChatClient, ChatThreadClient } from '@azure/communication-chat';
import { ChatErrorTargets } from './ChatClientState';

/**
* Internal type-assertion that explicitly listed {@Link ChatErrorTargets} correspond to the underlying base SDK API.
*/
export const ensureChatErrorTargetsContainsOnlyValidValues = (target: ChatErrorTargets): InferredChatErrorTargets =>
target;

/**
* Internal type-assertion that explicitly listed {@Link ChatErrorTargets} correspond to the underlying base SDK API.
*/
export const ensureChatErrorTargetsContainsAllValidValues = (target: InferredChatErrorTargets): ChatErrorTargets =>
target;

/**
* String literal type for all permissible keys in {@Link ChatErrors}.
*/
type InferredChatErrorTargets =
| ChatObjectMethodNames<'ChatClient', ChatClient>
| ChatObjectMethodNames<'ChatThreadClient', ChatThreadClient>;

/**
* Helper type to build a string literal type containing methods of an object.
*/
export type ChatObjectMethodNames<TName extends string, T> = {
[K in keyof T]: `${TName}.${ChatMethodName<T, K>}`;
}[keyof T];

/**
* Helper type to build a string literal type containing methods of an object.
*/
// eslint complains on all uses of `Function`. Using it as a type constraint is legitimate.
// eslint-disable-next-line @typescript-eslint/ban-types
export type ChatMethodName<T, K extends keyof T> = T[K] extends Function ? (K extends string ? K : never) : never;
4 changes: 1 addition & 3 deletions packages/chat-stateful-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ export type {
ChatErrors,
ChatThreadClientState,
ChatThreadProperties,
ChatErrorTargets,
ChatMethodName,
ChatObjectMethodNames
ChatErrorTargets
} from './ChatClientState';
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ export type ChatErrors = {
};

// @public
export type ChatErrorTargets = ChatObjectMethodNames<'ChatClient', ChatClient> | ChatObjectMethodNames<'ChatThreadClient', ChatThreadClient>;
export type ChatErrorTargets = 'ChatClient.createChatThread' | 'ChatClient.deleteChatThread' | 'ChatClient.getChatThreadClient' | 'ChatClient.listChatThreads' | 'ChatClient.off' | 'ChatClient.on' | 'ChatClient.startRealtimeNotifications' | 'ChatClient.stopRealtimeNotifications' | 'ChatThreadClient.addParticipants' | 'ChatThreadClient.deleteMessage' | 'ChatThreadClient.getMessage' | 'ChatThreadClient.getProperties' | 'ChatThreadClient.listMessages' | 'ChatThreadClient.listParticipants' | 'ChatThreadClient.listReadReceipts' | 'ChatThreadClient.removeParticipant' | 'ChatThreadClient.sendMessage' | 'ChatThreadClient.sendReadReceipt' | 'ChatThreadClient.sendTypingNotification' | 'ChatThreadClient.updateMessage' | 'ChatThreadClient.updateTopic';

// @public (undocumented)
export type ChatMessage = Message<'chat'>;
Expand All @@ -614,12 +614,6 @@ export type ChatMessageWithStatus = ChatMessage_2 & {
status: MessageStatus;
};

// @public
export type ChatMethodName<T, K extends keyof T> = T[K] extends Function ? (K extends string ? K : never) : never;

// @public
export type ChatObjectMethodNames<TName extends string, T> = any;

// @public
export type ChatOptions = {
showErrorBar?: boolean;
Expand Down

0 comments on commit 3329ea7

Please sign in to comment.