From 4e5ba4b599b8b962b5541bc259d8834cedff4816 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Mon, 11 May 2026 20:51:24 +0800 Subject: [PATCH] =?UTF-8?q?feat(api):=20=E5=AE=9E=E7=8E=B0=E7=94=B5?= =?UTF-8?q?=E6=B1=A0=20ORPC=20=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/api/interceptors.ts | 4 ++-- src/server/api/routers/battery.router.ts | 15 +++++++++++++++ src/server/api/routers/index.ts | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 src/server/api/routers/battery.router.ts diff --git a/src/server/api/interceptors.ts b/src/server/api/interceptors.ts index ab18a63..0b75b61 100644 --- a/src/server/api/interceptors.ts +++ b/src/server/api/interceptors.ts @@ -12,8 +12,8 @@ export const handleValidationError = (error: unknown) => { if (!(error instanceof ORPCError) || !(error.cause instanceof ValidationError)) return if (error.code === 'BAD_REQUEST') { - // ORPC widens issues to the Standard Schema shape; every contract here is built from Zod/drizzle-zod, - // so the runtime objects are Zod issues. Rehydrate to reuse z.prettifyError / z.flattenError. + // ORPC widens issues to the Standard Schema shape; contracts here are built from Zod. + // Rehydrate to reuse z.prettifyError / z.flattenError. const zodError = new z.ZodError(error.cause.issues as z.core.$ZodIssue[]) throw new ORPCError('INPUT_VALIDATION_FAILED', { diff --git a/src/server/api/routers/battery.router.ts b/src/server/api/routers/battery.router.ts new file mode 100644 index 0000000..e35c9bb --- /dev/null +++ b/src/server/api/routers/battery.router.ts @@ -0,0 +1,15 @@ +import { createBatteriesResponse, createDashboardSnapshot } from '@/domain/battery' +import { os } from '@/server/api/server' +import { getBatteryHistory, getLatestBatteryPerDevice } from '@/server/battery/mysql' + +export const dashboard = os.battery.dashboard.handler(async () => { + const items = await getLatestBatteryPerDevice() + + return createDashboardSnapshot(items) +}) + +export const batteries = os.battery.batteries.handler(async ({ input }) => { + const items = input.mac ? await getBatteryHistory(input.mac) : await getLatestBatteryPerDevice() + + return createBatteriesResponse(items) +}) diff --git a/src/server/api/routers/index.ts b/src/server/api/routers/index.ts index 31a82ed..34e293f 100644 --- a/src/server/api/routers/index.ts +++ b/src/server/api/routers/index.ts @@ -1,6 +1,6 @@ import { os } from '@/server/api/server' -import * as todo from './todo.router' +import * as battery from './battery.router' export const router = os.router({ - todo, + battery, })