1 | |
2 | |
3 | * Retrieve body metrics for a given user ID |
4 | * Fetches body metrics such as weight, height, body fat percentage etc. for a given user ID |
5 | */ |
6 | export async function main( |
7 | auth: RT.Terra, |
8 | user_id: string | undefined, |
9 | start_date: string | undefined, |
10 | end_date?: string | undefined, |
11 | to_webhook?: string | undefined, |
12 | with_samples?: string | undefined |
13 | ) { |
14 | const url = new URL(`https://api.tryterra.co/v2/body`) |
15 | for (const [k, v] of [ |
16 | ['user_id', user_id], |
17 | ['start_date', start_date], |
18 | ['end_date', end_date], |
19 | ['to_webhook', to_webhook], |
20 | ['with_samples', with_samples] |
21 | ]) { |
22 | if (v !== undefined && v !== '') { |
23 | url.searchParams.append(k, v) |
24 | } |
25 | } |
26 | const response = await fetch(url, { |
27 | method: 'GET', |
28 | headers: { |
29 | 'dev-id': auth.devId, |
30 | 'X-api-key': auth.apiKey |
31 | }, |
32 | body: undefined |
33 | }) |
34 | if (!response.ok) { |
35 | const text = await response.text() |
36 | throw new Error(`${response.status} ${text}`) |
37 | } |
38 | return await response.json() |
39 | } |
40 |
|