diff --git a/package.json b/package.json index 36a82adf..e1424e44 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ }, "dependencies": { "cookie": "^0.4.0", - "route-parser": "^0.0.5" + "path-to-regexp": "^6.1.0" }, "description": "Lambdify is a set of tools that makes building and consuming AWS Lambda functions easier", "devDependencies": { @@ -49,5 +49,5 @@ "staged": "lint-staged", "test": "./node_modules/.bin/jest" }, - "version": "4.2.7" + "version": "4.3.0" } diff --git a/src/router/add/path.js b/src/router/add/path.js index 1f33e51a..358058ed 100644 --- a/src/router/add/path.js +++ b/src/router/add/path.js @@ -1,5 +1,5 @@ /* eslint-disable max-params */ -const Route = require('route-parser'); +const { match } = require('path-to-regexp'); const addPath = (actions, method, path, fn, ...middleware) => { actions.push({ @@ -7,7 +7,7 @@ const addPath = (actions, method, path, fn, ...middleware) => { method, middleware, path, - pattern: new Route(path), + pattern: match(path), type: 'path', }); }; diff --git a/src/router/match/path.js b/src/router/match/path.js index 86267b90..73111264 100644 --- a/src/router/match/path.js +++ b/src/router/match/path.js @@ -1,8 +1,10 @@ const pathMatch = (req, action) => { try { if (action.method.toUpperCase() === 'ANY' || action.method.toUpperCase() === req.getMethod()) { - if (action.pattern.match(req.getPath())) { - const params = action.pattern.match(req.getPath()); + const isMatch = action.pattern(req.getPath()); + + if (isMatch) { + const { params } = isMatch; // This sets the path params Object.keys(params).forEach((key) => req.setPathParam(key, params[key])); diff --git a/tests/router.test.js b/tests/router.test.js index ba0def3f..e8a32970 100644 --- a/tests/router.test.js +++ b/tests/router.test.js @@ -1,7 +1,9 @@ const lambdify = require('./../src/index'); -const router = require('./../src/router/index')(); +const createRouter = require('./../src/router/index'); it('router 404', async () => { + const router = createRouter(); + const handler = lambdify(router.serve); const res = await handler({}, {}); @@ -9,6 +11,8 @@ it('router 404', async () => { }); it('router simple path', async () => { + const router = createRouter(); + router.path('any', '/', (req, res) => res.json({ status: 'success' })); const handler = lambdify(router.serve); @@ -18,6 +22,8 @@ it('router simple path', async () => { }); it('router simple path with specific method', async () => { + const router = createRouter(); + router.path('get', '/', (req, res) => res.json({ status: 'success' })); const handler = lambdify(router.serve); @@ -27,6 +33,8 @@ it('router simple path with specific method', async () => { }); it('router get id from path', async () => { + const router = createRouter(); + router.path('any', '/:id', (req, res) => res.json({ id: req.getPathParam('id'), status: 'success' })); const handler = lambdify(router.serve); @@ -37,6 +45,8 @@ it('router get id from path', async () => { }); it('router override pathParameters', async () => { + const router = createRouter(); + router.path('any', '/:id', (req, res) => res.json({ id: req.getPathParam('id'), status: 'success' })); const handler = lambdify(router.serve); @@ -47,6 +57,8 @@ it('router override pathParameters', async () => { }); it('router handle file extensions', async () => { + const router = createRouter(); + router.path('any', '/:id', (req, res) => res.json({ id: req.getPathParam('id'), status: 'success' })); const handler = lambdify(router.serve); @@ -56,7 +68,35 @@ it('router handle file extensions', async () => { await expect(JSON.parse(res.body).id).toEqual('1.png'); }); +it('router multiple matches with optional', async () => { + const router = createRouter(); + + router.path('any', '/:firstName/:lastName?', (req, res) => res.json({ ...req.getPathParams(), status: 'success' })); + + const handler = lambdify(router.serve); + const res = await handler({ path: '/John', pathParameters: { id: 2 } }, {}); + + await expect(res.statusCode).toEqual(200); + await expect(JSON.parse(res.body).firstName).toEqual('John'); + await expect(JSON.parse(res.body).lastName).toEqual(undefined); +}); + +it('router multiple matches', async () => { + const router = createRouter(); + + router.path('any', '/:firstName/:lastName', (req, res) => res.json({ ...req.getPathParams(), status: 'success' })); + + const handler = lambdify(router.serve); + const res = await handler({ path: '/John/Smith', pathParameters: { id: 2 } }, {}); + + await expect(res.statusCode).toEqual(200); + await expect(JSON.parse(res.body).firstName).toEqual('John'); + await expect(JSON.parse(res.body).lastName).toEqual('Smith'); +}); + it('router match sqs', async () => { + const router = createRouter(); + router.sqs('event', 'foo', (req, res) => res.json({ status: 'success' })); const handler = lambdify(router.serve); @@ -66,6 +106,8 @@ it('router match sqs', async () => { }); it('router failed match sqs', async () => { + const router = createRouter(); + router.sqs('event', 'foo', (req, res) => res.json({ status: 'success' })); const handler = lambdify(router.serve); @@ -76,6 +118,8 @@ it('router failed match sqs', async () => { // This has to go after the match cases otherwise it will always hit it('router simple sqs', async () => { + const router = createRouter(); + router.sqs((req, res) => res.json({ status: 'success' })); const handler = lambdify(router.serve); @@ -85,6 +129,8 @@ it('router simple sqs', async () => { }); it('setup notfound', async () => { + const router = createRouter(); + router.notFound((req, res) => res.json({ status: 'success' })); const handler = lambdify(router.serve); diff --git a/yarn.lock b/yarn.lock index a317e2b2..eabbe9e4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3267,6 +3267,11 @@ path-parse@^1.0.6: resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c" integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw== +path-to-regexp@^6.1.0: + version "6.1.0" + resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-6.1.0.tgz#0b18f88b7a0ce0bfae6a25990c909ab86f512427" + integrity sha512-h9DqehX3zZZDCEm+xbfU0ZmwCGFCAAraPJWMXJ4+v32NjZJilVg3k1TcKsRgIb8IQ/izZSaydDc1OhJCZvs2Dw== + path-type@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b"