This repository has been archived by the owner on May 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
231 lines (204 loc) · 9.65 KB
/
index.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
var composites = require('composites');
var Program = composites.Program;
var Statement = composites.Statement;
var logPrefix = require('./package.json').name + ': ';
module.exports = buildApiService;
function buildApiService(apiDoc, options) {
var apiFunctionDeclaration = new Program;
var apiFunctionBody = new Program;
var securityDefinitions = apiDoc.securityDefinitions;
var requiredSecurityHandlers = [];
var hasSecurity = apiDoc.security || function() {
var paths = apiDoc.paths;
var pathUris = (paths ? Object.keys(paths) : []);
for (var i = 0, ilen = pathUris.length; i < ilen; i++) {
var pathUri = pathUris[i];
var path = paths[pathUri];
var operations = (path ? Object.keys(path) : []);
for (var j = 0, jlen = operations.length; j < jlen; j++) {
var operation = path[operations[i]];
if (operation && Array.isArray(operation.security)) {
return true;
}
}
}
return false;
}();
if (apiDoc.security) {
addToRequiredSecurityHandlers(requiredSecurityHandlers, apiDoc.security);
}
options = options || {preset: 'node'};
if (options.preset === 'node') {
apiFunctionDeclaration.push('\'use strict\';');
apiFunctionDeclaration.push('module.exports = createApi;');
} else if (options.preset === 'es6') {
apiFunctionDeclaration.push('export default createApi;');
} else {
throw new Error(logPrefix + 'Unknown preset "' + options.preset + '"');
}
apiFunctionDeclaration.push('function createApi(options) {');
apiFunctionDeclaration.push(' const basePath = \'' + apiDoc.basePath + '\';');
apiFunctionDeclaration.push(
' const endpoint = options.endpoint || \'' + getEndpoint(apiDoc) + '\';');
apiFunctionDeclaration.push(' const cors = !!options.cors;');
apiFunctionDeclaration.push(' const mode = cors ? \'cors\' : \'basic\';');
if (hasSecurity) {
apiFunctionDeclaration.push(' const securityHandlers = options.securityHandlers || {};');
apiFunctionDeclaration.push(' const handleSecurity = (security, headers, params, operationId) => {');
apiFunctionDeclaration.push(' for (let i = 0, ilen = security.length; i < ilen; i++) {');
apiFunctionDeclaration.push(' let scheme = security[i];');
apiFunctionDeclaration.push(' let schemeParts = Object.keys(scheme);');
apiFunctionDeclaration.push(' for (let j = 0, jlen = schemeParts.length; j < jlen; j++) {');
apiFunctionDeclaration.push(' let schemePart = schemeParts[j];');
apiFunctionDeclaration.push(' let fulfilsSecurityRequirements = securityHandlers[schemePart](');
apiFunctionDeclaration.push(' headers, params, schemePart);');
apiFunctionDeclaration.push(' if (fulfilsSecurityRequirements) {');
apiFunctionDeclaration.push(' return;');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push('');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push(' throw new Error(\'No security scheme was fulfilled by the provided securityHandlers for operation \' + operationId);');
apiFunctionDeclaration.push(' };');
apiFunctionDeclaration.push(' const ensureRequiredSecurityHandlersExist = () => {');
apiFunctionDeclaration.push(' let requiredSecurityHandlers = [', requiredSecurityHandlers, '];');
apiFunctionDeclaration.push(' for (let i = 0, ilen = requiredSecurityHandlers.length; i < ilen; i++) {');
apiFunctionDeclaration.push(' let requiredSecurityHandler = requiredSecurityHandlers[i];');
apiFunctionDeclaration.push(' if (typeof securityHandlers[requiredSecurityHandler] !== \'function\') {');
apiFunctionDeclaration.push(' throw new Error(\'Expected to see a security handler for scheme "\' +');
apiFunctionDeclaration.push(' requiredSecurityHandler + \'" in options.securityHandlers\');');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push(' };');
apiFunctionDeclaration.push(' ensureRequiredSecurityHandlersExist();');
}
apiFunctionDeclaration.push(' const buildQuery = (obj) => {');
apiFunctionDeclaration.push(' return Object.keys(obj)');
apiFunctionDeclaration.push(' .filter(key => typeof obj[key] !== \'undefined\')');
apiFunctionDeclaration.push(' .map((key) => {');
apiFunctionDeclaration.push(' const value = obj[key];');
apiFunctionDeclaration.push(' if (value === undefined) {');
apiFunctionDeclaration.push(' return \'\';');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push(' if (value === null) {');
apiFunctionDeclaration.push(' return key;');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push(' if (Array.isArray(value)) {');
apiFunctionDeclaration.push(' if (value.length) {');
apiFunctionDeclaration.push(' return key + \'=\' + value.map(encodeURIComponent).join(\'&\' + key + \'=\');');
apiFunctionDeclaration.push(' } else {');
apiFunctionDeclaration.push(' return \'\';');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push(' } else {');
apiFunctionDeclaration.push(' return key + \'=\' + encodeURIComponent(value);');
apiFunctionDeclaration.push(' }');
apiFunctionDeclaration.push(' }).join(\'&\');');
apiFunctionDeclaration.push(' };');
apiFunctionDeclaration.push(' return {');
apiFunctionDeclaration.push(apiFunctionBody);
apiFunctionDeclaration.push(' };');
apiFunctionDeclaration.push('}');
generatePaths(apiDoc, options, apiFunctionBody, requiredSecurityHandlers);
return apiFunctionDeclaration.toString();
}
function generatePaths(apiDoc, options, apiFunctionBody, requiredSecurityHandlers) {
var paths = apiDoc.paths;
Object.keys(paths).forEach(function(path) {
var methods = paths[path];
path = path.replace(/\{([^}]+)\}/g, '\' + params[\'$1\'] + \'');
Object.keys(methods).forEach(function(method) {
if (method === 'parameters' || method === '$ref') {
return;
}
var methodDoc = methods[method];
var security = methodDoc.security || apiDoc.security;
addToRequiredSecurityHandlers(requiredSecurityHandlers, security);
if (Array.isArray(methodDoc.parameters)) {
var queryParams = methodDoc.parameters.filter(byQuery);
var bodyParams = methodDoc.parameters.filter(byBodyParams);
var headerParams = methodDoc.parameters.filter(byHeaders);
}
var body = new Program;
var headers = new Program;
var query = new Program;
apiFunctionBody.push(' ' + methodDoc.operationId + '(parameters) {');
apiFunctionBody.push(' const params = typeof parameters === \'undefined\' ? {} : parameters;');
apiFunctionBody.push(' let headers = {');
apiFunctionBody.push(headers);
apiFunctionBody.push(' };');
if (security && security.length) {
apiFunctionBody.push(' handleSecurity(', JSON.stringify(security));
apiFunctionBody.push(' , headers, params, \'', methodDoc.operationId, '\');');
}
apiFunctionBody.push(' return fetch(endpoint + basePath + \'' + path + '\'', query);
if (queryParams && queryParams.length) {
query.push(' + \'?\' + buildQuery({');
queryParams.forEach(function(param) {
query.push(' \'', param.name, '\': params[\'', param.name, '\'],');
});
query.push(' })');
}
apiFunctionBody.push(' , {');
apiFunctionBody.push(' method: \'', method.toUpperCase(), '\',');
apiFunctionBody.push(' headers,');
apiFunctionBody.push(' mode,');
if (bodyParams && bodyParams.length) {
if (bodyParams[0].in === 'formData') {
headers.push(' \'content-type\': \'application/x-www-form-urlencoded\',');
body.push(' body: buildQuery({');
bodyParams.filter(byFormData).forEach(function(param) {
body.push(' \'', param.name, '\': params[\'', param.name, '\'],');
});
body.push(' }),');
} else {
headers.push(' \'content-type\': \'application/json\',');
body.push(' body: JSON.stringify(params[\'' + bodyParams[0].name + '\']),');
}
apiFunctionBody.push(body);
}
if (headerParams && headerParams.length || bodyParams && bodyParams.length) {
headerParams.forEach(function(param) {
headers.push(' \'', param.name, '\': params[\'', param.name, '\'],');
});
}
apiFunctionBody.push(' });');
apiFunctionBody.push(' },');
});
});
}
function addToRequiredSecurityHandlers(requiredSecurityHandlers, security) {
if (Array.isArray(security)) {
security.forEach(function(scheme) {
Object.keys(scheme).map(toQuoted).forEach(function(quoted) {
if (requiredSecurityHandlers.indexOf(quoted) === -1) {
requiredSecurityHandlers.push(quoted);
}
});
});
requiredSecurityHandlers.sort();
}
}
function byBodyParams(param) {
return param.in === 'body' || param.in === 'formData';
}
function byFormData(param) {
return param.in === 'formData';
}
function byHeaders(param) {
return param.in === 'header';
}
function byQuery(param) {
return param && param.in === 'query';
}
function getEndpoint(apiDoc) {
var scheme = Array.isArray(apiDoc.schemes) ?
apiDoc.schemes[0] :
null;
if (scheme && apiDoc.host) {
return scheme + '://' + apiDoc.host;
}
return '';
}
function toQuoted(val) {
return '\'' + val + '\'';
}