//native
type Ably = {
apiKey: string;
};
/**
* Subscribe a device to a channel
* Subscribe either a single device or all devices associated with a client ID to receive push notifications from messages sent to a channel.
*/
export async function main(
auth: Ably,
format: "json" | "jsonp" | "msgpack" | "html" | undefined,
X_Ably_Version: string,
body:
| { channel?: string; deviceId?: string }
| { channel?: string; clientId?: string },
) {
const url = new URL(`https://rest.ably.io/push/channelSubscriptions`);
for (const [k, v] of [["format", format]]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "POST",
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 185 days ago