forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParse.Hooks.js
More file actions
132 lines (111 loc) · 3.27 KB
/
Parse.Hooks.js
File metadata and controls
132 lines (111 loc) · 3.27 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
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
var request = require("request");
const send = function(method, path, body) {
var Parse = require("parse/node").Parse;
var options = {
method: method,
url: Parse.serverURL + path,
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Master-Key': Parse.masterKey,
'Content-Type': 'application/json'
},
};
if (body) {
if (typeof body == "object") {
options.body = JSON.stringify(body);
} else {
options.body = body;
}
}
var promise = new Parse.Promise();
request(options, function(err, response, body){
if (err) {
promise.reject(err);
return;
}
body = JSON.parse(body);
if (body.error) {
promise.reject(body);
} else {
promise.resolve(body);
}
});
return promise;
}
var Hooks = {};
Hooks.getFunctions = function() {
return Hooks.get("functions");
}
Hooks.getTriggers = function() {
return Hooks.get("triggers");
}
Hooks.getFunction = function(name) {
return Hooks.get("functions", name);
}
Hooks.getTrigger = function(className, triggerName) {
return Hooks.get("triggers", className, triggerName);
}
Hooks.get = function(type, functionName, triggerName) {
var url = "/hooks/"+type;
if(functionName) {
url += "/"+functionName;
if (triggerName) {
url += "/"+triggerName;
}
}
return send("GET", url);
}
Hooks.createFunction = function(functionName, url) {
return Hooks.create({functionName: functionName, url: url});
}
Hooks.createTrigger = function(className, triggerName, url) {
return Hooks.create({className: className, triggerName: triggerName, url: url});
}
Hooks.create = function(hook) {
var url;
if (hook.functionName && hook.url) {
url = "/hooks/functions";
} else if (hook.className && hook.triggerName && hook.url) {
url = "/hooks/triggers";
} else {
return Promise.reject({error: 'invalid hook declaration', code: 143});
}
return send("POST", url, hook);
}
Hooks.updateFunction = function(functionName, url) {
return Hooks.update({functionName: functionName, url: url});
}
Hooks.updateTrigger = function(className, triggerName, url) {
return Hooks.update({className: className, triggerName: triggerName, url: url});
}
Hooks.update = function(hook) {
var url;
if (hook.functionName && hook.url) {
url = "/hooks/functions/"+hook.functionName;
delete hook.functionName;
} else if (hook.className && hook.triggerName && hook.url) {
url = "/hooks/triggers/"+hook.className+"/"+hook.triggerName;
delete hook.className;
delete hook.triggerName;
}
return send("PUT", url, hook);
}
Hooks.deleteFunction = function(functionName) {
return Hooks.delete({functionName: functionName});
}
Hooks.deleteTrigger = function(className, triggerName) {
return Hooks.delete({className: className, triggerName: triggerName});
}
Hooks.delete = function(hook) {
var url;
if (hook.functionName) {
url = "/hooks/functions/"+hook.functionName;
delete hook.functionName;
} else if (hook.className && hook.triggerName) {
url = "/hooks/triggers/"+hook.className+"/"+hook.triggerName;
delete hook.className;
delete hook.triggerName;
}
return send("PUT", url, '{ "__op": "Delete" }');
}
module.exports = Hooks