-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (34 loc) · 1.04 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
'use strict';
const str2fn = require('str2fn');
const defaults = {
// option to change the endpoint:
endpoint: '/methods',
auth: null
};
exports.register = (server, options, next) => {
// setup defaults:
const settings = Object.assign({}, defaults, options);
const endpoint = settings.endpoint;
server.route({
method: 'POST',
path: `${endpoint}`,
config: {
auth: settings.auth
},
handler(request, reply) {
str2fn.execute(request.payload.method, server.methods, {}, (err, result) => {
if (err) {
if (err.toString().indexOf('does not exist') > -1) {
return reply({ successful: false, result: `Method call ${request.payload.method} invokes a method that is not defined` }).code(404);
}
return reply({ successful: false, result: `Method call ${request.payload.method} threw this error: ${err}` }).code(500);
}
return reply({ successful: true, result });
});
}
});
next();
};
exports.register.attributes = {
pkg: require('./package.json')
};