From 92cd7a94f2a836423015960591fbb05df57468be Mon Sep 17 00:00:00 2001 From: Rico Huijbers Date: Thu, 18 Oct 2018 16:39:31 +0200 Subject: [PATCH] fix(aws-apigateway): change 'proxyPath' to 'proxyAll' The LambdaRestApi construct now takes a 'proxyAll' argument to enable forwarding all requests to the given Lambda Function. BREAKING CHANGE: specifying a path no longer works. If you used to provide a '/', now supply true. Otherwise, you will have to construct more complex resource paths yourself. Fixes #959. --- .../@aws-cdk/aws-apigateway/lib/lambda-api.ts | 41 ++++++++++++------- .../aws-apigateway/test/test.lambda-api.ts | 37 ++++------------- 2 files changed, 34 insertions(+), 44 deletions(-) diff --git a/packages/@aws-cdk/aws-apigateway/lib/lambda-api.ts b/packages/@aws-cdk/aws-apigateway/lib/lambda-api.ts index 1b08b2533b165..57cedd75d0896 100644 --- a/packages/@aws-cdk/aws-apigateway/lib/lambda-api.ts +++ b/packages/@aws-cdk/aws-apigateway/lib/lambda-api.ts @@ -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'); + + // 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`); } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-apigateway/test/test.lambda-api.ts b/packages/@aws-cdk/aws-apigateway/test/test.lambda-api.ts index 2c9ebfb2f21a8..b6f158e2c102f 100644 --- a/packages/@aws-cdk/aws-apigateway/test/test.lambda-api.ts +++ b/packages/@aws-cdk/aws-apigateway/test/test.lambda-api.ts @@ -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'); + }); + + // 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(); - } + }, }; \ No newline at end of file