@@ -11,28 +11,30 @@ import { type WorkspaceConfiguration } from "vscode";
11
11
import { ClientOptions } from "ws" ;
12
12
import { CertificateError } from "../error" ;
13
13
import { getHeaderCommand , getHeaders } from "../headers" ;
14
- import { Logger } from "../logging/logger" ;
15
14
import {
16
15
createRequestMeta ,
16
+ logRequest ,
17
17
logRequestError ,
18
- logRequestStart ,
19
- logRequestSuccess ,
20
- RequestConfigWithMeta ,
21
- WsLogger ,
22
- } from "../logging/netLog " ;
23
- import { OneWayCodeWebSocket } from "../websocket/oneWayCodeWebSocket " ;
18
+ logResponse ,
19
+ } from "../logging/httpLogger" ;
20
+ import { Logger } from "../logging/logger" ;
21
+ import { RequestConfigWithMeta , HttpLogLevel } from "../logging/types" ;
22
+ import { WsLogger } from "../logging/wsLogger " ;
23
+ import { OneWayWebSocket } from "../websocket/oneWayWebSocket " ;
24
24
import { createHttpAgent } from "./auth" ;
25
25
26
26
const coderSessionTokenHeader = "Coder-Session-Token" ;
27
27
28
+ type WorkspaceConfigurationProvider = ( ) => WorkspaceConfiguration ;
29
+
28
30
/**
29
31
* Unified API class that includes both REST API methods from the base Api class
30
32
* and WebSocket methods for real-time functionality.
31
33
*/
32
34
export class CodeApi extends Api {
33
35
private constructor (
34
36
private readonly output : Logger ,
35
- private readonly cfg : WorkspaceConfiguration ,
37
+ private readonly configProvider : WorkspaceConfigurationProvider ,
36
38
) {
37
39
super ( ) ;
38
40
}
@@ -45,15 +47,15 @@ export class CodeApi extends Api {
45
47
baseUrl : string ,
46
48
token : string | undefined ,
47
49
output : Logger ,
48
- cfg : WorkspaceConfiguration ,
50
+ configProvider : WorkspaceConfigurationProvider ,
49
51
) : CodeApi {
50
- const client = new CodeApi ( output , cfg ) ;
52
+ const client = new CodeApi ( output , configProvider ) ;
51
53
client . setHost ( baseUrl ) ;
52
54
if ( token ) {
53
55
client . setSessionToken ( token ) ;
54
56
}
55
57
56
- setupInterceptors ( client , baseUrl , output , cfg ) ;
58
+ setupInterceptors ( client , baseUrl , output , configProvider ) ;
57
59
return client ;
58
60
}
59
61
@@ -120,8 +122,8 @@ export class CodeApi extends Api {
120
122
coderSessionTokenHeader
121
123
] as string | undefined ;
122
124
123
- const httpAgent = createHttpAgent ( this . cfg ) ;
124
- const webSocket = new OneWayCodeWebSocket < TData > ( {
125
+ const httpAgent = createHttpAgent ( this . configProvider ( ) ) ;
126
+ const webSocket = new OneWayWebSocket < TData > ( {
125
127
location : baseUrl ,
126
128
...configs ,
127
129
options : {
@@ -167,12 +169,16 @@ function setupInterceptors(
167
169
client : CodeApi ,
168
170
baseUrl : string ,
169
171
output : Logger ,
170
- cfg : WorkspaceConfiguration ,
172
+ configProvider : WorkspaceConfigurationProvider ,
171
173
) : void {
172
- addLoggingInterceptors ( client . getAxiosInstance ( ) , output ) ;
174
+ addLoggingInterceptors ( client . getAxiosInstance ( ) , output , configProvider ) ;
173
175
174
176
client . getAxiosInstance ( ) . interceptors . request . use ( async ( config ) => {
175
- const headers = await getHeaders ( baseUrl , getHeaderCommand ( cfg ) , output ) ;
177
+ const headers = await getHeaders (
178
+ baseUrl ,
179
+ getHeaderCommand ( configProvider ( ) ) ,
180
+ output ,
181
+ ) ;
176
182
// Add headers from the header command.
177
183
Object . entries ( headers ) . forEach ( ( [ key , value ] ) => {
178
184
config . headers [ key ] = value ;
@@ -181,7 +187,7 @@ function setupInterceptors(
181
187
// Configure proxy and TLS.
182
188
// Note that by default VS Code overrides the agent. To prevent this, set
183
189
// `http.proxySupport` to `on` or `off`.
184
- const agent = createHttpAgent ( cfg ) ;
190
+ const agent = createHttpAgent ( configProvider ( ) ) ;
185
191
config . httpsAgent = agent ;
186
192
config . httpAgent = agent ;
187
193
config . proxy = false ;
@@ -198,12 +204,16 @@ function setupInterceptors(
198
204
) ;
199
205
}
200
206
201
- function addLoggingInterceptors ( client : AxiosInstance , logger : Logger ) {
207
+ function addLoggingInterceptors (
208
+ client : AxiosInstance ,
209
+ logger : Logger ,
210
+ configProvider : WorkspaceConfigurationProvider ,
211
+ ) {
202
212
client . interceptors . request . use (
203
213
( config ) => {
204
214
const meta = createRequestMeta ( ) ;
205
215
( config as RequestConfigWithMeta ) . metadata = meta ;
206
- logRequestStart ( logger , meta . requestId , config ) ;
216
+ logRequest ( logger , meta . requestId , config , getLogLevel ( configProvider ( ) ) ) ;
207
217
return config ;
208
218
} ,
209
219
( error : unknown ) => {
@@ -216,7 +226,7 @@ function addLoggingInterceptors(client: AxiosInstance, logger: Logger) {
216
226
( response ) => {
217
227
const meta = ( response . config as RequestConfigWithMeta ) . metadata ;
218
228
if ( meta ) {
219
- logRequestSuccess ( logger , meta , response ) ;
229
+ logResponse ( logger , meta , response , getLogLevel ( configProvider ( ) ) ) ;
220
230
}
221
231
return response ;
222
232
} ,
@@ -226,3 +236,10 @@ function addLoggingInterceptors(client: AxiosInstance, logger: Logger) {
226
236
} ,
227
237
) ;
228
238
}
239
+
240
+ function getLogLevel ( cfg : WorkspaceConfiguration ) : HttpLogLevel {
241
+ const logLevelStr = cfg
242
+ . get ( "coder.httpClientLogLevel" , HttpLogLevel [ HttpLogLevel . BASIC ] )
243
+ . toUpperCase ( ) ;
244
+ return HttpLogLevel [ logLevelStr as keyof typeof HttpLogLevel ] ;
245
+ }
0 commit comments