Skip to content

Commit

Permalink
Merge branch 'main' into Issue853
Browse files Browse the repository at this point in the history
  • Loading branch information
biffgaut authored Feb 17, 2023
2 parents 085e098 + dc48e48 commit ea61071
Show file tree
Hide file tree
Showing 55 changed files with 580 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ new ApiGatewayToDynamoDB(this, "test-api-gateway-dynamodb-default", new ApiGatew
|dynamoTableProps?|[`dynamodb.TableProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_dynamodb.TableProps.html)|Optional user provided props to override the default props for DynamoDB Table.|
|existingTableObj?|[`dynamodb.Table`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_dynamodb.Table.html)|Existing instance of DynamoDB table object, providing both this and `dynamoTableProps` will cause an error.|
|apiGatewayProps?|[`api.RestApiProps`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_apigateway.RestApiProps.html)|Optional user-provided props to override the default props for the API Gateway.|
|resourceName?|`string`|Optional name of the resource on the API Gateway. Defaults to the table's partitionKeyName|
|allowCreateOperation?|`boolean`|Whether to deploy an API Gateway Method for POST HTTP operations on the DynamoDB table (i.e. dynamodb:PutItem).|
|createRequestTemplate?|`string`|API Gateway Request Template for the create method for the default `application/json` content-type. This property is required if the `allowCreateOperation` property is set to true.|
|additionalCreateRequestTemplates?|`{ [contentType: string]: string; }`|Optional Create Request Templates for content-types other than `application/json`. Use the `createRequestTemplate` property to set the request template for the `application/json` content-type. This property can only be specified if the `allowCreateOperation` property is set to true.|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export interface ApiGatewayToDynamoDBProps {
* @default - Default properties are used.
*/
readonly apiGatewayProps?: api.RestApiProps;
/**
* Optional resource name on the API
* This property is useful if your integration does not directly use the partition key name
*
* @default - partition key name, retrieved from the DynamoDB table object
*/
readonly resourceName?: string;
/**
* Whether to deploy an API Gateway Method for POST HTTP operations on the DynamoDB table (i.e. dynamodb:PutItem).
*
Expand Down Expand Up @@ -228,6 +235,8 @@ export class ApiGatewayToDynamoDB extends Construct {
partitionKeyName = getPartitionKeyNameFromTable(props.existingTableObj);
}

const resourceName = props.resourceName ?? partitionKeyName;

// Since we are only invoking this function with an existing Table or tableProps,
// (not a table interface), we know that the implementation will always return
// a Table object and we can force assignment to the dynamoTable property.
Expand All @@ -249,7 +258,7 @@ export class ApiGatewayToDynamoDB extends Construct {
});

// Setup the API Gateway Resource
const apiGatewayResource: api.Resource = this.apiGateway.root.addResource("{" + partitionKeyName + "}");
const apiGatewayResource: api.Resource = this.apiGateway.root.addResource("{" + resourceName + "}");

// Setup API Gateway Method
// Create
Expand All @@ -275,7 +284,7 @@ export class ApiGatewayToDynamoDB extends Construct {
"KeyConditionExpression": "${partitionKeyName} = :v1", \
"ExpressionAttributeValues": { \
":v1": { \
"S": "$input.params('${partitionKeyName}')" \
"S": "$input.params('${resourceName}')" \
} \
} \
}`;
Expand Down Expand Up @@ -314,7 +323,7 @@ export class ApiGatewayToDynamoDB extends Construct {
"TableName": "${this.dynamoTable.tableName}", \
"Key": { \
"${partitionKeyName}": { \
"S": "$input.params('${partitionKeyName}')" \
"S": "$input.params('${resourceName}')" \
} \
}, \
"ReturnValues": "ALL_OLD" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,17 @@ test('Construct accepts additional delete request templates', () => {
});
});

test('Construct can customize the api resourceName', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
resourceName: 'my-resource-name',
});

expect(stack).toHaveResource("AWS::ApiGateway::Resource", {
PathPart: "{my-resource-name}",
});
});

test('Construct uses default integration responses', () => {
const stack = new Stack();
new ApiGatewayToDynamoDB(stack, 'api-gateway-dynamodb', {
Expand Down
Loading

0 comments on commit ea61071

Please sign in to comment.