forked from apigee-127/swagger-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (100 loc) · 4.08 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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Apigee Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
'use strict';
var _ = require('lodash');
var debug = require('debug')('swagger-tools:middleware');
var helpers = require('./lib/helpers');
var initializeMiddleware = function initializeMiddleware (rlOrSO, resources, callback) {
var args;
var spec;
debug('Initializing middleware');
if (_.isUndefined(rlOrSO)) {
throw new Error('rlOrSO is required');
} else if (!_.isPlainObject(rlOrSO)) {
throw new TypeError('rlOrSO must be an object');
}
args = [rlOrSO];
spec = helpers.getSpec(helpers.getSwaggerVersion(rlOrSO), true);
debug(' Identified Swagger version: %s', spec.version);
if (spec.version === '1.2') {
if (_.isUndefined(resources)) {
throw new Error('resources is required');
} else if (!_.isArray(resources)) {
throw new TypeError('resources must be an array');
}
debug(' Number of API Declarations: %d', resources.length);
args.push(resources);
} else {
callback = arguments[1];
}
if (_.isUndefined(callback)) {
throw new Error('callback is required');
} else if (!_.isFunction(callback)) {
throw new TypeError('callback must be a function');
}
args.push(function (err, results) {
if (results && results.errors.length + _.reduce(results.apiDeclarations || [], function (count, apiDeclaration) {
return count += (apiDeclaration ? apiDeclaration.errors.length : 0);
}, 0) > 0) {
err = new Error('Swagger document(s) failed validation so the server cannot start');
err.results = results;
}
debug(' Validation: %s', err ? 'failed' : 'succeeded');
if (err) {
if (process.env.NODE_ENV === 'test') {
throw err;
} else {
return helpers.printValidationResults(spec.version, rlOrSO, resources, results, true, true);
}
}
callback({
// Create a wrapper to avoid having to pass the non-optional arguments back to the swaggerMetadata middleware
swaggerMetadata: function () {
var swaggerMetadata = require('./middleware/swagger-metadata');
return swaggerMetadata.apply(undefined, args.slice(0, args.length - 1));
},
swaggerRouter: require('./middleware/swagger-router'),
swaggerSecurity: require('./middleware/swagger-security'),
// Create a wrapper to avoid having to pass the non-optional arguments back to the swaggerUi middleware
swaggerUi: function (options) {
var swaggerUi = require('./middleware/swagger-ui');
var suArgs = [rlOrSO];
if (spec.version === '1.2') {
suArgs.push(_.reduce(resources, function (map, resource) {
map[resource.resourcePath] = resource;
return map;
}, {}));
}
suArgs.push(options || {});
return swaggerUi.apply(undefined, suArgs);
},
swaggerValidator: require('./middleware/swagger-validator')
});
});
spec.validate.apply(spec, args);
};
module.exports = {
initializeMiddleware: initializeMiddleware,
specs: require('./lib/specs')
};