Skip to content

feat: add instance-level custom fetch support#295

Open
AyoubSayah wants to merge 1 commit into
formio:mainfrom
AyoubSayah:feat/instance-level-fetch
Open

feat: add instance-level custom fetch support#295
AyoubSayah wants to merge 1 commit into
formio:mainfrom
AyoubSayah:feat/instance-level-fetch

Conversation

@AyoubSayah

Copy link
Copy Markdown

What does this PR do? This PR adds support for instance-level fetch overrides within the Formio class, allowing developers to inject custom fetch logic (such as caching strategies, custom authentication, or request proxying) without globally mutating the static Formio.fetch polyfill.

Why is this necessary? Currently, @formio/core hardcodes all network requests to route through the static Formio.fetch property. This creates a severe limitation for modern frontend frameworks (like React, Vue, Angular) and libraries like React Query, because mutating a global static property to intercept network requests inherently causes race conditions when rendering multiple forms concurrently.

By pulling fetch from the FormioOptions down into the instance context, we allow developers to fully isolate custom network behaviors per-form instance.

Changes made:

Added fetch to FormioOptions: Strongly typed with JSDoc examples demonstrating the expected Web Fetch API signature.
Added fetch instance property: Mapped options.fetch to this.fetch during initialization so the fetcher can be supplied on load or mutated later on the instance.
Updated the Request Resolution Chain: Inside the static Formio.request method, updated the call to safely resolve the most specific fetcher available via a fallback chain:
typescript
const customFetch = (opts?.formio?.fetch) || (opts?.fetch) || Formio.fetch;
const result = Plugins.pluginAlter('wrapFetchRequestPromise', customFetch(url, options), ...);
Backwards Compatibility & Safety:

100% Backwards Compatible: If a user does not provide a custom fetcher, the chain seamlessly falls back to the legacy global Formio.fetch. Existing consumers will experience zero breaking changes.
Plugin System Intact: By preserving the Plugins.pluginAlter('wrapFetchRequestPromise', ...) wrapper, this change perfectly respects all existing token injection, pre-request hooks, and caching interception logic.
Cross-Environment Safety: Deliberately preserved the any typing to match the existing legacy compiler footprint, preventing unexpected lib.dom.d.ts type errors for Node.js consumers who lack standard Web Request/Response types in their environment.
Verification:

Validated that npm install and tsc compile successfully with no new errors.
Verified that instances correctly route their requests to the local fetcher without mutating Formio.fetch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant