mirror of
https://github.com/ajnart/homarr.git
synced 2026-02-28 01:10:54 +01:00
* refactor: move zod import from validation package to zod * refactor: move missing zod imports
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { observable } from "@trpc/server/observable";
|
|
import { z } from "zod";
|
|
|
|
import { sendPingRequestAsync } from "@homarr/ping";
|
|
import { pingChannel, pingUrlChannel } from "@homarr/redis";
|
|
|
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
|
|
|
export const appRouter = createTRPCRouter({
|
|
ping: publicProcedure.input(z.object({ url: z.string() })).query(async ({ input }) => {
|
|
const pingResult = await sendPingRequestAsync(input.url);
|
|
|
|
return {
|
|
url: input.url,
|
|
...pingResult,
|
|
};
|
|
}),
|
|
updatedPing: publicProcedure
|
|
.input(
|
|
z.object({
|
|
url: z.string(),
|
|
}),
|
|
)
|
|
.subscription(async ({ input }) => {
|
|
await pingUrlChannel.addAsync(input.url);
|
|
|
|
const pingResult = await sendPingRequestAsync(input.url);
|
|
|
|
return observable<{ url: string; statusCode: number } | { url: string; error: string }>((emit) => {
|
|
emit.next({ url: input.url, ...pingResult });
|
|
const unsubscribe = pingChannel.subscribe((message) => {
|
|
// Only emit if same url
|
|
if (message.url !== input.url) return;
|
|
emit.next(message);
|
|
});
|
|
|
|
return () => {
|
|
unsubscribe();
|
|
void pingUrlChannel.removeAsync(input.url);
|
|
};
|
|
});
|
|
}),
|
|
});
|