Skip to content

Refactor: Architectural cleanup of EmbeddedChatApi.ts #1185

@sudorishabh

Description

@sudorishabh

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions