This repository has been archived by the owner on Jul 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
documentation.js
255 lines (222 loc) · 8.58 KB
/
documentation.js
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
'use strict';
const objectHash = require('object-hash');
const globalDocumentationParts = require('./globalDocumentationParts.json');
const functionDocumentationParts = require('./functionDocumentationParts.json');
function getDocumentationProperties(def, propertiesToGet) {
const docProperties = new Map();
propertiesToGet.forEach((key) => {
if (def[key]) {
docProperties.set(key, def[key]);
}
});
return docProperties;
}
function _mapToObj(map) {
const returnObj = {};
map.forEach((val, key) => {
returnObj[key] = val;
});
return returnObj;
}
/*
* Different types support different extra properties beyond
* the basic ones, so we need to make sure we only look for
* the appropriate properties.
*/
function determinePropertiesToGet (type) {
const defaultProperties = ['description', 'summary']
let result = defaultProperties
switch (type) {
case 'API':
result.push('tags', 'info')
break
case 'METHOD':
result.push('tags')
break
}
return result
}
var autoVersion;
module.exports = function() {
return {
_createDocumentationPart: function _createDocumentationPart(part, def, knownLocation) {
const location = part.locationProps.reduce((loc, property) => {
loc[property] = knownLocation[property] || def[property];
return loc;
}, {});
location.type = part.type;
const propertiesToGet = determinePropertiesToGet(location.type)
const props = getDocumentationProperties(def, propertiesToGet);
if (props.size > 0) {
this.documentationParts.push({
location,
properties: _mapToObj(props),
restApiId: this.restApiId,
});
}
if (part.children) {
this.createDocumentationParts(part.children, def, location);
}
},
createDocumentationPart: function createDocumentationPart(part, def, knownLocation) {
if (part.isList) {
if (!(def instanceof Array)) {
const msg = `definition for type "${part.type}" is not an array`;
console.info('-------------------');
console.info(msg);
throw new Error(msg);
}
def.forEach((singleDef) => this._createDocumentationPart(part, singleDef, knownLocation));
} else {
this._createDocumentationPart(part, def, knownLocation);
}
},
createDocumentationParts: function createDocumentationParts(parts, def, knownLocation) {
Object.keys(parts).forEach((part) => {
if (def[part]) {
this.createDocumentationPart(parts[part], def[part], knownLocation);
}
});
},
_updateDocumentation: function _updateDocumentation() {
const aws = this.serverless.providers.aws;
return aws.request('APIGateway', 'getDocumentationVersion', {
restApiId: this.restApiId,
documentationVersion: this.getDocumentationVersion(),
}).then(() => {
const msg = 'documentation version already exists, skipping upload';
console.info('-------------------');
console.info(msg);
return Promise.reject(msg);
}, err => {
if (err.message === 'Invalid Documentation version specified') {
return Promise.resolve();
}
return Promise.reject(err);
})
.then(() =>
aws.request('APIGateway', 'getDocumentationParts', {
restApiId: this.restApiId,
limit: 9999,
})
)
.then(results => results.items.map(
part => aws.request('APIGateway', 'deleteDocumentationPart', {
documentationPartId: part.id,
restApiId: this.restApiId,
})
))
.then(promises => Promise.all(promises))
.then(() => this.documentationParts.reduce((promise, part) => {
return promise.then(() => {
part.properties = JSON.stringify(part.properties);
return aws.request('APIGateway', 'createDocumentationPart', part);
});
}, Promise.resolve()))
.then(() => aws.request('APIGateway', 'createDocumentationVersion', {
restApiId: this.restApiId,
documentationVersion: this.getDocumentationVersion(),
stageName: this.options.stage,
}));
},
getGlobalDocumentationParts: function getGlobalDocumentationParts() {
const globalDocumentation = this.customVars.documentation;
this.createDocumentationParts(globalDocumentationParts, globalDocumentation, {});
},
getFunctionDocumentationParts: function getFunctionDocumentationParts() {
const httpEvents = this._getHttpEvents();
Object.keys(httpEvents).forEach(funcNameAndPath => {
const httpEvent = httpEvents[funcNameAndPath];
const path = httpEvent.path;
const method = httpEvent.method.toUpperCase();
this.createDocumentationParts(functionDocumentationParts, httpEvent, { path, method });
});
},
_getHttpEvents: function _getHttpEvents() {
return this.serverless.service.getAllFunctions().reduce((documentationObj, functionName) => {
const func = this.serverless.service.getFunction(functionName);
func.events
.filter((eventTypes) => eventTypes.http && eventTypes.http.documentation)
.map((eventTypes) => eventTypes.http)
.forEach(currEvent => {
let key = functionName + currEvent.method + currEvent.path;
documentationObj[key] = currEvent;
});
return documentationObj;
}, {});
},
generateAutoDocumentationVersion: function generateAutoDocumentationVersion() {
const versionObject = {
globalDocs: this.customVars.documentation,
functionDocs: {},
}
const httpEvents = this._getHttpEvents();
Object.keys(httpEvents).forEach(funcName => {
versionObject.functionDocs[funcName] = httpEvents[funcName].documentation;
});
autoVersion = objectHash(versionObject);
return autoVersion;
},
getDocumentationVersion: function getDocumentationVersion() {
return this.customVars.documentation.version || autoVersion || this.generateAutoDocumentationVersion();
},
_buildDocumentation: function _buildDocumentation(result) {
this.restApiId = result.Stacks[0].Outputs
.filter(output => output.OutputKey === 'AwsDocApiId')
.map(output => output.OutputValue)[0];
this.getGlobalDocumentationParts();
this.getFunctionDocumentationParts();
if (this.options.noDeploy) {
console.info('-------------------');
console.info('documentation parts:');
console.info(this.documentationParts);
return;
}
return this._updateDocumentation();
},
addDocumentationToApiGateway: function addDocumentationToApiGateway(resource, documentationPart, mapPath) {
if (documentationPart && Object.keys(documentationPart).length > 0) {
if (!resource.Properties.RequestParameters) {
resource.Properties.RequestParameters = {};
}
documentationPart.forEach(function(qp) {
const source = `method.request.${mapPath}.${qp.name}`;
if (resource.Properties.RequestParameters.hasOwnProperty(source)) return; // don't mess with existing config
resource.Properties.RequestParameters[source] = qp.required || false;
});
}
},
updateCfTemplateFromHttp: function updateCfTemplateFromHttp(eventTypes) {
if (eventTypes.http && eventTypes.http.documentation) {
const resourceName = this.normalizePath(eventTypes.http.path);
const methodLogicalId = this.getMethodLogicalId(resourceName, eventTypes.http.method);
const resource = this.cfTemplate.Resources[methodLogicalId];
resource.DependsOn = new Set();
this.addMethodResponses(resource, eventTypes.http.documentation);
this.addRequestModels(resource, eventTypes.http.documentation);
if (!this.options['doc-safe-mode']) {
this.addDocumentationToApiGateway(
resource,
eventTypes.http.documentation.requestHeaders,
'header'
);
this.addDocumentationToApiGateway(
resource,
eventTypes.http.documentation.queryParams,
'querystring'
);
this.addDocumentationToApiGateway(
resource,
eventTypes.http.documentation.pathParams,
'path'
);
}
resource.DependsOn = Array.from(resource.DependsOn);
if (resource.DependsOn.length === 0) {
delete resource.DependsOn;
}
}
},
_getDocumentationProperties: getDocumentationProperties
};
};