- Add subscribeSystemStats tRPC subscription bridging PocketBase SSE - Add subscribeRealtimeMetrics to BeszelIntegration with SSE parsing - Add error handling: try/catch in subscription, response check in topic POST - Add cacheDurationForInput to request handler for per-period cache TTL - Add mock subscribeRealtimeMetrics with 1s interval for dev testing
44 lines
1.8 KiB
TypeScript
44 lines
1.8 KiB
TypeScript
import type { Duration } from "dayjs/plugin/duration";
|
|
|
|
import type { Modify } from "@homarr/common/types";
|
|
import type { Integration, IntegrationSecret } from "@homarr/db/schema";
|
|
import type { IntegrationKind } from "@homarr/definitions";
|
|
import { createIntegrationOptionsChannel } from "@homarr/redis";
|
|
|
|
import { createCachedRequestHandler } from "./cached-request-handler";
|
|
|
|
type IntegrationOfKind<TKind extends IntegrationKind> = Omit<Integration, "kind"> & {
|
|
kind: TKind;
|
|
decryptedSecrets: Modify<Pick<IntegrationSecret, "kind" | "value">, { value: string }>[];
|
|
externalUrl: string | null;
|
|
};
|
|
|
|
interface Options<TData, TKind extends IntegrationKind, TInput extends Record<string, unknown>> {
|
|
queryKey: string;
|
|
requestAsync: (integration: IntegrationOfKind<TKind>, input: TInput) => Promise<TData>;
|
|
cacheDuration: Duration;
|
|
cacheDurationForInput?: (input: TInput) => Duration | undefined;
|
|
}
|
|
|
|
export const createCachedIntegrationRequestHandler = <
|
|
TData,
|
|
TKind extends IntegrationKind,
|
|
TInput extends Record<string, unknown>,
|
|
>(
|
|
options: Options<TData, TKind, TInput>,
|
|
) => {
|
|
return {
|
|
handler: (integration: IntegrationOfKind<TKind>, itemOptions: TInput) =>
|
|
createCachedRequestHandler({
|
|
queryKey: options.queryKey,
|
|
requestAsync: async (input: { options: TInput; integration: IntegrationOfKind<TKind> }) => {
|
|
return await options.requestAsync(input.integration, input.options);
|
|
},
|
|
cacheDuration: options.cacheDurationForInput?.(itemOptions) ?? options.cacheDuration,
|
|
createRedisChannel(input, handlerOptions) {
|
|
return createIntegrationOptionsChannel<TData>(input.integration.id, handlerOptions.queryKey, input.options);
|
|
},
|
|
}).handler({ options: itemOptions, integration }),
|
|
};
|
|
};
|