The best way to control Claude Code, Codex and more from any computer or phone

main
ts 93 lines 3.22 KB
Raw
1 import * as machines from "@distilled.cloud/fly-io/machines";
2 import * as Effect from "effect/Effect";
3 import * as FetchHttpClient from "effect/http/FetchHttpClient";
4 import * as Layer from "effect/Layer";
5 import { CredentialsFromAmbientOrEnv } from "./Credentials.ts";
6 import { type SecretAuth, makeHttpAppBinding, unwrapSecretValue } from "./SecretHttp.ts";
7 import { WriteCertificates, type WriteCertificatesClient } from "./WriteCertificates.ts";
8
9 /**
10 * HTTP implementation of {@link WriteCertificates}. Provide it on the
11 * {@link Service} or Action Effect.
12 *
13 * ### Provide the layer
14 * **Example:** On a Service
15 * ```typescript
16 * Effect.gen(function* () {
17 * const certs = yield* Fly.WriteCertificates(Site);
18 * // ...
19 * }).pipe(Effect.provide(Fly.WriteCertificatesHttp))
20 * ```
21 *
22 * @layer
23 * @product Certificate
24 * @provides Fly.WriteCertificates
25 */
26 export const WriteCertificatesHttp = Layer.effect(
27 WriteCertificates,
28 Effect.suspend(() =>
29 makeHttpAppBinding<WriteCertificatesClient>({
30 makeClient: certificatesWriteClient,
31 }),
32 ),
33 ).pipe(Layer.provide(FetchHttpClient.layer), Layer.provide(CredentialsFromAmbientOrEnv));
34
35 /** Build the client over an injectable auth and App name. */
36 export const certificatesWriteClient = (
37 auth: SecretAuth,
38 appName: Effect.Effect<string>,
39 ): WriteCertificatesClient => {
40 const authorize = auth.authorize;
41 return {
42 request: Effect.fn("Fly.Certificates.request")(function* (hostname) {
43 return yield* authorize(
44 machines
45 .createAppAcmeCertificate({ app_name: yield* appName, hostname })
46 .pipe(Effect.catchTag("Conflict", () => Effect.undefined)),
47 );
48 }),
49 upload: Effect.fn("Fly.Certificates.upload")(function* (request) {
50 const app_name = yield* appName;
51 const body = {
52 app_name,
53 hostname: request.hostname,
54 fullchain: request.fullchain,
55 private_key: unwrapSecretValue(request.privateKey),
56 };
57 return yield* authorize(
58 machines.createAppCustomCertificate(body).pipe(
59 // An existing custom certificate conflicts: replace it.
60 Effect.catchTag("Conflict", () =>
61 machines
62 .deleteAppCustomCertificate({
63 app_name,
64 hostname: request.hostname,
65 })
66 .pipe(
67 Effect.catchTag("NotFound", () => Effect.void),
68 Effect.andThen(machines.createAppCustomCertificate(body)),
69 ),
70 ),
71 ),
72 );
73 }),
74 check: Effect.fn("Fly.Certificates.check")(function* (hostname) {
75 return yield* authorize(machines.checkAppCertificate({ app_name: yield* appName, hostname }));
76 }),
77 get: Effect.fn("Fly.Certificates.get")(function* (hostname) {
78 return yield* authorize(
79 machines
80 .getAppCertificate({ app_name: yield* appName, hostname })
81 .pipe(Effect.catchTag("NotFound", () => Effect.undefined)),
82 );
83 }),
84 remove: Effect.fn("Fly.Certificates.remove")(function* (hostname) {
85 return yield* authorize(
86 machines.deleteAppCertificate({ app_name: yield* appName, hostname }).pipe(
87 Effect.catchTag("NotFound", () => Effect.void),
88 Effect.asVoid,
89 ),
90 );
91 }),
92 };
93 };