0
List Plans
One script reply has been approved by the moderators Verified
Created by hugo697 111 days ago Viewed 33 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 111 days ago
1
type Baremetrics = {
2
	apiKey: string
3
}
4

5
export async function main(
6
	resource: Baremetrics,
7
	sourceId: string,
8
	params?: {
9
		page?: number
10
		per_page?: number
11
	}
12
) {
13
	const queryParams = new URLSearchParams()
14

15
	if (params?.page) {
16
		queryParams.append('page', params.page.toString())
17
	}
18

19
	if (params?.per_page) {
20
		queryParams.append('per_page', params.per_page.toString())
21
	}
22

23
	const endpoint = `https://api.baremetrics.com/v1/${sourceId}/plans?${queryParams.toString()}`
24

25
	const response = await fetch(endpoint, {
26
		method: 'GET',
27
		headers: {
28
			'Content-Type': 'application/json',
29
			Accept: 'application/json',
30
			Authorization: `Bearer ${resource.apiKey}`
31
		}
32
	})
33

34
	if (!response.ok) {
35
		throw new Error(`HTTP error! status: ${response.status}`)
36
	}
37

38
	const data = await response.json()
39

40
	return data
41
}
42