@@ -35,15 +35,69 @@ function determinePropertiesToGet (type) {
35
35
switch ( type ) {
36
36
case 'API' :
37
37
result . push ( 'tags' , 'info' )
38
- break
38
+ break ;
39
39
case 'METHOD' :
40
40
result . push ( 'tags' )
41
- break
41
+ break ;
42
+ case 'PATH_PARAMETER' :
43
+ case 'QUERY_PARAMETER' :
44
+ case 'REQUEST_HEADER' :
45
+ case 'REQUEST_BODY' :
46
+ result . push ( 'required' )
47
+ break ;
42
48
}
43
49
return result
44
50
45
51
}
46
52
53
+ function mapPathLogicalPart ( path ) {
54
+ return path . split ( '/' ) . map ( ( x ) => {
55
+ if ( x . startsWith ( '{' ) && x . endsWith ( '}' ) )
56
+ return x . slice ( 1 , x . length - 1 ) ;
57
+ return x [ 0 ] . toUpperCase ( ) + x . slice ( 1 ) ;
58
+ } ) . join ( '' )
59
+ }
60
+
61
+ function mapStringToSafeHex ( string ) {
62
+ return string . split ( ) . map ( ( x ) => x . charCodeAt ( 0 ) . toString ( 16 ) ) . join ( '' ) ;
63
+ }
64
+
65
+ function logicalIdCompatible ( text ) {
66
+ const alphanumericRegex = / [ ^ A - Z a - z 0 - 9 ] / g;
67
+ return text . replace ( alphanumericRegex , mapStringToSafeHex ) ;
68
+ }
69
+
70
+ function logicalIdForPart ( location ) {
71
+ switch ( location . type ) {
72
+ case 'API' :
73
+ return 'RestApiDocPart' ;
74
+ case 'RESOURCE' :
75
+ return mapPathLogicalPart ( location . path ) + 'ResourceDocPart' ;
76
+ case 'METHOD' :
77
+ return mapPathLogicalPart ( location . path ) + location . method + 'MethodDocPart' ;
78
+ case 'QUERY_PARAMETER' :
79
+ return mapPathLogicalPart ( location . path ) + location . method + logicalIdCompatible ( location . name ) + 'QueryParamDocPart' ;
80
+ case 'REQUEST_BODY' :
81
+ return mapPathLogicalPart ( location . path ) + location . method + 'ReqBodyDocPart' ;
82
+ case 'REQUEST_HEADER' :
83
+ return mapPathLogicalPart ( location . path ) + location . method + logicalIdCompatible ( location . name ) + 'ReqHeadDocPart' ;
84
+ case 'PATH_PARAMETER' :
85
+ return mapPathLogicalPart ( location . path ) + location . method + logicalIdCompatible ( location . name ) + 'PathParamDocPart' ;
86
+ case 'RESPONSE' :
87
+ return mapPathLogicalPart ( location . path ) + location . method + location . statusCode + 'ResDocPart' ;
88
+ case 'RESPONSE_HEADER' :
89
+ return mapPathLogicalPart ( location . path ) + location . method + logicalIdCompatible ( location . name ) + location . statusCode + 'ResHeadDocPart' ;
90
+ case 'RESPONSE_BODY' :
91
+ return mapPathLogicalPart ( location . path ) + location . method + location . statusCode + 'ResBodyDocPart' ;
92
+ case 'AUTHORIZER' :
93
+ return logicalIdCompatible ( location . name ) + 'AuthorizerDocPart' ;
94
+ case 'MODEL' :
95
+ return logicalIdCompatible ( location . name ) + 'ModelDocPart' ;
96
+ default :
97
+ throw new Error ( 'Unknown location type ' + location . type ) ;
98
+ }
99
+ }
100
+
47
101
var autoVersion ;
48
102
49
103
module . exports = function ( ) {
@@ -110,25 +164,6 @@ module.exports = function() {
110
164
111
165
return Promise . reject ( err ) ;
112
166
} )
113
- . then ( ( ) =>
114
- aws . request ( 'APIGateway' , 'getDocumentationParts' , {
115
- restApiId : this . restApiId ,
116
- limit : 9999 ,
117
- } )
118
- )
119
- . then ( results => results . items . map (
120
- part => aws . request ( 'APIGateway' , 'deleteDocumentationPart' , {
121
- documentationPartId : part . id ,
122
- restApiId : this . restApiId ,
123
- } )
124
- ) )
125
- . then ( promises => Promise . all ( promises ) )
126
- . then ( ( ) => this . documentationParts . reduce ( ( promise , part ) => {
127
- return promise . then ( ( ) => {
128
- part . properties = JSON . stringify ( part . properties ) ;
129
- return aws . request ( 'APIGateway' , 'createDocumentationPart' , part ) ;
130
- } ) ;
131
- } , Promise . resolve ( ) ) )
132
167
. then ( ( ) => aws . request ( 'APIGateway' , 'createDocumentationVersion' , {
133
168
restApiId : this . restApiId ,
134
169
documentationVersion : this . getDocumentationVersion ( ) ,
@@ -190,6 +225,12 @@ module.exports = function() {
190
225
. filter ( output => output . OutputKey === 'AwsDocApiId' )
191
226
. map ( output => output . OutputValue ) [ 0 ] ;
192
227
228
+ return this . _updateDocumentation ( ) ;
229
+ } ,
230
+
231
+ updateCfTemplateWithEndpoints : function updateCfTemplateWithEndpoints ( restApiId ) {
232
+ this . restApiId = restApiId ;
233
+
193
234
this . getGlobalDocumentationParts ( ) ;
194
235
this . getFunctionDocumentationParts ( ) ;
195
236
@@ -200,7 +241,25 @@ module.exports = function() {
200
241
return ;
201
242
}
202
243
203
- return this . _updateDocumentation ( ) ;
244
+ const documentationPartResources = this . documentationParts . reduce ( ( docParts , docPart ) => {
245
+ docParts [ logicalIdForPart ( docPart . location ) ] = {
246
+ Type : 'AWS::ApiGateway::DocumentationPart' ,
247
+ Properties : {
248
+ Location : {
249
+ Type : docPart . location . type ,
250
+ Name : docPart . location . name ,
251
+ Path : docPart . location . path ,
252
+ StatusCode : docPart . location . statusCode ,
253
+ Method : docPart . location . method ,
254
+ } ,
255
+ Properties : JSON . stringify ( docPart . properties ) ,
256
+ RestApiId : docPart . restApiId ,
257
+ }
258
+ } ;
259
+ return docParts ;
260
+ } , { } ) ;
261
+
262
+ Object . assign ( this . cfTemplate . Resources , documentationPartResources ) ;
204
263
} ,
205
264
206
265
addDocumentationToApiGateway : function addDocumentationToApiGateway ( resource , documentationPart , mapPath ) {
0 commit comments