Get targeting templates

Script adhook Verified

by hugo697 ยท 12/20/2024

The script

Submitted by hugo697 Bun
Verified 519 days ago
1
//native
2
type Adhook = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Adhook,
8
	subtenantId: string | undefined,
9
	status: 'ALL' | 'ACTIVE' | 'INACTIVE' | undefined,
10
	adTemplateId: string | undefined,
11
	postTemplateId: string | undefined,
12
	filter: string | undefined,
13
	skip: string | undefined,
14
	limit: string | undefined,
15
	adhookToken: string,
16
	Origin: string
17
) {
18
	const url = new URL(`https://app.adhook.io/v1/targetingTemplates`)
19

20
	for (const [k, v] of [
21
		['subtenantId', subtenantId],
22
		['status', status],
23
		['adTemplateId', adTemplateId],
24
		['postTemplateId', postTemplateId],
25
		['filter', filter],
26
		['skip', skip],
27
		['limit', limit]
28
	]) {
29
		if (v !== undefined && v !== '' && k !== undefined) {
30
			url.searchParams.append(k, v)
31
		}
32
	}
33

34
	const response = await fetch(url, {
35
		method: 'GET',
36
		headers: {
37
			adhookToken: adhookToken,
38
			Authorization: `Bearer ${auth.token}`,
39
			Origin: Origin
40
		},
41
		body: undefined
42
	})
43

44
	if (!response.ok) {
45
		const text = await response.text()
46
		throw new Error(`${response.status} ${text}`)
47
	}
48

49
	return await response.json()
50
}
51