forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
66 lines (62 loc) · 1.46 KB
/
common.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
const netlify = require("./netlify.app");
module.exports = {
dedupe: "unique",
props: {
netlify,
db: "$.service.db",
http: {
type: "$.interface.http",
customResponse: true,
},
siteId: { propDefinition: [netlify, "siteId"] },
},
hooks: {
async activate() {
const event = this.getHookEvent();
const opts = {
event,
url: this.http.endpoint,
siteId: this.siteId,
};
const { hookId, token } = await this.netlify.createHook(opts);
this.db.set("hookId", hookId);
this.db.set("token", token);
},
async deactivate() {
const hookId = this.db.get("hookId");
const opts = {
hookId,
siteId: this.siteId,
};
await this.netlify.deleteHook(opts);
},
},
methods: {
generateMeta(data) {
const { id, created_at } = data;
const ts = +new Date(created_at);
const summary = this.getMetaSummary(data);
return {
id,
summary,
ts,
};
},
},
async run(event) {
const { headers, body, bodyRaw } = event;
// Reject any calls not made by the proper Netlify webhook.
if (!this.netlify.isValidSource(headers, bodyRaw, this.db)) {
this.http.respond({
status: 404,
});
return;
}
// Acknowledge the event back to Netlify.
this.http.respond({
status: 200,
});
const meta = this.generateMeta(body);
this.$emit(body, meta);
},
};