Skip to content

Commit f453c70

Browse files
kewl
1 parent 7d254ad commit f453c70

1 file changed

Lines changed: 64 additions & 43 deletions

File tree

‎apps/desktop/src/ipc/methods/cloudAuth.ts‎

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import {
88
} from "@t3tools/shared/relayAuth";
99
import * as Data from "effect/Data";
1010
import * as Effect from "effect/Effect";
11+
import { identity } from "effect/Function";
12+
import * as Layer from "effect/Layer";
1113
import * as Option from "effect/Option";
1214
import * as Schema from "effect/Schema";
13-
import { Headers, HttpClient, HttpClientRequest } from "effect/unstable/http";
15+
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http";
1416

1517
import * as DesktopCloudAuth from "../../app/DesktopCloudAuth.ts";
1618
import * as DesktopCloudAuthTokenStore from "../../app/DesktopCloudAuthTokenStore.ts";
@@ -52,6 +54,66 @@ export function validateClerkFrontendApiUrl(rawUrl: string): URL {
5254
return url;
5355
}
5456

57+
function executeCloudAuthFetch(url: URL, input: typeof DesktopCloudAuthFetchInputSchema.Type) {
58+
return Effect.gen(function* () {
59+
const response = yield* HttpClientRequest.make((input.method ?? "GET") as "GET" | "POST")(
60+
url,
61+
).pipe(
62+
HttpClientRequest.setHeaders(input.headers),
63+
input.body === undefined
64+
? identity
65+
: HttpClientRequest.bodyText(input.body, input.headers["content-type"]),
66+
HttpClient.execute,
67+
Effect.mapError(
68+
(cause) =>
69+
new DesktopCloudAuthFetchError({
70+
reason: "Desktop cloud auth fetch failed to execute.",
71+
cause,
72+
}),
73+
),
74+
);
75+
76+
const body = yield* response.text.pipe(
77+
Effect.mapError(
78+
(cause) =>
79+
new DesktopCloudAuthFetchError({
80+
reason: "Desktop cloud auth fetch response could not be read.",
81+
cause,
82+
}),
83+
),
84+
);
85+
86+
return {
87+
ok: response.status >= 200 && response.status < 300,
88+
status: response.status,
89+
statusText: "",
90+
headers: response.headers,
91+
body,
92+
};
93+
});
94+
}
95+
96+
const electronNetFetchLayer = Layer.unwrap(
97+
Effect.gen(function* () {
98+
const electronFetch = yield* Effect.promise(async () => {
99+
try {
100+
const electron = (await import("electron")) as {
101+
readonly net?: { readonly fetch?: typeof globalThis.fetch };
102+
};
103+
return typeof electron.net?.fetch === "function"
104+
? electron.net.fetch.bind(electron.net)
105+
: null;
106+
} catch {
107+
return null;
108+
}
109+
}).pipe(Effect.catchCause(() => Effect.succeed(null)));
110+
111+
return FetchHttpClient.layer.pipe(
112+
Layer.provide(Layer.succeed(FetchHttpClient.Fetch, electronFetch ?? globalThis.fetch)),
113+
);
114+
}),
115+
);
116+
55117
export const createCloudAuthRequest = makeIpcMethod({
56118
channel: IpcChannels.CREATE_CLOUD_AUTH_REQUEST_CHANNEL,
57119
payload: Schema.Void,
@@ -108,47 +170,6 @@ export const fetchCloudAuth = makeIpcMethod({
108170
}),
109171
});
110172

111-
const requestWithoutBody = HttpClientRequest.make((input.method ?? "GET") as "GET" | "POST")(
112-
url,
113-
{
114-
headers: input.headers,
115-
},
116-
);
117-
const request =
118-
input.body === undefined
119-
? requestWithoutBody
120-
: HttpClientRequest.bodyText(
121-
requestWithoutBody,
122-
input.body,
123-
Option.getOrUndefined(Headers.get(requestWithoutBody.headers, "content-type")),
124-
);
125-
126-
const response = yield* HttpClient.execute(request).pipe(
127-
Effect.mapError(
128-
(cause) =>
129-
new DesktopCloudAuthFetchError({
130-
reason: "Desktop cloud auth fetch failed.",
131-
cause,
132-
}),
133-
),
134-
);
135-
136-
const body = yield* response.text.pipe(
137-
Effect.mapError(
138-
(cause) =>
139-
new DesktopCloudAuthFetchError({
140-
reason: "Desktop cloud auth fetch response could not be read.",
141-
cause,
142-
}),
143-
),
144-
);
145-
146-
return {
147-
ok: response.status >= 200 && response.status < 300,
148-
status: response.status,
149-
statusText: "",
150-
headers: response.headers,
151-
body,
152-
};
173+
return yield* executeCloudAuthFetch(url, input).pipe(Effect.provide(electronNetFetchLayer));
153174
}),
154175
});

0 commit comments

Comments
 (0)