Skip to content

Commit

Permalink
released v4.0.14 - fixes #179 - support optional paths
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Jul 5, 2023
1 parent 0034c47 commit dbc1cdb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "itty-router",
"version": "4.0.13",
"version": "4.0.14",
"description": "A tiny, zero-dependency router, designed to make beautiful APIs in any environment.",
"main": "./index.js",
"module": "./index.mjs",
Expand Down
4 changes: 4 additions & 0 deletions src/Router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ describe('ROUTE MATCHING', () => {
{ route: '/test.:x', path: '/test.a.b', returns: false }, // extensions only capture a single dot
{ route: '/test.:x', path: '/test.a', returns: { x: 'a' } },
{ route: '/:x?.y', path: '/test.y', returns: { x: 'test' } },
{ route: '/api(/v1)?/foo', path: '/api/v1/foo' }, // switching support preserved
{ route: '/api(/v1)?/foo', path: '/api/foo' }, // switching support preserved
{ route: '(/api)?/v1/:x', path: '/api/v1/foo', returns: { x: 'foo' } }, // switching support preserved
{ route: '(/api)?/v1/:x', path: '/v1/foo', returns: { x: 'foo' } }, // switching support preserved
])
})

Expand Down
5 changes: 3 additions & 2 deletions src/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ export const Router = <
routes.push(
[
prop.toUpperCase(),
RegExp(`^${(path = (base + '/' + route).replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
RegExp(`^${(path = (base + route)
.replace(/\/+(\/|$)/g, '$1')) // strip double & trailing splash
.replace(/(\/?\.?):(\w+)\+/g, '($1(?<$2>*))') // greedy params
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
.replace(/(\/?\.?):(\w+)/g, '($1(?<$2>[^$1/]+?))') // named params and image format
.replace(/\./g, '\\.') // dot in path
.replace(/(\/?)\*/g, '($1.*)?') // wildcard
}/*$`),
Expand Down
10 changes: 5 additions & 5 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ const testRoute = async (
// register route
router[method](route, handler)

// log && console.log({
// routes,
// route,
// path,
// })
log && console.log({
routes,
route,
path,
})

await router.handle(buildRequest({ method: method.toUpperCase(), path }))

Expand Down

0 comments on commit dbc1cdb

Please sign in to comment.