diff --git a/packages/transport/src/http.js b/packages/transport/src/http.js index db086a83..c5558fdd 100644 --- a/packages/transport/src/http.js +++ b/packages/transport/src/http.js @@ -21,11 +21,16 @@ import * as API from '@ucanto/interface' * @param {string} [options.method] * @returns {API.Channel} */ -export const open = ({ url, method = 'POST', fetch = globalThis.fetch }) => { +export const open = ({ url, method = 'POST', fetch }) => { + /* c8 ignore next 9 */ if (!fetch) { - throw new TypeError( - `ucanto HTTP transport got undefined \`fetch\`. Try passing in a \`fetch\` implementation explicitly.` - ) + if (typeof globalThis.fetch !== 'undefined') { + fetch = globalThis.fetch.bind(globalThis) + } else { + throw new TypeError( + `ucanto HTTP transport got undefined \`fetch\`. Try passing in a \`fetch\` implementation explicitly.` + ) + } } return new Channel({ url, method, fetch }) } diff --git a/packages/transport/test/https.spec.js b/packages/transport/test/https.spec.js index 886b19e8..6596a259 100644 --- a/packages/transport/test/https.spec.js +++ b/packages/transport/test/https.spec.js @@ -37,7 +37,7 @@ if (!globalThis.fetch) { }) } -test('faild request', async () => { +test('failed request', async () => { const channel = HTTP.open({ url: new URL('https://ucan.xyz/'), fetch: async (url, init) => { @@ -68,17 +68,18 @@ test('faild request', async () => { } }) -test('fail request without fetch impl', async () => { - try { - const channel = HTTP.open({ - url: new URL('https://ucan.xyz/'), - // @ts-ignore - fetch: null, - }) - assert.fail('expected to throw') - } catch (reason) { - const error = /** @type {any} */ (reason) - assert.match(String(error), /TypeError/) - assert.equal(error.name, 'TypeError') - } -}) +// Tests for environments that DO NOT have a globalThis.fetch implementation. +if (typeof globalThis.fetch === 'undefined') { + test('fail request without fetch impl', async () => { + try { + const channel = HTTP.open({ + url: new URL('https://ucan.xyz/') + }) + assert.fail('expected to throw') + } catch (reason) { + const error = /** @type {any} */ (reason) + assert.match(String(error), /TypeError/) + assert.equal(error.name, 'TypeError') + } + }) +}