0

Build catalogs report

by
Published Dec 20, 2024

Async request to create a report of the catalog owned by the "operation user_account".

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Build catalogs report
7
 * Async request to create a report of the catalog owned by the "operation user_account".
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  ad_account_id: string | undefined,
12
  body:
13
    | ({ catalog_type: "RETAIL" | "HOTEL" | "CREATIVE_ASSETS" } & {
14
        catalog_type: "RETAIL";
15
        report:
16
          | {
17
              report_type: "FEED_INGESTION_ISSUES";
18
              feed_id: string;
19
              processing_result_id?: string;
20
            }
21
          | { report_type: "DISTRIBUTION_ISSUES"; catalog_id?: string };
22
      })
23
    | ({ catalog_type: "RETAIL" | "HOTEL" | "CREATIVE_ASSETS" } & {
24
        catalog_type: "HOTEL";
25
        report:
26
          | {
27
              report_type: "FEED_INGESTION_ISSUES";
28
              feed_id: string;
29
              processing_result_id?: string;
30
            }
31
          | { report_type: "DISTRIBUTION_ISSUES"; catalog_id?: string };
32
      }),
33
) {
34
  const url = new URL(`https://api.pinterest.com/v5/catalogs/reports`);
35
  for (const [k, v] of [["ad_account_id", ad_account_id]]) {
36
    if (v !== undefined && v !== "" && k !== undefined) {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "POST",
42
    headers: {
43
      "Content-Type": "application/json",
44
      Authorization: "Bearer " + auth.token,
45
    },
46
    body: JSON.stringify(body),
47
  });
48
  if (!response.ok) {
49
    const text = await response.text();
50
    throw new Error(`${response.status} ${text}`);
51
  }
52
  return await response.json();
53
}
54