lint, fix redis creation
This commit is contained in:
parent
2c47290eff
commit
ed04747def
3 changed files with 38 additions and 57 deletions
|
@ -1,7 +1,7 @@
|
||||||
#changelog
|
## #changelog
|
||||||
---
|
|
||||||
"app-sdk": minor
|
## "app-sdk": minor
|
||||||
---
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- `src/APL/index.ts`: added redis-apl export
|
- `src/APL/index.ts`: added redis-apl export
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
|
import Redis from "ioredis-mock";
|
||||||
import { afterEach, describe, expect, it } from "vitest";
|
import { afterEach, describe, expect, it } from "vitest";
|
||||||
import Redis from "ioredis-mock"
|
|
||||||
|
|
||||||
import { AuthData } from "./apl";
|
import { AuthData } from "./apl";
|
||||||
import { RedisAPL } from "./redis-apl";
|
import { RedisAPL } from "./redis-apl";
|
||||||
|
|
||||||
const appApiBaseUrl = "https://localhost:4321/"
|
const appApiBaseUrl = "https://localhost:4321/";
|
||||||
const redisClient = new Redis()
|
const redisClient = new Redis();
|
||||||
const stubAuthData: AuthData = {
|
const stubAuthData: AuthData = {
|
||||||
domain: "example.com",
|
domain: "example.com",
|
||||||
token: "example-token",
|
token: "example-token",
|
||||||
|
@ -16,20 +16,20 @@ const stubAuthData: AuthData = {
|
||||||
|
|
||||||
describe("APL", () => {
|
describe("APL", () => {
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
const apl = new RedisAPL({ client: redisClient, appApiBaseUrl: appApiBaseUrl });
|
const apl = new RedisAPL({ client: redisClient, appApiBaseUrl });
|
||||||
await apl.delete(stubAuthData.saleorApiUrl);
|
await apl.delete(stubAuthData.saleorApiUrl);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("redisAPL", () => {
|
describe("redisAPL", () => {
|
||||||
describe("get", () => {
|
describe("get", () => {
|
||||||
it("Returns auth data for existing api url", async () => {
|
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);
|
await apl.set(stubAuthData);
|
||||||
expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData);
|
expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Returns undefined for unknown api url", async () => {
|
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();
|
expect(await apl.get("unknown-domain.example.com")).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
@ -37,7 +37,7 @@ describe("APL", () => {
|
||||||
|
|
||||||
describe("set", () => {
|
describe("set", () => {
|
||||||
it("should save to redis and return value afterwards", async () => {
|
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);
|
await apl.set(stubAuthData);
|
||||||
expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData);
|
expect(await apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData);
|
||||||
|
@ -46,14 +46,14 @@ describe("APL", () => {
|
||||||
|
|
||||||
describe("delete", () => {
|
describe("delete", () => {
|
||||||
it("Should delete when called with known domain", async () => {
|
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);
|
await apl.delete(stubAuthData.saleorApiUrl);
|
||||||
expect(await apl.get(stubAuthData.saleorApiUrl)).toBeUndefined();
|
expect(await apl.get(stubAuthData.saleorApiUrl)).toBeUndefined();
|
||||||
});
|
});
|
||||||
it("Should not delete when called with different domain", async () => {
|
it("Should not delete when called with different domain", async () => {
|
||||||
const apl = new RedisAPL({ client: redisClient, appApiBaseUrl: appApiBaseUrl });
|
const apl = new RedisAPL({ client: redisClient, appApiBaseUrl });
|
||||||
const otherAppApiBaseUrl = "https://localhost:4322/"
|
const otherAppApiBaseUrl = "https://localhost:4322/";
|
||||||
const apl2 = new RedisAPL({ client: redisClient, appApiBaseUrl: otherAppApiBaseUrl });
|
const apl2 = new RedisAPL({ client: redisClient, appApiBaseUrl: otherAppApiBaseUrl });
|
||||||
|
|
||||||
const otherStubAuthData: AuthData = {
|
const otherStubAuthData: AuthData = {
|
||||||
|
@ -61,11 +61,11 @@ describe("APL", () => {
|
||||||
token: "Another token",
|
token: "Another token",
|
||||||
domain: "example.net",
|
domain: "example.net",
|
||||||
jwks: "{}",
|
jwks: "{}",
|
||||||
appId: "22"
|
appId: "22",
|
||||||
}
|
};
|
||||||
|
|
||||||
await apl.set(stubAuthData)
|
await apl.set(stubAuthData);
|
||||||
await apl2.set(otherStubAuthData)
|
await apl2.set(otherStubAuthData);
|
||||||
|
|
||||||
/* good for debugging if something breaks :)
|
/* good for debugging if something breaks :)
|
||||||
await redisClient.keys("*", (err, keys) => {
|
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 apl.get(stubAuthData.saleorApiUrl)).toStrictEqual(stubAuthData);
|
||||||
expect(await apl2.get(otherStubAuthData.saleorApiUrl)).toStrictEqual(otherStubAuthData);
|
expect(await apl2.get(otherStubAuthData.saleorApiUrl)).toStrictEqual(otherStubAuthData);
|
||||||
await apl.delete(stubAuthData.saleorApiUrl);
|
await apl.delete(stubAuthData.saleorApiUrl);
|
||||||
|
|
|
@ -6,56 +6,37 @@ import { createAPLDebug } from "./apl-debug";
|
||||||
const debug = createAPLDebug("RedisAPL");
|
const debug = createAPLDebug("RedisAPL");
|
||||||
|
|
||||||
export type RedisAPLClientArgs = {
|
export type RedisAPLClientArgs = {
|
||||||
client: Redis,
|
client: Redis;
|
||||||
appApiBaseUrl: string
|
appApiBaseUrl: string;
|
||||||
}
|
};
|
||||||
export type RedisAPLUrlArgs = {
|
export type RedisAPLUrlArgs = {
|
||||||
redisUrl: URL
|
redisUrl: string;
|
||||||
appApiBaseUrl: string
|
appApiBaseUrl: string;
|
||||||
}
|
};
|
||||||
/**
|
/**
|
||||||
* Redis APL
|
* Redis APL
|
||||||
* @param redisUrl - in format redis[s]://[[username][:password]@][host][:port][/db-number],
|
* @param redisUrl - in format redis[s]://[[username][:password]@][host][:port][/db-number],
|
||||||
* so for example redis://alice:foobared@awesome.redis.server:6380
|
* 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 {
|
export class RedisAPL implements APL {
|
||||||
private client;
|
private client;
|
||||||
|
|
||||||
private appApiBaseUrl;
|
private appApiBaseUrl;
|
||||||
|
|
||||||
constructor(args: RedisAPLClientArgs | RedisAPLUrlArgs) {
|
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;
|
this.appApiBaseUrl = args.appApiBaseUrl;
|
||||||
|
|
||||||
if (('client' in args) && args.client) {
|
if ("client" in args && args.client) {
|
||||||
this.client = args.client
|
this.client = args.client;
|
||||||
debug("RedisAPL: created redis client");
|
debug("RedisAPL: created redis client");
|
||||||
}
|
} else if ("redisUrl" in args && args.redisUrl) {
|
||||||
else if (('redisUrl' in args) && args.redisUrl) {
|
this.client = new Redis(args.redisUrl, { lazyConnect: true });
|
||||||
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
|
|
||||||
});
|
|
||||||
debug("RedisAPL: created redis client");
|
debug("RedisAPL: created redis client");
|
||||||
}
|
} else {
|
||||||
else {
|
throw new Error("RedisAPL: No redis url or client defined");
|
||||||
throw new Error("RedisAPL: No redis url or client defined")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,13 +77,13 @@ export class RedisAPL implements APL {
|
||||||
}
|
}
|
||||||
|
|
||||||
async isReady(): Promise<AplReadyResult> {
|
async isReady(): Promise<AplReadyResult> {
|
||||||
const ready = await this.client.info() ? true : false
|
const ready = !!(await this.client.info());
|
||||||
await this.client.quit();
|
await this.client.quit();
|
||||||
return { ready: ready } as AplReadyResult;
|
return { ready } as AplReadyResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
async isConfigured(): Promise<AplConfiguredResult> {
|
async isConfigured(): Promise<AplConfiguredResult> {
|
||||||
const ready = await this.client.info() ? true : false
|
const ready = !!(await this.client.info());
|
||||||
await this.client.quit();
|
await this.client.quit();
|
||||||
return { configured: ready } as AplConfiguredResult;
|
return { configured: ready } as AplConfiguredResult;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue