Update automation

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
	force: string | undefined,
10
	body: {
11
		decision_graph?: {
12
			edges?: {
13
				end?: string
14
				predicate?: { key_name?: string; type?: string }
15
				start?: string
16
			}[]
17
			id?: string
18
			nodes?: { id?: number }[][]
19
		}
20
		description?: string
21
		name?: string
22
		start_node?: number[]
23
	}
24
) {
25
	const url = new URL(`https://actimo.com/api/v1/decisiongraphs/${id}`)
26

27
	for (const [k, v] of [['force', force]]) {
28
		if (v !== undefined && v !== '' && k !== undefined) {
29
			url.searchParams.append(k, v)
30
		}
31
	}
32

33
	const response = await fetch(url, {
34
		method: 'PUT',
35
		headers: {
36
			'api-key': auth.apiKey,
37
			'Content-Type': 'application/json'
38
		},
39
		body: JSON.stringify(body)
40
	})
41

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

47
	return await response.json()
48
}
49