This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
osprey.js
65 lines (55 loc) · 1.62 KB
/
osprey.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
const Router = require('osprey-router')
const compose = require('compose-middleware').compose
const ospreyMethodHandler = require('osprey-method-handler')
const errorHandler = require('request-error-handler')
const wap = require('webapi-parser').WebApiParser
const path = require('path')
const server = require('./lib/server')
const proxy = require('./lib/proxy')
const security = require('./lib/security')
/**
* Expose functions.
*/
exports.Router = Router
exports.server = server
exports.proxy = proxy
exports.security = security
exports.errorHandler = errorHandler
/**
* Proxy JSON schema addition to method handler.
*/
exports.addJsonSchema = function (schema, key) {
ospreyMethodHandler.addJsonSchema(schema, key)
}
/**
* Load an Osprey server directly from a RAML file.
*
* @param {String} fpath
* @param {Object} options
* @return {Promise}
*/
exports.loadFile = async function (fpath, options = {}) {
fpath = path.resolve(fpath)
fpath = fpath.startsWith('file:') ? fpath : `file://${fpath}`
let model
try {
model = await wap.raml10.parse(fpath)
model = await wap.raml10.resolve(model)
} catch (e) {
model = await wap.raml08.parse(fpath)
model = await wap.raml08.resolve(model)
}
const middleware = []
const handler = server(model, options.server)
const error = errorHandler(options.errorHandler)
if (options.security) {
middleware.push(security(model, options.security))
}
middleware.push(handler)
if (!options.disableErrorInterception) {
middleware.push(error)
}
const result = compose(middleware)
result.ramlUriParameters = handler.ramlUriParameters
return result
}