forked from apigee-127/swagger-node-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
333 lines (270 loc) · 8.75 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
'use strict';
/*
Runner properties:
config
swagger
api // (sway)
connectMiddleware()
resolveAppPath()
securityHandlers
bagpipes
Runner events:
responseValidationError
config properties:
appRoot
mockMode
configDir
controllersDirs
mockControllersDirs
securityHandlers
*/
module.exports = {
create: create
};
var _ = require('lodash');
var yaml = require('js-yaml');
var path = require('path');
var sway = require('sway');
var debug = require('debug')('swagger');
var bagpipes = require('bagpipes');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var SWAGGER_SELECTED_PIPE = 'x-swagger-pipe';
var SWAGGER_ROUTER_CONTROLLER = 'x-swagger-router-controller';
var DEFAULT_FITTINGS_DIRS = [ 'api/fittings' ];
var DEFAULT_VIEWS_DIRS = [ 'api/views' ];
var DEFAULT_SWAGGER_FILE = 'api/swagger/swagger.yaml'; // relative to appRoot
/*
SwaggerNode config priority:
1. swagger_* environment vars
2. config passed to create()
3. read from swagger node in default.yaml in config directory
4. defaults in this file
*/
function create(config, cb) {
if (!_.isFunction(cb)) { throw new Error('callback is required'); }
if (!config || !config.appRoot) { return cb(new Error('config.appRoot is required')); }
new Runner(config, cb);
}
util.inherits(Runner, EventEmitter);
function Runner(appJsConfig, cb) {
EventEmitter.call(this);
this.resolveAppPath = function resolveAppPath(to) {
return path.resolve(appJsConfig.appRoot, to);
};
this.connectMiddleware = function connectMiddleware() {
return require('./lib/connect_middleware')(this);
};
this.expressMiddleware = this.connectMiddleware;
this.restifyMiddleware = function restifyMiddleware() {
return require('./lib/restify_middleware')(this);
};
this.sailsMiddleware = function sailsMiddleware() {
return require('./lib/sails_middleware')(this);
};
this.hapiMiddleware = function hapiMiddleware() {
return require('./lib/hapi_middleware')(this);
};
this.defaultErrorHandler = function() {
return this.bagpipes.createPipeFromFitting(defaultErrorFitting, { name: 'defaultErrorHandler' });
function defaultErrorFitting(context, next) {
debug('default error handler: %s', context.error.message);
next();
}
};
this.getOperation = function getOperation(req) {
return this.api.getOperation(req);
};
this.getPath = function getPath(req) {
return this.api.getPath(req);
};
// adds req.swagger to the request
this.applyMetadata = function applyMetadata(req, operation, cb) {
var swagger = req.swagger = {};
swagger.operation = operation;
cb();
};
// must assign req.swagger (see #applyMetadata) before calling
this.getPipe = function getPipe(req) {
var operation = req.swagger.operation;
var path = operation ? operation.pathObject : this.getPath(req);
var config = this.config.swagger;
// prefer explicit pipe
var pipeName;
if (operation) {
pipeName = operation[SWAGGER_SELECTED_PIPE];
}
if (!pipeName) {
pipeName = path[SWAGGER_SELECTED_PIPE];
}
// no explicit pipe, but there's a controller
if (!pipeName) {
if ((operation && operation[SWAGGER_ROUTER_CONTROLLER]) || path[SWAGGER_ROUTER_CONTROLLER])
{
pipeName = config.swaggerControllerPipe;
}
}
debug('pipe requested:', pipeName);
// default pipe
if (!pipeName) { pipeName = config.defaultPipe; }
if (!pipeName) {
debug('no default pipe');
return null;
}
var pipe = this.bagpipes.pipes[pipeName];
if (!pipe) {
debug('no defined pipe: ', pipeName);
return null;
}
debug('executing pipe %s', pipeName);
return pipe;
};
// don't override if env var already set
if (!process.env.NODE_CONFIG_DIR) {
if (!appJsConfig.configDir) { appJsConfig.configDir = 'config'; }
process.env.NODE_CONFIG_DIR = path.resolve(appJsConfig.appRoot, appJsConfig.configDir);
}
var Config = require('config');
var swaggerConfigDefaults = {
enforceUniqueOperationId: false,
startWithErrors: false,
startWithWarnings: true
};
this.config = Config.util.cloneDeep(Config);
this.config.swagger =
Config.util.extendDeep(
swaggerConfigDefaults,
this.config.swagger,
appJsConfig,
readEnvConfig());
debug('resolved config: %j', this.config);
var self = this;
var swayOpts = {
definition: appJsConfig.swagger || appJsConfig.swaggerFile || this.resolveAppPath(DEFAULT_SWAGGER_FILE)
};
debug('initializing Sway');
// sway uses Promises
sway.create(swayOpts)
.then(function(api) {
debug('validating api');
var validateResult = api.validate();
debug('done validating api. errors: %d, warnings: %d', validateResult.errors.length, validateResult.warnings.length);
var errors = validateResult.errors;
if (errors && errors.length > 0) {
if (!self.config.swagger.enforceUniqueOperationId) {
errors = errors.filter(function(err) {
return (err.code !== 'DUPLICATE_OPERATIONID');
});
}
if (errors.length > 0) {
if (self.config.swagger.startWithErrors) {
var errorText = JSON.stringify(errors);
console.error(errorText, 2);
} else {
var err = new Error('Swagger validation errors:');
err.validationErrors = errors;
throw err;
}
}
}
var warnings = validateResult.warnings;
if (warnings && warnings.length > 0) {
var warningText = JSON.stringify(warnings);
if (self.config.swagger.startWithWarnings) {
console.error(warningText, 2);
} else {
var err = new Error('Swagger validation warnings:');
err.validationWarnings = warnings;
throw err;
}
}
self.api = api;
self.swagger = api.definition;
self.securityHandlers = appJsConfig.securityHandlers || appJsConfig.swaggerSecurityHandlers; // legacy name
self.bagpipes = createPipes(self);
cb(null, self);
})
.catch(function(err) {
cb(err);
})
.catch(function(err) {
console.error('Error in callback! Tossing to global error handler.', err.stack);
if (err.validationErrors) {
console.error('Details: ');
for (var i= 0; i<err.validationErrors.length; i++) {
console.error("\t#" + i + ".: " + err.validationErrors[i].message + " in swagger config at: >" + err.validationErrors[i].path.join('/') + "<");
}
}
process.nextTick(function() { throw err; });
})
}
function createPipes(self) {
var config = self.config.swagger;
var fittingsDirs = (config.fittingsDirs || DEFAULT_FITTINGS_DIRS).map(function(dir) {
return path.resolve(config.appRoot, dir);
});
var swaggerNodeFittingsDir = path.resolve(__dirname, './fittings');
fittingsDirs.push(swaggerNodeFittingsDir);
var viewsDirs = (config.viewsDirs || DEFAULT_VIEWS_DIRS).map(function(dir) {
return path.resolve(config.appRoot, dir);
});
// legacy support: set up a default piping for traditional swagger-node if nothing is specified
if (!config.bagpipes || config.bagpipes ==='DEFAULTS_TEST') {
debug('**** No bagpipes defined in config. Using default setup. ****');
config.swaggerControllerPipe = 'swagger_controllers';
config.bagpipes = {
_router: {
name: 'swagger_router',
mockMode: false,
mockControllersDirs: [ 'api/mocks' ],
controllersDirs: [ 'api/controllers' ]
},
_swagger_validate: {
name: 'swagger_validator',
validateReponse: true
},
swagger_controllers: [
'cors',
'swagger_params_parser',
'swagger_security',
'_swagger_validate',
'express_compatibility',
'_router'
]
};
if (config.mapErrorsToJson) {
config.bagpipes.swagger_controllers.unshift({ onError: 'json_error_handler' });
}
}
var pipesDefs = config.bagpipes;
var pipesConfig = {
userFittingsDirs: fittingsDirs,
userViewsDirs: viewsDirs,
swaggerNodeRunner: self
};
return bagpipes.create(pipesDefs, pipesConfig);
}
function readEnvConfig() {
var config = {};
_.each(process.env, function(value, key) {
var split = key.split('_');
if (split[0] === 'swagger') {
var configItem = config;
for (var i = 1; i < split.length; i++) {
var subKey = split[i];
if (i < split.length - 1) {
if (!configItem[subKey]) { configItem[subKey] = {}; }
configItem = configItem[subKey];
} else {
try {
configItem[subKey] = JSON.parse(value);
} catch (err) {
configItem[subKey] = value;
}
}
}
}
});
debug('loaded env vars: %j', config);
return config;
}