0

Fetch Learning Path Sections

by
Published Nov 5, 2024

Fetches Sections of an associated Learning Path. Ordered (ascending) by Section's `position`.

Script motimate Verified

The script

Submitted by hugo697 Bun
Verified 581 days ago
1
//native
2
type Motimate = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Motimate,
8
	learninig_path_id: string,
9
	identifier_type: 'id' | 'external_id' | undefined
10
) {
11
	const url = new URL(
12
		`https://motimateapp.com/public_api/learning_paths/${learninig_path_id}/sections`
13
	)
14

15
	for (const [k, v] of [['identifier_type', identifier_type]]) {
16
		if (v !== undefined && v !== '' && k !== undefined) {
17
			url.searchParams.append(k, v)
18
		}
19
	}
20

21
	const response = await fetch(url, {
22
		method: 'GET',
23
		headers: {
24
			Authorization: 'Bearer ' + auth.token
25
		}
26
	})
27

28
	if (!response.ok) {
29
		const text = await response.text()
30
		throw new Error(`${response.status} ${text}`)
31
	}
32

33
	return await response.json()
34
}
35