feat(api): 实现电池 ORPC 路由

This commit is contained in:
2026-05-11 20:51:24 +08:00
parent 657c7317f7
commit 4e5ba4b599
3 changed files with 19 additions and 4 deletions
+2 -2
View File
@@ -12,8 +12,8 @@ export const handleValidationError = (error: unknown) => {
if (!(error instanceof ORPCError) || !(error.cause instanceof ValidationError)) return if (!(error instanceof ORPCError) || !(error.cause instanceof ValidationError)) return
if (error.code === 'BAD_REQUEST') { if (error.code === 'BAD_REQUEST') {
// ORPC widens issues to the Standard Schema shape; every contract here is built from Zod/drizzle-zod, // ORPC widens issues to the Standard Schema shape; contracts here are built from Zod.
// so the runtime objects are Zod issues. Rehydrate to reuse z.prettifyError / z.flattenError. // Rehydrate to reuse z.prettifyError / z.flattenError.
const zodError = new z.ZodError(error.cause.issues as z.core.$ZodIssue[]) const zodError = new z.ZodError(error.cause.issues as z.core.$ZodIssue[])
throw new ORPCError('INPUT_VALIDATION_FAILED', { throw new ORPCError('INPUT_VALIDATION_FAILED', {
+15
View File
@@ -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)
})
+2 -2
View File
@@ -1,6 +1,6 @@
import { os } from '@/server/api/server' import { os } from '@/server/api/server'
import * as todo from './todo.router' import * as battery from './battery.router'
export const router = os.router({ export const router = os.router({
todo, battery,
}) })