File size: 1,717 Bytes
1f21206 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /**
* Desktop UI Preferences REST API
*
* GET /api/desktop-ui/preferences — read cc-haha UI preferences
* PUT /api/desktop-ui/preferences/sidebar — persist sidebar project preferences
*/
import { ApiError, errorResponse } from '../middleware/errorHandler.js'
import { DesktopUiPreferencesService } from '../services/desktopUiPreferencesService.js'
const desktopUiPreferencesService = new DesktopUiPreferencesService()
export async function handleDesktopUiApi(
req: Request,
url: URL,
segments: string[],
): Promise<Response> {
void url
try {
const sub = segments[2]
const detail = segments[3]
if (sub !== 'preferences') {
throw ApiError.notFound(`Unknown desktop UI endpoint: ${sub}`)
}
if (detail === undefined) {
if (req.method !== 'GET') throw methodNotAllowed(req.method)
return Response.json(await desktopUiPreferencesService.readPreferences())
}
if (detail === 'sidebar') {
if (req.method !== 'PUT') throw methodNotAllowed(req.method)
const body = await parseJsonBody(req)
return Response.json({
ok: true,
preferences: await desktopUiPreferencesService.updateSidebarPreferences(body),
})
}
throw ApiError.notFound(`Unknown desktop UI preferences endpoint: ${detail}`)
} catch (error) {
return errorResponse(error)
}
}
async function parseJsonBody(req: Request): Promise<Record<string, unknown>> {
try {
return (await req.json()) as Record<string, unknown>
} catch {
throw ApiError.badRequest('Invalid JSON body')
}
}
function methodNotAllowed(method: string): ApiError {
return new ApiError(405, `Method ${method} not allowed`, 'METHOD_NOT_ALLOWED')
}
|