Open History to see the last commit
1import {
2 ConnectionPersistenceError,
3 ConnectionRegistrationStore,
4 ConnectionTargetStore,
5 registerConnectionInCatalog,
6 removeConnectionFromCatalog,
7 removeCatalogValue,
8 replaceCatalogValue,
9} from "@t3tools/client-runtime/platform";
10import { TokenStore } from "@t3tools/client-runtime/authorization";
11import {
12 ConnectionTransientError,
13 CredentialStore,
14 ProfileStore,
15} from "@t3tools/client-runtime/connection";
16import * as Context from "effect/Context";
17import * as Effect from "effect/Effect";
18import * as Layer from "effect/Layer";
19import * as Option from "effect/Option";
20import * as CatalogStore from "./catalog-store";
21
22function targetPersistenceError(
23 operation: "list-targets" | "register-connection" | "remove-connection",
24 error: ConnectionTransientError,
25) {
26 return new ConnectionPersistenceError({
27 operation,
28 message: error.message,
29 });
30}
31
32export const connectionStorageLayer = Layer.effectContext(
33 Effect.gen(function* () {
34 const catalog = yield* CatalogStore.make();
35
36 const targetStore = ConnectionTargetStore.of({
37 list: catalog.read.pipe(
38 Effect.map((document) => document.targets),
39 Effect.mapError((error) => targetPersistenceError("list-targets", error)),
40 ),
41 });
42 const registrationStore = ConnectionRegistrationStore.of({
43 register: (registration) =>
44 catalog
45 .update((document) => registerConnectionInCatalog(document, registration))
46 .pipe(Effect.mapError((error) => targetPersistenceError("register-connection", error))),
47 remove: (target) =>
48 catalog
49 .update((document) => removeConnectionFromCatalog(document, target))
50 .pipe(Effect.mapError((error) => targetPersistenceError("remove-connection", error))),
51 });
52 const profileStore = ProfileStore.make({
53 get: (connectionId) =>
54 catalog.read.pipe(
55 Effect.map((document) =>
56 Option.fromUndefinedOr(
57 document.profiles.find((candidate) => candidate.connectionId === connectionId),
58 ),
59 ),
60 ),
61 put: (profile) =>
62 catalog.update((document) => ({
63 ...document,
64 profiles: replaceCatalogValue(document.profiles, (value) => value.connectionId, profile),
65 })),
66 remove: (connectionId) =>
67 catalog.update((document) => ({
68 ...document,
69 profiles: removeCatalogValue(
70 document.profiles,
71 (value) => value.connectionId,
72 connectionId,
73 ),
74 })),
75 });
76 const credentialStore = CredentialStore.make({
77 get: (connectionId) =>
78 catalog.read.pipe(
79 Effect.map((document) =>
80 Option.fromUndefinedOr(
81 document.credentials.find((entry) => entry.connectionId === connectionId)?.credential,
82 ),
83 ),
84 ),
85 put: (connectionId, credential) =>
86 catalog.update((document) => ({
87 ...document,
88 credentials: replaceCatalogValue(document.credentials, (value) => value.connectionId, {
89 connectionId,
90 credential,
91 }),
92 })),
93 remove: (connectionId) =>
94 catalog.update((document) => ({
95 ...document,
96 credentials: removeCatalogValue(
97 document.credentials,
98 (value) => value.connectionId,
99 connectionId,
100 ),
101 })),
102 });
103 const remoteTokenStore = TokenStore.make({
104 get: (environmentId) =>
105 catalog.read.pipe(
106 Effect.map((document) =>
107 Option.fromUndefinedOr(
108 document.remoteDpopTokens.find((token) => token.environmentId === environmentId),
109 ),
110 ),
111 ),
112 put: (token) =>
113 catalog.update((document) => ({
114 ...document,
115 remoteDpopTokens: replaceCatalogValue(
116 document.remoteDpopTokens,
117 (value) => value.environmentId,
118 token,
119 ),
120 })),
121 remove: (environmentId) =>
122 catalog.update((document) => ({
123 ...document,
124 remoteDpopTokens: removeCatalogValue(
125 document.remoteDpopTokens,
126 (value) => value.environmentId,
127 environmentId,
128 ),
129 })),
130 });
131 return Context.make(ConnectionTargetStore, targetStore).pipe(
132 Context.add(ConnectionRegistrationStore, registrationStore),
133 Context.add(ProfileStore.ConnectionProfileStore, profileStore),
134 Context.add(CredentialStore.ConnectionCredentialStore, credentialStore),
135 Context.add(TokenStore.RemoteDpopAccessTokenStore, remoteTokenStore),
136 );
137 }),
138);