@saleor/app-sdk
Version:
SDK for building great Saleor Apps
82 lines (77 loc) • 2.57 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _chunk3OYW6U6Kjs = require('../../chunk-3OYW6U6K.js');
require('../../chunk-DE4A7PET.js');
// src/APL/env/env-apl.ts
var debug = _chunk3OYW6U6Kjs.createAPLDebug.call(void 0, "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");
}
};
exports.EnvAPL = EnvAPL;