forked from mulesoft/osprey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osprey.js
57 lines (48 loc) · 1.35 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
var Router = require('osprey-router')
var compose = require('compose-middleware').compose
var methodHandler = require('osprey-method-handler')
var server = require('./lib/server')
var proxy = require('./lib/proxy')
var security = require('./lib/security')
var errorHandler = require('request-error-handler')
/**
* 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) {
methodHandler.addJsonSchema(schema, key)
}
/**
* Load an Osprey server directly from a RAML file.
*
* @param {String} path
* @param {Object} opts
* @return {Promise}
*/
exports.loadFile = function (path, opts) {
var options = opts || {}
return require('raml-parser')
.loadFile(path)
.then(function (raml) {
var middleware = []
var handler = server(raml, options.server)
var error = errorHandler(options.errorHandler)
if (options.security) {
middleware.push(security(raml, options.security))
}
middleware.push(handler)
if (!options.disableErrorInterception) {
middleware.push(error)
}
var result = compose(middleware)
result.ramlUriParameters = handler.ramlUriParameters
return result
})
}