1 | |
2 | type Shutterstock = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Search for tracks |
7 | * This endpoint searches for tracks. If you specify more than one search parameter, the API uses an AND condition. Array parameters can be specified multiple times; in this case, the API uses an AND or an OR condition with those values, depending on the parameter. |
8 | */ |
9 | export async function main( |
10 | auth: Shutterstock, |
11 | artists: string | undefined, |
12 | bpm: string | undefined, |
13 | bpm_from: string | undefined, |
14 | bpm_to: string | undefined, |
15 | duration: string | undefined, |
16 | duration_from: string | undefined, |
17 | duration_to: string | undefined, |
18 | genre: string | undefined, |
19 | is_instrumental: string | undefined, |
20 | instruments: string | undefined, |
21 | moods: string | undefined, |
22 | page: string | undefined, |
23 | per_page: string | undefined, |
24 | query: string | undefined, |
25 | sort: |
26 | | "score" |
27 | | "ranking_all" |
28 | | "artist" |
29 | | "title" |
30 | | "bpm" |
31 | | "freshness" |
32 | | "duration" |
33 | | undefined, |
34 | sort_order: "asc" | "desc" | undefined, |
35 | vocal_description: string | undefined, |
36 | view: "minimal" | "full" | undefined, |
37 | fields: string | undefined, |
38 | library: "shutterstock" | "premier" | undefined, |
39 | language: string | undefined, |
40 | ) { |
41 | const url = new URL(`https://api.shutterstock.com/v2/audio/search`); |
42 | for (const [k, v] of [ |
43 | ["artists", artists], |
44 | ["bpm", bpm], |
45 | ["bpm_from", bpm_from], |
46 | ["bpm_to", bpm_to], |
47 | ["duration", duration], |
48 | ["duration_from", duration_from], |
49 | ["duration_to", duration_to], |
50 | ["genre", genre], |
51 | ["is_instrumental", is_instrumental], |
52 | ["instruments", instruments], |
53 | ["moods", moods], |
54 | ["page", page], |
55 | ["per_page", per_page], |
56 | ["query", query], |
57 | ["sort", sort], |
58 | ["sort_order", sort_order], |
59 | ["vocal_description", vocal_description], |
60 | ["view", view], |
61 | ["fields", fields], |
62 | ["library", library], |
63 | ["language", language], |
64 | ]) { |
65 | if (v !== undefined && v !== "" && k !== undefined) { |
66 | url.searchParams.append(k, v); |
67 | } |
68 | } |
69 | const response = await fetch(url, { |
70 | method: "GET", |
71 | headers: { |
72 | Authorization: "Bearer " + auth.token, |
73 | }, |
74 | body: undefined, |
75 | }); |
76 | if (!response.ok) { |
77 | const text = await response.text(); |
78 | throw new Error(`${response.status} ${text}`); |
79 | } |
80 | return await response.json(); |
81 | } |
82 |
|