From 3329ea72724c9bbcca77a79b45feead53a91eb65 Mon Sep 17 00:00:00 2001 From: Prathmesh Prabhu <82062616+prprabhu-ms@users.noreply.github.com> Date: Wed, 4 Aug 2021 09:27:20 -0700 Subject: [PATCH] Explicitly list error targets in chat stateful client (#636) --- ...-1590f6eb-8e49-4add-8856-500109777482.json | 7 --- ...-4ab8067f-e02d-4ae7-af21-7644e44a1c93.json | 7 +++ .../review/chat-stateful-client.api.md | 9 +--- .../src/ChatClientState.ts | 43 ++++++++++--------- .../src/TypeAssertions.ts | 38 ++++++++++++++++ packages/chat-stateful-client/src/index.ts | 4 +- .../review/communication-react.api.md | 8 +--- 7 files changed, 70 insertions(+), 46 deletions(-) delete mode 100644 change/@internal-chat-stateful-client-1590f6eb-8e49-4add-8856-500109777482.json create mode 100644 change/@internal-chat-stateful-client-4ab8067f-e02d-4ae7-af21-7644e44a1c93.json create mode 100644 packages/chat-stateful-client/src/TypeAssertions.ts diff --git a/change/@internal-chat-stateful-client-1590f6eb-8e49-4add-8856-500109777482.json b/change/@internal-chat-stateful-client-1590f6eb-8e49-4add-8856-500109777482.json deleted file mode 100644 index 08097e44282..00000000000 --- a/change/@internal-chat-stateful-client-1590f6eb-8e49-4add-8856-500109777482.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "type": "prerelease", - "comment": "Remove type blocking usage of the package on older typescript versions", - "packageName": "@internal/chat-stateful-client", - "email": "2684369+JamesBurnside@users.noreply.github.com", - "dependentChangeType": "patch" -} diff --git a/change/@internal-chat-stateful-client-4ab8067f-e02d-4ae7-af21-7644e44a1c93.json b/change/@internal-chat-stateful-client-4ab8067f-e02d-4ae7-af21-7644e44a1c93.json new file mode 100644 index 00000000000..12ae9611437 --- /dev/null +++ b/change/@internal-chat-stateful-client-4ab8067f-e02d-4ae7-af21-7644e44a1c93.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Add explicit string literals for error targets", + "packageName": "@internal/chat-stateful-client", + "email": "82062616+prprabhu-ms@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/packages/chat-stateful-client/review/chat-stateful-client.api.md b/packages/chat-stateful-client/review/chat-stateful-client.api.md index 0a88357d506..dd883435638 100644 --- a/packages/chat-stateful-client/review/chat-stateful-client.api.md +++ b/packages/chat-stateful-client/review/chat-stateful-client.api.md @@ -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'; @@ -38,7 +37,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 ChatMessageWithStatus = ChatMessage & { @@ -46,12 +45,6 @@ export type ChatMessageWithStatus = ChatMessage & { status: MessageStatus; }; -// @public -export type ChatMethodName = T[K] extends Function ? (K extends string ? K : never) : never; - -// @public -export type ChatObjectMethodNames = any; - // @public (undocumented) export type ChatThreadClientState = { chatMessages: { diff --git a/packages/chat-stateful-client/src/ChatClientState.ts b/packages/chat-stateful-client/src/ChatClientState.ts index 898d1e982ff..51a39b2601e 100644 --- a/packages/chat-stateful-client/src/ChatClientState.ts +++ b/packages/chat-stateful-client/src/ChatClientState.ts @@ -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'; @@ -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}`; }[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 = 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 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'; diff --git a/packages/chat-stateful-client/src/TypeAssertions.ts b/packages/chat-stateful-client/src/TypeAssertions.ts new file mode 100644 index 00000000000..678263a86ae --- /dev/null +++ b/packages/chat-stateful-client/src/TypeAssertions.ts @@ -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 = { + [K in keyof T]: `${TName}.${ChatMethodName}`; +}[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 Function ? (K extends string ? K : never) : never; diff --git a/packages/chat-stateful-client/src/index.ts b/packages/chat-stateful-client/src/index.ts index 1dd49fe039f..811d97eacd3 100644 --- a/packages/chat-stateful-client/src/index.ts +++ b/packages/chat-stateful-client/src/index.ts @@ -11,7 +11,5 @@ export type { ChatErrors, ChatThreadClientState, ChatThreadProperties, - ChatErrorTargets, - ChatMethodName, - ChatObjectMethodNames + ChatErrorTargets } from './ChatClientState'; diff --git a/packages/communication-react/review/communication-react.api.md b/packages/communication-react/review/communication-react.api.md index 98a5bd1a5a3..dfbab0b63c9 100644 --- a/packages/communication-react/review/communication-react.api.md +++ b/packages/communication-react/review/communication-react.api.md @@ -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'>; @@ -614,12 +614,6 @@ export type ChatMessageWithStatus = ChatMessage_2 & { status: MessageStatus; }; -// @public -export type ChatMethodName = T[K] extends Function ? (K extends string ? K : never) : never; - -// @public -export type ChatObjectMethodNames = any; - // @public export type ChatOptions = { showErrorBar?: boolean;