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

storage.ts

155 lines5.4 KB

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