Edits history of script submission #11374 for ' Search pins by a given search term (pinterest)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Pinterest = {
      token: string;
    };
    /**
     * Search pins by a given search term
     * This endpoint is currently in beta and not available to all apps. Learn more.
    
    Get the top 10 Pins by a given search term.
     */
    export async function main(
      auth: Pinterest,
      term: string | undefined,
      country_code: string | undefined,
      bookmark: string | undefined,
      locale: string | undefined,
      limit: string | undefined,
    ) {
      const url = new URL(`https://api.pinterest.com/v5/search/partner/pins`);
      for (const [k, v] of [
        ["term", term],
        ["country_code", country_code],
        ["bookmark", bookmark],
        ["locale", locale],
        ["limit", limit],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 536 days ago