Skip to content

chore: Improve type safety in EmbeddedChatApi by removing any#1238

Open
sudorishabh wants to merge 1 commit intoRocketChat:developfrom
sudorishabh:api/improve-type-safety
Open

chore: Improve type safety in EmbeddedChatApi by removing any#1238
sudorishabh wants to merge 1 commit intoRocketChat:developfrom
sudorishabh:api/improve-type-safety

Conversation

@sudorishabh
Copy link

@sudorishabh sudorishabh commented Mar 24, 2026

refactor(api): remove explicit any from EmbeddedChatApi with safer payload typing

This PR improves type safety in packages/api/src/EmbeddedChatApi.ts by removing explicit any usages and replacing them with concrete local types plus runtime narrowing where Rocket.Chat payloads are dynamic.

Acceptance Criteria fulfillment

  • No explicit any remains in packages/api/src/EmbeddedChatApi.ts.
  • connect() stream handling uses guarded/narrowed access.
  • sendMessage and handleUiKitInteraction use typed inputs.
  • packages/api builds successfully.
  • No lint/type errors introduced.

Fixes #1237

What Changed

  • Added local type aliases/interfaces for:
    • message payloads (MessagePayload)
    • DDP stream payload envelope (DdpStreamMessage)
    • action and UI interaction payloads
    • auth input/token credential shapes for autoLogin
  • Replaced explicit any in:
    • callback collections and listener method signatures
    • stream callbacks in connect()
    • sendMessage(...) argument type
    • handleUiKitInteraction(...) argument type
  • Added payload normalization/narrowing helpers for:
    • fields.args handling
    • message ts normalization (including $date)
    • token credential shape normalization (service/access_token and serviceName/accessToken)
  • Kept dynamic payload compatibility with index signatures ([key: string]: unknown) to avoid breaking external Rocket.Chat fields.

Why

  • Prevent unsafe property access in stream and message callbacks.
  • Improve TypeScript inference and editor autocomplete.
  • Reduce runtime bug risk while preserving existing behavior.

Risk / Compatibility Notes

  • Rocket.Chat SDK callback signatures are broad, so runtime guards are still required.
  • Token credentials can arrive in multiple naming conventions; normalization handles both.

PR Test Details

  • Verified no explicit any remains in EmbeddedChatApi.ts.
  • Type/lint diagnostics for edited file are clean.
  • yarn build in packages/api passes.

Note: The PR will be ready for live testing at https://rocketchat.github.io/EmbeddedChat/pulls/pr-1238 after approval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor API: Remove 'any' from EmbeddedChatApi and improve payload typing

1 participant