berstend / tiny-request-router

:rocket: Fast, generic and type safe router (match request method and path).
MIT License
202 stars 9 forks source link

Query parameters in path cause exception #85

Open dimitrovs opened 3 years ago

dimitrovs commented 3 years ago

I understand query parameters are not supported for routing but even using a path with them throws exception:

import { Router, Method } from 'tiny-request-router'

const router = new Router()
router.post('/v1/relays', handleAddRelay);
router.post('/?action=host', handleInitHost);

Causes:

Uncaught TypeError: Unexpected MODIFIER at 1, expected END\n  at line 21 in h\n  at line 21 in i\n  at line 21 in a\n  at line 21 in s\n  at line 21 in e._push\n  at line 21 in e.post\n  at line 1\n  at line 1 in n\n  at line 1\n  at line 1\n
steverandy commented 3 years ago

I'm also getting exception when using star wildcard

router.get("/api*", handleApiRoute);
router.get("/", handleStaticRoute);
TypeError: Unexpected MODIFIER at 4, expected END
metaskippy commented 2 years ago

Same issue here. It would be awesome if the query string parameters were stripped from the URL, dumped into the response object, and routing worked as normal.

Even better would be to parse those query string parameters and put the key/value pairs into the response object.

bribes commented 2 years ago

I understand query parameters are not supported for routing but even using a path with them throws exception:

import { Router, Method } from 'tiny-request-router'

const router = new Router()
router.post('/v1/relays', handleAddRelay);
router.post('/?action=host', handleInitHost);

Causes:

Uncaught TypeError: Unexpected MODIFIER at 1, expected END\n  at line 21 in h\n  at line 21 in i\n  at line 21 in a\n  at line 21 in s\n  at line 21 in e._push\n  at line 21 in e.post\n  at line 1\n  at line 1 in n\n  at line 1\n  at line 1\n

Can't you just put the router as just '/' and then get the parameters of it? router.post('/', handleInitHost);.

In the fetch event listener you would have to define the parameter const { searchParams } = new URL(request.url);, then pass it in event.respondWith(match.handler(match.params, searchParams)), then in the function you can get the parameters by using .get(parameter) on the searchParams.