-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathlibrary.js
88 lines (74 loc) · 2.7 KB
/
library.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';
const nconf = require.main.require('nconf');
const winston = require.main.require('winston');
const meta = require.main.require('./src/meta');
const controllers = require('./lib/controllers');
const routeHelpers = require.main.require('./src/routes/helpers');
const plugin = {};
plugin.init = async (params) => {
const { router /* , middleware , controllers */ } = params;
// Settings saved in the plugin settings can be retrieved via settings methods
const { setting1, setting2 } = await meta.settings.get('quickstart');
if (setting1) {
console.log(setting2);
}
/**
* We create two routes for every view. One API call, and the actual route itself.
* Use the `setupPageRoute` helper and NodeBB will take care of everything for you.
*
* Other helpers include `setupAdminPageRoute` and `setupAPIRoute`
* */
routeHelpers.setupPageRoute(router, '/quickstart', [(req, res, next) => {
winston.info(`[plugins/quickstart] In middleware. This argument can be either a single middleware or an array of middlewares`);
setImmediate(next);
}], (req, res) => {
winston.info(`[plugins/quickstart] Navigated to ${nconf.get('relative_path')}/quickstart`);
res.render('quickstart', { uid: req.uid });
});
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/quickstart', controllers.renderAdminPage);
};
/**
* If you wish to add routes to NodeBB's RESTful API, listen to the `static:api.routes` hook.
* Define your routes similarly to above, and allow core to handle the response via the
* built-in helpers.formatApiResponse() method.
*
* In this example route, the `ensureLoggedIn` middleware is added, which means a valid login
* session or bearer token (which you can create via ACP > Settings > API Access) needs to be
* passed in.
*
* To call this example route:
* curl -X GET \
* http://example.org/api/v3/plugins/quickstart/test \
* -H "Authorization: Bearer some_valid_bearer_token"
*
* Will yield the following response JSON:
* {
* "status": {
* "code": "ok",
* "message": "OK"
* },
* "response": {
* "foobar": "test"
* }
* }
*/
plugin.addRoutes = async ({ router, middleware, helpers }) => {
const middlewares = [
middleware.ensureLoggedIn, // use this if you want only registered users to call this route
// middleware.admin.checkPrivileges, // use this to restrict the route to administrators
];
routeHelpers.setupApiRoute(router, 'get', '/quickstart/:param1', middlewares, (req, res) => {
helpers.formatApiResponse(200, res, {
foobar: req.params.param1,
});
});
};
plugin.addAdminNavigation = (header) => {
header.plugins.push({
route: '/plugins/quickstart',
icon: 'fa-tint',
name: 'Quickstart',
});
return header;
};
module.exports = plugin;