2024-11-23 17:16:44 +01:00
|
|
|
import type { Duration } from "dayjs/plugin/duration";
|
|
|
|
|
|
|
|
|
|
import type { Modify } from "@homarr/common/types";
|
2024-12-19 16:10:22 +01:00
|
|
|
import type { Integration, IntegrationSecret } from "@homarr/db/schema";
|
2024-11-23 17:16:44 +01:00
|
|
|
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 }>[];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
interface Options<TData, TKind extends IntegrationKind, TInput extends Record<string, unknown>> {
|
|
|
|
|
// Unique key for this request handler
|
|
|
|
|
queryKey: string;
|
|
|
|
|
requestAsync: (integration: IntegrationOfKind<TKind>, input: TInput) => Promise<TData>;
|
|
|
|
|
cacheDuration: Duration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.cacheDuration,
|
|
|
|
|
createRedisChannel(input, options) {
|
|
|
|
|
return createIntegrationOptionsChannel<TData>(input.integration.id, options.queryKey, input.options);
|
|
|
|
|
},
|
|
|
|
|
}).handler({ options: itemOptions, integration }),
|
|
|
|
|
};
|
|
|
|
|
};
|