universal-router icon indicating copy to clipboard operation
universal-router copied to clipboard

Simple Change: Make resolveRoute a class member of UniversalRouter instead of privately scoped

Open joezappie opened this issue 3 years ago • 1 comments

I'm submitting a ...

  • [ ] bug report
  • [x] feature request
  • [ ] other (Please do not submit support requests here (below))

Feature request

Would there be any backwards compatibility issues with moving the resolveRoute function to be inside the UniversalRouter class? The reason for this is when extending UniversalRouter, you cannot override the resolveRoute function as its a privately scoped function in the UniversalRouter.js file, outside of the UniversalRouter class.

Currently I have my resolveRouter passed in as a parameter, but I have a custom class for my router to add in functionality like history API. Being able to override resolveRouter inside that class would be a cleaner interface, keeping like logic together. I don't see any conflicts with this change and would improve class support. Is there a good reason for this to stay as a private function?

Proposed Change

class UniversalRouter {
    constructor(routes, options) {
         ...
    }

    resolve(pathnameOrContext) {
        ....
        const resolve = this.options.resolveRoute || this.resolveRoute;
        ...
    }
    
    resolveRoute(context, params) {
        if (typeof context.route.action === 'function') {
            return context.route.action(context, params);
        }
        return undefined;
    }
}

Then custom router classes can override it or it can still be passed in as an option.

class MyRouter extends UniversalRouter {
    resolveRoute(context, params) {
        // custom logic
    }
}

joezappie avatar Dec 15 '21 22:12 joezappie

You can override resolveRoute in your custom class like this:

class YourRouter extends UniversalRouter {
  constructor(routes, options) {
    super(routes, { ...options, resolveRoute: this.resolveRoute })
  }
  resolveRoute(context, params) {
    // custom logic
  }
}

But could you tell us a little about why you need to extend the UniversalRouter class, i.e. what functionality are you missing?

frenzzy avatar Dec 17 '21 17:12 frenzzy