0
Update Customer
One script reply has been approved by the moderators Verified
Created by hugo697 111 days ago Viewed 35 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
	oid: string,
9
	body: {
10
		name?: string
11
		email?: string
12
		notes?: string
13
	}
14
) {
15
	if (!body.name && !body.email && !body.notes) {
16
		throw new Error('At least one property (name, email, or notes) is required in the body.')
17
	}
18

19
	const endpoint = `https://api.baremetrics.com/v1/${sourceId}/customers/${oid}`
20

21
	const response = await fetch(endpoint, {
22
		method: 'PUT',
23
		headers: {
24
			'Content-Type': 'application/json',
25
			Accept: 'application/json',
26
			Authorization: `Bearer ${resource.apiKey}`
27
		},
28
		body: JSON.stringify(body)
29
	})
30

31
	if (!response.ok) {
32
		throw new Error(`HTTP error! status: ${response.status}`)
33
	}
34

35
	const data = await response.json()
36

37
	return data.customer
38
}
39