-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
182 lines (155 loc) · 4.78 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
'use strict'
let express = require('express'),
APIError = require('./lib/APIError'),
lift = require('./lib/lift'),
versions = require('./lib/versions'),
genId = require('./lib/genId'),
API = require('./lib/API')
/**
* @module api-lift
*/
/**
* The module responsible for reading the files in the target folder
* @external lift-it
* @see https://www.npmjs.com/package/lift-it
*/
/**
* Express module
* @external express
* @see https://www.npmjs.com/package/express
*/
/**
* Lift the API
* @param {Object} [options] - see defaults in README
* @returns {API}
*/
module.exports = function (options) {
options = prepareOptions(options)
// Lift and call error callbacks
let lifted = lift(options)
let api = new API(lifted, options)
// Prepare express middlewares
api.router.use((req, res, next) => {
// Save the time at the beggining
req.runInfo = {
beginTime: Date.now(),
requestId: genId(),
req
}
if ((req.method === 'POST' || req.method === 'PUT') && req.body && !req.is('json')) {
return next(APIError.create(406, 101,
'Invalid Content-Type header, application/json was expected'))
}
next()
})
// Create the endpoints using versioning
api._prepareEndpoints(options.minVersion)
// Prepare open API routes
if (options.openApi.serve) {
api.router.get('/' + options.openApi.serveAs, getOpenApiMiddlewares())
api.router.get('/v-last/' + options.openApi.serveAs, getOpenApiMiddlewares(api.maxVersion))
let version
for (version = api.minVersion; version <= api.maxVersion; version++) {
api.router.get('/v' + version + '/' + options.openApi.serveAs, getOpenApiMiddlewares(version))
}
}
// Prepare express endpoint handler
api.router.use((req, res, next) => {
api._runRequest(req, res, (err, out) => {
if (err || !out) {
return next(err)
}
if (res.statusCode === 302) {
return res.redirect(out.redirect)
}
res.json(out)
})
})
// Error handler
// eslint-disable-next-line no-unused-vars
api.router.use((err, req, res, next) => {
if (err instanceof Error && err.status === 400 && typeof err.body === 'string') {
// JSON parsing error
err = APIError.create(400, 101, 'Invalid JSON: ' + err)
}
api._handleError(err, req.body, req.runInfo, null, res, out => {
res.json(out)
})
})
function getOpenApiMiddlewares(version) {
return [options.openApi.middleware, function (req, res) {
let spec = api.getOpenAPISpec(version)
if (!spec.basePath) {
spec.basePath = req.baseUrl || '/'
}
res.json(spec)
}]
}
return api
}
/**
* @param {Object} [options]
* @returns {Versions}
*/
module.exports.info = function (options) {
options = prepareOptions(options)
return versions(options.minVersion, lift.lean(options).actions)
}
/** Exposes used version of {@link external:express} */
module.exports.express = express
/** Exposes {@link APIError} class */
module.exports.APIError = APIError
/**
* @param {?Object} options
* @returns {Object}
* @private
*/
function prepareOptions(options) {
let vO
// Set defaults
options = options || {}
options.folder = options.folder || './api'
options.profile = Boolean(options.profile)
options.plugins = options.plugins || []
options.dataScrub = options.dataScrub || [/session|password|serial|token/i]
options.callToJSON = options.callToJSON || function (x) {
return x.toJSON()
}
options.timeout = options.timeout || 30e3
options.validate = options.validate || {}
// Set validateOutput defaults
vO = options.validateOutput = options.validateOutput || {}
vO.direction = 'output'
vO.exportName = vO.exportName || 'outFields'
vO.optional = vO.optional === undefined ? true : vO.optional
vO.getDefaultValue = vO.getDefaultValue || function () {
return {}
}
vO.code = vO.code || 100
vO.errorHandler = vO.errorHandler || function (action, value, err) {
throw err
}
vO.options = vO.options || {}
vO.options.strict = vO.options.strict === undefined ? true : vO.options.strict
options.filters = options.filters || './filters'
options.bodyParser = options.bodyParser || {}
options.minVersion = options.minVersion === undefined ? 1 : options.minVersion
options.isRest = Boolean(options.isRest)
options.hasApiKeyAuth = Boolean(options.hasApiKeyAuth)
options.hasOAuth = Boolean(options.hasOAuth)
options.errors = options.errors || {}
options.checkId = options.checkId || (() => false)
options.openApi = options.openApi || {}
options.openApi.serve = Boolean(options.openApi.serve)
options.openApi.serveAs = options.openApi.serveAs || 'swagger.json'
options.openApi.middleware = options.openApi.middleware || function (req, res, next) {
next()
}
options.openApi.prepareEndpoint = options.openApi.prepareEndpoint || function (endpoint, pathItem) {
return pathItem
}
options.openApi.prepareSpec = options.openApi.prepareSpec || function (spec) {
return spec
}
return options
}