-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.ts
77 lines (70 loc) · 2.05 KB
/
connector.ts
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
import { v4 as uuidv4 } from "uuid";
import {
ConnectorInput,
TriggerBase,
ConnectorDefinition,
ActionOutput,
WebhookParams,
} from "grindery-nexus-common-utils";
let debugOutput = "Hello World!";
function addDebugOutput(output: string) {
if (debugOutput.length > 10000) {
debugOutput = "(truncated)";
}
debugOutput += `\n[${new Date().toISOString()}] ${output}`;
}
type HelloWorldTriggerFields = {
recurring: boolean;
interval: number;
};
class HelloWorldTrigger extends TriggerBase<HelloWorldTriggerFields> {
async main() {
const send = () => {
if (!this.isRunning) {
addDebugOutput(`[${this.sessionId}] Stopping trigger`);
return;
}
addDebugOutput(`[${this.sessionId}] Sending signal`);
this.sendNotification({ random: `ABC-${uuidv4()}` });
if (this.fields.recurring) {
addDebugOutput(`[${this.sessionId}] Will trigger again in ${this.fields.interval}ms`);
setTimeout(send, this.fields.interval);
}
};
setTimeout(send, this.fields.interval);
await this.waitForStop();
}
}
async function helloWorldAction(params: ConnectorInput<unknown>): Promise<ActionOutput> {
const fields = params.fields as {
message: string;
};
console.log(`Hello World! ${fields.message}`);
addDebugOutput(`[${params.sessionId}] Hello World! ${fields.message}`);
return {
payload: {
message: `Hello World! ${fields.message}`,
},
};
}
async function onWebhook(params: ConnectorInput<WebhookParams>): Promise<ActionOutput> {
console.log("Webhook received:", params.fields);
addDebugOutput(`[${params.sessionId}] Webhook received: ` + JSON.stringify(params.fields));
return {
payload: {
message: "Hello World!",
},
};
}
export const CONNECTOR_DEFINITION: ConnectorDefinition = {
actions: { helloWorldAction },
triggers: { helloWorldTrigger: HelloWorldTrigger },
webhooks: {
helloWorldTrigger: onWebhook,
},
options: {
mutateRoutes: (app) => {
app.get("/debug", (req, res) => res.type("text").send(debugOutput).end());
},
},
};