UNPKG

@saleor/app-sdk

Version:
182 lines (180 loc) 5.73 kB
import { OTEL_APL_SERVICE_NAME, getOtelTracer } from "../../chunk-UCTHLSSD.mjs"; import { createAPLDebug } from "../../chunk-ORQVZRNL.mjs"; import "../../chunk-CPDLIPGD.mjs"; // src/APL/redis/redis-apl.ts import { SpanKind, SpanStatusCode } from "@opentelemetry/api"; import { SemanticAttributes } from "@opentelemetry/semantic-conventions"; var RedisAPL = class { constructor(config) { this.debug = createAPLDebug("RedisAPL"); this.tracer = getOtelTracer(); this.client = config.client; this.hashCollectionKey = config.hashCollectionKey || "saleor_app_auth"; this.debug("Redis APL initialized"); } async ensureConnection() { if (!this.client.isOpen) { this.debug("Connecting to Redis..."); await this.client.connect(); this.debug("Connected to Redis"); } } async get(saleorApiUrl) { await this.ensureConnection(); this.debug("Will get auth data from Redis for %s", saleorApiUrl); return this.tracer.startActiveSpan( "RedisAPL.get", { attributes: { saleorApiUrl, [SemanticAttributes.PEER_SERVICE]: OTEL_APL_SERVICE_NAME }, kind: SpanKind.CLIENT }, async (span) => { try { const authData = await this.client.hGet(this.hashCollectionKey, saleorApiUrl); this.debug("Received response from Redis"); if (!authData) { this.debug("AuthData is empty for %s", saleorApiUrl); span.setStatus({ code: SpanStatusCode.OK }).end(); return void 0; } const parsedAuthData = JSON.parse(authData); span.setStatus({ code: SpanStatusCode.OK }).end(); return parsedAuthData; } catch (e) { this.debug("Failed to get auth data from Redis"); this.debug(e); span.recordException(e); span.setStatus({ code: SpanStatusCode.ERROR, message: "Failed to get auth data from Redis" }).end(); throw e; } } ); } async set(authData) { await this.ensureConnection(); this.debug("Will set auth data in Redis for %s", authData.saleorApiUrl); return this.tracer.startActiveSpan( "RedisAPL.set", { attributes: { saleorApiUrl: authData.saleorApiUrl, appId: authData.appId, [SemanticAttributes.PEER_SERVICE]: OTEL_APL_SERVICE_NAME }, kind: SpanKind.CLIENT }, async (span) => { try { await this.client.hSet( this.hashCollectionKey, authData.saleorApiUrl, JSON.stringify(authData) ); this.debug("Successfully set auth data in Redis"); span.setStatus({ code: SpanStatusCode.OK }).end(); } catch (e) { this.debug("Failed to set auth data in Redis"); this.debug(e); span.recordException(e); span.setStatus({ code: SpanStatusCode.ERROR, message: "Failed to set auth data in Redis" }).end(); throw e; } } ); } async delete(saleorApiUrl) { await this.ensureConnection(); this.debug("Will delete auth data from Redis for %s", saleorApiUrl); return this.tracer.startActiveSpan( "RedisAPL.delete", { attributes: { saleorApiUrl, [SemanticAttributes.PEER_SERVICE]: OTEL_APL_SERVICE_NAME }, kind: SpanKind.CLIENT }, async (span) => { try { await this.client.hDel(this.hashCollectionKey, saleorApiUrl); this.debug("Successfully deleted auth data from Redis"); span.setStatus({ code: SpanStatusCode.OK }).end(); } catch (e) { this.debug("Failed to delete auth data from Redis"); this.debug(e); span.recordException(e); span.setStatus({ code: SpanStatusCode.ERROR, message: "Failed to delete auth data from Redis" }).end(); throw e; } } ); } async getAll() { await this.ensureConnection(); this.debug("Will get all auth data from Redis"); return this.tracer.startActiveSpan( "RedisAPL.getAll", { attributes: { [SemanticAttributes.PEER_SERVICE]: OTEL_APL_SERVICE_NAME }, kind: SpanKind.CLIENT }, async (span) => { try { const allData = await this.client.hGetAll(this.hashCollectionKey); this.debug("Successfully retrieved all auth data from Redis"); span.setStatus({ code: SpanStatusCode.OK }).end(); return Object.values(allData || {}).map((data) => JSON.parse(data)); } catch (e) { this.debug("Failed to get all auth data from Redis"); this.debug(e); span.recordException(e); span.setStatus({ code: SpanStatusCode.ERROR, message: "Failed to get all auth data from Redis" }).end(); throw e; } } ); } async isReady() { try { await this.ensureConnection(); const ping = await this.client.ping(); return ping === "PONG" ? { ready: true } : { ready: false, error: new Error("Redis server did not respond with PONG") }; } catch (error) { return { ready: false, error }; } } async isConfigured() { try { await this.ensureConnection(); const ping = await this.client.ping(); return ping === "PONG" ? { configured: true } : { configured: false, error: new Error("Redis connection not configured properly") }; } catch (error) { return { configured: false, error }; } } }; export { RedisAPL };