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

storage.ts

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