Creates a namespace
One script reply has been approved by the moderators Verified

Creates a namespace for the specified application ID.

Created by hugo697 185 days ago
Submitted by hugo697 Bun
Verified 185 days ago
1
//native
2
type Ably = {
3
  accessToken: string;
4
};
5
/**
6
 * Creates a namespace
7
 * Creates a namespace for the specified application ID.
8
 */
9
export async function main(
10
  auth: Ably,
11
  app_id: string,
12
  body: {
13
    id: string;
14
    authenticated?: false | true;
15
    persisted?: false | true;
16
    persistLast?: false | true;
17
    pushEnabled?: false | true;
18
    batchingEnabled?: false | true;
19
    batchingPolicy?: string;
20
    batchingInterval?: number;
21
    tlsOnly?: false | true;
22
    exposeTimeserial?: false | true;
23
  },
24
) {
25
  const url = new URL(`https://control.ably.net/v1/apps/${app_id}/namespaces`);
26

27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      "Content-Type": "application/json",
31
      Authorization: "Bearer " + auth.accessToken,
32
    },
33
    body: JSON.stringify(body),
34
  });
35
  if (!response.ok) {
36
    const text = await response.text();
37
    throw new Error(`${response.status} ${text}`);
38
  }
39
  return await response.json();
40
}
41