Edits history of script submission #17997 for ' Perform a search using a specific field from an item type (pipedrive)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Pipedrive = {
      apiToken: string;
    };
    /**
     * Perform a search using a specific field from an item type
     * Performs a search from the values of a specific field. Results can either be the distinct values of the field (useful for searching autocomplete field values), or the IDs of actual items (deals, leads, persons, organizations or products).
     */
    export async function main(
      auth: Pipedrive,
      term: string | undefined,
      entity_type:
        | "deal"
        | "lead"
        | "person"
        | "organization"
        | "product"
        | "project"
        | undefined,
      match: "exact" | "beginning" | "middle" | undefined,
      field: string | undefined,
      limit: string | undefined,
      cursor: string | undefined,
    ) {
      const url = new URL(`https://api.pipedrive.com/api/v2/itemSearch/field`);
      for (const [k, v] of [
        ["term", term],
        ["entity_type", entity_type],
        ["match", match],
        ["field", field],
        ["limit", limit],
        ["cursor", cursor],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          "x-api-token": auth.apiToken,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago