1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Get keywords |
7 | * Get a list of keywords based on the filters provided. |
8 | */ |
9 | export async function main( |
10 | auth: Pinterest, |
11 | ad_account_id: string, |
12 | campaign_id: string | undefined, |
13 | ad_group_id: string | undefined, |
14 | match_types: string | undefined, |
15 | page_size: string | undefined, |
16 | bookmark: string | undefined, |
17 | ) { |
18 | const url = new URL( |
19 | `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/keywords`, |
20 | ); |
21 | for (const [k, v] of [ |
22 | ["campaign_id", campaign_id], |
23 | ["ad_group_id", ad_group_id], |
24 | ["match_types", match_types], |
25 | ["page_size", page_size], |
26 | ["bookmark", bookmark], |
27 | ]) { |
28 | if (v !== undefined && v !== "" && k !== undefined) { |
29 | url.searchParams.append(k, v); |
30 | } |
31 | } |
32 | const response = await fetch(url, { |
33 | method: "GET", |
34 | headers: { |
35 | Authorization: "Bearer " + auth.token, |
36 | }, |
37 | body: undefined, |
38 | }); |
39 | if (!response.ok) { |
40 | const text = await response.text(); |
41 | throw new Error(`${response.status} ${text}`); |
42 | } |
43 | return await response.json(); |
44 | } |
45 |
|