1 | |
2 | type Motimate = { |
3 | token: string |
4 | } |
5 |
|
6 | export async function main( |
7 | auth: Motimate, |
8 | id: string, |
9 | identifier_type: 'id' | 'external_id' | undefined, |
10 | include: string | undefined |
11 | ) { |
12 | const url = new URL(`https://motimateapp.com/public_api/motis/${id}/chapters`) |
13 |
|
14 | for (const [k, v] of [ |
15 | ['identifier_type', identifier_type], |
16 | ['include', include] |
17 | ]) { |
18 | if (v !== undefined && v !== '' && k !== undefined) { |
19 | url.searchParams.append(k, v) |
20 | } |
21 | } |
22 |
|
23 | const response = await fetch(url, { |
24 | method: 'GET', |
25 | headers: { |
26 | Authorization: 'Bearer ' + auth.token |
27 | } |
28 | }) |
29 |
|
30 | if (!response.ok) { |
31 | const text = await response.text() |
32 | throw new Error(`${response.status} ${text}`) |
33 | } |
34 |
|
35 | return await response.json() |
36 | } |
37 |
|