Nuxt is based on Vue.js and I find their building blocks to be much neater compared to the React based Next.js.
26 lines
815 B
TypeScript
26 lines
815 B
TypeScript
import { readFile, writeFile } from "fs/promises";
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
|
let subscriptions: PushSubscriptionJSON[];
|
|
try {
|
|
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
|
|
} catch (err: any) {
|
|
if (err.code !== "ENOENT") {
|
|
throw err;
|
|
}
|
|
subscriptions = [];
|
|
}
|
|
const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint);
|
|
if (existingIndex !== -1) {
|
|
subscriptions.splice(existingIndex, 1);
|
|
} else {
|
|
return { message: "No subscription registered."};
|
|
}
|
|
await writeFile(
|
|
"push-subscriptions.json",
|
|
JSON.stringify(subscriptions, undefined, "\t"),
|
|
"utf-8"
|
|
);
|
|
return { message: "Existing subscription removed."};
|
|
})
|