Repository navigation
Expand file tree
/
Copy paththreads.ts
More file actions
67 lines (55 loc) · 1.86 KB
/
Copy paththreads.ts
File metadata and controls
67 lines (55 loc) · 1.86 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
import { MailtrapClient } from "mailtrap";
const TOKEN = "<YOUR-TOKEN-HERE>";
const INBOX_ID = Number("<YOUR-INBOX-ID-HERE>");
// Set to a real thread id to try deletion below. Left empty, the example is
// read-only (list + get).
const THREAD_ID = "";
// Inbound is scoped to the token's account, so no accountId is required.
const client = new MailtrapClient({ token: TOKEN });
const threadsClient = client.inbound.threads;
async function threadsFlow() {
try {
const list = await threadsClient.getList(INBOX_ID);
console.log("Threads:", list);
// Fetch the next page with the returned cursor.
if (list.last_id) {
console.log(
"Next page:",
await threadsClient.getList(INBOX_ID, { last_id: list.last_id })
);
}
const search = "acme";
const filtered = await threadsClient.getList(INBOX_ID, { search });
console.log("Threads matching search:", filtered);
if (filtered.last_id) {
console.log(
"Next filtered page:",
await threadsClient.getList(INBOX_ID, {
search,
last_id: filtered.last_id,
})
);
}
if (list.data.length > 0) {
const thread = await threadsClient.get(INBOX_ID, list.data[0].id);
console.log("First thread:", thread);
thread.messages.forEach((message) => {
if (message.direction === "outbound") {
console.log("Delivery:", message.delivery);
} else {
console.log("Forwards:", message.forwards);
}
});
}
if (!THREAD_ID) {
console.log("Set THREAD_ID above to try deletion.");
return;
}
// Deletes a real thread — only run against one you own.
await threadsClient.delete(INBOX_ID, THREAD_ID);
console.log("Deleted thread", THREAD_ID);
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
}
}
threadsFlow();