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