This repository has been archived by the owner on Apr 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
server.js
112 lines (97 loc) · 2.92 KB
/
server.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
'use strict';
module.exports = SwaggerServer;
var _ = require('lodash'),
express = require('express'),
EventEmitter = require('events').EventEmitter,
SwaggerMiddleware = require('swagger-express-middleware'),
swaggerMethods = require('swagger-methods'),
application = require('./application'),
SwaggerParser = require('./parser'),
Handlers = require('./handlers'),
SwaggerWatcher = require('./watcher');
/**
* The Swagger Server class, which wraps an Express Application and extends it.
*
* @param {e.application} [app] An Express Application. If not provided, a new app is created.
* @constructor
* @extends EventEmitter
*/
function SwaggerServer(app) {
app = app || express();
// Event Emitter
_.extend(this, EventEmitter.prototype);
EventEmitter.call(this);
/**
* @type {Middleware}
* @protected
*/
this.__middleware = new SwaggerMiddleware(app);
/**
* @type {SwaggerParser}
* @protected
*/
this.__parser = new SwaggerParser(this);
/**
*
* @type {Handlers|exports|module.exports}
* @private
*/
this.__handlers = new Handlers(this);
/**
* @type {SwaggerWatcher}
* @protected
*/
this.__watcher = new SwaggerWatcher(this);
/**
* The Express Application.
* @type {e.application}
*/
this.app = app;
/**
* The {@link DataStore} object that's used by the mock middleware.
* See https://github.com/BigstickCarpet/swagger-express-middleware/blob/master/docs/exports/DataStore.md
*
* @type {DataStore}
*/
Object.defineProperty(this, 'dataStore', {
configurable: true,
enumerable: true,
get: function() {
return app.get('mock data store');
},
set: function(value) {
app.set('mock data store', value);
}
});
/**
* Parses the given Swagger API.
*
* @param {string|object} swagger
* The file path or URL of a Swagger 2.0 API spec, in YAML or JSON format.
* Or a valid Swagger API object (see https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md#swagger-object).
*
* @param {function} [callback]
* function(err, api, parser)
*/
this.parse = function(swagger, callback) {
this.__parser.parse(swagger);
if (_.isFunction(callback)) {
this.__parser.whenParsed(callback);
}
};
/**
* Adds the Swaggerize Express handlers to the Express Instance
* (see https://github.com/krakenjs/swaggerize-express for info and acceptable formats)
*/
this.addHandlers = function() {
this.__handlers.setupHandlers();
};
// Patch the Express app to support Swagger
application.patch(this);
// For convenience, expose many of the Express app's methods directly on the Swagger Server object
var server = this;
swaggerMethods.concat(['enable', 'disable', 'set', 'listen', 'use', 'route', 'all'])
.forEach(function(method) {
server[method] = app[method].bind(app);
});
}