Delete a registered device's update token
One script reply has been approved by the moderators Verified

Delete a device details object.

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