-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
366 lines (336 loc) · 10.7 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"use strict";
const Swagger = require("swagger-client");
const params = require("./src/params");
const jsYaml = require("js-yaml");
const utils = require("./src/utils");
const Router = require("./src/router").Router;
const fs = require("fs")
var resolver = ((container, path) => {
if (typeof container.controllerMap === "undefined") {
let p = process.cwd() + `/test/controllers/${path}`;
return require(p)
}
else {
let cls = `./${path}.js`;
return container.controllerMap(cls);
}
})
let container = {
controllers: "./controllers",
controllerResolver: resolver
};
module.exports.cacheWith = (cache) => {
const existing = container;
utils.cacheWith(cache);
container = cache;
if (existing.controllerResolver) {
container.controllerResolver = existing.controllerResolver;
}
if (existing.controllers) {
container.controllers = existing.controllers;
}
if (existing.requestInterceptor) {
container.requestInterceptor = existing.requestInterceptor;
}
if (existing.responseInterceptor) {
container.responseInterceptor = existing.responseInterceptor;
}
if (existing.extraHeaders) {
container.extraHeaders = existing.extraHeaders;
}
container.Swambda = Swambda;
container.respondWith = utils.respondWith;
};
module.exports.fromCache = () => {
return new Promise((resolve, reject) => {
if (container.swambda) {
resolve(container.swambda);
}
else {
reject();
}
});
};
const Swambda = module.exports.Swambda = class Swambda {
constructor(prefix) {
this.prefix = prefix;
container.respondWith = utils.respondWith;
container.requestInterceptor = function (event, args) {
return new Promise((resolve, reject) => {
resolve(args);
});
};
container.responseInterceptor = function (response) {
return new Promise((resolve, reject) => {
resolve(response);
});
};
return this;
}
};
Swambda.prototype.spec = function (specLocator) {
this.specLocator = specLocator;
return this;
};
Swambda.prototype.pathPrefix = function (prefix) {
this.prefix = prefix;
return this;
};
Swambda.prototype.preProcessor = function (preProcessor) {
container.requestInterceptor = preProcessor;
return this;
};
Swambda.prototype.postProcessor = function (postProcessor) {
container.responseInterceptor = postProcessor;
return this;
};
Swambda.prototype.controllerMap = function (controllerMap) {
container.controllerMap = controllerMap;
return this;
}
Swambda.prototype.controllerDir = function (controllerDir) {
container.controllers = controllerDir;
return this;
};
Swambda.prototype.cors = function (extra) {
const headers = extra || {};
if (!headers["Access-Control-Allow-Origin"]) {
headers["Access-Control-Allow-Origin"] = "*";
}
if (!headers["Access-Control-Allow-Methods"]) {
headers["Access-Control-Allow-Methods"] = "GET, POST, DELETE, PUT, OPTIONS";
}
if (!headers["Access-Control-Allow-Headers"]) {
headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization";
}
container.extraHeaders = headers;
return this;
};
Swambda.prototype.load = function (identifier) {
return new Promise((resolve, reject) => {
if (container.swambda) {
resolve(container.swambda);
return;
}
const self = this;
resolveSpec(identifier)
.then(res => {
self.spec = res.spec;
if (self.spec.swagger) {
self.spec.basePath = this.prefix;
}
else {
self.spec.servers = [{
url: this.prefix
}];
}
const router = new Router();
router.compilePaths(self.spec.paths);
self.router = router;
container.swambda = self;
resolve(self);
})
.catch((err) => {
reject(err);
})
});
};
var resolveSpec = function (identifier) {
// passed a spec object
return new Promise((resolve, reject) => {
if (typeof identifier === "object") {
Swagger.resolve({
spec: identifier,
allowMetaPatches: false
}).then((res) => {
resolve(res);
}).catch((err) => {
reject(err)
});
return;
}
// passed a URL
if (identifier.indexOf("http://") === 0 || identifier.indexOf("https://") === 0) {
Swagger.resolve({
allowMetaPatches: false,
url: identifier
}).then((res) => {
resolve(res);
}).catch((err) => {
reject(err)
});
return;
}
// passed a file location
require("fs").readFile(identifier, "utf-8", (err, contents) => {
if (err) {
reject(err);
}
else {
let spec;
try {
const spec = jsYaml.safeLoad(contents, 'utf8');
Swagger.resolve({
spec: spec,
allowMetaPatches: false
}).then((res) => resolve(res));
}
catch (e) {
reject(e);
}
}
});
});
}
Swambda.prototype.process = function (event) {
return new Promise((resolve, reject) => {
if (!event || !event.path) {
container.respondWith(resolve, 500, "invalid request sent");
return;
}
let path = event.path;
let basePath;
if (this.spec.swagger) {
basePath = this.spec.basePath;
}
else {
basePath = this.spec.servers[0].url;
}
path = path.substring(path.indexOf(basePath) + basePath.length);
const httpMethod = event.httpMethod.toLowerCase();
if (httpMethod === "options") {
container.respondWith(resolve, 200, "");
return;
}
if (!path) {
container.respondWith(resolve, 404, {
code: 404,
message: "invalid path"
});
return;
}
if (path === "/swagger.json") {
container.respondWith(resolve, 200, this.spec);
return;
}
if (path === "/openapi.json") {
container.respondWith(resolve, 200, this.spec);
return;
}
if (path === "/openapi.yaml") {
let yaml = jsYaml.dump(this.spec);
container.respondWith(resolve, 200, yaml);
return;
}
const route = this.router.lookup(path);
if (!route) {
container.respondWith(resolve, 404, {
code: 404,
message: `path ${path} not found`
});
return;
}
const operation = route.value[httpMethod];
if (!operation) {
container.respondWith(resolve, 404, {
code: 404,
message: `operation '${httpMethod}' not found`
});
return;
}
const operationParams = {};
const args = {};
const controller = operation["x-swagger-router-controller"];
if (!controller) {
container.respondWith(resolve, 404, {
code: 404,
message: "controller `" + controller + "` not found"
});
return;
}
const operationId = operation.operationId;
if (!operationId) {
container.respondWith(resolve, 404, {
code: 404,
message: "operationId `" + operationId + "` not found"
});
return;
}
(operation.parameters || [])
.map((param) => {
if (param.in === "path") {
const value = params.extract(param, route.params);
if (typeof value !== "undefined") {
args[param.name] = value;
}
}
if (param.in === "body") {
const value = parseBody(event.headers, event.body);
if (typeof value !== "undefined") {
args[param.name] = value;
}
}
if (param.in === "query") {
const value = params.extract(param, event.queryStringParameters);
if (typeof value !== "undefined") {
args[param.name] = value;
}
}
if (param.in === "header") {
const value = params.extract(param, event.headers);
if (typeof value !== "undefined") {
args[param.name] = value;
}
}
operationParams[param.name] = param;
});
if (operation.requestBody && operation.requestBody.content) {
const requestBodyJson = operation.requestBody.content["application/json"];
if(typeof requestBodyJson !== "undefined") {
try {
args["body"] = JSON.parse(event.body);
}
catch (e) {
args["body"] = event.body;
}
}
}
var cls = container.controllerResolver(container, controller);
if (!cls || typeof cls[operationId] !== "function") {
container.respondWith(resolve, 404, {
code: 404,
message: "operation not found"
});
return;
}
container.requestInterceptor(event, args)
.then((result) => {
cls[operationId](args)
.then(response => {
resolve(response);
})
.catch(err => {
container.respondWith(resolve, 500, {
code: 500,
message: "bad operation"
});
});
});
});
};
module.exports.prettyPrint = (error, value) => {
console.log(JSON.stringify(value, null, 2));
};
const parseBody = (headers, body) => {
if (typeof body === "undefined") {
return;
}
if (headers && headers["content-type"]) {
try {
return JSON.parse(body);
}
catch (e) {
return body;
}
}
};