0

Fetch Positions

by
Published Nov 5, 2024

Fetches Positions.

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
	filter_id_: string | undefined,
9
	filter_external_id_: string | undefined,
10
	filter_assignment_id_: string | undefined,
11
	search_name_: string | undefined,
12
	filter_created_at_lteq_: string | undefined,
13
	filter_updated_at_lteq_: string | undefined
14
) {
15
	const url = new URL(`https://motimateapp.com/public_api/positions`)
16

17
	for (const [k, v] of [
18
		['filter[id]', filter_id_],
19
		['filter[external_id]', filter_external_id_],
20
		['filter[assignment_id]', filter_assignment_id_],
21
		['search[name]', search_name_],
22
		['filter[created_at_lteq]', filter_created_at_lteq_],
23
		['filter[updated_at_lteq]', filter_updated_at_lteq_]
24
	]) {
25
		if (v !== undefined && v !== '' && k !== undefined) {
26
			url.searchParams.append(k, v)
27
		}
28
	}
29

30
	const response = await fetch(url, {
31
		method: 'GET',
32
		headers: {
33
			Authorization: 'Bearer ' + auth.token
34
		}
35
	})
36

37
	if (!response.ok) {
38
		const text = await response.text()
39
		throw new Error(`${response.status} ${text}`)
40
	}
41

42
	return await response.json()
43
}
44