Skip to content
This repository was archived by the owner on Aug 8, 2024. It is now read-only.

Commit 4c2330a

Browse files
authored
Merge pull request #33 from spring-media/open-api-parameters
added support for open api path parameters
2 parents 5ebedd9 + e8c02bf commit 4c2330a

5 files changed

Lines changed: 49 additions & 477 deletions

File tree

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ export const handler = router.handler({
5252
action: (request, context) => {
5353
return "You called me with: " + request.paths.id;
5454
}
55+
},
56+
{
57+
// request-path-pattern with a path variable in Open API style:
58+
path: '/section/{id}',
59+
method: 'GET',
60+
// we can use the path param 'id' in the action call:
61+
action: (request, context) => {
62+
return "You called me with: " + request.paths.id;
63+
}
5564
}
5665
]
5766
}
@@ -77,7 +86,7 @@ const router = require('aws-lambda-router');
7786

7887
exports.handler = router.handler({
7988
proxyIntegration: {
80-
proxyPath: proxy
89+
proxyPath: proxy,
8190
routes: [
8291
{
8392
path: '/article/list',
@@ -348,7 +357,7 @@ return {
348357
body: JSON.stringify({
349358
foo: 'bar'
350359
})
351-
};
360+
}
352361
```
353362
354363
## Local developement
@@ -359,6 +368,8 @@ See here: https://yarnpkg.com/en/docs/cli/link
359368
360369
361370
## Release History
371+
* 0.8.2
372+
* added support for Open API parameter definitions e.g.: /section/{id}
362373
* 0.8.1
363374
* fix: changed ProxyIntegrationEvent body type to be generic but defaults to unknown
364375
* fix: changed @types/aws-lambda from devDependency to dependency

lib/proxyIntegration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,13 @@ const findMatchingActionConfig = (httpMethod: string, httpPath: string, routeCon
233233
}
234234

235235
const extractPathValues = (pathExpression: string, httpPath: string) => {
236-
const pathValueRegex = new RegExp('^' + pathExpression.replace(/:[\w]+/g, '([^/]+)') + '$')
236+
const pathValueRegex = new RegExp('^' + pathExpression.replace(/{[\w]+}|:[\w]+/g, '([^/]+)') + '$')
237237
const pathValues = pathValueRegex.exec(httpPath)
238238
return pathValues && pathValues.length > 0 ? pathValues.slice(1) : null
239239
}
240240

241241
const extractPathNames = (pathExpression: string) => {
242-
const pathNameRegex = new RegExp('^' + pathExpression.replace(/:[\w.]+/g, ':([\\w]+)') + '$')
242+
const pathNameRegex = new RegExp('^' + pathExpression.replace(/{[\w.]+}|:[\w.]+/g, '[:{]([\\w]+)}?') + '$')
243243
const pathNames = pathNameRegex.exec(pathExpression)
244244
return pathNames && pathNames.length > 0 ? pathNames.slice(1) : null
245245
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-lambda-router",
3-
"version": "0.8.1",
3+
"version": "0.8.2",
44
"description": "AWS lambda router",
55
"main": "index.js",
66
"types": "index.d.ts",

test/proxyIntegration.test.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,44 @@ describe('proxyIntegration.routeHandler.selection', () => {
4747
}, { httpMethod: 'GET', path: '/123' } as APIGatewayProxyEvent, context)
4848
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/123', paths: {} }, context)
4949
})
50-
it('should select parameter match', () => {
50+
forEach([
51+
['/:param'],
52+
['/{param}']
53+
]).it('should select parameter match', (path) => {
5154
const actionSpy = jasmine.createSpy('action')
5255
proxyIntegration({
5356
routes: [
5457
{ path: '/', method: 'GET', action: () => '/' as any },
5558
{ path: '/123', method: 'GET', action: () => '123' as any },
56-
{ path: '/:param', method: 'GET', action: actionSpy }
59+
{ path: path, method: 'GET', action: actionSpy }
5760
]
5861
}, { httpMethod: 'GET', path: '/456' } as APIGatewayProxyEvent, context)
5962
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/456', paths: { param: '456' } }, context)
6063
})
61-
it('should select static match', () => {
64+
forEach([
65+
['/:param'],
66+
['/{param}']
67+
]).it('should select static match', (path) => {
6268
const actionSpy = jasmine.createSpy('action')
6369
proxyIntegration({
6470
routes: [
6571
{ path: '/', method: 'GET', action: () => '/' as any },
6672
{ path: '/123', method: 'GET', action: actionSpy },
67-
{ path: '/:param', method: 'GET', action: () => 'param' as any }
73+
{ path: path, method: 'GET', action: () => 'param' as any }
6874
]
6975
}, { httpMethod: 'GET', path: '/123' } as APIGatewayProxyEvent, context)
7076
expect(actionSpy).toHaveBeenCalledWith({ httpMethod: 'GET', path: '/123', paths: {} }, context)
7177
})
72-
it('should match urlencoded path', () => {
78+
forEach([
79+
['/:param'],
80+
['/{param}']
81+
]).it('should match urlencoded path', (path) => {
7382
const actionSpy = jasmine.createSpy('action')
7483
proxyIntegration({
7584
routes: [
7685
{ path: '/', method: 'GET', action: () => '/' as any },
7786
{ path: '/123', method: 'GET', action: () => '123' as any },
78-
{ path: '/:param', method: 'GET', action: actionSpy }
87+
{ path: path, method: 'GET', action: actionSpy }
7988
]
8089
}, { httpMethod: 'GET', path: '/%2Fwirtschaft%2Farticle85883...tml' } as APIGatewayProxyEvent, context)
8190
expect(actionSpy).toHaveBeenCalledWith({
@@ -84,11 +93,14 @@ describe('proxyIntegration.routeHandler.selection', () => {
8493
paths: { param: '/wirtschaft/article85883...tml' }
8594
}, context)
8695
})
87-
it('should select match containing hyphen', () => {
96+
forEach([
97+
['/:param'],
98+
['/{param}']
99+
]).it('should select match containing hyphen', (path) => {
88100
const actionSpy = jasmine.createSpy('action')
89101
proxyIntegration({
90102
routes: [
91-
{ path: '/:param', method: 'GET', action: actionSpy }
103+
{ path: path, method: 'GET', action: actionSpy }
92104
]
93105
}, { httpMethod: 'GET', path: '/%2Fdeutschland-bewegt-sich%2F' } as APIGatewayProxyEvent, context)
94106
expect(actionSpy).toHaveBeenCalledWith({
@@ -97,11 +109,14 @@ describe('proxyIntegration.routeHandler.selection', () => {
97109
paths: { param: '/deutschland-bewegt-sich/' }
98110
}, context)
99111
})
100-
it('should select match containing question marks and dots', () => {
112+
forEach([
113+
['/:param'],
114+
['/{param}']
115+
]).it('should select match containing question marks and dots', (path) => {
101116
const actionSpy = jasmine.createSpy('action')
102117
proxyIntegration({
103118
routes: [
104-
{ path: '/:param', method: 'GET', action: actionSpy }
119+
{ path: path, method: 'GET', action: actionSpy }
105120
]
106121
}, {
107122
httpMethod: 'GET',
@@ -264,7 +279,14 @@ describe('proxyIntegration.routeHandler', () => {
264279
['/abc/:param1', '/abc/p1', { param1: 'p1' }],
265280
['/abc/def/:param1', '/abc/def/p1', { param1: 'p1' }],
266281
['/:param1/abc/:param2', '/p1/abc/p2', { param1: 'p1', param2: 'p2' }],
267-
['/:param1/abc/def/:param2', '/p1/abc/def/p2', { param1: 'p1', param2: 'p2' }]
282+
['/:param1/abc/def/:param2', '/p1/abc/def/p2', { param1: 'p1', param2: 'p2' }],
283+
['/{param1}', '/p1', { param1: 'p1' }],
284+
['/abc/{param1}', '/abc/p1', { param1: 'p1' }],
285+
['/abc/def/{param1}', '/abc/def/p1', { param1: 'p1' }],
286+
['/{param1}/abc/{param2}', '/p1/abc/p2', { param1: 'p1', param2: 'p2' }],
287+
['/{param1}/abc/def/{param2}', '/p1/abc/def/p2', { param1: 'p1', param2: 'p2' }],
288+
['/:param1/abc/{param2}', '/p1/abc/p2', { param1: 'p1', param2: 'p2' }],
289+
['/{param1}/abc/def/:param2', '/p1/abc/def/p2', { param1: 'p1', param2: 'p2' }]
268290
]).it('should call action with path params for method/path', async (pathConfig, path, expectedPathValues) => {
269291
const spiedAction = jasmine.createSpy('action').and.returnValue({ foo: 'bar' })
270292
const routeConfig: ProxyIntegrationConfig = {

0 commit comments

Comments
 (0)