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

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

17
	if (params?.customer_oid) {
18
		queryParams.append('customer_oid', params.customer_oid)
19
	}
20

21
	if (params?.order) {
22
		queryParams.append('order', params.order)
23
	}
24

25
	if (params?.page) {
26
		queryParams.append('page', params.page.toString())
27
	}
28

29
	if (params?.per_page) {
30
		queryParams.append('per_page', params.per_page.toString())
31
	}
32

33
	const endpoint = `https://api.baremetrics.com/v1/${sourceId}/subscriptions?${queryParams.toString()}`
34

35
	const response = await fetch(endpoint, {
36
		method: 'GET',
37
		headers: {
38
			'Content-Type': 'application/json',
39
			Accept: 'application/json',
40
			Authorization: `Bearer ${resource.apiKey}`
41
		}
42
	})
43

44
	if (!response.ok) {
45
		throw new Error(`HTTP error! status: ${response.status}`)
46
	}
47

48
	const data = await response.json()
49

50
	return data
51
}
52