Place the actual push subscription data into a push property on the subscription so that other properties can be added to it.
16 lines
624 B
TypeScript
16 lines
624 B
TypeScript
import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
|
const subscriptions = await readSubscriptions();
|
|
const existingIndex = subscriptions.findIndex(
|
|
sub => sub.type === "push" && sub.push.endpoint === body.subscription.endpoint
|
|
);
|
|
if (existingIndex !== -1) {
|
|
subscriptions.splice(existingIndex, 1);
|
|
} else {
|
|
return { message: "No subscription registered."};
|
|
}
|
|
await writeSubscriptions(subscriptions);
|
|
return { message: "Existing subscription removed."};
|
|
})
|