0

List all submissions for a given template

by
Published Dec 20, 2024
Script docspring Verified

The script

Submitted by hugo697 Bun
Verified 537 days ago
1
//native
2
type Docspring = {
3
	tokenId: string
4
	tokenSecret: string
5
}
6
/**
7
 * List all submissions for a given template
8
 *
9
 */
10
export async function main(
11
	auth: Docspring,
12
	template_id: string,
13
	cursor: string | undefined,
14
	limit: string | undefined,
15
	created_after: string | undefined,
16
	created_before: string | undefined,
17
	type: string | undefined,
18
	include_data: string | undefined
19
) {
20
	const url = new URL(`https://api.docspring.com/api/v1/templates/${template_id}/submissions`)
21

22
	const token = btoa(auth.tokenId + ':' + auth.tokenSecret)
23

24
	for (const [k, v] of [
25
		['cursor', cursor],
26
		['limit', limit],
27
		['created_after', created_after],
28
		['created_before', created_before],
29
		['type', type],
30
		['include_data', include_data]
31
	]) {
32
		if (v !== undefined && v !== '' && k !== undefined) {
33
			url.searchParams.append(k, v)
34
		}
35
	}
36
	const response = await fetch(url, {
37
		method: 'GET',
38
		headers: {
39
			Authorization: `Basic ${token}`
40
		},
41
		body: undefined
42
	})
43
	if (!response.ok) {
44
		const text = await response.text()
45
		throw new Error(`${response.status} ${text}`)
46
	}
47
	return await response.text()
48
}
49