Edits history of script submission #14017 for ' Get metadata of a channel (ably)'

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