@@ -155,6 +155,147 @@ plan.addApiStage({
155
155
});
156
156
```
157
157
158
+ ### Working with models
159
+
160
+ When you work with Lambda integrations that are not Proxy integrations, you
161
+ have to define your models and mappings for the request, response, and integration.
162
+
163
+ ``` ts
164
+ const hello = new lambda .Function (this , ' hello' , {
165
+ runtime: lambda .Runtime .Nodejs10x ,
166
+ handler: ' hello.handler' ,
167
+ code: lambda .Code .asset (' lambda' )
168
+ });
169
+
170
+ const api = new apigateway .RestApi (this , ' hello-api' , { });
171
+ const resource = api .root .addResource (' v1' );
172
+ ```
173
+
174
+ You can define more parameters on the integration to tune the behavior of API Gateway
175
+
176
+ ``` ts
177
+ const integration = new LambdaIntegration (hello , {
178
+ proxy: false ,
179
+ requestParameters: {
180
+ // You can define mapping parameters from your method to your integration
181
+ // - Destination parameters (the key) are the integration parameters (used in mappings)
182
+ // - Source parameters (the value) are the source request parameters or expressions
183
+ // @see: https://docs.aws.amazon.com/apigateway/latest/developerguide/request-response-data-mappings.html
184
+ " integration.request.querystring.who" : " method.request.querystring.who"
185
+ },
186
+ allowTestInvoke: true ,
187
+ requestTemplates: {
188
+ // You can define a mapping that will build a payload for your integration, based
189
+ // on the integration parameters that you have specified
190
+ // Check: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
191
+ " application/json" : ' { "action": "sayHello", "pollId": "$util.escapeJavaScript($input.params(\' who\' ))" }'
192
+ },
193
+ // This parameter defines the behavior of the engine is no suitable response template is found
194
+ passthroughBehavior: PassthroughBehavior .Never ,
195
+ integrationResponses: [
196
+ {
197
+ // Successful response from the Lambda function, no filter defined
198
+ // - the selectionPattern filter only tests the error message
199
+ // We will set the response status code to 200
200
+ statusCode: " 200" ,
201
+ responseTemplates: {
202
+ // This template takes the "message" result from the Lambda function, adn embeds it in a JSON response
203
+ // Check https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
204
+ " application/json" : ' { "state": "ok", "greeting": "$util.escapeJavaScript($input.body)" }'
205
+ },
206
+ responseParameters: {
207
+ // We can map response parameters
208
+ // - Destination parameters (the key) are the response parameters (used in mappings)
209
+ // - Source parameters (the value) are the integration response parameters or expressions
210
+ ' method.response.header.Content-Type' : " 'application/json'" ,
211
+ ' method.response.header.Access-Control-Allow-Origin' : " '*'" ,
212
+ ' method.response.header.Access-Control-Allow-Credentials' : " 'true'"
213
+ }
214
+ },
215
+ {
216
+ // For errors, we check if the error message is not empty, get the error data
217
+ selectionPattern: ' (\n |.)+' ,
218
+ // We will set the response status code to 200
219
+ statusCode: " 400" ,
220
+ responseTemplates: {
221
+ " application/json" : ' { "state": "error", "message": "$util.escapeJavaScript($input.path(\' $.errorMessage\' ))" }'
222
+ },
223
+ responseParameters: {
224
+ ' method.response.header.Content-Type' : " 'application/json'" ,
225
+ ' method.response.header.Access-Control-Allow-Origin' : " '*'" ,
226
+ ' method.response.header.Access-Control-Allow-Credentials' : " 'true'"
227
+ }
228
+ }
229
+ ]
230
+ });
231
+
232
+ ```
233
+
234
+ You can define validation models for your responses (and requests)
235
+
236
+ ``` ts
237
+ // We define the JSON Schema for the transformed valid response
238
+ const responseModel = api .addModel (' ResponseModel' , {
239
+ contentType: " application/json" ,
240
+ modelName: ' ResponseModel' ,
241
+ schema: { " $schema" : " http://json-schema.org/draft-04/schema#" , " title" : " pollResponse" , " type" : " object" , " properties" : { " state" : { " type" : " string" }, " greeting" : { " type" : " string" } } }
242
+ });
243
+
244
+ // We define the JSON Schema for the transformed error response
245
+ const errorResponseModel = api .addModel (' ErrorResponseModel' , {
246
+ contentType: " application/json" ,
247
+ modelName: ' ErrorResponseModel' ,
248
+ schema: { " $schema" : " http://json-schema.org/draft-04/schema#" , " title" : " errorResponse" , " type" : " object" , " properties" : { " state" : { " type" : " string" }, " message" : { " type" : " string" } } }
249
+ });
250
+
251
+ ```
252
+
253
+ And reference all on your method definition.
254
+
255
+ ``` ts
256
+ // If you want to define parameter mappings for the request, you need a validator
257
+ const validator = api .addRequestValidator (' DefaultValidator' , {
258
+ validateRequestBody: false ,
259
+ validateRequestParameters: true
260
+ });
261
+ resource .addMethod (' GET' , integration , {
262
+ // We can mark the parameters as required
263
+ requestParameters: {
264
+ " method.request.querystring.who" : true
265
+ },
266
+ // We need to set the validator for ensuring they are passed
267
+ requestValidator: validator ,
268
+ methodResponses: [
269
+ {
270
+ // Successful response from the integration
271
+ statusCode: " 200" ,
272
+ // Define what parameters are allowed or not
273
+ responseParameters: {
274
+ ' method.response.header.Content-Type' : true ,
275
+ ' method.response.header.Access-Control-Allow-Origin' : true ,
276
+ ' method.response.header.Access-Control-Allow-Credentials' : true
277
+ },
278
+ // Validate the schema on the response
279
+ responseModels: {
280
+ " application/json" : responseModel
281
+ }
282
+ },
283
+ {
284
+ // Same thing for the error responses
285
+ statusCode: " 400" ,
286
+ responseParameters: {
287
+ ' method.response.header.Content-Type' : true ,
288
+ ' method.response.header.Access-Control-Allow-Origin' : true ,
289
+ ' method.response.header.Access-Control-Allow-Credentials' : true
290
+ },
291
+ responseModels: {
292
+ " application/json" : errorResponseModel
293
+ }
294
+ }
295
+ ]
296
+ });
297
+ ```
298
+
158
299
#### Default Integration and Method Options
159
300
160
301
The ` defaultIntegration ` and ` defaultMethodOptions ` properties can be used to
@@ -259,12 +400,9 @@ to allow users revert the stage to an old deployment manually.
259
400
260
401
### Missing Features
261
402
262
- See [ awslabs/aws-cdk #723 ] ( https://github.com/awslabs/aws-cdk/issues/723 ) for a
263
- list of missing features.
264
403
265
404
### Roadmap
266
405
267
- - [ ] Support defining REST API Models [ #1695 ] ( https://github.com/awslabs/aws-cdk/issues/1695 )
268
406
269
407
----
270
408
0 commit comments