UNPKG

@saleor/app-sdk

Version:
82 lines (80 loc) 2.46 kB
import { createAPLDebug } from "../../chunk-ORQVZRNL.mjs"; import "../../chunk-CPDLIPGD.mjs"; // src/APL/env/env-apl.ts var debug = createAPLDebug("EnvAPL"); var EnvAPL = class { constructor(options) { this.defaultOptions = { printAuthDataOnRegister: false }; if (!this.isAuthDataValid(options.env)) { console.warn( 'EnvAPL constructor not filled with valid AuthData config. Try to install the app with "printAuthDataOnRegister" enabled and check console logs' ); } this.options = { ...this.defaultOptions, ...options }; } isAuthDataValid(authData) { const keysToValidateAgainst = ["appId", "saleorApiUrl", "token"]; return keysToValidateAgainst.every( (key) => authData[key] && typeof authData[key] === "string" && authData[key].length > 0 ); } async isReady() { return this.isAuthDataValid(this.options.env) ? { ready: true } : { ready: false, error: new Error("Auth data not valid, check constructor and pass env variables") }; } /** * Always return its configured, because otherwise .set() will never be called * so env can't be printed */ async isConfigured() { return { configured: true }; } async set(authData) { if (this.options.printAuthDataOnRegister) { console.log("Displaying registration values for the app. Use them to configure EnvAPL"); console.log(JSON.stringify(authData, null, 2)); console.warn( "\u{1F6D1}'printAuthDataOnRegister' option should be turned off once APL is configured, to avoid possible leaks" ); } debug("Called set method"); } async get(saleorApiUrl) { if (!this.isAuthDataValid(this.options.env)) { debug("Trying to get AuthData but APL constructor was not filled with proper AuthData"); return void 0; } if (saleorApiUrl !== this.options.env.saleorApiUrl) { throw new Error( `Requested AuthData for domain "${saleorApiUrl}", however APL is configured for ${this.options.env.saleorApiUrl}. You may trying to install app in invalid Saleor URL ` ); } return this.options.env; } async getAll() { if (!this.isAuthDataValid(this.options.env)) { return []; } const authData = await this.get(this.options.env.saleorApiUrl); return authData ? [authData] : []; } async delete() { debug("Called delete method"); } }; export { EnvAPL };