Get metadata of a channel
One script reply has been approved by the moderators Verified

Get metadata of a channel

Created by hugo697 138 days ago
Submitted by hugo697 Bun
Verified 138 days ago
1
//native
2
type Ably = {
3
  apiKey: string;
4
};
5
/**
6
 * Get metadata of a channel
7
 * Get metadata of a channel
8
 */
9
export async function main(
10
  auth: Ably,
11
  channel_id: string,
12
  format: "json" | "jsonp" | "msgpack" | "html" | undefined,
13
  X_Ably_Version: string,
14
) {
15
  const url = new URL(`https://rest.ably.io/channels/${channel_id}`);
16
  for (const [k, v] of [["format", format]]) {
17
    if (v !== undefined && v !== "" && k !== undefined) {
18
      url.searchParams.append(k, v);
19
    }
20
  }
21
  const response = await fetch(url, {
22
    method: "GET",
23
    headers: {
24
      "X-Ably-Version": X_Ably_Version,
25
      Authorization: "Bearer " + auth.apiKey,
26
    },
27
    body: undefined,
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.json();
34
}
35