Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): return route params on findRoute #342

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ const handler = (req, res, params) => {
router.on('GET', '/:file(^\\S+).png', handler)

router.findRoute('GET', '/:file(^\\S+).png')
// => { handler: Function, store: Object }
// => { handler: Function, store: Object, params: ['file'] }

router.findRoute('GET', '/:file(^\\D+).jpg')
// => null
Expand All @@ -423,10 +423,10 @@ const handler = (req, res, params) => {
router.on('GET', '/:param1', handler)

router.findRoute('GET', '/:param1')
// => { handler: Function, store: Object }
// => { handler: Function, store: Object, params: ['param1'] }

router.findRoute('GET', '/:param2')
// => { handler: Function, store: Object }
// => { handler: Function, store: Object, params: ['param1'] }
```

#### hasRoute (method, path, [constraints])
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ declare namespace Router {
interface FindRouteResult<V extends HTTPVersion> {
handler: Handler<V>;
store: any;
params: string[];
}

interface Instance<V extends HTTPVersion> {
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ Router.prototype.findRoute = function findNode (method, path, constraints = {})
) {
return {
handler: existRoute.handler,
store: existRoute.store
store: existRoute.store,
params: existRoute.params || []
}
}
}
Expand Down
63 changes: 36 additions & 27 deletions test/find-route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ function equalRouters (t, router1, router2) {
t.same(router1.routes, router2.routes)
t.same(router1.trees, router2.trees)

t.strictSame(
router1.constrainer.strategies,
router2.constrainer.strategies
)
t.strictSame(router1.constrainer.strategies, router2.constrainer.strategies)
t.strictSame(
router1.constrainer.strategiesInUse,
router2.constrainer.strategiesInUse
Expand All @@ -24,7 +21,7 @@ function equalRouters (t, router1, router2) {
)
}

test('findRoute returns null if there is no routes', t => {
test('findRoute returns null if there is no routes', (t) => {
t.plan(7)

const findMyWay = FindMyWay()
Expand All @@ -36,8 +33,8 @@ test('findRoute returns null if there is no routes', t => {
equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns handler and store for a static route', t => {
t.plan(8)
test('findRoute returns handler and store for a static route', (t) => {
t.plan(9)

const findMyWay = FindMyWay()

Expand All @@ -50,11 +47,12 @@ test('findRoute returns handler and store for a static route', t => {
const route = findMyWay.findRoute('GET', '/example')
t.equal(route.handler, handler)
t.equal(route.store, store)
t.deepEqual(route.params, [])

equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns null for a static route', t => {
test('findRoute returns null for a static route', (t) => {
t.plan(7)

const findMyWay = FindMyWay()
Expand All @@ -70,8 +68,8 @@ test('findRoute returns null for a static route', t => {
equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns handler for a parametric route', t => {
t.plan(7)
test('findRoute returns handler and params for a parametric route', (t) => {
t.plan(8)

const findMyWay = FindMyWay()

Expand All @@ -82,11 +80,12 @@ test('findRoute returns handler for a parametric route', t => {

const route = findMyWay.findRoute('GET', '/:param')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param'])

equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns null for a parametric route', t => {
test('findRoute returns null for a parametric route', (t) => {
t.plan(7)

const findMyWay = FindMyWay()
Expand All @@ -102,8 +101,8 @@ test('findRoute returns null for a parametric route', t => {
equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns handler for a parametric route with static suffix', t => {
t.plan(7)
test('findRoute returns handler and params for a parametric route with static suffix', (t) => {
t.plan(8)

const findMyWay = FindMyWay()

Expand All @@ -114,11 +113,12 @@ test('findRoute returns handler for a parametric route with static suffix', t =>

const route = findMyWay.findRoute('GET', '/:param-static')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param'])

equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns null for a parametric route with static suffix', t => {
test('findRoute returns null for a parametric route with static suffix', (t) => {
t.plan(7)

const findMyWay = FindMyWay()
Expand All @@ -132,8 +132,8 @@ test('findRoute returns null for a parametric route with static suffix', t => {
equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns handler even if a param name different', t => {
t.plan(7)
test('findRoute returns handler and original params even if a param name different', (t) => {
t.plan(8)

const findMyWay = FindMyWay()

Expand All @@ -144,12 +144,13 @@ test('findRoute returns handler even if a param name different', t => {

const route = findMyWay.findRoute('GET', '/:param2')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param1'])

equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns handler for a multi-parametric route', t => {
t.plan(7)
test('findRoute returns handler and params for a multi-parametric route', (t) => {
t.plan(8)

const findMyWay = FindMyWay()

Expand All @@ -160,11 +161,12 @@ test('findRoute returns handler for a multi-parametric route', t => {

const route = findMyWay.findRoute('GET', '/:param1-:param2')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param1', 'param2'])

equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns null for a multi-parametric route', t => {
test('findRoute returns null for a multi-parametric route', (t) => {
t.plan(7)

const findMyWay = FindMyWay()
Expand All @@ -178,8 +180,8 @@ test('findRoute returns null for a multi-parametric route', t => {
equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns handler for a regexp route', t => {
t.plan(7)
test('findRoute returns handler and regexp param for a regexp route', (t) => {
t.plan(8)

const findMyWay = FindMyWay()

Expand All @@ -190,11 +192,12 @@ test('findRoute returns handler for a regexp route', t => {

const route = findMyWay.findRoute('GET', '/:param(^\\d+$)')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param'])

equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns null for a regexp route', t => {
test('findRoute returns null for a regexp route', (t) => {
t.plan(7)

const findMyWay = FindMyWay()
Expand All @@ -208,8 +211,8 @@ test('findRoute returns null for a regexp route', t => {
equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns handler for a wildcard route', t => {
t.plan(7)
test('findRoute returns handler and wildcard param for a wildcard route', (t) => {
t.plan(8)

const findMyWay = FindMyWay()

Expand All @@ -220,11 +223,12 @@ test('findRoute returns handler for a wildcard route', t => {

const route = findMyWay.findRoute('GET', '/example/*')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['*'])

equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns null for a wildcard route', t => {
test('findRoute returns null for a wildcard route', (t) => {
t.plan(7)

const findMyWay = FindMyWay()
Expand All @@ -238,13 +242,18 @@ test('findRoute returns null for a wildcard route', t => {
equalRouters(t, findMyWay, fundMyWayClone)
})

test('findRoute returns handler for a constrained route', t => {
test('findRoute returns handler for a constrained route', (t) => {
t.plan(9)

const findMyWay = FindMyWay()

const handler = () => {}
findMyWay.on('GET', '/example', { constraints: { version: '1.0.0' } }, handler)
findMyWay.on(
'GET',
'/example',
{ constraints: { version: '1.0.0' } },
handler
)

const fundMyWayClone = rfdc(findMyWay)

Expand Down
4 changes: 4 additions & 0 deletions test/types/router.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ let http2Res!: Http2ServerResponse;
expectType<Router.FindResult<Router.HTTPVersion.V1> | null>(router.find('GET', '/'))
expectType<Router.FindResult<Router.HTTPVersion.V1> | null>(router.find('GET', '/', {}))
expectType<Router.FindResult<Router.HTTPVersion.V1> | null>(router.find('GET', '/', {version: '1.0.0'}))

expectType<Router.FindRouteResult<Router.HTTPVersion.V1> | null>(router.findRoute('GET', '/'));
expectType<Router.FindRouteResult<Router.HTTPVersion.V1> | null>(router.findRoute('GET', '/', {}));
expectType<Router.FindRouteResult<Router.HTTPVersion.V1> | null>(router.findRoute('GET', '/', {version: '1.0.0'}));

expectType<void>(router.reset())
expectType<string>(router.prettyPrint())
Expand Down