Edits history of script submission #11566 for ' Get historical usage for a group (bitly)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Bitly = {
      token: string;
    };
    /**
     * Get historical usage for a group
     * Get a group's historical usage for specific limit(s) and date range. Refer to GET /v4/groups/{group_guid}/feature_usage endpoint response for available limit names.
     */
    export async function main(
      auth: Bitly,
      group_guid: string,
      name: string | undefined,
      start_date: string | undefined,
      end_date: string | undefined,
      unit: "day" | "week" | "month" | undefined,
    ) {
      const url = new URL(
        `https://api-ssl.bitly.com/v4/groups/${group_guid}/historical_usage`,
      );
      for (const [k, v] of [
        ["name", name],
        ["start_date", start_date],
        ["end_date", end_date],
        ["unit", unit],
      ]) {
        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 428 days ago