-
Notifications
You must be signed in to change notification settings - Fork 4k
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
fix(aws-apigateway): make LambdaRestApi proxy by default #963
Merged
+54
−52
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import lambda = require('@aws-cdk/aws-lambda'); | ||
import cdk = require('@aws-cdk/cdk'); | ||
import { LambdaIntegration } from './integrations'; | ||
import { Method } from './method'; | ||
import { Resource } from './resource'; | ||
import { RestApi, RestApiProps } from './restapi'; | ||
|
||
export interface LambdaRestApiProps { | ||
|
@@ -13,15 +15,14 @@ export interface LambdaRestApiProps { | |
handler: lambda.Function; | ||
|
||
/** | ||
* An API path for a greedy proxy with an "ANY" method, which will route all | ||
* requests under that path to the defined handler. | ||
* If true, route all requests to the Lambda Function | ||
* | ||
* If not defined, you will need to explicitly define the API model using | ||
* If not set to true, you will need to explicitly define the API model using | ||
* `addResource` and `addMethod` (or `addProxy`). | ||
* | ||
* @default undefined | ||
* @default false | ||
*/ | ||
proxyPath?: string; | ||
proxyAll?: boolean; | ||
|
||
/** | ||
* Further customization of the REST API. | ||
|
@@ -49,16 +50,26 @@ export class LambdaRestApi extends RestApi { | |
...props.options | ||
}); | ||
|
||
// if proxyPath is specified, add a proxy at the specified path | ||
// we will need to create all resources along the path. | ||
const proxyPath = props.proxyPath; | ||
if (proxyPath) { | ||
const route = proxyPath.split('/').filter(x => x); | ||
let curr = this.root; | ||
for (const part of route) { | ||
curr = curr.addResource(part); | ||
} | ||
curr.addProxy(); | ||
if (props.proxyAll) { | ||
this.root.addProxy(); | ||
this.root.addMethod('ANY'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering if this should always be done in |
||
|
||
// Make sure users cannot call any other resource adding function | ||
this.root.addResource = addResourceThrows; | ||
this.root.addMethod = addMethodThrows; | ||
this.root.addProxy = addProxyThrows; | ||
} | ||
} | ||
} | ||
|
||
function addResourceThrows(): Resource { | ||
throw new Error(`Cannot call 'addResource' on a proyxing LambdaRestApi; set 'proxyAll' to false`); | ||
} | ||
|
||
function addMethodThrows(): Method { | ||
throw new Error(`Cannot call 'addMethod' on a proyxing LambdaRestApi; set 'proxyAll' to false`); | ||
} | ||
|
||
function addProxyThrows(): Resource { | ||
throw new Error(`Cannot call 'addProxy' on a proyxing LambdaRestApi; set 'proxyAll' to false`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,14 @@ export = { | |
}); | ||
|
||
// WHEN | ||
new apigw.LambdaRestApi(stack, 'lambda-rest-api', { handler, proxyPath: '/' }); | ||
const api = new apigw.LambdaRestApi(stack, 'lambda-rest-api', { handler, proxyAll: true }); | ||
|
||
// THEN | ||
// THEN -- can't customize further | ||
test.throws(() => { | ||
api.root.addResource('cant-touch-this'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😉 |
||
}); | ||
|
||
// THEN -- template proxies everything | ||
expect(stack).to(haveResource('AWS::ApiGateway::Resource', { | ||
"PathPart": "{proxy+}" | ||
})); | ||
|
@@ -81,32 +86,6 @@ export = { | |
test.done(); | ||
}, | ||
|
||
'proxyPath can be used to attach the proxy to any route'(test: Test) { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
||
const handler = new lambda.Function(stack, 'handler', { | ||
handler: 'index.handler', | ||
code: lambda.Code.inline('boom'), | ||
runtime: lambda.Runtime.NodeJS610, | ||
}); | ||
|
||
// WHEN | ||
new apigw.LambdaRestApi(stack, 'lambda-rest-api', { | ||
handler, | ||
proxyPath: '/backend/v2' | ||
}); | ||
|
||
// THEN | ||
expect(stack).to(haveResource('AWS::ApiGateway::Method', { | ||
"ResourceId": { | ||
"Ref": "lambdarestapibackendv2proxyC4980BD5" | ||
} | ||
})); | ||
|
||
test.done(); | ||
}, | ||
|
||
'when "proxyPath" is not specified, users need to define the model'(test: Test) { | ||
// GIVEN | ||
const stack = new cdk.Stack(); | ||
|
@@ -162,5 +141,5 @@ export = { | |
}), /Cannot specify \"options\.defaultIntegration\" since Lambda integration is automatically defined/); | ||
|
||
test.done(); | ||
} | ||
}, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want the default to be true