hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.64k stars 65 forks source link

use() routes with RegExps should work. #156

Closed mmillies closed 2 years ago

mmillies commented 3 years ago

Received the following error message after registering a Regular Expression as the route path, and a incoming request was being evaluated on it:

handlers[(i++)] is not a function

Found that the use function thought the base argument was a subapp handler because it wasn't a string. (it was an instance of RegExp)

pull request with unit test show casing example coming shortly.

Might consider some asserts on the input arguments to use to catch this sort of issue and provide a clearer error message. I was migrating some middleware from an existing app (that did self registering of routes) and had to jump through some hoops to figure out where this was. I don't mind adding the asserts, just let me know.

edit: also, not 100% sure why this module is responsible for ensuring a leading slash is on there. let me know if I need to modify anything with respect to that for backwards compatibility.