Refactor subscription format
Place the actual push subscription data into a push property on the subscription so that other properties can be added to it.
This commit is contained in:
parent
b4934005ae
commit
abdcc83eb9
5 changed files with 29 additions and 11 deletions
|
@ -1,6 +1,7 @@
|
|||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { Schedule } from "~/shared/types/schedule";
|
||||
import { generateDemoSchedule } from "./generate-demo-schedule";
|
||||
import { Subscription } from "~/shared/types/account";
|
||||
|
||||
// For this demo I'm just storing the runtime data in JSON files. When making
|
||||
// this into proper application this should be replaced with an actual database.
|
||||
|
@ -29,10 +30,14 @@ export async function writeSchedule(schedule: Schedule) {
|
|||
}
|
||||
|
||||
export async function readSubscriptions() {
|
||||
let subscriptions = await readJson<PushSubscriptionJSON[]>(subscriptionsPath, []);
|
||||
let subscriptions = await readJson<Subscription[]>(subscriptionsPath, []);
|
||||
if (subscriptions.length && "keys" in subscriptions[0]) {
|
||||
// Discard old format
|
||||
subscriptions = [];
|
||||
}
|
||||
return subscriptions;
|
||||
}
|
||||
|
||||
export async function writeSubscriptions(subscriptions: PushSubscriptionJSON[]) {
|
||||
export async function writeSubscriptions(subscriptions: Subscription[]) {
|
||||
await writeFile(subscriptionsPath, JSON.stringify(subscriptions, undefined, "\t") + "\n", "utf-8");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue