0

Get a list of all templates

by
Published Dec 20, 2024
Script docspring Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Docspring = {
3
	tokenId: string
4
	tokenSecret: string
5
}
6
/**
7
 * Get a list of all templates
8
 *
9
 */
10
export async function main(
11
	auth: Docspring,
12
	query: string | undefined,
13
	parent_folder_id: string | undefined,
14
	page: string | undefined,
15
	per_page: string | undefined
16
) {
17
	const url = new URL(`https://api.docspring.com/api/v1/templates`)
18

19
	const token = btoa(auth.tokenId + ':' + auth.tokenSecret)
20

21
	for (const [k, v] of [
22
		['query', query],
23
		['parent_folder_id', parent_folder_id],
24
		['page', page],
25
		['per_page', per_page]
26
	]) {
27
		if (v !== undefined && v !== '' && k !== undefined) {
28
			url.searchParams.append(k, v)
29
		}
30
	}
31
	const response = await fetch(url, {
32
		method: 'GET',
33
		headers: {
34
			Authorization: `Basic ${token}`
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.text()
43
}
44