Port application from Next.js to Nuxt
Nuxt is based on Vue.js and I find their building blocks to be much neater compared to the React based Next.js.
This commit is contained in:
parent
8c8b561f1a
commit
250ca9a1ac
45 changed files with 662 additions and 1358 deletions
29
server/api/subscribe.post.ts
Normal file
29
server/api/subscribe.post.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { readFile, writeFile } from "node: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[existingIndex] = body.subscription;
|
||||
} else {
|
||||
subscriptions.push(body.subscription);
|
||||
}
|
||||
await writeFile(
|
||||
"push-subscriptions.json",
|
||||
JSON.stringify(subscriptions, undefined, "\t"),
|
||||
"utf-8"
|
||||
);
|
||||
if (existingIndex !== -1) {
|
||||
return { message: "Existing subscription refreshed."};
|
||||
}
|
||||
return { message: "New subscription registered."};
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue