Skip to content

Commit

Permalink
feat(aws-apigateway-dynamodb): add optional resourceName parameter (#898
Browse files Browse the repository at this point in the history
)

* feat(aws-apigateway-dynamodb): add optional resourceName parameter

* test(aws-apigateway-dynamodb): add resourceName test

* docs(aws-apigateway-dynamodb): add resourceName documentation

---------

Co-authored-by: biffgaut <[email protected]>
  • Loading branch information
fargito and biffgaut authored Feb 15, 2023
1 parent f1a51ce commit 09e54ec
Show file tree
Hide file tree
Showing 5 changed files with 513 additions and 3 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 safely cast away the optional aspect of the type.
Expand All @@ -246,7 +255,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 @@ -272,7 +281,7 @@ export class ApiGatewayToDynamoDB extends Construct {
"KeyConditionExpression": "${partitionKeyName} = :v1", \
"ExpressionAttributeValues": { \
":v1": { \
"S": "$input.params('${partitionKeyName}')" \
"S": "$input.params('${resourceName}')" \
} \
} \
}`;
Expand Down Expand Up @@ -311,7 +320,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 09e54ec

Please sign in to comment.