Skip to content

Commit

Permalink
lint, fix redis creation
Browse files Browse the repository at this point in the history
  • Loading branch information
djkato committed Oct 4, 2023
1 parent 2c47290 commit ed04747
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 57 deletions.
8 changes: 4 additions & 4 deletions .changeset/honky-dory-fox.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#changelog
---
"app-sdk": minor
---
## #changelog

## "app-sdk": minor

### Added

- `src/APL/index.ts`: added redis-apl export
Expand Down
30 changes: 15 additions & 15 deletions src/APL/redis-apl.test.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -16,28 +16,28 @@ 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();
});
});

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);
Expand All @@ -46,26 +46,26 @@ 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 = {
saleorApiUrl: "https://example.com/graphql",
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) => {
Expand All @@ -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);
Expand Down
57 changes: 19 additions & 38 deletions src/APL/redis-apl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]: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");
}
}

Expand Down Expand Up @@ -96,13 +77,13 @@ export class RedisAPL implements APL {
}

async isReady(): Promise<AplReadyResult> {
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<AplConfiguredResult> {
const ready = await this.client.info() ? true : false
const ready = !!(await this.client.info());
await this.client.quit();
return { configured: ready } as AplConfiguredResult;
}
Expand Down

0 comments on commit ed04747

Please sign in to comment.