useDock
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { mkdir } from 'node:fs/promises';
|
||||
import { dirname } from 'node:path';
|
||||
import { createRouter } from '../../create-router';
|
||||
import { getUserDockFile } from '@@/data-path';
|
||||
|
||||
const ensureDir = (filePath: string) => mkdir(dirname(filePath), { recursive: true });
|
||||
|
||||
export const dockRouter = createRouter();
|
||||
|
||||
// GET / — return dock.json, or null if it doesn't exist (frontend uses defaults)
|
||||
dockRouter.get('/', async (ctx) => {
|
||||
const email = ctx.get('user').email;
|
||||
const filePath = getUserDockFile(email);
|
||||
const file = Bun.file(filePath);
|
||||
|
||||
if (await file.exists()) {
|
||||
const data = await file.json();
|
||||
return ctx.json(data);
|
||||
}
|
||||
|
||||
return ctx.json(null);
|
||||
});
|
||||
|
||||
// PUT / — full replacement of dock paths array
|
||||
dockRouter.put('/', async (ctx) => {
|
||||
const email = ctx.get('user').email;
|
||||
const body = ctx.get('body');
|
||||
const filePath = getUserDockFile(email);
|
||||
|
||||
await ensureDir(filePath);
|
||||
await Bun.write(filePath, JSON.stringify(body, null, 2));
|
||||
return ctx.json(body);
|
||||
});
|
||||
Reference in New Issue
Block a user