0
Project query
One script reply has been approved by the moderators Verified

Query collections at a project level.

Created by hugo697 52 days ago Viewed 636 times
0
Submitted by hugo697 Bun
Verified 52 days ago
1
//native
2
type Botify = {
3
	token: string
4
}
5
/**
6
 * Project query
7
 * Query collections at a project level.
8
 */
9
export async function main(
10
	auth: Botify,
11
	username: string,
12
	project_slug: string,
13
	page: string | undefined,
14
	size: string | undefined
15
) {
16
	const url = new URL(`https://api.botify.com/projects/${username}/${project_slug}/query`)
17
	for (const [k, v] of [
18
		['page', page],
19
		['size', size]
20
	]) {
21
		if (v !== undefined && v !== '' && k !== undefined) {
22
			url.searchParams.append(k, v)
23
		}
24
	}
25
	const response = await fetch(url, {
26
		method: 'POST',
27
		headers: {
28
			Authorization: 'Token ' + auth.token
29
		},
30
		body: undefined
31
	})
32
	if (!response.ok) {
33
		const text = await response.text()
34
		throw new Error(`${response.status} ${text}`)
35
	}
36
	return await response.text()
37
}
38