feat(api): implement authenticated habit tracking and combined calendars
This commit is contained in:
102
src/habits/routes.ts
Normal file
102
src/habits/routes.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { Hono, type Context, type MiddlewareHandler } from 'hono';
|
||||
import { bodyLimit } from 'hono/body-limit';
|
||||
import { z } from 'zod';
|
||||
import type { AppDatabase, AuthEnv, createAuth } from '../auth';
|
||||
import { HabitService, ApiError } from './service';
|
||||
import { calendarSettingsSchema, chartInput, chartPatch, dateSchema, doneInput, habitInput, habitPatch, progressInput, taskInput, taskPatch, timezoneInput } from './contracts';
|
||||
import { dayNumber } from './calendar';
|
||||
type Env = AuthEnv & { Variables: { service: HabitService } };
|
||||
async function body<T extends z.ZodType>(c: Context<Env>, schema: T): Promise<z.output<T>> {
|
||||
if (!c.req.header('Content-Type')?.toLowerCase().startsWith('application/json')) throw new ApiError(400, 'Expected application/json');
|
||||
let value: unknown;
|
||||
try { value = await c.req.json(); } catch { throw new ApiError(400, 'Malformed JSON'); }
|
||||
const result = schema.safeParse(value);
|
||||
if (!result.success) throw new ApiError(422, result.error.issues.map(i => `${i.path.join('.') || 'body'}: ${i.message}`).join('; '));
|
||||
return result.data;
|
||||
}
|
||||
function date(c: Context<Env>) {
|
||||
const result = dateSchema.safeParse(c.req.param('date'));
|
||||
if (!result.success) throw new ApiError(422, 'Expected a real YYYY-MM-DD date');
|
||||
return result.data;
|
||||
}
|
||||
function range(c: Context<Env>) {
|
||||
const result = z.object({ from: dateSchema, to: dateSchema }).strict().safeParse(c.req.query());
|
||||
if (!result.success || result.data.from > result.data.to || dayNumber(result.data.to) - dayNumber(result.data.from) > 1829) throw new ApiError(422, 'Supply from and to dates in order, spanning at most 1830 days');
|
||||
return result.data;
|
||||
}
|
||||
export function createHabitRoutes(db: AppDatabase, auth: ReturnType<typeof createAuth>, now: () => number) {
|
||||
const app = new Hono<Env>();
|
||||
const protectedPath = (path: string, method: string) => /^\/api\/(habits|charts|today|days)(\/|$)/.test(path) || (path === '/api/me' && method === 'PATCH');
|
||||
const authenticate = auth.requireAuth as unknown as MiddlewareHandler<Env>;
|
||||
const origin = auth.requireSameOrigin as unknown as MiddlewareHandler<Env>;
|
||||
const limit = bodyLimit({ maxSize: 65536, onError: c => c.json({ error: 'Request body exceeds 64 KiB' }, 413) });
|
||||
app.use('*', async (c, next) => {
|
||||
if (!protectedPath(c.req.path, c.req.method)) return next();
|
||||
return authenticate(c, async () => {
|
||||
const proceed = async () => { const response = await limit(c, async () => {
|
||||
const service = new HabitService(db, c.get('user'), now());
|
||||
service.sync(); c.set('service', service); await next();
|
||||
}); if (response) c.res = response; };
|
||||
if (!['GET', 'HEAD', 'OPTIONS'].includes(c.req.method)) {
|
||||
const response = await origin(c, proceed); if (response) c.res = response;
|
||||
} else await proceed();
|
||||
});
|
||||
});
|
||||
const service = (c: Context<Env>) => c.get('service');
|
||||
const id = (c: Context<Env>) => c.req.param('id')!;
|
||||
app.get('/habits', c => {
|
||||
const q = z.object({ archived: z.enum(['true', 'false']).optional() }).strict().safeParse(c.req.query());
|
||||
if (!q.success) throw new ApiError(422, 'archived must be true or false');
|
||||
return c.json({ habits: service(c).list(q.data.archived === 'true') });
|
||||
});
|
||||
app.post('/habits', async c => {
|
||||
const habit = service(c).create(await body(c, habitInput));
|
||||
c.header('Location', `/api/habits/${habit.id}`); return c.json(habit, 201);
|
||||
});
|
||||
app.get('/habits/:id', c => c.json(service(c).current(id(c))));
|
||||
app.patch('/habits/:id', async c => c.json(service(c).edit(id(c), await body(c, habitPatch))));
|
||||
app.delete('/habits/:id', c => { service(c).edit(id(c), { archived: true }); return c.body(null, 204); });
|
||||
app.get('/habits/:id/history', c => c.json({ revisions: service(c).history(id(c)) }));
|
||||
app.get('/habits/:id/tasks', c => c.json({ tasks: service(c).tasks(id(c)) }));
|
||||
app.post('/habits/:id/tasks', async c => c.json(service(c).changeTask(id(c), undefined, await body(c, taskInput)), 201));
|
||||
app.get('/habits/:id/tasks/:taskId', c => {
|
||||
const task = service(c).tasks(id(c)).find(t => t.id === c.req.param('taskId'));
|
||||
if (!task) throw new ApiError(404, 'Not found'); return c.json(task);
|
||||
});
|
||||
app.patch('/habits/:id/tasks/:taskId', async c => c.json(service(c).changeTask(id(c), c.req.param('taskId'), await body(c, taskPatch))));
|
||||
app.delete('/habits/:id/tasks/:taskId', c => { service(c).changeTask(id(c), c.req.param('taskId'), null); return c.body(null, 204); });
|
||||
app.get('/habits/:id/days/:date', c => c.json(service(c).day(id(c), date(c))));
|
||||
app.get('/habits/:id/days/:date/audit', c => c.json({ records: service(c).audit(id(c), date(c)) }));
|
||||
app.put('/habits/:id/days/:date/progress', async c => c.json(service(c).progress(id(c), date(c), await body(c, progressInput))));
|
||||
app.put('/habits/:id/days/:date/tasks/:taskId', async c => c.json(service(c).completeTask(id(c), date(c), c.req.param('taskId'), (await body(c, doneInput)).done)));
|
||||
app.get('/habits/:id/calendar-settings', c => c.json(service(c).settings(id(c))));
|
||||
app.put('/habits/:id/calendar-settings', async c => c.json(service(c).saveSettings(id(c), await body(c, calendarSettingsSchema))));
|
||||
app.get('/habits/:id/calendar', c => {
|
||||
const { from, to } = range(c); return c.json(service(c).calendar([id(c)], from, to, service(c).settings(id(c)), false));
|
||||
});
|
||||
app.get('/today', c => {
|
||||
const s = service(c); const habits = s.list().map(h => s.day(h.id, s.today));
|
||||
return c.json({ date: s.today, timezone: s.user.timezone, habits, due: habits.filter(h => h.due).length, completed: habits.filter(h => h.complete).length });
|
||||
});
|
||||
app.get('/days/:date', c => {
|
||||
const s = service(c); const day = date(c); return c.json({ date: day, habits: s.list(true).map(h => s.day(h.id, day)) });
|
||||
});
|
||||
app.get('/charts', c => c.json({ charts: service(c).charts() }));
|
||||
app.post('/charts', async c => {
|
||||
const chart = service(c).saveChart(await body(c, chartInput)); c.header('Location', `/api/charts/${chart.id}`); return c.json(chart, 201);
|
||||
});
|
||||
app.get('/charts/:id', c => c.json(service(c).chart(id(c))));
|
||||
app.patch('/charts/:id', async c => {
|
||||
const patch = await body(c, chartPatch); const old = service(c).chart(id(c));
|
||||
return c.json(service(c).saveChart({ name: patch.name ?? old.name, habitIds: patch.habitIds ?? old.habitIds, settings: patch.settings ?? old.settings }, id(c)));
|
||||
});
|
||||
app.delete('/charts/:id', c => { service(c).deleteChart(id(c)); return c.body(null, 204); });
|
||||
app.get('/charts/:id/calendar', c => {
|
||||
const chart = service(c).chart(id(c)); const { from, to } = range(c); return c.json(service(c).calendar(chart.habitIds, from, to, chart.settings, true));
|
||||
});
|
||||
app.get('/charts/:id/days/:date', c => {
|
||||
const chart = service(c).chart(id(c)); const day = date(c); return c.json(service(c).calendar(chart.habitIds, day, day, chart.settings, true).days[0]);
|
||||
});
|
||||
app.patch('/me', async c => c.json(service(c).setTimezone((await body(c, timezoneInput)).timezone)));
|
||||
return app;
|
||||
}
|
||||
Reference in New Issue
Block a user