-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
integration.ts
276 lines (243 loc) · 7.3 KB
/
integration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { Construct } from 'constructs';
import { IWebSocketApi } from './api';
import { IWebSocketRoute } from './route';
import { CfnIntegration } from '.././index';
import { IRole } from '../../../aws-iam';
import { Resource } from '../../../core';
import { IIntegration } from '../common';
/**
* Represents an Integration for an WebSocket API.
*/
export interface IWebSocketIntegration extends IIntegration {
/** The WebSocket API associated with this integration */
readonly webSocketApi: IWebSocketApi;
}
/**
* WebSocket Integration Types
*/
export enum WebSocketIntegrationType {
/**
* AWS Proxy Integration Type
*/
AWS_PROXY = 'AWS_PROXY',
/**
* Mock Integration Type
*/
MOCK = 'MOCK',
/**
* AWS Integration Type
*/
AWS = 'AWS',
}
/**
* Integration Passthrough Behavior
*/
export enum PassthroughBehavior {
/**
* Passes the request body for unmapped content types through to the
* integration back end without transformation.
*/
WHEN_NO_MATCH = 'WHEN_NO_MATCH',
/**
* Rejects unmapped content types with an HTTP 415 'Unsupported Media Type'
* response
*/
NEVER = 'NEVER',
/**
* Allows pass-through when the integration has NO content types mapped to
* templates. However if there is at least one content type defined,
* unmapped content types will be rejected with the same 415 response.
*/
WHEN_NO_TEMPLATES = 'WHEN_NO_TEMPLATES',
}
/**
* The integration properties
*/
export interface WebSocketIntegrationProps {
/**
* The WebSocket API to which this integration should be bound.
*/
readonly webSocketApi: IWebSocketApi;
/**
* Integration type
*/
readonly integrationType: WebSocketIntegrationType;
/**
* Integration URI.
*/
readonly integrationUri: string;
/**
* Specifies the integration's HTTP method type.
*
* @default - No HTTP method required.
*/
readonly integrationMethod?: string;
/**
* Specifies the IAM role required for the integration.
*
* @default - No IAM role required.
*/
readonly credentialsRole?: IRole;
/**
* The request parameters that API Gateway sends with the backend request.
* Specify request parameters as key-value pairs (string-to-string
* mappings), with a destination as the key and a source as the value.
*
* @default - No request parameters required.
*/
readonly requestParameters?: { [dest: string]: string };
/**
* A map of Apache Velocity templates that are applied on the request
* payload.
*
* ```
* { "application/json": "{ \"statusCode\": 200 }" }
* ```
*
* @default - No request templates required.
*/
readonly requestTemplates?: { [contentType: string]: string };
/**
* The template selection expression for the integration.
*
* @default - No template selection expression required.
*/
readonly templateSelectionExpression?: string;
/**
* Specifies the pass-through behavior for incoming requests based on the
* Content-Type header in the request, and the available mapping templates
* specified as the requestTemplates property on the Integration resource.
* There are three valid values: WHEN_NO_MATCH, WHEN_NO_TEMPLATES, and
* NEVER.
*
* @default - No passthrough behavior required.
*/
readonly passthroughBehavior?: PassthroughBehavior;
}
/**
* The integration for an API route.
* @resource AWS::ApiGatewayV2::Integration
*/
export class WebSocketIntegration extends Resource implements IWebSocketIntegration {
public readonly integrationId: string;
public readonly webSocketApi: IWebSocketApi;
constructor(scope: Construct, id: string, props: WebSocketIntegrationProps) {
super(scope, id);
const integ = new CfnIntegration(this, 'Resource', {
apiId: props.webSocketApi.apiId,
integrationType: props.integrationType,
integrationUri: props.integrationUri,
integrationMethod: props.integrationMethod,
credentialsArn: props.credentialsRole?.roleArn,
requestParameters: props.requestParameters,
requestTemplates: props.requestTemplates,
passthroughBehavior: props.passthroughBehavior,
templateSelectionExpression: props.templateSelectionExpression,
});
this.integrationId = integ.ref;
this.webSocketApi = props.webSocketApi;
}
}
/**
* Options to the WebSocketRouteIntegration during its bind operation.
*/
export interface WebSocketRouteIntegrationBindOptions {
/**
* The route to which this is being bound.
*/
readonly route: IWebSocketRoute;
/**
* The current scope in which the bind is occurring.
* If the `WebSocketRouteIntegration` being bound creates additional constructs,
* this will be used as their parent scope.
*/
readonly scope: Construct;
}
/**
* The interface that various route integration classes will inherit.
*/
export abstract class WebSocketRouteIntegration {
private integration?: WebSocketIntegration;
/**
* Initialize an integration for a route on websocket api.
* @param id id of the underlying `WebSocketIntegration` construct.
*/
constructor(private readonly id: string) {}
/**
* Internal method called when binding this integration to the route.
* @internal
*/
public _bindToRoute(options: WebSocketRouteIntegrationBindOptions): { readonly integrationId: string } {
if (this.integration && this.integration.webSocketApi.node.addr !== options.route.webSocketApi.node.addr) {
throw new Error('A single integration cannot be associated with multiple APIs.');
}
if (!this.integration) {
const config = this.bind(options);
this.integration = new WebSocketIntegration(options.scope, this.id, {
webSocketApi: options.route.webSocketApi,
integrationType: config.type,
integrationUri: config.uri,
integrationMethod: config.method,
credentialsRole: config.credentialsRole,
requestTemplates: config.requestTemplates,
requestParameters: config.requestParameters,
passthroughBehavior: config.passthroughBehavior,
templateSelectionExpression: config.templateSelectionExpression,
});
}
return { integrationId: this.integration.integrationId };
}
/**
* Bind this integration to the route.
*/
public abstract bind(options: WebSocketRouteIntegrationBindOptions): WebSocketRouteIntegrationConfig;
}
/**
* Config returned back as a result of the bind.
*/
export interface WebSocketRouteIntegrationConfig {
/**
* Integration type.
*/
readonly type: WebSocketIntegrationType;
/**
* Integration URI
*/
readonly uri: string;
/**
* Integration method
*
* @default - No integration method.
*/
readonly method?: string;
/**
* Credentials role
*
* @default - No role provided.
*/
readonly credentialsRole?: IRole;
/**
* Request template
*
* @default - No request template provided.
*/
readonly requestTemplates?: { [contentType: string]: string };
/**
* Request parameters
*
* @default - No request parameters provided.
*/
readonly requestParameters?: { [dest: string]: string };
/**
* Template selection expression
*
* @default - No template selection expression.
*/
readonly templateSelectionExpression?: string;
/**
* Integration passthrough behaviors.
*
* @default - No pass through bahavior.
*/
readonly passthroughBehavior?: PassthroughBehavior;
}