159 lines
5.5 KiB
TypeScript
159 lines
5.5 KiB
TypeScript
import { command, query } from '$app/server';
|
|
import { v } from '$lib';
|
|
|
|
import { systemDetails } from './server.remote';
|
|
import { nadirForMachine, throwNadirError } from './utils';
|
|
|
|
export const systemTime = query(v.string(), async (machineId) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { data, error: err } = await nadir.GET('/api/system/time');
|
|
if (!data) throwNadirError(err);
|
|
return data;
|
|
});
|
|
|
|
export const systemLocale = query(v.string(), async (machineId) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { data, error: err } = await nadir.GET('/api/system/locale');
|
|
if (!data) throwNadirError(err);
|
|
return data;
|
|
});
|
|
|
|
export const listTimezones = query(v.string(), async (machineId) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { data } = await nadir.GET('/api/system/timezones');
|
|
return data?.timezones ?? [];
|
|
});
|
|
|
|
export const listLocales = query(v.string(), async (machineId) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { data } = await nadir.GET('/api/system/locales');
|
|
return data?.locales ?? [];
|
|
});
|
|
|
|
export const setTimezone = command(
|
|
v.object({ machineId: v.string(), timezone: v.string() }),
|
|
async ({ machineId, timezone }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/timezone', { body: { timezone } });
|
|
if (err) throwNadirError(err);
|
|
await systemTime(machineId).refresh();
|
|
await systemDetails(machineId).refresh();
|
|
}
|
|
);
|
|
|
|
export const setNtp = command(
|
|
v.object({ enabled: v.boolean(), machineId: v.string() }),
|
|
async ({ enabled, machineId }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/ntp', { body: { enabled } });
|
|
if (err) throwNadirError(err);
|
|
await systemTime(machineId).refresh();
|
|
await systemDetails(machineId).refresh();
|
|
}
|
|
);
|
|
|
|
export const setTime = command(
|
|
v.object({ machineId: v.string(), time: v.string() }),
|
|
async ({ machineId, time }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/time', { body: { time } });
|
|
if (err) throwNadirError(err);
|
|
await systemTime(machineId).refresh();
|
|
}
|
|
);
|
|
|
|
export const systemHostname = query(v.string(), async (machineId) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { data, error: err } = await nadir.GET('/api/system/hostname');
|
|
console.log(err);
|
|
if (!data) throwNadirError(err);
|
|
return data;
|
|
});
|
|
|
|
export const setHostname = command(
|
|
v.object({ hostname: v.string(), machineId: v.string() }),
|
|
async ({ hostname, machineId }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/hostname', { body: { hostname } });
|
|
if (err) throwNadirError(err);
|
|
await systemHostname(machineId).refresh();
|
|
}
|
|
);
|
|
|
|
export const listKeymaps = query(v.string(), async (machineId) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { data } = await nadir.GET('/api/system/keymaps');
|
|
const d = data as { keymaps?: null | string[]; reason?: string } | undefined;
|
|
return { keymaps: d?.keymaps ?? [], reason: d?.reason ?? '' };
|
|
});
|
|
|
|
export const setKeymap = command(
|
|
v.object({ keymap: v.string(), machineId: v.string() }),
|
|
async ({ keymap, machineId }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/keymap', { body: { keymap } });
|
|
if (err) throwNadirError(err);
|
|
await systemLocale(machineId).refresh();
|
|
await systemDetails(machineId).refresh();
|
|
}
|
|
);
|
|
|
|
export const powerOff = command(
|
|
v.object({ machineId: v.string(), when: v.optional(v.string(), '') }),
|
|
async ({ machineId, when }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/poweroff', { body: { when } });
|
|
if (err) throwNadirError(err);
|
|
}
|
|
);
|
|
|
|
export const reboot = command(
|
|
v.object({ machineId: v.string(), when: v.optional(v.string(), '') }),
|
|
async ({ machineId, when }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/reboot', { body: { when } });
|
|
if (err) throwNadirError(err);
|
|
}
|
|
);
|
|
|
|
export const setLocale = command(
|
|
v.object({
|
|
lang: v.string(),
|
|
language: v.optional(v.string()),
|
|
machineId: v.string()
|
|
}),
|
|
async ({ lang, language, machineId }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/locale', {
|
|
body: { lang, language: language || undefined }
|
|
});
|
|
if (err) throwNadirError(err);
|
|
await systemLocale(machineId).refresh();
|
|
await systemDetails(machineId).refresh();
|
|
}
|
|
);
|
|
|
|
export const generateLocale = command(
|
|
v.object({ locale: v.string(), machineId: v.string() }),
|
|
async ({ locale, machineId }) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { error: err } = await nadir.POST('/api/system/locale/generate', { body: { locale } });
|
|
if (err) throwNadirError(err);
|
|
await listLocales(machineId).refresh();
|
|
}
|
|
);
|
|
|
|
export const getModules = query(v.string(), async (machineId) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { data, error: err } = await nadir.GET('/api/_modules');
|
|
if (err) throwNadirError(err);
|
|
return data;
|
|
});
|
|
|
|
export const getWhoami = query(v.string(), async (machineId) => {
|
|
const { client: nadir } = await nadirForMachine(machineId);
|
|
const { data, error: err } = await nadir.GET('/api/whoami');
|
|
if (err) throwNadirError(err);
|
|
return data;
|
|
});
|