hassanzohdy / react-router

A Simple powerful react router system
6 stars 2 forks source link

Make Middlewares access the variables which we wanted from previous Middlewares. (like `context` in koa.js , or `request` in express) #4

Closed KariMuhammad closed 1 week ago

KariMuhammad commented 2 weeks ago

New Feature

Question

Why not make middleware in the same array access all the variables we wanted and returned from previous middlewares?

router.add("/blog/:id/:slug", BlogDetails, [Guardian, isValidId, BlogExists, getAllPreviousVarsInMiddlewares], BaseLayout);

Desire

export default BlogExists(id: string) {
   const blog = database.find(+id);
   if (!blog) return navigateTo("/404");

   // store variable blog if exist for next middlewares
   // context.blog || req.blog || next(blog)

   // no return
}

If middleware doesn't contain anything force it to navigate or throw an Error Make middleware doesn't return anything (no falsy values)

because return makes the brain think in stop execution, and that causes a lot of confusion in reading code.



export default getAllPreviousVarsInMiddlewares() {
const allPrevVariables = prev(); || context || req;

// Logic.
}
hassanzohdy commented 2 weeks ago

That would fit in a server side project (Koa, Next, Expres...etc), but with a CSR, there is no such complications like that, each middleware receives the route params (see middleware documentation), you can a state management to scope values for certain routes.