mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-10 07:25:48 +01:00
🏗️ Migrate dashdot info to tRPC
This commit is contained in:
@@ -4,6 +4,7 @@ import { rssRouter } from './routers/rss';
|
||||
import { configRouter } from './routers/config';
|
||||
import { dockerRouter } from './routers/docker/router';
|
||||
import { iconRouter } from './routers/icon';
|
||||
import { dashDotRouter } from './routers/dash-dot';
|
||||
|
||||
/**
|
||||
* This is the primary router for your server.
|
||||
@@ -16,6 +17,7 @@ export const rootRouter = createTRPCRouter({
|
||||
config: configRouter,
|
||||
docker: dockerRouter,
|
||||
icon: iconRouter,
|
||||
dashDot: dashDotRouter,
|
||||
});
|
||||
|
||||
// export type definition of API
|
||||
|
||||
45
src/server/api/routers/dash-dot.ts
Normal file
45
src/server/api/routers/dash-dot.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import axios from 'axios';
|
||||
import { z } from 'zod';
|
||||
import { TRPCError } from '@trpc/server';
|
||||
import { createTRPCRouter, publicProcedure } from '../trpc';
|
||||
|
||||
const dashDotUrlSchema = z.string().url();
|
||||
|
||||
const removeLeadingSlash = (x: string) => (x.endsWith('/') ? x.substring(0, x.length - 1) : x);
|
||||
|
||||
export const dashDotRouter = createTRPCRouter({
|
||||
info: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
url: dashDotUrlSchema.transform(removeLeadingSlash),
|
||||
})
|
||||
)
|
||||
.output(
|
||||
z.object({
|
||||
storage: z.array(
|
||||
z.object({
|
||||
size: z.number(),
|
||||
})
|
||||
),
|
||||
network: z.object({
|
||||
speedUp: z.number(),
|
||||
speedDown: z.number(),
|
||||
}),
|
||||
})
|
||||
)
|
||||
.query(async ({ input }) => {
|
||||
const response = await axios.get(`${input.url}/info`).catch((error) => {
|
||||
if (error.response.status === 404) {
|
||||
throw new TRPCError({
|
||||
code: 'NOT_FOUND',
|
||||
message: 'Unable to find specified dash-dot instance',
|
||||
});
|
||||
}
|
||||
|
||||
throw new TRPCError({
|
||||
code: 'INTERNAL_SERVER_ERROR',
|
||||
});
|
||||
});
|
||||
return response.data;
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user