Edits history of script submission #14014 for ' Enumerate all active channels of the application (ably)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Ably = {
      apiKey: string;
    };
    /**
     * Enumerate all active channels of the application
     * Enumerate all active channels of the application
     */
    export async function main(
      auth: Ably,
      format: "json" | "jsonp" | "msgpack" | "html" | undefined,
      limit: string | undefined,
      prefix: string | undefined,
      by: "value" | "id" | undefined,
      X_Ably_Version: string,
    ) {
      const url = new URL(`https://rest.ably.io/channels`);
      for (const [k, v] of [
        ["format", format],
        ["limit", limit],
        ["prefix", prefix],
        ["by", by],
      ]) {
        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