feat: add instance-level custom fetch support#295
Open
AyoubSayah wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.