-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcode-example.js
More file actions
32 lines (26 loc) · 773 Bytes
/
Copy pathcode-example.js
File metadata and controls
32 lines (26 loc) · 773 Bytes
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
// ************ Your package ************
const { hooks } = require('promised-hooks');
const api = {
doSomethingGreat() {
return Promise.resolve({ data: 123 });
},
};
hooks.wrap(api);
module.exports = api;
// *********** CONSUMERS of your package ********
const { AmazingService } = require('your-package');
// Add a middleware to be executed *before* doSomethingGreat()
// to authorize the method call
AmazingService.pre('doSomethingGreat', function() {
return someService.checkIfDoSomethingGreatAllowed();
});
// ...
// Anywhere in the Application, the hook will authorize the method call
async function someHandler() {
let res;
try {
res = await AmazingService.doSomethingGreat();
} catch(e) {
// not allowed
}
}