1 | |
2 | type Pinterest = { |
3 | token: string; |
4 | }; |
5 | |
6 | * Create Pin |
7 | * Create a Pin on a board or board section owned by the "operation user_account". |
8 | */ |
9 | export async function main( |
10 | auth: Pinterest, |
11 | ad_account_id: string | undefined, |
12 | body: { |
13 | id?: string; |
14 | created_at?: string; |
15 | link?: string; |
16 | title?: string; |
17 | description?: string; |
18 | dominant_color?: string; |
19 | alt_text?: string; |
20 | board_id?: string; |
21 | board_section_id?: string; |
22 | board_owner?: { username?: string }; |
23 | media?: { media_type?: string }; |
24 | media_source?: |
25 | | { |
26 | source_type: "image_base64"; |
27 | content_type: "image/jpeg" | "image/png"; |
28 | data: string; |
29 | is_standard?: false | true; |
30 | } |
31 | | { source_type: "image_url"; url: string; is_standard?: false | true } |
32 | | { |
33 | source_type: "video_id"; |
34 | cover_image_url?: string; |
35 | cover_image_content_type?: "image/jpeg" | "image/png"; |
36 | cover_image_data?: string; |
37 | media_id: string; |
38 | is_standard?: false | true; |
39 | } |
40 | | { |
41 | source_type?: "multiple_image_base64"; |
42 | items: { |
43 | title?: string; |
44 | description?: string; |
45 | link?: string; |
46 | content_type: "image/jpeg" | "image/png"; |
47 | data: string; |
48 | }[]; |
49 | index?: number; |
50 | } |
51 | | { |
52 | source_type?: "multiple_image_urls"; |
53 | items: { |
54 | title?: string; |
55 | description?: string; |
56 | link?: string; |
57 | url: string; |
58 | }[]; |
59 | index?: number; |
60 | } |
61 | | { source_type: "pin_url"; is_affiliate_link?: false | true }; |
62 | parent_pin_id?: string; |
63 | note?: string; |
64 | }, |
65 | ) { |
66 | const url = new URL(`https://api.pinterest.com/v5/pins`); |
67 | for (const [k, v] of [["ad_account_id", ad_account_id]]) { |
68 | if (v !== undefined && v !== "" && k !== undefined) { |
69 | url.searchParams.append(k, v); |
70 | } |
71 | } |
72 | const response = await fetch(url, { |
73 | method: "POST", |
74 | headers: { |
75 | "Content-Type": "application/json", |
76 | Authorization: "Bearer " + auth.token, |
77 | }, |
78 | body: JSON.stringify(body), |
79 | }); |
80 | if (!response.ok) { |
81 | const text = await response.text(); |
82 | throw new Error(`${response.status} ${text}`); |
83 | } |
84 | return await response.json(); |
85 | } |
86 |
|