0

Retrieve Location Photos

by
Published Nov 5, 2024

Retrieves photos for a specific location.

Script tripadvisor Verified

The script

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

6
export async function main(
7
	resource: Tripadvisor,
8
	locationId: string,
9
	options?: {
10
		language: string
11
		limit: number
12
		offset: number
13
		source: string
14
	}
15
) {
16
	const queryParams = new URLSearchParams({
17
		key: resource.apiKey
18
	})
19

20
	if (options?.language) {
21
		queryParams.append('language', options.language)
22
	}
23

24
	if (options?.limit) {
25
		queryParams.append('limit', options.limit.toString())
26
	}
27

28
	if (options?.offset) {
29
		queryParams.append('offset', options.offset.toString())
30
	}
31

32
	if (options?.source) {
33
		queryParams.append('source', options.source)
34
	}
35

36
	const endpoint = `https://api.content.tripadvisor.com/api/v1/location/${locationId}/photos?${queryParams.toString()}`
37

38
	const response = await fetch(endpoint, {
39
		method: 'GET',
40
		headers: {
41
			accept: 'application/json'
42
		}
43
	})
44

45
	if (!response.ok) {
46
		throw new Error(`HTTP error! status: ${response.status}`)
47
	}
48

49
	const data = await response.json()
50

51
	return data
52
}
53