Skip to content
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

feat(apigateway): WebSocketIntegrationResponse implementation #29661

Open
wants to merge 45 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
8e020be
feat: response class (wip)
nmussy Mar 29, 2024
c045d71
feat: response mapping, lambda unit test
nmussy Mar 29, 2024
c5e81cd
chore: throws JSDoc
nmussy Mar 29, 2024
aa40b0f
chore: remove unused import
nmussy Mar 31, 2024
25c9485
chore: unit tests
nmussy Mar 31, 2024
7dc2af0
chore: README wip
nmussy Mar 31, 2024
664e1b2
chore: add returnResponse check, refactor WebSocketTwoWayRouteIntegra…
nmussy Mar 31, 2024
45e5cd3
chore: fix unit tests
nmussy Mar 31, 2024
0b83d03
chore: remove unused imports
nmussy Mar 31, 2024
9b34229
chore: build fixes
nmussy Mar 31, 2024
8c5432d
fix: IntegrationResponse type compatibility
nmussy Apr 1, 2024
b0bfe57
chore: add jsdoc
nmussy Apr 1, 2024
38a0d73
chore: update AWS integ
nmussy Apr 1, 2024
1bf091d
chore: add breaking change
nmussy Apr 1, 2024
3a00e50
chore: update integ
nmussy Apr 1, 2024
7590d96
feat: add missing mock properties
nmussy Apr 1, 2024
70ce981
chore: update mock integ
nmussy Apr 1, 2024
fa2e68e
chore: README update
nmussy Apr 2, 2024
6655588
fix: WebSocketIntegrationResponse constructor
nmussy Apr 2, 2024
5bdc242
chore: docs fixes
nmussy Apr 2, 2024
0287dc2
chore: refactor CustomResponseWebSocketRoute
nmussy Apr 2, 2024
71fe931
chore: add $input integ/example
nmussy Apr 2, 2024
4863001
chore: docs
nmussy Apr 2, 2024
633c04b
Merge remote-tracking branch 'upstream/main' into feat-apigatewayv2-w…
nmussy Apr 14, 2024
d4c87f2
Merge remote-tracking branch 'upstream/main' into feat-apigatewayv2-w…
nmussy Apr 14, 2024
3f0686a
docs: wording
nmussy Jun 10, 2024
0774e88
docs: punctuation
nmussy Jun 10, 2024
7e0b76d
docs: punctuation
nmussy Jun 10, 2024
cdc120d
chore: missing new line
nmussy Jun 10, 2024
fc37366
Merge remote-tracking branch 'upstream/main' into feat-apigatewayv2-w…
nmussy Jun 10, 2024
99cdd68
docs: fix fromStatusRegExp example
nmussy Jun 10, 2024
b64002f
docs: grammar
nmussy Jun 21, 2024
31302b8
Merge remote-tracking branch 'upstream/main' into feat-apigatewayv2-w…
nmussy Jun 22, 2024
279bac0
revert: extra Mock props
nmussy Jun 22, 2024
51ce60a
Merge remote-tracking branch 'upstream/main' into feat-apigatewayv2-w…
nmussy Jun 25, 2024
32a2aaf
fix: restore breaking change, deprecate field
nmussy Jun 25, 2024
747cc08
chore: remove nl
nmussy Jun 25, 2024
f35c28c
feat: add common status response keys
nmussy Jun 25, 2024
62ebc9c
chore: refactor addResponse params
nmussy Jun 25, 2024
45066bb
Merge remote-tracking branch 'upstream/main' into feat-apigatewayv2-w…
nmussy Jul 6, 2024
4e9a901
fix: jsii build
nmussy Jul 6, 2024
5cfbb34
fix: use uniqueResourceName to generate integ response id
nmussy Jul 6, 2024
0d57e25
docs: fix status description
nmussy Jul 6, 2024
a1b0928
feat: WebSocketIntegrationResponseKey.fromKeys method
nmussy Jul 6, 2024
ddbf57b
docs: fix rosetta example
nmussy Jul 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion packages/aws-cdk-lib/aws-apigatewayv2-integrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [WebSocket APIs](#websocket-apis)
- [Lambda WebSocket Integration](#lambda-websocket-integration)
- [AWS WebSocket Integration](#aws-websocket-integration)
- [Integration Responses](#integration-responses)

## HTTP APIs

Expand Down Expand Up @@ -306,4 +307,46 @@ webSocketApi.addRoute('$connect', {
```

You can also set additional properties to change the behavior of your integration, such as `contentHandling`.
See [Working with binary media types for WebSocket APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-develop-binary-media-types.html).
See [Working with binary media types for WebSocket APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-api-develop-binary-media-types.html).


### Integration Responses

```ts
import * as integrations from 'aws-cdk-lib/aws-apigatewayv2-integrations';

const webSocketApi = new apigwv2.WebSocketApi(this, 'mywsapi');
new apigwv2.WebSocketStage(this, 'mystage', {
webSocketApi,
stageName: 'dev',
autoDeploy: true,
});

declare const messageHandler: lambda.Function;
const integration = new integrations.WebSocketLambdaIntegration(
'SendMessageIntegration',
messageHandler,
{
responses: [
// Default response key, will be used if no other matched
{ responseKey: integrations.WebSocketIntegrationResponseKey.default },
// Success response key, will match all 2xx response HTTP status codes
{ responseKey: integrations.WebSocketIntegrationResponseKey.success },
// You can also create custom response integrations for specific status codes
{
responseKey: integrations.WebSocketIntegrationResponseKey.fromStatusCode(404),
contentHandling: ContentHandling.CONVERT_TO_BINARY,
responseParameters: {
'method.response.header.Accept': "'application/json'",
},
templateSelectionExpression: '$default',
responseTemplates: {
'application/json': '{ "message": $context.error.message, "statusCode": 404 }',
},
},
],
},
);

webSocketApi.addRoute('sendMessage', { integration });
```
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
WebSocketRouteIntegration,
WebSocketIntegrationType,
WebSocketRouteIntegrationConfig,
WebSocketRouteIntegrationBindOptions,
PassthroughBehavior,
ContentHandling,
WebSocketTwoWayRouteIntegration,
InternalWebSocketIntegrationResponseProps,
} from '../../../aws-apigatewayv2';
import { IRole } from '../../../aws-iam';
import { Duration } from '../../../core';
Expand Down Expand Up @@ -59,6 +60,14 @@ export interface WebSocketAwsIntegrationProps {
*/
readonly requestTemplates?: { [contentType: string]: string };

/**
* Integration responses configuration
*
* @default - No response configuration provided.
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-integration-responses.html
*/
readonly responses?: InternalWebSocketIntegrationResponseProps[];

/**
* The template selection expression for the integration.
*
Expand Down Expand Up @@ -89,7 +98,7 @@ export interface WebSocketAwsIntegrationProps {
/**
* AWS WebSocket AWS Type Integration
*/
export class WebSocketAwsIntegration extends WebSocketRouteIntegration {
export class WebSocketAwsIntegration extends WebSocketTwoWayRouteIntegration {
/**
* @param id id of the underlying integration construct
*/
Expand All @@ -106,6 +115,7 @@ export class WebSocketAwsIntegration extends WebSocketRouteIntegration {
credentialsRole: this.props.credentialsRole,
requestParameters: this.props.requestParameters,
requestTemplates: this.props.requestTemplates,
responses: this.props.responses,
passthroughBehavior: this.props.passthroughBehavior,
templateSelectionExpression: this.props.templateSelectionExpression,
timeout: this.props.timeout,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
WebSocketRouteIntegration,
WebSocketIntegrationType,
WebSocketRouteIntegrationBindOptions,
WebSocketRouteIntegrationConfig,
ContentHandling,
WebSocketTwoWayRouteIntegration,
InternalWebSocketIntegrationResponseProps,
} from '../../../aws-apigatewayv2';
import { ServicePrincipal } from '../../../aws-iam';
import { IFunction } from '../../../aws-lambda';
Expand All @@ -28,12 +29,20 @@ export interface WebSocketLambdaIntegrationProps {
* the route response or method response without modification.
*/
readonly contentHandling?: ContentHandling;

/**
* Integration responses configuration
*
* @default - No response configuration provided.
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-integration-responses.html
*/
readonly responses?: InternalWebSocketIntegrationResponseProps[];
}

/**
* Lambda WebSocket Integration
*/
export class WebSocketLambdaIntegration extends WebSocketRouteIntegration {
export class WebSocketLambdaIntegration extends WebSocketTwoWayRouteIntegration {

private readonly _id: string;

Expand Down Expand Up @@ -75,6 +84,7 @@ export class WebSocketLambdaIntegration extends WebSocketRouteIntegration {
uri: integrationUri,
timeout: this.props.timeout,
contentHandling: this.props.contentHandling,
responses: this.props.responses,
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebSocketAwsIntegration } from './../../lib/websocket/aws';
import { Template } from '../../../assertions';
import { ContentHandling, PassthroughBehavior, WebSocketApi } from '../../../aws-apigatewayv2';
import { ContentHandling, PassthroughBehavior, WebSocketApi, WebSocketIntegrationResponseKey } from '../../../aws-apigatewayv2';
import * as iam from '../../../aws-iam';
import { Duration, Stack } from '../../../core';

Expand Down Expand Up @@ -47,7 +47,9 @@ describe('AwsWebSocketIntegration', () => {
passthroughBehavior: PassthroughBehavior.WHEN_NO_TEMPLATES,
contentHandling: ContentHandling.CONVERT_TO_BINARY,
timeout: Duration.seconds(10),
responses: [{ responseKey: WebSocketIntegrationResponseKey.success }],
}),
returnResponse: true,
},
});

Expand All @@ -66,5 +68,10 @@ describe('AwsWebSocketIntegration', () => {
ContentHandlingStrategy: 'CONVERT_TO_BINARY',
TimeoutInMillis: 10000,
});
Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::IntegrationResponse', {
ApiId: { Ref: 'ApiF70053CD' },
IntegrationId: { Ref: 'ApidefaultRouteAwsIntegration9C4CC31F' },
IntegrationResponseKey: '/2\\d{2}/',
});
});
});
Loading
Loading