|
| 1 | +import type { FetchImplementation } from './FetchImplementation.ts' |
| 2 | + |
| 3 | +export const fetchWithDebug: ( |
| 4 | + log?: (type: 'request' | 'response', details: any) => void, |
| 5 | + error?: (details: any) => void, |
| 6 | + body?: (body: any) => void, |
| 7 | +) => FetchImplementation = |
| 8 | + (log = console.log, logError = console.error, body = console.debug) => |
| 9 | + async (url, options) => { |
| 10 | + // Log request details |
| 11 | + log('request', { |
| 12 | + url: url.toString(), |
| 13 | + method: options?.method ?? 'GET', |
| 14 | + headers: options?.headers, |
| 15 | + body: options?.body, |
| 16 | + ...(options?.signal !== undefined && { signal: 'AbortSignal provided' }), |
| 17 | + ...(options?.credentials !== undefined && { |
| 18 | + credentials: options.credentials, |
| 19 | + }), |
| 20 | + ...(options?.cache !== undefined && { cache: options.cache }), |
| 21 | + ...(options?.redirect !== undefined && { redirect: options.redirect }), |
| 22 | + ...(options?.referrer !== undefined && { referrer: options.referrer }), |
| 23 | + ...(options?.referrerPolicy !== undefined && { |
| 24 | + referrerPolicy: options.referrerPolicy, |
| 25 | + }), |
| 26 | + ...(options?.integrity !== undefined && { integrity: options.integrity }), |
| 27 | + ...(options?.keepalive !== undefined && { keepalive: options.keepalive }), |
| 28 | + ...(options?.mode !== undefined && { mode: options.mode }), |
| 29 | + }) |
| 30 | + |
| 31 | + const startTime = Date.now() |
| 32 | + |
| 33 | + try { |
| 34 | + const response = await fetch(url, options) |
| 35 | + const duration = Date.now() - startTime |
| 36 | + |
| 37 | + // Log response details |
| 38 | + log('response', { |
| 39 | + url: url.toString(), |
| 40 | + status: response.status, |
| 41 | + statusText: response.statusText, |
| 42 | + ok: response.ok, |
| 43 | + headers: Object.fromEntries(response.headers.entries()), |
| 44 | + duration: `${duration}ms`, |
| 45 | + type: response.type, |
| 46 | + redirected: response.redirected, |
| 47 | + ...(response.url !== url.toString() && { finalUrl: response.url }), |
| 48 | + }) |
| 49 | + |
| 50 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 51 | + const originalTextBody = response.text |
| 52 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 53 | + const originalJSONBody = response.json |
| 54 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 55 | + const originalArrayBufferBody = response.arrayBuffer |
| 56 | + |
| 57 | + response.text = async () => { |
| 58 | + const textBody = await originalTextBody.call(response) |
| 59 | + body(textBody) |
| 60 | + return textBody |
| 61 | + } |
| 62 | + |
| 63 | + response.json = async () => { |
| 64 | + const jsonBody = await originalJSONBody.call(response) |
| 65 | + body(jsonBody) |
| 66 | + return jsonBody |
| 67 | + } |
| 68 | + |
| 69 | + response.arrayBuffer = async () => { |
| 70 | + const arrayBufferBody = await originalArrayBufferBody.call(response) |
| 71 | + body(arrayBufferBody) |
| 72 | + return arrayBufferBody |
| 73 | + } |
| 74 | + |
| 75 | + return response |
| 76 | + } catch (error) { |
| 77 | + const duration = Date.now() - startTime |
| 78 | + |
| 79 | + // Log error details |
| 80 | + logError({ |
| 81 | + url: url.toString(), |
| 82 | + duration: `${duration}ms`, |
| 83 | + error: |
| 84 | + error instanceof Error |
| 85 | + ? { |
| 86 | + name: error.name, |
| 87 | + message: error.message, |
| 88 | + ...(error.cause !== undefined && |
| 89 | + typeof error.cause === 'object' && |
| 90 | + error.cause !== null |
| 91 | + ? { cause: error.cause } |
| 92 | + : {}), |
| 93 | + } |
| 94 | + : error, |
| 95 | + }) |
| 96 | + |
| 97 | + throw error |
| 98 | + } |
| 99 | + } |
0 commit comments