From ff717fe55d5b24ad0929385b195d2f9737210274 Mon Sep 17 00:00:00 2001 From: "bobo.yang" Date: Sun, 29 Dec 2024 19:32:25 +0800 Subject: [PATCH] fix: Refactor service port handling in DevChatClient - Remove redundant baseURL checks in _get, _post, and chat methods - Simplify port initialization logic for HTTP requests - Consolidate error handling for missing service port configuration --- src/toolwrapper/devchatClient.ts | 40 ++++++++++++++------------------ 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/toolwrapper/devchatClient.ts b/src/toolwrapper/devchatClient.ts index 12ee191..4332a02 100644 --- a/src/toolwrapper/devchatClient.ts +++ b/src/toolwrapper/devchatClient.ts @@ -150,15 +150,13 @@ export class DevChatClient { } async _get(path: string, config?: any): Promise { - if (!this.baseURL) { - if (!process.env.DC_LOCALSERVICE_PORT) { - logger.channel()?.info("No local service port found."); - throw new DCLocalServicePortNotSetError(); - } - logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT); - const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10); - this.baseURL = `http://localhost:${port}`; + if (!process.env.DC_LOCALSERVICE_PORT) { + logger.channel()?.info("No local service port found."); + throw new DCLocalServicePortNotSetError(); } + logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT); + const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10); + this.baseURL = `http://localhost:${port}`; try { logger.channel()?.debug(`GET request to ${this.baseURL}${path}`); @@ -170,15 +168,13 @@ export class DevChatClient { } } async _post(path: string, data: any = undefined): Promise { - if (!this.baseURL) { - if (!process.env.DC_LOCALSERVICE_PORT) { - logger.channel()?.info("No local service port found."); - throw new DCLocalServicePortNotSetError(); - } - logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT); - const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10); - this.baseURL = `http://localhost:${port}`; + if (!process.env.DC_LOCALSERVICE_PORT) { + logger.channel()?.info("No local service port found."); + throw new DCLocalServicePortNotSetError(); } + logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT); + const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10); + this.baseURL = `http://localhost:${port}`; try { logger.channel()?.debug(`POST request to ${this.baseURL}${path}`); @@ -249,14 +245,12 @@ export class DevChatClient { message: ChatRequest, onData: (data: ChatResponse) => void ): Promise { - if (!this.baseURL) { - if (!process.env.DC_LOCALSERVICE_PORT) { - logger.channel()?.info("No local service port found."); - } - logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT); - const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10); - this.baseURL = `http://localhost:${port}`; + if (!process.env.DC_LOCALSERVICE_PORT) { + logger.channel()?.info("No local service port found."); } + logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT); + const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10); + this.baseURL = `http://localhost:${port}`; this._cancelMessageToken = axios.CancelToken.source(); const workspace = UiUtilWrapper.workspaceFoldersFirstPath();