Skip to content

Commit

Permalink
Fix Vercel KV APL data set and get by removing serialization (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkostrowski authored Dec 12, 2023
1 parent c707e10 commit bd48ded
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-candles-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@saleor/app-sdk": patch
---

Removed JSON.parse from VercelKVAPL, used raw js objects to read and write them
6 changes: 2 additions & 4 deletions src/APL/vercel-kv/vercel-kv-apl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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();

Expand All @@ -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(),
});
});
});
Expand Down
14 changes: 5 additions & 9 deletions src/APL/vercel-kv/vercel-kv-apl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(this.hashCollectionKey, saleorApiUrl);
const authData = await kv.hget<AuthData>(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);
Expand All @@ -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");
Expand All @@ -70,17 +70,13 @@ export class VercelKvApl implements APL {
}

async getAll() {
const results = await kv.hgetall<Record<string, string>>(this.hashCollectionKey);
const results = await kv.hgetall<Record<string, AuthData>>(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<AplReadyResult> {
Expand Down

0 comments on commit bd48ded

Please sign in to comment.