Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

credentialStore.ts

36 lines1.5 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5​
6import type { ConnectionCredential } from "./catalog.ts";
7import type { ConnectionAttemptError } from "./model.ts";
8​
9export class ConnectionCredentialStore extends Context.Service<
10 ConnectionCredentialStore,
11 {
12 readonly get: (
13 connectionId: string,
14 ) => Effect.Effect<Option.Option<ConnectionCredential>, ConnectionAttemptError>;
15 readonly put: (
16 connectionId: string,
17 credential: ConnectionCredential,
18 ) => Effect.Effect<void, ConnectionAttemptError>;
19 readonly remove: (connectionId: string) => Effect.Effect<void, ConnectionAttemptError>;
20 }
21>()("@t3tools/client-runtime/connection/credentialStore/ConnectionCredentialStore") {}
22​
23export const make = (service: ConnectionCredentialStore["Service"]) =>
24 ConnectionCredentialStore.of(service);
25​
26/** A store that lives only as long as its layer, for tests and other throwaway runtimes. */
27export const layerMemory = (initial: Iterable<readonly [string, ConnectionCredential]> = []) =>
28 Layer.sync(ConnectionCredentialStore, () => {
29 const stored = new Map(initial);
30 return make({
31 get: (connectionId) => Effect.sync(() => Option.fromNullishOr(stored.get(connectionId))),
32 put: (connectionId, credential) =>
33 Effect.sync(() => void stored.set(connectionId, credential)),
34 remove: (connectionId) => Effect.sync(() => void stored.delete(connectionId)),
35 });
36 });