@henrylabs-interview/payment-processor 0.2.12 → 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/store.js +9 -3
- package/package.json +1 -1
package/dist/utils/store.js
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
const path = new URL('../db-store/history.json', import.meta.url);
|
|
2
|
+
async function ensureFileExists() {
|
|
3
|
+
const file = Bun.file(path);
|
|
4
|
+
if (!(await file.exists())) {
|
|
5
|
+
await Bun.write(path, '[]');
|
|
6
|
+
}
|
|
7
|
+
}
|
|
2
8
|
/**
|
|
3
9
|
* Reads all history records from the JSON store.
|
|
4
10
|
*/
|
|
5
11
|
export async function readHistory() {
|
|
12
|
+
await ensureFileExists();
|
|
6
13
|
const file = Bun.file(path);
|
|
7
|
-
if (!(await file.exists())) {
|
|
8
|
-
return [];
|
|
9
|
-
}
|
|
10
14
|
const text = await file.text();
|
|
11
15
|
if (!text)
|
|
12
16
|
return [];
|
|
@@ -14,6 +18,8 @@ export async function readHistory() {
|
|
|
14
18
|
return JSON.parse(text);
|
|
15
19
|
}
|
|
16
20
|
catch {
|
|
21
|
+
// If file somehow becomes corrupted, reset it
|
|
22
|
+
await Bun.write(path, '[]');
|
|
17
23
|
return [];
|
|
18
24
|
}
|
|
19
25
|
}
|