Repository navigation
Expand file tree
/
Copy pathuserDataManager.ts
More file actions
100 lines (77 loc) · 3.08 KB
/
Copy pathuserDataManager.ts
File metadata and controls
100 lines (77 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { HistoricalHttpRequest } from '../models/httpRequest';
import { JsonFileUtility } from './jsonFileUtility';
const restClientDir = 'rest-client';
const rootPath = path.join(os.homedir(), `.${restClientDir}`);
function getCachePath(): string {
if (fs.existsSync(rootPath)) {
return rootPath;
}
if (process.env.XDG_CACHE_HOME !== undefined) {
return path.join(process.env.XDG_CACHE_HOME, restClientDir);
}
return rootPath;
}
function getConfigPath(): string {
if (fs.existsSync(rootPath)) {
return rootPath;
}
if (process.env.XDG_CONFIG_HOME !== undefined) {
return path.join(process.env.XDG_CONFIG_HOME, restClientDir);
}
return rootPath;
}
export class UserDataManager {
private static readonly historyItemsMaxCount = 50;
private static readonly cachePath: string = getCachePath();
private static readonly configPath: string = getConfigPath();
public static get cookieFilePath() {
return path.join(this.cachePath, 'cookie.json');
}
private static get historyFilePath() {
return path.join(this.cachePath, 'history.json');
}
private static get environmentFilePath() {
return path.join(this.configPath, 'environment.json');
}
private static get responseSaveFolderPath() {
return path.join(this.cachePath, 'responses/raw');
}
private static get responseBodySaveFolderPath() {
return path.join(this.cachePath, 'responses/body');
}
public static async initialize(): Promise<void> {
await Promise.all([
fs.ensureFile(this.historyFilePath),
fs.ensureFile(this.cookieFilePath),
fs.ensureFile(this.environmentFilePath),
fs.ensureDir(this.responseSaveFolderPath),
fs.ensureDir(this.responseBodySaveFolderPath)
]);
}
public static async addToRequestHistory(request: HistoricalHttpRequest) {
const requests = await JsonFileUtility.deserializeFromFile<HistoricalHttpRequest[]>(this.historyFilePath, []);
requests.unshift(request);
await JsonFileUtility.serializeToFile(this.historyFilePath, requests.slice(0, this.historyItemsMaxCount));
}
public static clearRequestHistory(): Promise<void> {
return JsonFileUtility.serializeToFile(this.historyFilePath, []);
}
public static getRequestHistory(): Promise<HistoricalHttpRequest[]> {
return JsonFileUtility.deserializeFromFile(this.historyFilePath, []);
}
public static getEnvironment() {
return JsonFileUtility.deserializeFromFile(this.environmentFilePath);
}
public static setEnvironment(item: unknown) {
return JsonFileUtility.serializeToFile(this.environmentFilePath, item);
}
public static getResponseSaveFilePath(fileName: string) {
return path.join(this.responseSaveFolderPath, fileName);
}
public static getResponseBodySaveFilePath(fileName: string) {
return path.join(this.responseBodySaveFolderPath, fileName);
}
}