From bd48dedb9aa4d8eaa2e4ebb3e0cb47816617758f Mon Sep 17 00:00:00 2001 From: Lukasz Ostrowski Date: Tue, 12 Dec 2023 12:48:07 +0100 Subject: [PATCH] Fix Vercel KV APL data set and get by removing serialization (#331) --- .changeset/smart-candles-wink.md | 5 +++++ src/APL/vercel-kv/vercel-kv-apl.test.ts | 6 ++---- src/APL/vercel-kv/vercel-kv-apl.ts | 14 +++++--------- 3 files changed, 12 insertions(+), 13 deletions(-) create mode 100644 .changeset/smart-candles-wink.md diff --git a/.changeset/smart-candles-wink.md b/.changeset/smart-candles-wink.md new file mode 100644 index 00000000..ccd84048 --- /dev/null +++ b/.changeset/smart-candles-wink.md @@ -0,0 +1,5 @@ +--- +"@saleor/app-sdk": patch +--- + +Removed JSON.parse from VercelKVAPL, used raw js objects to read and write them diff --git a/src/APL/vercel-kv/vercel-kv-apl.test.ts b/src/APL/vercel-kv/vercel-kv-apl.test.ts index b4bcbfde..927b3013 100644 --- a/src/APL/vercel-kv/vercel-kv-apl.test.ts +++ b/src/APL/vercel-kv/vercel-kv-apl.test.ts @@ -26,8 +26,6 @@ const getMockAuthData = (saleorApiUrl = "https://demo.saleor.io/graphql"): AuthD jwks: "{}", }); -const getMockAuthDataString = () => JSON.stringify(getMockAuthData()); - const APP_NAME_NAMESPACE = "test-app"; describe("VercelKvApl", () => { @@ -51,7 +49,7 @@ describe("VercelKvApl", () => { describe("get", () => { it("returns parsed auth data", async () => { - (kv.hget as Mock).mockImplementationOnce(async () => getMockAuthDataString()); + (kv.hget as Mock).mockImplementationOnce(async () => getMockAuthData()); const apl = new VercelKvApl(); @@ -68,7 +66,7 @@ describe("VercelKvApl", () => { await apl.set(getMockAuthData()); expect(kv.hset).toHaveBeenCalledWith(APP_NAME_NAMESPACE, { - "https://demo.saleor.io/graphql": getMockAuthDataString(), + "https://demo.saleor.io/graphql": getMockAuthData(), }); }); }); diff --git a/src/APL/vercel-kv/vercel-kv-apl.ts b/src/APL/vercel-kv/vercel-kv-apl.ts index faaceaf0..8844d4ec 100644 --- a/src/APL/vercel-kv/vercel-kv-apl.ts +++ b/src/APL/vercel-kv/vercel-kv-apl.ts @@ -30,9 +30,9 @@ export class VercelKvApl implements APL { this.debug("Will call Vercel KV to get auth data for %s", saleorApiUrl); try { - const authData = await kv.hget(this.hashCollectionKey, saleorApiUrl); + const authData = await kv.hget(this.hashCollectionKey, saleorApiUrl); - return authData ? (JSON.parse(authData) as AuthData) : undefined; + return authData ?? undefined; } catch (e) { this.debug("Failed to get auth data from Vercel KV"); this.debug(e); @@ -46,7 +46,7 @@ export class VercelKvApl implements APL { try { await kv.hset(this.hashCollectionKey, { - [authData.saleorApiUrl]: JSON.stringify(authData), + [authData.saleorApiUrl]: authData, }); } catch (e) { this.debug("Failed to set auth data in Vercel KV"); @@ -70,17 +70,13 @@ export class VercelKvApl implements APL { } async getAll() { - const results = await kv.hgetall>(this.hashCollectionKey); + const results = await kv.hgetall>(this.hashCollectionKey); if (results === null) { throw new Error("Missing KV collection, data was never written"); } - return Object.values(results).map((item) => { - const authData = JSON.parse(item) as AuthData; - - return authData; - }); + return Object.values(results); } async isReady(): Promise {