UNPKG

@saleor/app-sdk

Version:
131 lines (123 loc) 4.92 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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 _chunk3OYW6U6Kjs = require('../../chunk-3OYW6U6K.js'); require('../../chunk-DE4A7PET.js'); // src/APL/upstash/upstash-apl.ts var debug = _chunk3OYW6U6Kjs.createAPLDebug.call(void 0, "UpstashAPL"); var UpstashAPLVariables = { UPSTASH_TOKEN: "UPSTASH_TOKEN", UPSTASH_URL: "UPSTASH_URL" }; var UpstashAplMisconfiguredError = class extends Error { constructor(missingVars) { super( `Configuration values for: ${missingVars.map((v) => `"${v}"`).join(", ")} not found or is empty. Pass values to constructor of use env variables.` ); this.missingVars = missingVars; } }; var UpstashAplNotConfiguredError = class extends Error { }; var UpstashAPL = class { constructor(config) { const restURL = _optionalChain([config, 'optionalAccess', _ => _.restURL]) || process.env[UpstashAPLVariables.UPSTASH_URL]; const restToken = _optionalChain([config, 'optionalAccess', _2 => _2.restToken]) || process.env[UpstashAPLVariables.UPSTASH_TOKEN]; this.restURL = restURL; this.restToken = restToken; } async upstashRequest(request) { debug("Sending request to Upstash"); if (!this.restURL || !this.restToken) { throw new Error( "UpstashAPL is not configured. See https://docs.saleor.io/docs/3.x/developer/extending/apps/developing-apps/app-sdk/apl" ); } let response; try { response = await fetch(this.restURL, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${this.restToken}` }, body: JSON.stringify(request) }); } catch (error) { debug("Error during sending the data:", error); throw new Error(`UpstashAPL was unable to perform a request ${error}`); } const parsedResponse = await response.json(); if (!response.ok || "error" in parsedResponse) { debug(`Operation unsuccessful. Upstash API has responded with ${response.status} code`); if ("error" in parsedResponse) { debug("Error message: %s", parsedResponse.error); throw new Error( `Upstash APL was not able to perform operation. Status code: ${response.status}. Error: ${parsedResponse.error}` ); } throw new Error( `Upstash APL was not able to perform operation. Status code: ${response.status}` ); } debug("Upstash service responded successfully"); return parsedResponse.result; } async saveDataToUpstash(authData) { debug("saveDataToUpstash() called with: %j", { saleorApiUrl: authData.saleorApiUrl, token: authData.token.substring(0, 4) }); const data = JSON.stringify(authData); await this.upstashRequest(["SET", authData.saleorApiUrl, data]); } async deleteDataFromUpstash(saleorApiUrl) { await this.upstashRequest(["DEL", saleorApiUrl]); } async fetchDataFromUpstash(saleorApiUrl) { const result = await this.upstashRequest(["GET", saleorApiUrl]); if (result) { const authData = JSON.parse(result); return authData; } return void 0; } async get(saleorApiUrl) { return this.fetchDataFromUpstash(saleorApiUrl); } async set(authData) { await this.saveDataToUpstash(authData); } async delete(saleorApiUrl) { await this.deleteDataFromUpstash(saleorApiUrl); } async getAll() { throw new Error("UpstashAPL does not support getAll method"); return []; } // eslint-disable-next-line class-methods-use-this async isReady() { const missingConf = []; if (!this.restToken) { missingConf.push("restToken"); } if (!this.restURL) { missingConf.push("restURL"); } if (missingConf.length > 0) { return { ready: false, error: new UpstashAplMisconfiguredError(missingConf) }; } return { ready: true }; } async isConfigured() { return this.restToken && this.restURL ? { configured: true } : { configured: false, error: new UpstashAplNotConfiguredError( "UpstashAPL not configured. Check if REST URL and token provided in constructor or env" ) }; } }; exports.UpstashAPL = UpstashAPL; exports.UpstashAPLVariables = UpstashAPLVariables; exports.UpstashAplMisconfiguredError = UpstashAplMisconfiguredError; exports.UpstashAplNotConfiguredError = UpstashAplNotConfiguredError;