diff --git a/lib/caching.ts b/lib/caching.ts index 3ee20e1..b51afa0 100644 --- a/lib/caching.ts +++ b/lib/caching.ts @@ -10,7 +10,7 @@ async function initialFetch(options: { id: string }): Promise { if (!series) { return null; } - const stored = storage.write(series); + const stored = storage.writeSeries(series); if (!stored) { console.error(`Failed to store series ${options.id}`); return null; @@ -50,7 +50,7 @@ async function updateFetch(existingSeries: Series): Promise { - const seriesFromStorage = await storage.read(options); + const seriesFromStorage = await storage.readSeries(options); /** * We don't have the feed in storage, diff --git a/lib/storage.test.ts b/lib/storage.test.ts index 602374f..214aee0 100644 --- a/lib/storage.test.ts +++ b/lib/storage.test.ts @@ -4,18 +4,18 @@ import { testUtils } from "./test-utils.ts"; Deno.test("can store a series", async () => { const series = testUtils.generateSeries(); - await storage.write(series); + await storage.writeSeries(series); - const readSeries = await storage.read(series); + const readSeries = await storage.readSeries(series); assertEquals(readSeries, series); }); Deno.test("can retrieve a series", async () => { const series = testUtils.generateSeries(); - await storage.write(series); + await storage.writeSeries(series); - const readSeries = await storage.read(series); + const readSeries = await storage.readSeries(series); assertEquals(readSeries, series); }); diff --git a/lib/storage.ts b/lib/storage.ts index c90d0d6..b9fe4d3 100644 --- a/lib/storage.ts +++ b/lib/storage.ts @@ -18,26 +18,43 @@ export type Series = { episodes: Episode[]; }; +export type VippsAgreement = { + id: string; + agreementId: string; + userEmail: string; + createdAt: Date; + revokedAt: Date | null; +}; + const kv = await Deno.openKv(); -function seriesKey(series: { id: string }) { - return ["series", series.id]; +type Identifiable = { id: string }; +type Collection = "series" | "vipps-agreements"; + +function read(collection: Collection) { + return async function (series: Identifiable) { + const key = [collection, series.id]; + const read = await kv.get(key); + return read.value as T | null; + }; } -async function readSeries(options: { id: string }): Promise { - const { id } = options; - const key = seriesKey({ id }); - const read = await kv.get(key); - return read.value; +function write(collection: Collection) { + return async function (entity: T) { + const key = [collection, entity.id]; + const stored = await kv.set(key, entity); + return stored.ok; + }; } -async function writeSeries(series: Series): Promise { - const key = seriesKey(series); - const stored = await kv.set(key, series); - return stored.ok; -} +const readSeries = read("series"); +const writeSeries = write("series"); +const readVippsAgreement = read("vipps-agreements"); +const writeVippsAgreement = write("vipps-agreements"); export const storage = { readSeries, writeSeries, + readVippsAgreement, + writeVippsAgreement, };