-
Notifications
You must be signed in to change notification settings - Fork 3
/
example.js
42 lines (34 loc) · 1.24 KB
/
example.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
const LocalWebhook = require("./LocalWebhook");
const fetch = require("node-fetch");
(async function() {
try {
// Start LocalWebhook server.
await LocalWebhook.startServer({ subdomain: "sushi" });
const simulatedTriggers = [];
// 1) Promise example:
const promise = LocalWebhook.getPromise("wasabi");
promise.then(({ req, res }) => {
res.send("Hello from promise, wasabi");
// Do stuff here like updating your database.
});
// Trigger webhook, normally this is done by a third-party.
simulatedTriggers.push(fetch(promise.getWebhookUrl()));
// 2) Observable example:
const observable = LocalWebhook.getObservable("ichiban");
observable.subscribe({
next: ({ req, res }) => {
res.send("Hello from observable, ichiban");
// Do stuff here like logging the request.
},
});
// Trigger webhook twice, normally this is done by a third-party.
simulatedTriggers.push(fetch(observable.getWebhookUrl()));
simulatedTriggers.push(fetch(observable.getWebhookUrl()));
// Wait for all the webhooks to be triggered.
await Promise.all(simulatedTriggers);
// Clean up.
LocalWebhook.stopServer();
} catch (error) {
console.error(error);
}
})();