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

storage.ts

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