Edits history of script submission #11205 for ' Create Pin (pinterest)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Pinterest = {
      token: string;
    };
    /**
     * Create Pin
     * Create a Pin on a board or board section owned by the "operation user_account".
     */
    export async function main(
      auth: Pinterest,
      ad_account_id: string | undefined,
      body: {
        id?: string;
        created_at?: string;
        link?: string;
        title?: string;
        description?: string;
        dominant_color?: string;
        alt_text?: string;
        board_id?: string;
        board_section_id?: string;
        board_owner?: { username?: string };
        media?: { media_type?: string };
        media_source?:
          | {
              source_type: "image_base64";
              content_type: "image/jpeg" | "image/png";
              data: string;
              is_standard?: false | true;
            }
          | { source_type: "image_url"; url: string; is_standard?: false | true }
          | {
              source_type: "video_id";
              cover_image_url?: string;
              cover_image_content_type?: "image/jpeg" | "image/png";
              cover_image_data?: string;
              media_id: string;
              is_standard?: false | true;
            }
          | {
              source_type?: "multiple_image_base64";
              items: {
                title?: string;
                description?: string;
                link?: string;
                content_type: "image/jpeg" | "image/png";
                data: string;
              }[];
              index?: number;
            }
          | {
              source_type?: "multiple_image_urls";
              items: {
                title?: string;
                description?: string;
                link?: string;
                url: string;
              }[];
              index?: number;
            }
          | { source_type: "pin_url"; is_affiliate_link?: false | true };
        parent_pin_id?: string;
        note?: string;
      },
    ) {
      const url = new URL(`https://api.pinterest.com/v5/pins`);
      for (const [k, v] of [["ad_account_id", ad_account_id]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 536 days ago