Coder Social home page Coder Social logo

koa support about universal-redux HOT 10 CLOSED

bdefore avatar bdefore commented on July 28, 2024
koa support

from universal-redux.

Comments (10)

bartolkaruza avatar bartolkaruza commented on July 28, 2024

I currently use koa under universal-redux, simply wrapping the express middleware; https://github.com/vkurchatkin/koa-connect

I'm having a look at making the renderer more dynamic, so it could work with either express or koa middleware. This part is tripping me up though;

if (config.redux.middleware) {
      const customMiddleware = require(path.resolve(config.redux.middleware)).default;
      each(customMiddleware, (customMiddlewareToAdd) => {
        if (typeof customMiddlewareToAdd === 'function') {
          middleware.push(customMiddlewareToAdd(req, res));
        }
      });
    }

The custom middleware I've worked with so far were not dependent on the express req, res objects, but I can imagine others might use it in some other way. Can you tell me what the intention is for passing the req and res into the custom middleware? Is there an example?

from universal-redux.

bdefore avatar bdefore commented on July 28, 2024

@bartolkaruza I believe you are correct that this is unconventional, and was related to migrating the ApiClient.js from the react-redux-universal-hot-example project. That library needed access to the request object in order to retrieve and set cookies for authentication purposes. The modified version is in use in the JWT example here: https://github.com/bdefore/react-redux-universal-hot-example/blob/react-router-2/src/redux/middleware/fetcher.js

That said, I think it's more appropriate to follow Redux middleware conventions, and if need be, find another way to implement that fetcher. Would you agree that the appropriate way to assign the middleware should be without checking for function, nor executing it? Like so:

    const middleware = [];
    if (config.redux.middleware) {
      const customMiddleware = require(path.resolve(config.redux.middleware)).default;
      each(customMiddleware, (customMiddlewareToAdd) => {
        middleware.push(customMiddlewareToAdd);
      });
    }

Hit me up on Reactiflux if you'd like to chat more about it.

from universal-redux.

bdefore avatar bdefore commented on July 28, 2024

You'll also want to similarly modify client.js. In fact now that req and res need not be applied, these should be pulled into a helper.

from universal-redux.

bdefore avatar bdefore commented on July 28, 2024

Well, hey, even more terse: const middleware = config.redux.middleware ? require(path.resolve(config.redux.middleware)).default : [];

from universal-redux.

bdefore avatar bdefore commented on July 28, 2024

@bartolkaruza I've released 3.0.0-rc3 based on these changes

from universal-redux.

bartolkaruza avatar bartolkaruza commented on July 28, 2024

I've made a first attempt, untested;
bartolkaruza@ec50f67

I'm curious what you think of this approach.

from universal-redux.

bdefore avatar bdefore commented on July 28, 2024

@bartolkaruza that actually looks about like what i was thinking too. i like how you've managed to contain the original logic into dynamicMiddleware. would be great to see this in a PR with an addition to examples that's working with koa.

one thing I'm wondering: would it be possible to glean whether the server is koa or express based on the incoming arguments rather than require configuration? does the response object already come with the identifier by the time it hits this middleware?

from universal-redux.

bdefore avatar bdefore commented on July 28, 2024

@bartolkaruza ping!

from universal-redux.

bartolkaruza avatar bartolkaruza commented on July 28, 2024

@bdefore I saw you merged the PR. Great stuff. Close this issue?

from universal-redux.

bdefore avatar bdefore commented on July 28, 2024

@bartolkaruza yup, thanks!

from universal-redux.

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.