2023-10-02 14:27:08 +00:00
|
|
|
import Redis from "ioredis";
|
2023-09-30 18:21:30 +00:00
|
|
|
|
|
|
|
import { APL, AplConfiguredResult, AplReadyResult, AuthData } from "./apl";
|
|
|
|
import { createAPLDebug } from "./apl-debug";
|
|
|
|
|
2023-10-02 14:27:08 +00:00
|
|
|
const debug = createAPLDebug("RedisAPL");
|
2023-09-30 18:21:30 +00:00
|
|
|
|
2023-10-02 14:27:08 +00:00
|
|
|
export type RedisAPLClientArgs = {
|
2023-10-04 15:28:33 +00:00
|
|
|
client: Redis;
|
|
|
|
appApiBaseUrl: string;
|
|
|
|
};
|
2023-10-02 14:27:08 +00:00
|
|
|
export type RedisAPLUrlArgs = {
|
2023-10-04 15:28:33 +00:00
|
|
|
redisUrl: string;
|
|
|
|
appApiBaseUrl: string;
|
|
|
|
};
|
2023-09-30 18:21:30 +00:00
|
|
|
/**
|
|
|
|
* Redis APL
|
|
|
|
* @param redisUrl - in format redis[s]://[[username][:password]@][host][:port][/db-number],
|
|
|
|
* so for example redis://alice:foobared@awesome.redis.server:6380
|
2023-10-04 15:28:33 +00:00
|
|
|
* For saleor-platform, thats: `redis://redis:6379/2`
|
2023-09-30 18:21:30 +00:00
|
|
|
*/
|
|
|
|
export class RedisAPL implements APL {
|
|
|
|
private client;
|
2023-10-04 15:28:33 +00:00
|
|
|
|
2023-09-30 19:50:49 +00:00
|
|
|
private appApiBaseUrl;
|
2023-09-30 18:21:30 +00:00
|
|
|
|
2023-10-02 14:27:08 +00:00
|
|
|
constructor(args: RedisAPLClientArgs | RedisAPLUrlArgs) {
|
2023-10-04 15:28:33 +00:00
|
|
|
if (!args.appApiBaseUrl)
|
|
|
|
throw new Error("The RedisAPL requires to know the app api url beforehand");
|
2023-10-02 14:27:08 +00:00
|
|
|
this.appApiBaseUrl = args.appApiBaseUrl;
|
|
|
|
|
2023-10-04 15:28:33 +00:00
|
|
|
if ("client" in args && args.client) {
|
|
|
|
this.client = args.client;
|
2023-10-02 14:27:08 +00:00
|
|
|
debug("RedisAPL: created redis client");
|
2023-10-04 15:28:33 +00:00
|
|
|
} else if ("redisUrl" in args && args.redisUrl) {
|
|
|
|
this.client = new Redis(args.redisUrl, { lazyConnect: true });
|
2023-10-02 14:27:08 +00:00
|
|
|
debug("RedisAPL: created redis client");
|
2023-10-04 15:28:33 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("RedisAPL: No redis url or client defined");
|
2023-10-02 14:27:08 +00:00
|
|
|
}
|
2023-09-30 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private prepareKey(saleorApiUrl: string) {
|
2023-09-30 19:50:49 +00:00
|
|
|
return `${this.appApiBaseUrl}:${saleorApiUrl}`;
|
2023-09-30 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async get(saleorApiUrl: string): Promise<AuthData | undefined> {
|
|
|
|
try {
|
|
|
|
const res = await this.client.get(this.prepareKey(saleorApiUrl));
|
|
|
|
debug("RedisAPL: get - received: %j", res);
|
|
|
|
if (res) {
|
2023-10-02 14:27:08 +00:00
|
|
|
await this.client.quit();
|
2023-09-30 18:21:30 +00:00
|
|
|
return JSON.parse(res) as AuthData;
|
2023-09-30 18:30:50 +00:00
|
|
|
}
|
2023-10-02 14:27:08 +00:00
|
|
|
await this.client.quit();
|
2023-09-30 18:30:50 +00:00
|
|
|
return undefined;
|
2023-09-30 18:21:30 +00:00
|
|
|
} catch (e) {
|
2023-10-02 14:27:08 +00:00
|
|
|
await this.client.quit();
|
2023-09-30 18:21:30 +00:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async set(authData: AuthData): Promise<void> {
|
|
|
|
await this.client.set(this.prepareKey(authData.saleorApiUrl), JSON.stringify(authData));
|
|
|
|
debug("RedisAPL: set - set sucessfully: %j", authData);
|
2023-10-02 14:27:08 +00:00
|
|
|
await this.client.quit();
|
2023-09-30 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async delete(saleorApiUrl: string): Promise<void> {
|
2023-10-02 14:27:08 +00:00
|
|
|
const val = await this.client.getdel(this.prepareKey(saleorApiUrl));
|
2023-09-30 18:21:30 +00:00
|
|
|
debug("RedisAPL: del - deleted successfuly: %j", val);
|
2023-10-02 14:27:08 +00:00
|
|
|
await this.client.quit();
|
2023-09-30 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<AuthData[]> {
|
|
|
|
throw new Error("redisAPL does not support getAll method");
|
|
|
|
}
|
|
|
|
|
|
|
|
async isReady(): Promise<AplReadyResult> {
|
2023-10-04 15:28:33 +00:00
|
|
|
const ready = !!(await this.client.info());
|
2023-10-02 14:27:08 +00:00
|
|
|
await this.client.quit();
|
2023-10-04 15:28:33 +00:00
|
|
|
return { ready } as AplReadyResult;
|
2023-09-30 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async isConfigured(): Promise<AplConfiguredResult> {
|
2023-10-04 15:28:33 +00:00
|
|
|
const ready = !!(await this.client.info());
|
2023-10-02 14:27:08 +00:00
|
|
|
await this.client.quit();
|
|
|
|
return { configured: ready } as AplConfiguredResult;
|
2023-09-30 18:21:30 +00:00
|
|
|
}
|
|
|
|
}
|