Skip to content

Commit

Permalink
feat(api): improve storage API, move store & http from app to global
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalygashkov committed Dec 1, 2024
1 parent c97c5f0 commit 8c3fa75
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion apps/cli
Submodule cli updated from ba6084 to f1d681
4 changes: 0 additions & 4 deletions packages/api/types/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import type { AppHttp } from './http';
import type { AppLogger } from './logger';
import type { AppPrompt } from './prompt';
import type { AppStorage } from './storage';
import type { AppUtils } from './utils';

export type App = {
log: AppLogger;
http: AppHttp;
prompt: AppPrompt;
store: AppStorage;
utils: AppUtils;
};
2 changes: 2 additions & 0 deletions packages/api/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { App } from './app';
import { AppStorage } from './storage';
import { AppHttp } from './http';

declare global {
const app: App;
const storage: AppStorage;
const http: AppHttp;
}
7 changes: 7 additions & 0 deletions packages/api/types/prompt.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export type PromptForm = {
title?: string;
subtitle?: string;
} & {
[field: string]: { label: string; defaultValue?: string };
};

export type PromptFormResponse = { [field: string]: string };

export type AppPrompt = {
Expand Down
13 changes: 8 additions & 5 deletions packages/api/types/storage.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export type AppStorage = {
state: Record<string, any>;
getState: <T = any>(cookiesKey?: string | null) => Promise<T>;
setState: <T = Record<string, any>>(data?: T) => Promise<void>;
};
export type AppStorage<T = Record<string, any>> = {
load(): Promise<void>;
get(key: string): Promise<any>;
set(key: string, value: any): Promise<void>;
delete(key: string): Promise<void>;
clear(): Promise<void>;
save(items: T): Promise<void>;
} & T;
52 changes: 52 additions & 0 deletions packages/core/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,58 @@ const getCookiesFromTxt = async (dir: string) => {
return cookies;
};

export const createStorage = async (name: string) => {
const storageDir = initDir(join(getSettings().servicesDir, name));
const storagePath = join(storageDir, `${name}.storage.json`);

const configPath = join(storageDir, 'config.json');
if (fs.exists(configPath)) await fs.rename(configPath, storagePath);

const serializable = (obj: any) => {
const result: any = {};
for (const [key, value] of Object.entries(obj)) {
if (typeof value !== 'function') result[key] = value;
}
return result;
};

const storage: Record<string, any> = {
async load() {
const data = (await fs.readJson<any>(storagePath).catch(() => {})) || {};
for (const [key, value] of Object.entries(data)) storage[key] = value;
// Automatically load cookies from cookies.txt
const cookies = await getCookiesFromTxt(storagePath);
if (!!cookies.length) await storage.set('cookies', cookies);
},
async get(key: string) {
const data = (await fs.readJson<any>(storagePath).catch(() => {})) || {};
return data[key];
},
async set(key: string, value: any) {
storage[key] = value;
await fs.writeJson(storagePath, serializable(storage));
},
async delete(key: string) {
delete storage[key];
await fs.writeJson(storagePath, serializable(storage));
},
async clear() {
for (const [key, value] of Object.entries(storage)) {
const isFn = typeof value === 'function';
if (!isFn) delete storage[key];
}
await fs.writeJson(storagePath, serializable(storage));
},
async save(items?: Record<string, any>) {
const data = items || serializable(storage);
for (const [key, value] of Object.entries(data)) storage[key] = value;
await fs.writeJson(storagePath, data);
},
};

return storage;
};

export const createStore = (name: string) => {
const storePath = createStorePath(name);
const state = {} as Record<string, any>;
Expand Down

0 comments on commit 8c3fa75

Please sign in to comment.