-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (66 loc) · 2.28 KB
/
index.js
File metadata and controls
84 lines (66 loc) · 2.28 KB
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
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-component-explorer
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
/*!
* Adds dynamically-updated docs as /explorer
*/
const path = require('path');
const _defaults = require('lodash').defaults;
const ACL_BUILDER_UI_ROOT = path.resolve(__dirname, 'dist');
const STATIC_ROOT = path.join(__dirname, 'public');
'use strict';
module.exports = aclbuilder;
aclbuilder.routes = routes;
/**
* Example usage:
*
* var explorer = require('loopback-component-explorer');
* explorer(app, options);
*/
function aclbuilder(loopbackApplication, options) {
options = _defaults({}, options, {mountPath: '/acl-builder'});
loopbackApplication.use(options.mountPath, routes(loopbackApplication, options));
loopbackApplication.set('loopback-component-acl-builder', options);
}
function routes(loopbackApplication, options) {
let loopback = loopbackApplication.loopback;
let loopbackMajor = loopback && loopback.version && loopback.version.split('.')[0] || 1;
if (loopbackMajor < 2) {
throw new Error(g.f('{{loopback-component-acl-builder}} requires {{loopback}} 2.0 or newer'));
}
let router = new loopback.Router();
// File in node_modules are overridden by a few customizations
router.use(loopback.static(STATIC_ROOT));
// Swagger UI distribution
router.use(loopback.static(ACL_BUILDER_UI_ROOT));
console.log("Setting Up Models /acl-builder/api/models");
router.get('/api/models', function (req, res) {
res.send(JSON.stringify(getModels(loopbackApplication)));
});
router.get('/api/models/:model', function (req, res) {
res.send(JSON.stringify(getModel(loopbackApplication, req.params['model'])));
});
return router;
}
function getModels(app) {
let models = Object.keys(app.models);
let data = {};
for (let i = 0; i < models.length; i++) {
let m = models[i];
data[app.models[m].definition.name] = getModel(app, m);
}
return data;
}
function getModel(app, m) {
let model = app.models[m].definition.toJSON();
let methods = app.models[m].sharedClass.methods();
model.settings.methods = methods.map(m => {
return {
name: m.name,
description: m.description
};
});
return model;
}