0

List Batteries

by
Published Nov 5, 2024

Returns a paginated list of all Batteries.

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
	after: string | undefined,
9
	before: string | undefined,
10
	pageSize: string | undefined
11
) {
12
	const url = new URL(`https://enode-api.production.enode.io/batteries`)
13

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

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

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

37
	return await response.json()
38
}
39