1+ import * as path from 'path' ;
2+ import * as lambda from '@aws-cdk/aws-lambda' ;
3+ import * as cdk from '@aws-cdk/core' ;
4+ import { IntegTest } from '@aws-cdk/integ-tests' ;
5+ import * as apigateway from '../lib' ;
6+
7+ class Test extends cdk . Stack {
8+ constructor ( scope : cdk . App , id : string ) {
9+ super ( scope , id ) ;
10+
11+ const api = new apigateway . SpecRestApi ( this , 'my-api' , {
12+ apiDefinition : apigateway . ApiDefinition . fromAsset ( path . join ( __dirname , 'sample-definition.yaml' ) ) ,
13+ disableExecuteApiEndpoint : true ,
14+ retainDeployments : true ,
15+ cloudWatchRole : true ,
16+ deployOptions : {
17+ cacheClusterEnabled : true ,
18+ stageName : 'beta' ,
19+ description : 'beta stage' ,
20+ loggingLevel : apigateway . MethodLoggingLevel . INFO ,
21+ dataTraceEnabled : true ,
22+ methodOptions : {
23+ '/api/appliances/GET' : {
24+ cachingEnabled : true ,
25+ } ,
26+ } ,
27+ } ,
28+ } ) ;
29+
30+ const handler = new lambda . Function ( this , 'MyHandler' , {
31+ runtime : lambda . Runtime . NODEJS_14_X ,
32+ code : lambda . Code . fromInline ( `exports.handler = ${ handlerCode } ` ) ,
33+ handler : 'index.handler' ,
34+ } ) ;
35+
36+ const v1 = api . root . addResource ( 'v1' ) ;
37+
38+ const integration = new apigateway . LambdaIntegration ( handler ) ;
39+
40+ const toys = v1 . addResource ( 'toys' ) ;
41+ const getToysMethod : apigateway . Method = toys . addMethod ( 'GET' , integration , { apiKeyRequired : true } ) ;
42+ toys . addMethod ( 'POST' ) ;
43+ toys . addMethod ( 'PUT' ) ;
44+
45+ const appliances = v1 . addResource ( 'appliances' ) ;
46+ appliances . addMethod ( 'GET' ) ;
47+
48+ const books = v1 . addResource ( 'books' ) ;
49+ books . addMethod ( 'GET' , integration ) ;
50+ books . addMethod ( 'POST' , integration ) ;
51+
52+ function handlerCode ( event : any , _ : any , callback : any ) {
53+ return callback ( undefined , {
54+ isBase64Encoded : false ,
55+ statusCode : 200 ,
56+ headers : { 'content-type' : 'application/json' } ,
57+ body : JSON . stringify ( event ) ,
58+ } ) ;
59+ }
60+
61+ const key = api . addApiKey ( 'ApiKey' ) ;
62+ const plan = api . addUsagePlan ( 'UsagePlan' , {
63+ name : 'Basic' ,
64+ apiKey : key ,
65+ description : 'Free tier monthly usage plan' ,
66+ throttle : { rateLimit : 5 } ,
67+ quota : {
68+ limit : 10000 ,
69+ period : apigateway . Period . MONTH ,
70+ } ,
71+ } ) ;
72+ plan . addApiStage ( {
73+ stage : api . deploymentStage ,
74+ throttle : [
75+ {
76+ method : getToysMethod ,
77+ throttle : {
78+ rateLimit : 10 ,
79+ burstLimit : 2 ,
80+ } ,
81+ } ,
82+ ] ,
83+ } ) ;
84+ }
85+ }
86+
87+ const app = new cdk . App ( ) ;
88+
89+ const testCase = new Test ( app , 'test-apigateway-spec-restapi' ) ;
90+ new IntegTest ( app , 'apigateway-spec-restapi' , {
91+ testCases : [ testCase ] ,
92+ } ) ;
0 commit comments