forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHooksRouter.js
145 lines (133 loc) · 4.08 KB
/
HooksRouter.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { Parse } from 'parse/node';
import PromiseRouter from '../PromiseRouter';
import * as middleware from '../middlewares';
export class HooksRouter extends PromiseRouter {
createHook(aHook, config) {
return config.hooksController.createHook(aHook).then(hook => ({ response: hook }));
}
updateHook(aHook, config) {
return config.hooksController.updateHook(aHook).then(hook => ({ response: hook }));
}
handlePost(req) {
return this.createHook(req.body || {}, req.config);
}
handleGetFunctions(req) {
var hooksController = req.config.hooksController;
if (req.params.functionName) {
return hooksController.getFunction(req.params.functionName).then(foundFunction => {
if (!foundFunction) {
throw new Parse.Error(143, `no function named: ${req.params.functionName} is defined`);
}
return Promise.resolve({ response: foundFunction });
});
}
return hooksController.getFunctions().then(
functions => {
return { response: functions || [] };
},
err => {
throw err;
}
);
}
handleGetTriggers(req) {
var hooksController = req.config.hooksController;
if (req.params.className && req.params.triggerName) {
return hooksController
.getTrigger(req.params.className, req.params.triggerName)
.then(foundTrigger => {
if (!foundTrigger) {
throw new Parse.Error(143, `class ${req.params.className} does not exist`);
}
return Promise.resolve({ response: foundTrigger });
});
}
return hooksController.getTriggers().then(triggers => ({ response: triggers || [] }));
}
handleDelete(req) {
var hooksController = req.config.hooksController;
if (req.params.functionName) {
return hooksController.deleteFunction(req.params.functionName).then(() => ({ response: {} }));
} else if (req.params.className && req.params.triggerName) {
return hooksController
.deleteTrigger(req.params.className, req.params.triggerName)
.then(() => ({ response: {} }));
}
return Promise.resolve({ response: {} });
}
handleUpdate(req) {
var hook;
if (req.params.functionName && req.body?.url) {
hook = {};
hook.functionName = req.params.functionName;
hook.url = req.body.url;
} else if (req.params.className && req.params.triggerName && req.body?.url) {
hook = {};
hook.className = req.params.className;
hook.triggerName = req.params.triggerName;
hook.url = req.body.url;
} else {
throw new Parse.Error(143, 'invalid hook declaration');
}
return this.updateHook(hook, req.config);
}
handlePut(req) {
var body = req.body || {};
if (body.__op == 'Delete') {
return this.handleDelete(req);
} else {
return this.handleUpdate(req);
}
}
mountRoutes() {
this.route(
'GET',
'/hooks/functions',
middleware.promiseEnforceMasterKeyAccess,
this.handleGetFunctions.bind(this)
);
this.route(
'GET',
'/hooks/triggers',
middleware.promiseEnforceMasterKeyAccess,
this.handleGetTriggers.bind(this)
);
this.route(
'GET',
'/hooks/functions/:functionName',
middleware.promiseEnforceMasterKeyAccess,
this.handleGetFunctions.bind(this)
);
this.route(
'GET',
'/hooks/triggers/:className/:triggerName',
middleware.promiseEnforceMasterKeyAccess,
this.handleGetTriggers.bind(this)
);
this.route(
'POST',
'/hooks/functions',
middleware.promiseEnforceMasterKeyAccess,
this.handlePost.bind(this)
);
this.route(
'POST',
'/hooks/triggers',
middleware.promiseEnforceMasterKeyAccess,
this.handlePost.bind(this)
);
this.route(
'PUT',
'/hooks/functions/:functionName',
middleware.promiseEnforceMasterKeyAccess,
this.handlePut.bind(this)
);
this.route(
'PUT',
'/hooks/triggers/:className/:triggerName',
middleware.promiseEnforceMasterKeyAccess,
this.handlePut.bind(this)
);
}
}
export default HooksRouter;