@saleor/app-sdk
Version:
SDK for building great Saleor Apps
182 lines (174 loc) • 6.04 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _chunkKM2345JLjs = require('../../chunk-KM2345JL.js');
var _chunk3OYW6U6Kjs = require('../../chunk-3OYW6U6K.js');
require('../../chunk-DE4A7PET.js');
// src/APL/redis/redis-apl.ts
var _api = require('@opentelemetry/api');
var _semanticconventions = require('@opentelemetry/semantic-conventions');
var RedisAPL = class {
constructor(config) {
this.debug = _chunk3OYW6U6Kjs.createAPLDebug.call(void 0, "RedisAPL");
this.tracer = _chunkKM2345JLjs.getOtelTracer.call(void 0, );
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,
[_semanticconventions.SemanticAttributes.PEER_SERVICE]: _chunkKM2345JLjs.OTEL_APL_SERVICE_NAME
},
kind: _api.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: _api.SpanStatusCode.OK }).end();
return void 0;
}
const parsedAuthData = JSON.parse(authData);
span.setStatus({ code: _api.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: _api.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,
[_semanticconventions.SemanticAttributes.PEER_SERVICE]: _chunkKM2345JLjs.OTEL_APL_SERVICE_NAME
},
kind: _api.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: _api.SpanStatusCode.OK }).end();
} catch (e) {
this.debug("Failed to set auth data in Redis");
this.debug(e);
span.recordException(e);
span.setStatus({
code: _api.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,
[_semanticconventions.SemanticAttributes.PEER_SERVICE]: _chunkKM2345JLjs.OTEL_APL_SERVICE_NAME
},
kind: _api.SpanKind.CLIENT
},
async (span) => {
try {
await this.client.hDel(this.hashCollectionKey, saleorApiUrl);
this.debug("Successfully deleted auth data from Redis");
span.setStatus({ code: _api.SpanStatusCode.OK }).end();
} catch (e) {
this.debug("Failed to delete auth data from Redis");
this.debug(e);
span.recordException(e);
span.setStatus({
code: _api.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: {
[_semanticconventions.SemanticAttributes.PEER_SERVICE]: _chunkKM2345JLjs.OTEL_APL_SERVICE_NAME
},
kind: _api.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: _api.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: _api.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 };
}
}
};
exports.RedisAPL = RedisAPL;