Giter Site home page Giter Site logo

Comments (14)

jrrudolph avatar jrrudolph commented on June 26, 2024 3

This is also a problem for isomorphic apps. What makes it especially problematic is that the code where the global fetch is assumed happens in the global scope of middleware.js, so an error is thrown as soon redux-api-middleware is included (ie, on startup). So we don't even get a chance to pass in fetch from node-fetch or the like, as we were in 2.3.0, and our app fails out of the gate.

I propose waiting until within createMiddleware() to set the default value of fetch, assuming it wasn't passed in through options.

from redux-api-middleware.

Jedidiah avatar Jedidiah commented on June 26, 2024 1

For anyone else wanting to get this working with Gatsby, I got rid of the error by adding isomorphic-fetch as a webpack plugin in gatsby-node.js.

exports.onCreateWebpackConfig = ({ actions }) => {
  actions.setWebpackConfig({
    plugins: [
      new webpack.ProvidePlugin({
        fetch: path.resolve(path.join(__dirname, "node_modules/isomorphic-fetch/fetch-npm-node"))
      })
    ]
  });
};

from redux-api-middleware.

essensx avatar essensx commented on June 26, 2024 1

Hmm, in my opinion this seems a bit harsh, considering that most of the consumers of the library probably use polyfills to resolve such issues.

If implementing globalThis proposal is a bit of a hassle, I think we could actually implement a simpler solution - to evaluate user-defined fetch implementation first (if set), then try to use global fetch 🤔

In this case, the library should be usable in all use cases.

Any thoughts?

from redux-api-middleware.

iamandrewluca avatar iamandrewluca commented on June 26, 2024 1

i suggest investing in proper solution

Proper sollution I think is to drop apiMiddleware in favor of createMiddleware.
And get rid of all global dependencies.

we will still need to update the babel preset eventually

I'll take a look again when will have some time.

from redux-api-middleware.

darthrellimnad avatar darthrellimnad commented on June 26, 2024

I don't believe node implements a global fetch method like browsers do, so you may need to add a standards compliant version to jest's runtime environment to match behavior.

Not totally sure why an upgrade to v3 would cause this though, since the internal isomorphic-fetch dependency was removed in v2 I believe. Usually, this can be handled with a setupScripts.js file (when using react-scripts), or a setupFiles config when using Jest directly.

Here, you can create global.fetch and window.fetch value for unit and/or integration tests using something like isomorphic-fetch or a mock implementation like jest-fetch-mock.

Additionally, with redux-api-middleware, you can often simply stub the dispatch method of the store used by a unit test, since many tests may only need to verify that dispatched RSAAs are well-formed and match an expected RSAA object.

More info on setting up Jest with a fetch implementation can be found in the following issue:
facebook/create-react-app#967

Hope this helps :)

from redux-api-middleware.

Jedidiah avatar Jedidiah commented on June 26, 2024

Hitting this same problem trying to use this in a Gatsby app. The build fails as fetch is undefined.

from redux-api-middleware.

nason avatar nason commented on June 26, 2024

@darthrellimnad is right about fetch

Any suggestions as to how this could be handled better?

from redux-api-middleware.

essensx avatar essensx commented on June 26, 2024

I have the same issue with nextjs, as node does not have fetch. Because of defaults trying to access fetch, you can't set your own fetch implementation, which renders the functionality unusable in node env. I think the easier solution would be to just check if fetch is defined, before trying to set it into the object.

Edit: created a PR to address the issue.

from redux-api-middleware.

iamandrewluca avatar iamandrewluca commented on June 26, 2024

Use createMiddleware with your own fetch compilant function
https://github.com/agraboso/redux-api-middleware#createmiddlewareoptions

from redux-api-middleware.

essensx avatar essensx commented on June 26, 2024

It will not work, as the build fails due to undefined fetch.

https://i.imgur.com/iIq2XeI.png

from redux-api-middleware.

iamandrewluca avatar iamandrewluca commented on June 26, 2024

@nason I would suggest to make a breaking change and remove apiMiddleware and let users replace it with

const apiMiddleware = createMiddleware({ fetch: /* any fetch compilant library */ })

This way we get rid of any global dependencies. And less issues.

from redux-api-middleware.

iamandrewluca avatar iamandrewluca commented on June 26, 2024

Long story short. I have a fix. But I don't know how exactly it will behave. I think it will behave like in v2
In v2 if fetch was not provided is was silently catch by try block and dispatched as an failure action
in v3 it was catch by 'use strict'; mode and was thrown very early
And I'm still for that to deprecate apiMiddleware in favor of createMiddleware.

#251

The explanation
In v2 global fetch was used at the moment when action was called. At that moment a global fetch was already provided.
In v3 we moved that global fetch as a default option. At that moment global fetch was not provided yet, and 'use strict'; complained that fetch does not exist.

Actually in v2 'use strict'; also complained but the error was caught silently and dispatched as failure action

from redux-api-middleware.

iamandrewluca avatar iamandrewluca commented on June 26, 2024

Released in https://github.com/agraboso/redux-api-middleware/releases/tag/v3.2.1

from redux-api-middleware.

essensx avatar essensx commented on June 26, 2024

Awesome! Though i suggest investing in proper solution, as we will still need to update the babel preset eventually.. Sorry for not contributing, did not have the time

from redux-api-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.