Skip to content

Commit ff717fe

Browse files
committed
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
1 parent 341380c commit ff717fe

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

src/toolwrapper/devchatClient.ts

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,13 @@ export class DevChatClient {
150150
}
151151

152152
async _get(path: string, config?: any): Promise<AxiosResponse> {
153-
if (!this.baseURL) {
154-
if (!process.env.DC_LOCALSERVICE_PORT) {
155-
logger.channel()?.info("No local service port found.");
156-
throw new DCLocalServicePortNotSetError();
157-
}
158-
logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT);
159-
const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10);
160-
this.baseURL = `http://localhost:${port}`;
153+
if (!process.env.DC_LOCALSERVICE_PORT) {
154+
logger.channel()?.info("No local service port found.");
155+
throw new DCLocalServicePortNotSetError();
161156
}
157+
logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT);
158+
const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10);
159+
this.baseURL = `http://localhost:${port}`;
162160

163161
try {
164162
logger.channel()?.debug(`GET request to ${this.baseURL}${path}`);
@@ -170,15 +168,13 @@ export class DevChatClient {
170168
}
171169
}
172170
async _post(path: string, data: any = undefined): Promise<AxiosResponse> {
173-
if (!this.baseURL) {
174-
if (!process.env.DC_LOCALSERVICE_PORT) {
175-
logger.channel()?.info("No local service port found.");
176-
throw new DCLocalServicePortNotSetError();
177-
}
178-
logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT);
179-
const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10);
180-
this.baseURL = `http://localhost:${port}`;
171+
if (!process.env.DC_LOCALSERVICE_PORT) {
172+
logger.channel()?.info("No local service port found.");
173+
throw new DCLocalServicePortNotSetError();
181174
}
175+
logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT);
176+
const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10);
177+
this.baseURL = `http://localhost:${port}`;
182178

183179
try {
184180
logger.channel()?.debug(`POST request to ${this.baseURL}${path}`);
@@ -249,14 +245,12 @@ export class DevChatClient {
249245
message: ChatRequest,
250246
onData: (data: ChatResponse) => void
251247
): Promise<ChatResponse> {
252-
if (!this.baseURL) {
253-
if (!process.env.DC_LOCALSERVICE_PORT) {
254-
logger.channel()?.info("No local service port found.");
255-
}
256-
logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT);
257-
const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10);
258-
this.baseURL = `http://localhost:${port}`;
248+
if (!process.env.DC_LOCALSERVICE_PORT) {
249+
logger.channel()?.info("No local service port found.");
259250
}
251+
logger.channel()?.trace("Using local service port:", process.env.DC_LOCALSERVICE_PORT);
252+
const port: number = parseInt(process.env.DC_LOCALSERVICE_PORT || '8008', 10);
253+
this.baseURL = `http://localhost:${port}`;
260254

261255
this._cancelMessageToken = axios.CancelToken.source();
262256
const workspace = UiUtilWrapper.workspaceFoldersFirstPath();

0 commit comments

Comments
 (0)