Edits history of script submission #14037 for ' Retrieve usage statistics for an application (ably)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Ably = {
      apiKey: string;
    };
    /**
     * Retrieve usage statistics for an application
     * The Ably system can be queried to obtain usage statistics for a given application, and results are provided aggregated across all channels in use in the application in the specified period. Stats may be used to track usage against account quotas.
     */
    export async function main(
      auth: Ably,
      format: "json" | "jsonp" | "msgpack" | "html" | undefined,
      start: string | undefined,
      limit: string | undefined,
      end: string | undefined,
      direction: "forwards" | "backwards" | undefined,
      unit: "minute" | "hour" | "day" | "month" | undefined,
      X_Ably_Version: string,
    ) {
      const url = new URL(`https://rest.ably.io/stats`);
      for (const [k, v] of [
        ["format", format],
        ["start", start],
        ["limit", limit],
        ["end", end],
        ["direction", direction],
        ["unit", unit],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          "X-Ably-Version": X_Ably_Version,
          Authorization: "Bearer " + auth.apiKey,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 220 days ago