index.ts 787 B

123456789101112131415161718192021222324252627282930313233
  1. import { createWebDavClient } from "./webdav";
  2. import { createUpstashClient } from "./upstash";
  3. export enum ProviderType {
  4. WebDAV = "webdav",
  5. UpStash = "upstash",
  6. }
  7. export const SyncClients = {
  8. [ProviderType.UpStash]: createUpstashClient,
  9. [ProviderType.WebDAV]: createWebDavClient,
  10. } as const;
  11. type SyncClientConfig = {
  12. [K in keyof typeof SyncClients]: (typeof SyncClients)[K] extends (
  13. _: infer C,
  14. ) => any
  15. ? C
  16. : never;
  17. };
  18. export type SyncClient = {
  19. get: (key: string) => Promise<string>;
  20. set: (key: string, value: string) => Promise<void>;
  21. check: () => Promise<boolean>;
  22. };
  23. export function createSyncClient<T extends ProviderType>(
  24. provider: T,
  25. config: SyncClientConfig[T],
  26. ): SyncClient {
  27. return SyncClients[provider](config as any) as any;
  28. }