-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend-event.ts
47 lines (39 loc) · 1.13 KB
/
send-event.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
import { EventsClient, withEndpoint } from "@sajari/sdk-node";
import program, { withEndpointOptions } from "./program";
import { handleError } from "./api-util";
withEndpointOptions(program);
program.requiredOption("--account-id <account_id>", "account ID", "my-account");
program.requiredOption("--event-name <event_name>", "event name", "purchase");
program.requiredOption("--event-token <event_token>", "event token", "eyJ...");
program.requiredOption<number>(
"--event-weight <event_weight>",
"event weight",
(value: string, previous: number) => {
const parsed = parseInt(value, 10);
if (isNaN(parsed)) {
throw new Error(
`error: option '--event-weight ${value}' argument is invalid`
);
}
return parsed;
},
1
);
program.parse(process.argv);
async function main(
endpoint = program.endpoint,
accountId = program.accountId,
event = {
name: program.eventName,
token: program.eventToken,
weight: program.weight,
}
) {
const client = new EventsClient(withEndpoint(endpoint));
try {
await client.sendEvent(accountId, event);
} catch (e) {
handleError(e);
}
}
main();