mirror of
https://github.com/wkulhanek/bonob.git
synced 2025-12-21 17:33:29 +01:00
Save tokens
This commit is contained in:
96
src/smapi_token_store.ts
Normal file
96
src/smapi_token_store.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import logger from "./logger";
|
||||
import { SmapiToken } from "./smapi_auth";
|
||||
|
||||
export interface SmapiTokenStore {
|
||||
get(token: string): SmapiToken | undefined;
|
||||
set(token: string, fullSmapiToken: SmapiToken): void;
|
||||
delete(token: string): void;
|
||||
getAll(): { [tokenKey: string]: SmapiToken };
|
||||
}
|
||||
|
||||
export class InMemorySmapiTokenStore implements SmapiTokenStore {
|
||||
private tokens: { [tokenKey: string]: SmapiToken } = {};
|
||||
|
||||
get(token: string): SmapiToken | undefined {
|
||||
return this.tokens[token];
|
||||
}
|
||||
|
||||
set(token: string, fullSmapiToken: SmapiToken): void {
|
||||
this.tokens[token] = fullSmapiToken;
|
||||
}
|
||||
|
||||
delete(token: string): void {
|
||||
delete this.tokens[token];
|
||||
}
|
||||
|
||||
getAll(): { [tokenKey: string]: SmapiToken } {
|
||||
return this.tokens;
|
||||
}
|
||||
}
|
||||
|
||||
export class FileSmapiTokenStore implements SmapiTokenStore {
|
||||
private tokens: { [tokenKey: string]: SmapiToken } = {};
|
||||
private readonly filePath: string;
|
||||
|
||||
constructor(filePath: string) {
|
||||
this.filePath = filePath;
|
||||
this.loadFromFile();
|
||||
}
|
||||
|
||||
private loadFromFile(): void {
|
||||
try {
|
||||
// Ensure the directory exists
|
||||
const dir = path.dirname(this.filePath);
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
logger.info(`Created token storage directory: ${dir}`);
|
||||
}
|
||||
|
||||
// Load existing tokens if file exists
|
||||
if (fs.existsSync(this.filePath)) {
|
||||
const data = fs.readFileSync(this.filePath, "utf8");
|
||||
this.tokens = JSON.parse(data);
|
||||
logger.info(
|
||||
`Loaded ${Object.keys(this.tokens).length} token(s) from ${this.filePath}`
|
||||
);
|
||||
} else {
|
||||
logger.info(`No existing token file found at ${this.filePath}, starting fresh`);
|
||||
this.tokens = {};
|
||||
this.saveToFile();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(`Failed to load tokens from ${this.filePath}`, { error });
|
||||
this.tokens = {};
|
||||
}
|
||||
}
|
||||
|
||||
private saveToFile(): void {
|
||||
try {
|
||||
const data = JSON.stringify(this.tokens, null, 2);
|
||||
fs.writeFileSync(this.filePath, data, "utf8");
|
||||
logger.debug(`Saved ${Object.keys(this.tokens).length} token(s) to ${this.filePath}`);
|
||||
} catch (error) {
|
||||
logger.error(`Failed to save tokens to ${this.filePath}`, { error });
|
||||
}
|
||||
}
|
||||
|
||||
get(token: string): SmapiToken | undefined {
|
||||
return this.tokens[token];
|
||||
}
|
||||
|
||||
set(token: string, fullSmapiToken: SmapiToken): void {
|
||||
this.tokens[token] = fullSmapiToken;
|
||||
this.saveToFile();
|
||||
}
|
||||
|
||||
delete(token: string): void {
|
||||
delete this.tokens[token];
|
||||
this.saveToFile();
|
||||
}
|
||||
|
||||
getAll(): { [tokenKey: string]: SmapiToken } {
|
||||
return this.tokens;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user