Updates app's APNs info from a `.p12` file

Updates the application's Apple Push Notification service (APNs) information.

Script ably Verified

by hugo697 ยท 10/17/2025

The script

Submitted by hugo697 Bun
Verified 220 days ago
1
//native
2
type Ably = {
3
  accessToken: string;
4
};
5
type Base64 = string;
6
/**
7
 * Updates app's APNs info from a `.p12` file
8
 * Updates the application's Apple Push Notification service (APNs) information.
9
 */
10
export async function main(
11
  auth: Ably,
12
  id: string,
13
  body: {
14
    p12File: {
15
      base64: Base64;
16
      type:
17
        | "image/png"
18
        | "image/jpeg"
19
        | "image/gif"
20
        | "application/pdf"
21
        | "appication/json"
22
        | "text/csv"
23
        | "text/plain"
24
        | "audio/mpeg"
25
        | "audio/wav"
26
        | "video/mp4";
27
      name: string;
28
    };
29
    p12Pass: string;
30
  },
31
) {
32
  const url = new URL(`https://control.ably.net/v1/apps/${id}/pkcs12`);
33

34
  const formData = new FormData();
35
  for (const [k, v] of Object.entries(body)) {
36
    if (v !== undefined && v !== "") {
37
      if (["p12File"].includes(k)) {
38
        const { base64, type, name } = v as {
39
          base64: Base64;
40
          type: string;
41
          name: string;
42
        };
43
        formData.append(
44
          k,
45
          new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
46
            type,
47
          }),
48
          name,
49
        );
50
      } else {
51
        formData.append(k, String(v));
52
      }
53
    }
54
  }
55
  const response = await fetch(url, {
56
    method: "POST",
57
    headers: {
58
      Authorization: "Bearer " + auth.accessToken,
59
    },
60
    body: formData,
61
  });
62
  if (!response.ok) {
63
    const text = await response.text();
64
    throw new Error(`${response.status} ${text}`);
65
  }
66
  return await response.json();
67
}
68