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

storage.ts

172 lines6.3 KB

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