UNPKG

@datastax/astra-db-ts

Version:
36 lines (35 loc) 1.15 kB
// Copyright Datastax, Inc // SPDX-License-Identifier: Apache-2.0 export class FetchNative { async fetch(init) { try { const timeoutSignal = AbortSignal.timeout(init.timeout); init.signal = (init.signal) ? AbortSignal.any([init.signal, timeoutSignal]) : timeoutSignal; const resp = await fetch(init.url, init); const headers = {}; resp.headers.forEach((value, key) => { headers[key] = value; }); return { url: resp.url, statusText: resp.statusText, httpVersion: 1, headers: headers, body: await resp.text(), status: resp.status, }; } catch (e) { if (e instanceof Error && e.name.includes('TimeoutError')) { throw init.mkTimeoutError(); } if (e instanceof TypeError && e.message === 'fetch failed' && 'cause' in e) { throw e.cause; } throw e; } } async close() { } }