1 | |
2 | type Shutterstock = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Search editorial images |
7 | * This endpoint searches for editorial images. If you specify more than one search parameter, the API uses an AND condition. For example, if you set the `category` parameter to "Alone,Performing" and also specify a `query` parameter, the results include only images that match the query and are in both the Alone and Performing categories. You can also filter search terms out in the `query` parameter by prefixing the term with NOT. |
8 | */ |
9 | export async function main( |
10 | auth: Shutterstock, |
11 | query: string | undefined, |
12 | sort: "relevant" | "newest" | "oldest" | undefined, |
13 | category: string | undefined, |
14 | country: string | undefined, |
15 | supplier_code: string | undefined, |
16 | date_start: string | undefined, |
17 | date_end: string | undefined, |
18 | per_page: string | undefined, |
19 | cursor: string | undefined, |
20 | ) { |
21 | const url = new URL( |
22 | `https://api.shutterstock.com/v2/editorial/images/search`, |
23 | ); |
24 | for (const [k, v] of [ |
25 | ["query", query], |
26 | ["sort", sort], |
27 | ["category", category], |
28 | ["country", country], |
29 | ["supplier_code", supplier_code], |
30 | ["date_start", date_start], |
31 | ["date_end", date_end], |
32 | ["per_page", per_page], |
33 | ["cursor", cursor], |
34 | ]) { |
35 | if (v !== undefined && v !== "" && k !== undefined) { |
36 | url.searchParams.append(k, v); |
37 | } |
38 | } |
39 | const response = await fetch(url, { |
40 | method: "GET", |
41 | headers: { |
42 | Authorization: "Bearer " + auth.token, |
43 | }, |
44 | body: undefined, |
45 | }); |
46 | if (!response.ok) { |
47 | const text = await response.text(); |
48 | throw new Error(`${response.status} ${text}`); |
49 | } |
50 | return await response.json(); |
51 | } |
52 |
|