UNPKG

@saleor/app-sdk

Version:
131 lines (129 loc) 4.1 kB
import { createAPLDebug } from "../../chunk-ORQVZRNL.mjs"; import "../../chunk-CPDLIPGD.mjs"; // src/APL/upstash/upstash-apl.ts var debug = createAPLDebug("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 = config?.restURL || process.env[UpstashAPLVariables.UPSTASH_URL]; const restToken = config?.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" ) }; } }; export { UpstashAPL, UpstashAPLVariables, UpstashAplMisconfiguredError, UpstashAplNotConfiguredError };