ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.47k stars 245 forks source link

passthrough onNoMatch does not work with axios 1.2.0 or above #357

Closed teetotum closed 1 year ago

teetotum commented 1 year ago

I maintain axios-response-mock which covers a similar use-case as does axios-mock-adapter. My lib broke with axios version 1.2.0 with regard to passthrough requests; I got runtime errors in the vein of 'originalAdapter is not a function'. I finished analysing the issue and thought I let you know what I found out; because I'm quite sure it broke your lib in the very same way.

The following section contains my analysis and thoughts.

Analysis

the change was introduced with #5277 (Changelog entry: refactor: allowing adapters to be loaded by name #5277) (merged November 22nd) and released with v1.2.0 on November 22nd

If a custom adapter now wants to selectively decide which requests to handle itself and which requests to passthrough to the default adapter / delegate to the default adapter, how best would it now obtain a reference to the default adapter?

Before the change, a custom adapter could secure a reference to the default adapter via axiosInstance.defaults.adapter and call it later to delegate.

Example:

this.originalAdapter = axiosInstance.defaults.adapter;
axiosInstance.defaults.adapter = this._processRequest.bind(this);

// and later
if (matchedRoute)
    return this._respond(matchedRoute, config);
else
    return this.originalAdapter(config);

I opened a discussion on the axios github repo.

What options do I have now?

Conclusion:

Today I published version 0.2.2-alpha.1 of axios-response-mock and it seems I could fix the issue. But it is only a temporary stopgap. When axios starts supporting a stable way to obtain a reference to the defaultAdapter I will change my fix; if the axios maintainers dismiss the feature request I will probably refactor my lib into the two steps approach sketched above.

conduct0 commented 1 year ago

Hi! I am experimenting this Issue with the package. Is there a workaround while still using the library? For now I downgraded axios to 1.1.3 and its functional. Could I help in some way?

teetotum commented 1 year ago

@nicolascampbell The only workaround I'm aware of is to stay below axios v1.2.0 (the release v1.2.0-alpha.1 should also work though) if you still want to be able to passthrough requests.

paztis commented 1 year ago

I've create this issue in axios: https://github.com/axios/axios/issues/5474 Here you absolutely need to access to getAdapter() method to access original adapter, as it is not always a function.

by waiting this you can at least manage the support of the array internally, or temporary do an import of axios/lib/adapters/adapter.js to access getAdapter method

marcbachmann commented 1 year ago

A fix for this module is in #363

marco-gagliardi commented 1 year ago

hello @marcbachmann any update on the fix? :)

marcbachmann commented 1 year ago

Sadly not yet. The PR is ready to merge

teetotum commented 7 months ago

@marcbachmann Today I discovered that axios made getAdapter accessible (the feature PR was merged Aug 9, 2023 and released with 1.5.0 Aug 26, 2023).

I will now revisit my workaround in my library and fix it properly. Not sure if you want to refactor your fix for axios-mock-adapter now to use getAdapter; which would allow you to get rid of the code that currently strips the interceptors and transformations and fields a separate call to axios.