Skip to content

Commit

Permalink
adding more supported router
Browse files Browse the repository at this point in the history
  • Loading branch information
William committed Jan 27, 2020
1 parent 8206086 commit e2d9fff
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -49,5 +49,5 @@
"staged": "lint-staged",
"test": "./node_modules/.bin/jest"
},
"version": "4.2.7"
"version": "4.3.0"
}
4 changes: 2 additions & 2 deletions src/router/add/path.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable max-params */
const Route = require('route-parser');
const { match } = require('path-to-regexp');

const addPath = (actions, method, path, fn, ...middleware) => {
actions.push({
fn,
method,
middleware,
path,
pattern: new Route(path),
pattern: match(path),
type: 'path',
});
};
Expand Down
6 changes: 4 additions & 2 deletions src/router/match/path.js
Original file line number Diff line number Diff line change
@@ -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]));
Expand Down
48 changes: 47 additions & 1 deletion tests/router.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
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({}, {});

await expect(res.statusCode).toEqual(404);
});

it('router simple path', async () => {
const router = createRouter();

router.path('any', '/', (req, res) => res.json({ status: 'success' }));

const handler = lambdify(router.serve);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e2d9fff

Please sign in to comment.