diff --git a/docs/eth-connect.fetchfunction.md b/docs/eth-connect.fetchfunction.md index b4c43e4..83045df 100644 --- a/docs/eth-connect.fetchfunction.md +++ b/docs/eth-connect.fetchfunction.md @@ -10,7 +10,6 @@ export type FetchFunction = (url: string, params: { body?: any; method?: string; - mode?: string; headers?: any; }) => Promise; ``` diff --git a/report/eth-connect.api.md b/report/eth-connect.api.md index eb06a37..6c1a9b4 100644 --- a/report/eth-connect.api.md +++ b/report/eth-connect.api.md @@ -880,7 +880,6 @@ export function extractTypeName(name: string): string; export type FetchFunction = (url: string, params: { body?: any; method?: string; - mode?: string; headers?: any; }) => Promise; diff --git a/src/providers/HTTPProvider.ts b/src/providers/HTTPProvider.ts index 3472f1c..dcf1066 100644 --- a/src/providers/HTTPProvider.ts +++ b/src/providers/HTTPProvider.ts @@ -1,12 +1,16 @@ import { RPCMessage, Callback, toRPC } from './common' export { RPCMessage, Callback } from './common' +// Structurally compatible with the global native `fetch` (the same WHATWG signature on web +// and server) as well as native-fetch components (e.g. @dcl/fetch-component) and node-fetch, +// so callers can pass any of them directly without a cast. Only the fields HTTPProvider sets +// are listed; a previous `mode?: string` was dropped because it is unused here and made the +// native fetch's `RequestInit` (`mode?: RequestMode`) non-assignable to this type. export type FetchFunction = ( url: string, params: { body?: any method?: string - mode?: string headers?: any } ) => Promise diff --git a/test/HTTPProvider.spec.ts b/test/HTTPProvider.spec.ts index e5cf52b..4215bac 100644 --- a/test/HTTPProvider.spec.ts +++ b/test/HTTPProvider.spec.ts @@ -1,4 +1,4 @@ -import { HTTPProvider } from '../src/providers/HTTPProvider' +import { HTTPProvider, HTTPProviderOptions } from '../src/providers/HTTPProvider' type SendResult = { err: Error | null; result: any } @@ -110,3 +110,25 @@ describe('when sending an async request through the HTTPProvider', () => { }) }) }) + +describe('when configuring the fetch option', () => { + describe('and the global native fetch is supplied', () => { + it('should be accepted without a cast', () => { + // This compiles only if the global native fetch (same WHATWG signature on web and + // server) is assignable to the fetch option — the point of this change. + const options: HTTPProviderOptions = { fetch: globalThis.fetch } + + expect(typeof options.fetch).toBe('function') + }) + }) + + describe('and a native-fetch component is supplied', () => { + it('should be accepted without a cast', () => { + // Mirrors the shape of native-fetch components such as @dcl/fetch-component. + const fetchComponent: (url: string, init?: RequestInit) => Promise = globalThis.fetch + const options: HTTPProviderOptions = { fetch: fetchComponent } + + expect(typeof options.fetch).toBe('function') + }) + }) +})