← Files BranchaARCHIVED FILE

skills/brancha/app/src/server/settings/settings.routes.ts

1.28 KB · Oct 2, 2026 · 00:29 UTC

↓ Download file

import { readJsonBody } from '../http/body.js';
import { HttpError } from '../http/errors.js';
import { Router } from '../http/router.js';
import { sendJson } from '../http/response.js';
import { projectEvents } from '../realtime/events.js';
import { isLocale, isThemePreference } from '../../shared/types.js';
import { SettingsStore } from './settings.store.js';

export function registerSettingsRoutes(router: Router, store: SettingsStore) {
  router.get('/api/settings', ({ res }) => {
    sendJson(res, 200, store.read());
  });

  router.put('/api/settings/theme', async ({ req, res }) => {
    const { theme } = await readJsonBody(req);
    if (!isThemePreference(theme)) {
      throw new HttpError(400, 'theme must be light, dark, or system', 'INVALID_THEME');
    }
    const settings = store.write({ ...store.read(), theme });
    projectEvents.publishSettings(settings);
    sendJson(res, 200, settings);
  });

  router.put('/api/settings/locale', async ({ req, res }) => {
    const { locale } = await readJsonBody(req);
    if (!isLocale(locale)) {
      throw new HttpError(400, 'locale must be zh-CN, en, ja, es, or de', 'INVALID_LOCALE');
    }
    const settings = store.write({ ...store.read(), locale });
    projectEvents.publishSettings(settings);
    sendJson(res, 200, settings);
  });
}

SHA-256: ecaa908b98a59a9167d214a2205517e53f40f7c69ee3942bb9900f09a2de00f4