0

getTicket

by
Published Apr 18, 2023

get a ticket from hubspot

Script hubspot
  • Submitted by sindre svendby964 Deno
    Created 1166 days ago
    1
    // Ctrl+. to cache dependencies on imports hover, Ctrl+S to format.
    2
    
    
    3
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts";
    4
    
    
    5
    type HS<TObj extends readonly string[]> = {
    6
      id: string;
    7
      createdAt: string;
    8
      updatedAt: string;
    9
      archived: boolean;
    10
      properties:
    11
        & {
    12
          createdate: string;
    13
          hs_lastmodifieddate: string;
    14
          hs_object_id: string;
    15
        }
    16
        & {
    17
          [TIndex in TObj[number]]: string;
    18
        };
    19
    };
    20
    
    
    21
    export async function main<Props extends readonly string[]>(
    22
      hubspot: wmill.Resource<"hubspot">,
    23
      ticketId: number,
    24
      properties: Props,
    25
    ): Promise<HS<Props>> {
    26
      let props;
    27
      if (properties.length > 0) {
    28
        props = "?properties=" + properties.join();
    29
      } else {
    30
        props = "";
    31
      }
    32
      const res = await fetch(
    33
        "https://api.hubapi.com/crm/v3/objects/tickets/" + ticketId + props,
    34
        {
    35
          headers: {
    36
            Authorization: `Bearer ${hubspot.token}`,
    37
            Accept: "application/json",
    38
            "content-type": "application/json",
    39
          },
    40
          method: "GET",
    41
        },
    42
      );
    43
      if (!res.ok) {
    44
        console.error(res);
    45
        throw new Error(res.statusText);
    46
      }
    47
      const jsonRes = await res.json();
    48
      if (jsonRes.status == "error") {
    49
        console.error(jsonRes);
    50
        throw new Error(jsonRes.message);
    51
      }
    52
      return jsonRes;
    53
    }
    54