1 | |
2 | type Yelp = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Event Search |
7 | * Returns events that match search criteria |
8 | */ |
9 | export async function main( |
10 | auth: Yelp, |
11 | locale: string | undefined, |
12 | offset: string | undefined, |
13 | limit: string | undefined, |
14 | sort_by: "asc" | "desc" | undefined, |
15 | sort_on: "popularity" | "time_start" | undefined, |
16 | start_date: string | undefined, |
17 | end_date: string | undefined, |
18 | categories: string | undefined, |
19 | is_free: string | undefined, |
20 | excluded_events: string | undefined, |
21 | location: string | undefined, |
22 | latitude: string | undefined, |
23 | longitude: string | undefined, |
24 | radius: string | undefined, |
25 | ) { |
26 | const url = new URL(`https://api.yelp.com//v3/events`); |
27 | for (const [k, v] of [ |
28 | ["locale", locale], |
29 | ["offset", offset], |
30 | ["limit", limit], |
31 | ["sort_by", sort_by], |
32 | ["sort_on", sort_on], |
33 | ["start_date", start_date], |
34 | ["end_date", end_date], |
35 | ["categories", categories], |
36 | ["is_free", is_free], |
37 | ["excluded_events", excluded_events], |
38 | ["location", location], |
39 | ["latitude", latitude], |
40 | ["longitude", longitude], |
41 | ["radius", radius], |
42 | ]) { |
43 | if (v !== undefined && v !== "" && k !== undefined) { |
44 | url.searchParams.append(k, v); |
45 | } |
46 | } |
47 | const response = await fetch(url, { |
48 | method: "GET", |
49 | headers: { |
50 | Authorization: "Bearer " + auth.apiKey, |
51 | }, |
52 | body: undefined, |
53 | }); |
54 | if (!response.ok) { |
55 | const text = await response.text(); |
56 | throw new Error(`${response.status} ${text}`); |
57 | } |
58 | return await response.json(); |
59 | } |
60 |
|