diff --git a/.changeset/honky-dory-fox.md b/.changeset/honky-dory-fox.md index dfe6e00d..75709cd6 100644 --- a/.changeset/honky-dory-fox.md +++ b/.changeset/honky-dory-fox.md @@ -1,7 +1,7 @@ -#changelog ---- -"app-sdk": minor ---- +## #changelog + +## "app-sdk": minor + ### Added - `src/APL/index.ts`: added redis-apl export diff --git a/src/APL/redis-apl.test.ts b/src/APL/redis-apl.test.ts index b8cf1792..010e475b 100644 --- a/src/APL/redis-apl.test.ts +++ b/src/APL/redis-apl.test.ts @@ -1,11 +1,11 @@ +import Redis from "ioredis-mock"; import { afterEach, describe, expect, it } from "vitest"; -import Redis from "ioredis-mock" import { AuthData } from "./apl"; import { RedisAPL } from "./redis-apl"; -const appApiBaseUrl = "https://localhost:4321/" -const redisClient = new Redis() +const appApiBaseUrl = "https://localhost:4321/"; +const redisClient = new Redis(); const stubAuthData: AuthData = { domain: "example.com", token: "example-token", @@ -16,20 +16,20 @@ const stubAuthData: AuthData = { describe("APL", () => { afterEach(async () => { - const apl = new RedisAPL({ client: redisClient, appApiBaseUrl: appApiBaseUrl }); + const apl = new RedisAPL({ client: redisClient, appApiBaseUrl }); await apl.delete(stubAuthData.saleorApiUrl); }); describe("redisAPL", () => { describe("get", () => { it("Returns auth data for existing api url", async () => { - const apl = new RedisAPL({ client: redisClient, appApiBaseUrl: appApiBaseUrl }); + const apl = new RedisAPL({ client: redisClient, appApiBaseUrl }); await apl.set(stubAuthData); expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData); }); it("Returns undefined for unknown api url", async () => { - const apl = new RedisAPL({ client: redisClient, appApiBaseUrl: appApiBaseUrl }); + const apl = new RedisAPL({ client: redisClient, appApiBaseUrl }); expect(await apl.get("unknown-domain.example.com")).toBeUndefined(); }); @@ -37,7 +37,7 @@ describe("APL", () => { describe("set", () => { it("should save to redis and return value afterwards", async () => { - const apl = new RedisAPL({ client: redisClient, appApiBaseUrl: appApiBaseUrl }); + const apl = new RedisAPL({ client: redisClient, appApiBaseUrl }); await apl.set(stubAuthData); expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData); @@ -46,14 +46,14 @@ describe("APL", () => { describe("delete", () => { it("Should delete when called with known domain", async () => { - const apl = new RedisAPL({ client: redisClient, appApiBaseUrl: appApiBaseUrl }); + const apl = new RedisAPL({ client: redisClient, appApiBaseUrl }); await apl.delete(stubAuthData.saleorApiUrl); expect(await apl.get(stubAuthData.saleorApiUrl)).toBeUndefined(); }); it("Should not delete when called with different domain", async () => { - const apl = new RedisAPL({ client: redisClient, appApiBaseUrl: appApiBaseUrl }); - const otherAppApiBaseUrl = "https://localhost:4322/" + const apl = new RedisAPL({ client: redisClient, appApiBaseUrl }); + const otherAppApiBaseUrl = "https://localhost:4322/"; const apl2 = new RedisAPL({ client: redisClient, appApiBaseUrl: otherAppApiBaseUrl }); const otherStubAuthData: AuthData = { @@ -61,11 +61,11 @@ describe("APL", () => { token: "Another token", domain: "example.net", jwks: "{}", - appId: "22" - } + appId: "22", + }; - await apl.set(stubAuthData) - await apl2.set(otherStubAuthData) + await apl.set(stubAuthData); + await apl2.set(otherStubAuthData); /* good for debugging if something breaks :) await redisClient.keys("*", (err, keys) => { @@ -78,7 +78,7 @@ describe("APL", () => { } }) */ - expect(stubAuthData != otherStubAuthData).toBeTruthy() + expect(stubAuthData != otherStubAuthData).toBeTruthy(); expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData); expect(await apl2.get(otherStubAuthData.saleorApiUrl)).toStrictEqual(otherStubAuthData); await apl.delete(stubAuthData.saleorApiUrl); diff --git a/src/APL/redis-apl.ts b/src/APL/redis-apl.ts index fe597196..0c0eeb17 100644 --- a/src/APL/redis-apl.ts +++ b/src/APL/redis-apl.ts @@ -6,56 +6,37 @@ import { createAPLDebug } from "./apl-debug"; const debug = createAPLDebug("RedisAPL"); export type RedisAPLClientArgs = { - client: Redis, - appApiBaseUrl: string -} + client: Redis; + appApiBaseUrl: string; +}; export type RedisAPLUrlArgs = { - redisUrl: URL - appApiBaseUrl: string -} + redisUrl: string; + appApiBaseUrl: string; +}; /** * Redis APL * @param redisUrl - in format redis[s]://[[username][:password]@][host][:port][/db-number], * so for example redis://alice:foobared@awesome.redis.server:6380 - * For saleor-platform, thats: `redis://redis:6379/1` + * For saleor-platform, thats: `redis://redis:6379/2` */ export class RedisAPL implements APL { private client; + private appApiBaseUrl; constructor(args: RedisAPLClientArgs | RedisAPLUrlArgs) { - if (!args.appApiBaseUrl) throw new Error("The RedisAPL requires to know the app api url beforehand"); + if (!args.appApiBaseUrl) + throw new Error("The RedisAPL requires to know the app api url beforehand"); this.appApiBaseUrl = args.appApiBaseUrl; - if (('client' in args) && args.client) { - this.client = args.client + if ("client" in args && args.client) { + this.client = args.client; debug("RedisAPL: created redis client"); - } - else if (('redisUrl' in args) && args.redisUrl) { - let redisUrl = args.redisUrl - let port, db; - - if (redisUrl.pathname) { - const parsed_port = parseInt(redisUrl.pathname) - db = typeof parsed_port === "number" ? parsed_port : undefined - } - if (redisUrl.port) { - const parsed_port = parseInt(redisUrl.port) - port = typeof parsed_port === "number" ? parsed_port : undefined - } - - this.client = new Redis({ - port: port, - host: redisUrl.host, - username: redisUrl.username, - password: redisUrl.password, - db: db, - lazyConnect: true - }); + } else if ("redisUrl" in args && args.redisUrl) { + this.client = new Redis(args.redisUrl, { lazyConnect: true }); debug("RedisAPL: created redis client"); - } - else { - throw new Error("RedisAPL: No redis url or client defined") + } else { + throw new Error("RedisAPL: No redis url or client defined"); } } @@ -96,13 +77,13 @@ export class RedisAPL implements APL { } async isReady(): Promise { - const ready = await this.client.info() ? true : false + const ready = !!(await this.client.info()); await this.client.quit(); - return { ready: ready } as AplReadyResult; + return { ready } as AplReadyResult; } async isConfigured(): Promise { - const ready = await this.client.info() ? true : false + const ready = !!(await this.client.info()); await this.client.quit(); return { configured: ready } as AplConfiguredResult; }