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

storage.ts

132 lines4.4 KB

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