()
| 44 | const dbFilePath = 'mocked-db.json'; |
| 45 | |
| 46 | export const loadDb = async () => { |
| 47 | // If we are running in a Node.js environment |
| 48 | if (typeof window === 'undefined') { |
| 49 | const { readFile, writeFile } = await import('fs/promises'); |
| 50 | try { |
| 51 | const data = await readFile(dbFilePath, 'utf8'); |
| 52 | return JSON.parse(data); |
| 53 | } catch (error: any) { |
| 54 | if (error?.code === 'ENOENT') { |
| 55 | const emptyDB = {}; |
| 56 | await writeFile(dbFilePath, JSON.stringify(emptyDB, null, 2)); |
| 57 | return emptyDB; |
| 58 | } else { |
| 59 | console.error('Error loading mocked DB:', error); |
| 60 | return null; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | // If we are running in a browser environment |
| 65 | return Object.assign( |
| 66 | JSON.parse(window.localStorage.getItem('msw-db') || '{}'), |
| 67 | ); |
| 68 | }; |
| 69 | |
| 70 | export const storeDb = async (data: string) => { |
| 71 | // If we are running in a Node.js environment |
no outgoing calls
no test coverage detected