-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
61 lines (52 loc) · 1.56 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
const loadRoutes = require('./lib/loadModules');
const jsr = require('json-stream-response');
const httpSuccess = 200;
let passport = null;
const getPassportInstance = () => {
// eslint-disable-next-line global-require
passport = passport || require('passport-restify');
return passport;
};
const loadPassportStrategy = (authMethods, strategyInstance, friendlyName) => {
authMethods = authMethods || {};
getPassportInstance().use(strategyInstance);
friendlyName = friendlyName || strategyInstance.name;
authMethods[friendlyName] = getPassportInstance().authenticate(strategyInstance.name);
return authMethods;
};
module.exports = {
// eslint-disable-next-line max-params
'configureServer': (restifyServer, conf, params, cb) => {
// throw descriptive error when config not provided
// TODO: maybe replace this with config defaults
if (typeof conf === 'undefined') {
throw new Error('Config not provided.');
}
conf.handlersDir = conf.handlersDir || './endpoints';
conf.authMethods = conf.authMethods || {};
loadRoutes(conf.handlersDir, conf.authMethods, restifyServer, params, cb);
},
loadPassportStrategy,
'initAuthWithAnonymous': () => {
return {
'anonymous': (req, res, next) => {
return next();
},
};
},
'streamResponse': (res, next) => {
res.on('finish', next);
return jsr(res);
},
'jsonResponder': (res, next) => {
return (err, result) => {
if (err) {
next(err);
return;
}
else {
res.send(httpSuccess, result);
}
};
},
};