Get filtered message stats

Script actimo Verified

by hugo697 ยท 11/5/2024

The script

Submitted by hugo697 Bun
Verified 572 days ago
1
//native
2
type Actimo = {
3
	apiKey: string
4
}
5

6
export async function main(
7
	auth: Actimo,
8
	id: string,
9
	filter: string,
10
	bounce: string | undefined,
11
	moduleDetails: string | undefined,
12
	modules: string | undefined,
13
	open: string | undefined,
14
	unique: string | undefined,
15
	since: string | undefined
16
) {
17
	const url = new URL(`https://actimo.com/api/v1/messages/${id}/stats/${filter}`)
18

19
	for (const [k, v] of [
20
		['bounce', bounce],
21
		['moduleDetails', moduleDetails],
22
		['modules', modules],
23
		['open', open],
24
		['unique', unique],
25
		['since', since]
26
	]) {
27
		if (v !== undefined && v !== '' && k !== undefined) {
28
			url.searchParams.append(k, v)
29
		}
30
	}
31

32
	const response = await fetch(url, {
33
		method: 'GET',
34
		headers: {
35
			'api-key': auth.apiKey
36
		},
37
		body: undefined
38
	})
39

40
	if (!response.ok) {
41
		const text = await response.text()
42
		throw new Error(`${response.status} ${text}`)
43
	}
44

45
	return await response.json()
46
}
47