2022-08-29 17:00:02 +00:00
|
|
|
import { promises as fsPromises } from "fs";
|
|
|
|
|
|
|
|
import { APL, AuthData } from "./apl";
|
2022-09-05 09:09:06 +00:00
|
|
|
import { createAPLDebug } from "./apl-debug";
|
2022-08-29 17:00:02 +00:00
|
|
|
|
2022-09-05 09:09:06 +00:00
|
|
|
const debug = createAPLDebug("FileAPL");
|
2022-08-29 17:00:02 +00:00
|
|
|
|
|
|
|
export type FileAPLConfig = {
|
|
|
|
fileName?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File APL
|
|
|
|
*
|
|
|
|
* The APL store auth data in the json file.
|
|
|
|
*
|
|
|
|
* Before using this APL, please take in consideration:
|
|
|
|
* - only stores single auth data entry (setting up a new one will overwrite previous values)
|
|
|
|
* - it's not recommended for production use - redeployment of the application will override
|
|
|
|
* existing values, or data persistence will not be guaranteed at all depending on chosen
|
|
|
|
* hosting solution
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
export class FileAPL implements APL {
|
|
|
|
private fileName: string;
|
|
|
|
|
|
|
|
constructor(config: FileAPLConfig = {}) {
|
2022-09-01 16:40:58 +00:00
|
|
|
this.fileName = config?.fileName || ".saleor-app-auth.json";
|
2022-08-29 17:00:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 13:39:15 +00:00
|
|
|
/**
|
|
|
|
* Load auth data from a file and return it as AuthData format.
|
|
|
|
* In case of incomplete or invalid data, return `undefined`.
|
|
|
|
*
|
|
|
|
* @param {string} fileName
|
|
|
|
*/
|
|
|
|
private async loadDataFromFile(): Promise<AuthData | undefined> {
|
|
|
|
debug(`Load auth data from the ${this.fileName} file`);
|
|
|
|
let parsedData: Record<string, string> = {};
|
|
|
|
try {
|
|
|
|
await fsPromises.access(this.fileName);
|
|
|
|
parsedData = JSON.parse(await fsPromises.readFile(this.fileName, "utf-8"));
|
|
|
|
} catch (err) {
|
|
|
|
debug(`Could not read auth data from the ${this.fileName} file`, err);
|
|
|
|
throw new Error(`File APL could not read auth data from the ${this.fileName} file`);
|
|
|
|
}
|
|
|
|
const { token, domain } = parsedData;
|
|
|
|
if (token && domain) {
|
|
|
|
return { token, domain };
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save auth data to file.
|
|
|
|
* When `authData` argument is empty, will overwrite file with empty values.
|
|
|
|
*
|
|
|
|
* @param {string} fileName
|
|
|
|
* @param {AuthData} [authData]
|
|
|
|
*/
|
|
|
|
private async saveDataToFile(authData?: AuthData) {
|
|
|
|
debug(`Save auth data to the ${this.fileName} file`);
|
|
|
|
const newData = authData ? JSON.stringify(authData) : "{}";
|
|
|
|
try {
|
|
|
|
await fsPromises.writeFile(this.fileName, newData);
|
|
|
|
} 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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 17:00:02 +00:00
|
|
|
async get(domain: string) {
|
2022-09-02 13:39:15 +00:00
|
|
|
const authData = await this.loadDataFromFile();
|
2022-08-29 17:00:02 +00:00
|
|
|
if (domain === authData?.domain) {
|
|
|
|
return authData;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
async set(authData: AuthData) {
|
2022-09-02 13:39:15 +00:00
|
|
|
await this.saveDataToFile(authData);
|
2022-08-29 17:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async delete(domain: string) {
|
2022-09-02 13:39:15 +00:00
|
|
|
const authData = await this.loadDataFromFile();
|
2022-08-29 17:00:02 +00:00
|
|
|
|
|
|
|
if (domain === authData?.domain) {
|
2022-09-02 13:39:15 +00:00
|
|
|
await this.saveDataToFile();
|
2022-08-29 17:00:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll() {
|
2022-09-02 13:39:15 +00:00
|
|
|
const authData = await this.loadDataFromFile();
|
2022-08-29 17:00:02 +00:00
|
|
|
|
|
|
|
if (!authData) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [authData];
|
|
|
|
}
|
|
|
|
}
|