RocketChat / RocketChat/EmbeddedChat

Refactor: Architectural cleanup of EmbeddedChatApi.ts

Open
#1,185 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
165
Forks
381
Avg merge
1d 2h
Merged PRs (30d)
1

Description

Description

The EmbeddedChatApi.ts file currently contains significant code duplication. Almost every API method (30+) manually retrieves the user session, constructs headers, stringifies JSON, and handles errors using redundant try/catch blocks.

Problems

  1. High Redundancy: Construction of fetch options, manual JSON.stringify, and repetitive error logging account for 30% of the file size (1200+ lines). Any change to the base request logic requires modification in dozens of places.

  2. Inconsistent Error Handling: Some methods log to console.error, some to console.log, and others fail silently without clear error reporting to the caller.

  3. FormData Handling: Methods like sendAttachment require specific header handling (omitting Content-Type for the browser to set the boundary) which is currently handled as a one-off case.

Example

try {
  const { userId, authToken } = (await this.auth.getCurrentUser()) || {};
  const response = await fetch(`${this.host}/api/v1/SOME_ENDPOINT`, {
    headers: {
      "Content-Type": "application/json",
      "X-Auth-Token": authToken,
      "X-User-Id": userId,
    },
    method: "GET/POST",
  });
  return await response.json();
} catch (err) {
  console.error(err);
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading EmbeddedChatApi.ts and mapping the repeated session, header, request, and error-handling paths across its API methods. Inspect how callers depend on current responses and errors; done means common request handling is introduced without breaking FormData boundary handling or existing API behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.