feat(api): 实现电池 ORPC 路由
This commit is contained in:
@@ -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', {
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
@@ -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,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user