Get a device registration
One script reply has been approved by the moderators Verified

Get the full details of a device.

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 a device registration
7
 * Get the full details of a device.
8
 */
9
export async function main(
10
  auth: Ably,
11
  device_id: string,
12
  format: "json" | "jsonp" | "msgpack" | "html" | undefined,
13
  X_Ably_Version: string,
14
) {
15
  const url = new URL(
16
    `https://rest.ably.io/push/deviceRegistrations/${device_id}`,
17
  );
18
  for (const [k, v] of [["format", format]]) {
19
    if (v !== undefined && v !== "" && k !== undefined) {
20
      url.searchParams.append(k, v);
21
    }
22
  }
23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      "X-Ably-Version": X_Ably_Version,
27
      Authorization: "Bearer " + auth.apiKey,
28
    },
29
    body: undefined,
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.json();
36
}
37