@saleor/app-sdk
Version:
SDK for building great Saleor Apps
83 lines (78 loc) • 3.25 kB
JavaScript
;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/file/file-apl.ts
var _fs = require('fs');
var debug = _chunk3OYW6U6Kjs.createAPLDebug.call(void 0, "FileAPL");
var FileAPL = class {
constructor(config = {}) {
this.fileName = _optionalChain([config, 'optionalAccess', _ => _.fileName]) || ".saleor-app-auth.json";
}
/**
* Load auth data from a file and return it as AuthData format.
* In case of incomplete or invalid data, return `undefined`.
*/
async loadDataFromFile() {
debug(`Will try to load auth data from the ${this.fileName} file`);
let parsedData = {};
try {
parsedData = JSON.parse(await _fs.promises.readFile(this.fileName, "utf-8"));
debug("%s read successfully", this.fileName);
} catch (err) {
debug(`Could not read auth data from the ${this.fileName} file`, err);
debug(
"Maybe apl.get() was called before app was registered. Returning empty, fallback data (undefined)"
);
return void 0;
}
const { token, saleorApiUrl, appId, jwks } = parsedData;
if (token && saleorApiUrl && appId) {
debug("Token found, returning values: %s", `${token[0]}***`);
const authData = { token, saleorApiUrl, appId };
if (jwks) {
authData.jwks = jwks;
}
return authData;
}
return void 0;
}
/**
* Save auth data to file.
* When `authData` argument is empty, will overwrite file with empty values.
*/
async saveDataToFile(authData) {
debug(`Trying to save auth data to the ${this.fileName} file`);
const newData = authData ? JSON.stringify(authData) : "{}";
try {
await _fs.promises.writeFile(this.fileName, newData);
debug("Successfully written file %", this.fileName);
} catch (err) {
debug(`Could not save auth data to the ${this.fileName} file`, err);
throw new Error("File APL was unable to save auth data");
}
}
async get(saleorApiUrl) {
const authData = await this.loadDataFromFile();
if (saleorApiUrl === _optionalChain([authData, 'optionalAccess', _2 => _2.saleorApiUrl])) {
return authData;
}
return void 0;
}
async set(authData) {
await this.saveDataToFile(authData);
}
async delete(saleorApiUrl) {
const authData = await this.loadDataFromFile();
if (saleorApiUrl === _optionalChain([authData, 'optionalAccess', _3 => _3.saleorApiUrl])) {
await this.saveDataToFile();
}
}
async getAll() {
const authData = await this.loadDataFromFile();
if (!authData) {
return [];
}
return [authData];
}
};
exports.FileAPL = FileAPL;