0

List User HVAC units

by
Published Nov 5, 2024

Paginated list of HVAC units for the given User

Script enode Verified

The script

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

6
export async function main(
7
	auth: Enode,
8
	userId: string,
9
	after: string | undefined,
10
	before: string | undefined,
11
	pageSize: string | undefined
12
) {
13
	const url = new URL(`https://enode-api.production.enode.io/users/${userId}/hvacs`)
14

15
	for (const [k, v] of [
16
		['after', after],
17
		['before', before],
18
		['pageSize', pageSize]
19
	]) {
20
		if (v !== undefined && v !== '' && k !== undefined) {
21
			url.searchParams.append(k, v)
22
		}
23
	}
24

25
	const response = await fetch(url, {
26
		method: 'GET',
27
		headers: {
28
			Authorization: 'Bearer ' + auth.token
29
		},
30
		body: undefined
31
	})
32

33
	if (!response.ok) {
34
		const text = await response.text()
35
		throw new Error(`${response.status} ${text}`)
36
	}
37

38
	return await response.json()
39
}
40