1 | |
2 | type Shutterstock = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Search for images |
7 | * This endpoint searches for images. |
8 | */ |
9 | export async function main( |
10 | auth: Shutterstock, |
11 | library: string | undefined, |
12 | added_date: string | undefined, |
13 | added_date_start: string | undefined, |
14 | aspect_ratio_min: string | undefined, |
15 | aspect_ratio_max: string | undefined, |
16 | aspect_ratio: string | undefined, |
17 | added_date_end: string | undefined, |
18 | category: string | undefined, |
19 | color: string | undefined, |
20 | contributor: string | undefined, |
21 | contributor_country: string | undefined, |
22 | fields: string | undefined, |
23 | height: string | undefined, |
24 | height_from: string | undefined, |
25 | height_to: string | undefined, |
26 | image_type: string | undefined, |
27 | keyword_safe_search: string | undefined, |
28 | language: |
29 | | "ar" |
30 | | "bg" |
31 | | "bn" |
32 | | "cs" |
33 | | "da" |
34 | | "de" |
35 | | "el" |
36 | | "en" |
37 | | "es" |
38 | | "fi" |
39 | | "fr" |
40 | | "gu" |
41 | | "he" |
42 | | "hi" |
43 | | "hr" |
44 | | "hu" |
45 | | "id" |
46 | | "it" |
47 | | "ja" |
48 | | "kn" |
49 | | "ko" |
50 | | "ml" |
51 | | "mr" |
52 | | "nb" |
53 | | "nl" |
54 | | "or" |
55 | | "pl" |
56 | | "pt" |
57 | | "ro" |
58 | | "ru" |
59 | | "sk" |
60 | | "sl" |
61 | | "sv" |
62 | | "ta" |
63 | | "te" |
64 | | "th" |
65 | | "tr" |
66 | | "uk" |
67 | | "ur" |
68 | | "vi" |
69 | | "zh" |
70 | | "zh-Hant" |
71 | | undefined, |
72 | license: string | undefined, |
73 | model: string | undefined, |
74 | orientation: "horizontal" | "vertical" | undefined, |
75 | page: string | undefined, |
76 | per_page: string | undefined, |
77 | people_model_released: string | undefined, |
78 | people_age: |
79 | | "infants" |
80 | | "children" |
81 | | "teenagers" |
82 | | "20s" |
83 | | "30s" |
84 | | "40s" |
85 | | "50s" |
86 | | "60s" |
87 | | "older" |
88 | | undefined, |
89 | people_ethnicity: string | undefined, |
90 | people_gender: "male" | "female" | "both" | undefined, |
91 | people_number: string | undefined, |
92 | query: string | undefined, |
93 | region: string | undefined, |
94 | safe: string | undefined, |
95 | sort: "newest" | "popular" | "relevance" | "random" | undefined, |
96 | spellcheck_query: string | undefined, |
97 | view: "minimal" | "full" | undefined, |
98 | width: string | undefined, |
99 | width_from: string | undefined, |
100 | width_to: string | undefined, |
101 | ) { |
102 | const url = new URL(`https://api.shutterstock.com/v2/images/search`); |
103 | for (const [k, v] of [ |
104 | ["library", library], |
105 | ["added_date", added_date], |
106 | ["added_date_start", added_date_start], |
107 | ["aspect_ratio_min", aspect_ratio_min], |
108 | ["aspect_ratio_max", aspect_ratio_max], |
109 | ["aspect_ratio", aspect_ratio], |
110 | ["added_date_end", added_date_end], |
111 | ["category", category], |
112 | ["color", color], |
113 | ["contributor", contributor], |
114 | ["contributor_country", contributor_country], |
115 | ["fields", fields], |
116 | ["height", height], |
117 | ["height_from", height_from], |
118 | ["height_to", height_to], |
119 | ["image_type", image_type], |
120 | ["keyword_safe_search", keyword_safe_search], |
121 | ["language", language], |
122 | ["license", license], |
123 | ["model", model], |
124 | ["orientation", orientation], |
125 | ["page", page], |
126 | ["per_page", per_page], |
127 | ["people_model_released", people_model_released], |
128 | ["people_age", people_age], |
129 | ["people_ethnicity", people_ethnicity], |
130 | ["people_gender", people_gender], |
131 | ["people_number", people_number], |
132 | ["query", query], |
133 | ["region", region], |
134 | ["safe", safe], |
135 | ["sort", sort], |
136 | ["spellcheck_query", spellcheck_query], |
137 | ["view", view], |
138 | ["width", width], |
139 | ["width_from", width_from], |
140 | ["width_to", width_to], |
141 | ]) { |
142 | if (v !== undefined && v !== "" && k !== undefined) { |
143 | url.searchParams.append(k, v); |
144 | } |
145 | } |
146 | const response = await fetch(url, { |
147 | method: "GET", |
148 | headers: { |
149 | Authorization: "Bearer " + auth.token, |
150 | }, |
151 | body: undefined, |
152 | }); |
153 | if (!response.ok) { |
154 | const text = await response.text(); |
155 | throw new Error(`${response.status} ${text}`); |
156 | } |
157 | return await response.json(); |
158 | } |
159 |
|