Files
zn-ai/electron/api/routes/settings.ts
duanshuwen 721344883f refactor: reorganize imports and update type references across services
- Updated import paths for types and utilities in config-service, menu-service, script-execution-service, script-store-service, and window-service to reflect new structure.
- Moved utility functions like debounce and cloneDeep to shared utilities.
- Created new runtime and script types files to centralize type definitions.
- Removed deprecated task-types and locale messages files.
- Updated constants to use shared definitions and added new placeholders for providers.
- Enhanced provider type information with additional placeholders for different languages.
2026-04-21 23:25:51 +08:00

101 lines
2.6 KiB
TypeScript

import type { ConfigKeys, IConfig } from '@electron/types/runtime';
import configManager from '@electron/service/config-service';
import type { HostApiResult } from '@src/types/runtime';
import type { HostApiContext } from '../context';
import type { NormalizedHostApiRequest } from '../route-utils';
import { fail, ok, parseJsonBody } from '../route-utils';
function extractSettingKey(pathname: string): string | null {
if (!pathname.startsWith('/api/settings/')) {
return null;
}
const rawKey = pathname.slice('/api/settings/'.length).trim();
if (!rawKey) {
return null;
}
return decodeURIComponent(rawKey);
}
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function unwrapSettingValue(body: unknown): unknown {
if (isPlainObject(body) && Object.prototype.hasOwnProperty.call(body, 'value')) {
return body.value;
}
return body;
}
export async function handleSettingsRoutes(
request: NormalizedHostApiRequest,
_ctx: HostApiContext,
): Promise<HostApiResult<unknown> | null> {
const { pathname, method } = request;
if (pathname === '/api/settings' && method === 'GET') {
try {
return ok(configManager.getConfig());
} catch (error) {
return fail(500, error instanceof Error ? error.message : String(error));
}
}
if (pathname === '/api/settings' && method === 'PUT') {
try {
const body = parseJsonBody<unknown>(request.body);
if (!isPlainObject(body)) {
return fail(400, 'settings payload must be an object');
}
configManager.update(body as Partial<IConfig>);
return ok({
success: true,
settings: configManager.getConfig(),
});
} catch (error) {
return fail(500, error instanceof Error ? error.message : String(error));
}
}
const key = extractSettingKey(pathname);
if (pathname.startsWith('/api/settings/') && !key) {
return fail(400, 'setting key is required');
}
if (!key) {
return null;
}
if (method === 'GET') {
try {
return ok({
value: configManager.get(key as ConfigKeys),
});
} catch (error) {
return fail(500, error instanceof Error ? error.message : String(error));
}
}
if (method === 'PUT') {
try {
const value = unwrapSettingValue(parseJsonBody<unknown>(request.body));
configManager.set(key as ConfigKeys, value);
return ok({
success: true,
key,
value,
});
} catch (error) {
return fail(500, error instanceof Error ? error.message : String(error));
}
}
return null;
}