Edits history of script submission #14042 for ' Update a device registration (ably)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Ably = {
      apiKey: string;
    };
    /**
     * Update a device registration
     * Specific attributes of an existing registration can be updated. Only clientId, metadata and push.recipient are mutable.
     */
    export async function main(
      auth: Ably,
      device_id: string,
      format: "json" | "jsonp" | "msgpack" | "html" | undefined,
      X_Ably_Version: string,
      body: {
        id?: string;
        clientId?: string;
        formFactor?:
          | "phone"
          | "tablet"
          | "desktop"
          | "tv"
          | "watch"
          | "car"
          | "embedded";
        metadata?: {};
        platform?: "ios" | "android" | "browser";
        deviceSecret?: string;
        "push.recipient"?: {
          transportType?: "apns" | "fcm" | "gcm" | "web";
          deviceToken?: string;
          registrationToken?: string;
          encryptionKey?: {};
          clientId?: string;
          deviceId?: string;
        };
        "push.state"?: "Active" | "Failing" | "Failed";
      },
    ) {
      const url = new URL(
        `https://rest.ably.io/push/deviceRegistrations/${device_id}`,
      );
      for (const [k, v] of [["format", format]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "PATCH",
        headers: {
          "X-Ably-Version": X_Ably_Version,
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.apiKey,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 220 days ago