UNPKG

@saleor/app-sdk

Version:
168 lines (160 loc) 6.28 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _chunkKM2345JLjs = require('../../chunk-KM2345JL.js'); var _chunk3OYW6U6Kjs = require('../../chunk-3OYW6U6K.js'); require('../../chunk-DE4A7PET.js'); // src/APL/vercel-kv/vercel-kv-apl.ts var _api = require('@opentelemetry/api'); var _semanticconventions = require('@opentelemetry/semantic-conventions'); var _kv = require('@vercel/kv'); var VercelKvApl = class { constructor(options) { this.debug = _chunk3OYW6U6Kjs.createAPLDebug.call(void 0, "VercelKvApl"); this.tracer = _chunkKM2345JLjs.getOtelTracer.call(void 0, ); /** * Store all items inside hash collection, to enable read ALL items when needed. * Otherwise, multiple redis calls will be needed to iterate over every key. * * To allow connecting many apps to single KV storage, we need to use different hash collection key for each app. */ this.hashCollectionKey = process.env.KV_STORAGE_NAMESPACE; if (!this.envVariablesRequiredByKvExist()) { throw new Error("Missing KV env variables, please link KV storage to your project"); } this.hashCollectionKey = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.hashCollectionKey]), () => ( this.hashCollectionKey)); } async get(saleorApiUrl) { this.debug("Will call Vercel KV to get auth data for %s", saleorApiUrl); return this.tracer.startActiveSpan( "VercelKvApl.get", { attributes: { saleorApiUrl, [_semanticconventions.SemanticAttributes.PEER_SERVICE]: _chunkKM2345JLjs.OTEL_APL_SERVICE_NAME }, kind: _api.SpanKind.CLIENT }, async (span) => { try { const authData = await _kv.kv.hget(this.hashCollectionKey, saleorApiUrl); this.debug("Received response from VercelKV"); if (!authData) { this.debug("AuthData is empty for %s", saleorApiUrl); } span.setStatus({ code: _api.SpanStatusCode.OK, message: "Received response from VercelKV" }).end(); return _nullishCoalesce(authData, () => ( void 0)); } catch (e) { this.debug("Failed to get auth data from Vercel KV"); this.debug(e); span.recordException("Failed to get auth data from Vercel KV"); span.setStatus({ code: _api.SpanStatusCode.ERROR, message: "Failed to get auth data from Vercel KV" }).end(); throw e; } } ); } async set(authData) { this.debug("Will call Vercel KV to set auth data for %s", authData.saleorApiUrl); return this.tracer.startActiveSpan( "VercelKvApl.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 _kv.kv.hset(this.hashCollectionKey, { [authData.saleorApiUrl]: authData }); span.setStatus({ code: _api.SpanStatusCode.OK, message: "Successfully written auth data to VercelKV" }).end(); } catch (e) { this.debug("Failed to set auth data in Vercel KV"); this.debug(e); span.recordException("Failed to set auth data in Vercel KV"); span.setStatus({ code: _api.SpanStatusCode.ERROR }).end(); throw e; } } ); } async delete(saleorApiUrl) { this.debug("Will call Vercel KV to delete auth data for %s", saleorApiUrl); return this.tracer.startActiveSpan( "VercelKvApl.delete", { attributes: { saleorApiUrl, [_semanticconventions.SemanticAttributes.PEER_SERVICE]: _chunkKM2345JLjs.OTEL_APL_SERVICE_NAME }, kind: _api.SpanKind.CLIENT }, async (span) => { try { await _kv.kv.hdel(this.hashCollectionKey, saleorApiUrl); span.setStatus({ code: _api.SpanStatusCode.OK, message: "Successfully deleted auth data to VercelKV" }).end(); } catch (e) { this.debug("Failed to delete auth data from Vercel KV"); this.debug(e); span.recordException("Failed to delete auth data from Vercel KV"); span.setStatus({ code: _api.SpanStatusCode.ERROR }).end(); throw e; } } ); } async getAll() { const results = await _kv.kv.hgetall(this.hashCollectionKey); if (results === null) { throw new Error("Missing KV collection, data was never written"); } return Object.values(results); } async isReady() { const ready = this.envVariablesRequiredByKvExist(); return ready ? { ready: true } : { ready: false, error: new Error("Missing KV env variables, please link KV storage to your project") }; } async isConfigured() { const configured = this.envVariablesRequiredByKvExist(); return configured ? { configured: true } : { configured: false, error: new Error("Missing KV env variables, please link KV storage to your project") }; } envVariablesRequiredByKvExist() { const variables = [ "KV_REST_API_URL", "KV_REST_API_TOKEN", "KV_REST_API_READ_ONLY_TOKEN", "KV_STORAGE_NAMESPACE", "KV_URL" ]; return variables.every((variable) => !!process.env[variable]); } }; exports.VercelKvApl = VercelKvApl;