(
method: string,
path: string,
body?: unknown,
query?: Record<string, string | number | boolean | undefined>,
allowCodes: number[] = [],
)
| 263 | } |
| 264 | |
| 265 | private async request<T>( |
| 266 | method: string, |
| 267 | path: string, |
| 268 | body?: unknown, |
| 269 | query?: Record<string, string | number | boolean | undefined>, |
| 270 | allowCodes: number[] = [], |
| 271 | ): Promise<T> { |
| 272 | // Build URL, appending query string (omit undefined values) |
| 273 | let url = buildRestUrl(this.origin, path); |
| 274 | if (query) { |
| 275 | const params = new URLSearchParams(); |
| 276 | for (const [key, value] of Object.entries(query)) { |
| 277 | if (value !== undefined) { |
| 278 | params.set(key, String(value)); |
| 279 | } |
| 280 | } |
| 281 | const qs = params.toString(); |
| 282 | if (qs) url = `${url}?${qs}`; |
| 283 | } |
| 284 | |
| 285 | // Build headers |
| 286 | const requestId = createRequestId(); |
| 287 | const headers: Record<string, string> = { |
| 288 | 'X-Request-Id': requestId, |
| 289 | }; |
| 290 | this.addClientHeaders(headers); |
| 291 | if (body !== undefined) { |
| 292 | headers['Content-Type'] = 'application/json; charset=utf-8'; |
| 293 | } |
| 294 | |
| 295 | const startedAt = Date.now(); |
| 296 | traceRestRequest({ method, path, url, requestId, body }); |
| 297 | |
| 298 | // Execute fetch |
| 299 | let response: Response; |
| 300 | try { |
| 301 | response = await fetch(url, { |
| 302 | method, |
| 303 | headers, |
| 304 | body: body !== undefined ? JSON.stringify(body) : undefined, |
| 305 | signal: timeoutSignal(), |
| 306 | }); |
| 307 | } catch (err) { |
| 308 | traceRestFailure({ method, path, requestId, phase: 'fetch', durationMs: Date.now() - startedAt, error: err }); |
| 309 | throw new DaemonNetworkError({ |
| 310 | message: `Network error calling ${method} ${path}`, |
| 311 | cause: err, |
| 312 | method, |
| 313 | path, |
| 314 | url, |
| 315 | requestId, |
| 316 | phase: 'fetch', |
| 317 | timeoutMs: REQUEST_TIMEOUT_MS, |
| 318 | timestamp: Date.now(), |
| 319 | durationMs: Date.now() - startedAt, |
| 320 | }); |
| 321 | } |
| 322 |
no test coverage detected