@@ -7,7 +7,7 @@ var fs = require('fs');
7
7
var path = require ( 'path' ) ;
8
8
var doctrine = require ( 'doctrine' ) ;
9
9
var jsYaml = require ( 'js-yaml' ) ;
10
- var swaggerTools = require ( 'swagger-tools ' ) ;
10
+ var parser = require ( 'swagger-parser ' ) ;
11
11
12
12
13
13
/**
@@ -84,18 +84,14 @@ function addDataToSwaggerObject(swaggerObject, swaggerJsDocComments) {
84
84
}
85
85
86
86
87
- // This is the Swagger object that conforms to the Swagger 2.0 specification.
88
- module . exports . swaggerObject = [ ] ;
89
-
90
-
91
87
/**
92
- * Initializes the module. This is intended to be called only once.
88
+ * Generates the swagger spec
93
89
* @function
94
- * @param {object } app - Express application
95
90
* @param {object } options - Configuration options
96
- * @requires swagger-tools
91
+ * @returns {array } Swagger spec
92
+ * @requires swagger-parser
97
93
*/
98
- module . exports . init = function ( app , options ) {
94
+ module . exports = function ( options ) {
99
95
/* istanbul ignore if */
100
96
if ( ! options ) {
101
97
throw new Error ( '\'options\' is required.' ) ;
@@ -106,37 +102,23 @@ module.exports.init = function(app, options) {
106
102
}
107
103
108
104
// Build basic swagger json
109
- module . exports . swaggerObject = options . swaggerDefinition ;
110
- module . exports . swaggerObject . swagger = '2.0' ;
111
- module . exports . swaggerObject . paths = { } ;
105
+ var swaggerObject = [ ] ;
106
+ swaggerObject = options . swaggerDefinition ;
107
+ swaggerObject . swagger = '2.0' ;
108
+ swaggerObject . paths = { } ;
112
109
113
110
// Parse the documentation in the APIs array.
114
111
for ( var i = 0 ; i < options . apis . length ; i = i + 1 ) {
115
112
var jsDocComments = parseApiFile ( options . apis [ i ] ) ;
116
113
var swaggerJsDocComments = filterJsDocComments ( jsDocComments ) ;
117
- addDataToSwaggerObject ( module . exports . swaggerObject , swaggerJsDocComments ) ;
114
+ addDataToSwaggerObject ( swaggerObject , swaggerJsDocComments ) ;
118
115
}
119
116
120
- var swaggerToolsUIOptions = {
121
- apiDocs : options . apiDocs ,
122
- swaggerUi : options . swaggerUi ,
123
- } ;
124
-
125
- // Initialize the Swagger middleware
126
- swaggerTools . initializeMiddleware ( module . exports . swaggerObject ,
127
- function ( middleware ) {
128
- // Interpret Swagger resources and attach metadata to request
129
- // must be first in swagger-tools middleware chain
130
- app . use ( middleware . swaggerMetadata ( ) ) ;
131
-
132
- // Validate Swagger requests
133
- app . use ( middleware . swaggerValidator ( ) ) ;
134
-
135
- // Route validated requests to appropriate controller
136
- app . use ( middleware . swaggerRouter ( ) ) ;
137
-
138
- // Serve the Swagger documents and Swagger UI
139
- app . use ( middleware . swaggerUi ( swaggerToolsUIOptions ) ) ;
117
+ parser . parse ( swaggerObject , function ( err , api ) {
118
+ if ( ! err ) {
119
+ swaggerObject = api ;
140
120
}
141
- ) ;
121
+ } ) ;
122
+
123
+ return swaggerObject ;
142
124
} ;
0 commit comments