-
Notifications
You must be signed in to change notification settings - Fork 354
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
-
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. -
Inconsistent Error Handling: Some methods log to
console.error, some toconsole.log, and others fail silently without clear error reporting to the caller. -
FormData Handling: Methods like
sendAttachmentrequire 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);
}