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

main
ts 91 lines 3.35 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import type { RuntimeContext } from "alchemy/RuntimeContext";
3 import type * as Effect from "effect/Effect";
4 import type * as HttpClientError from "effect/http/HttpClientError";
5 import type * as HttpClientRequest from "effect/http/HttpClientRequest";
6 import type * as HttpClientResponse from "effect/http/HttpClientResponse";
7 import type { MtlsCertificate } from "./MtlsCertificate.ts";
8
9 /**
10 * Send requests from an Effect Worker that present a leaf
11 * {@link MtlsCertificate} to the origin.
12 *
13 * `Fetch` is a single identifier that is the binding's Context tag, its type,
14 * and the callable: `yield* Cloudflare.MtlsCertificate.Fetch(cert)` binds the
15 * certificate to the Worker as an `mtls_certificate` binding and returns a
16 * function from `HttpClientRequest` to `HttpClientResponse`. Every request it
17 * sends goes out over TLS presenting the certificate, so origins that require
18 * client authentication (mTLS) accept it.
19 *
20 * Bind a leaf certificate uploaded with its private key (`ca: false`). Provide
21 * {@link FetchBinding} on the Worker's Effect to resolve the native binding at
22 * request time. Async Workers get the same binding by passing the certificate
23 * in `env` instead, where it is typed as a `Fetcher`.
24 *
25 * ### Calling an mTLS origin
26 * **Example:** Present a client certificate from an Effect Worker
27 * ```typescript
28 * import * as Cloudflare from "@alchemy.run/cloudflare";
29 * import * as Config from "effect/Config";
30 * import * as Effect from "effect/Effect";
31 * import * as HttpClientRequest from "effect/http/HttpClientRequest";
32 * import * as HttpServerResponse from "effect/http/HttpServerResponse";
33 *
34 * export const OriginCert = Effect.gen(function* () {
35 * return yield* Cloudflare.MtlsCertificate.MtlsCertificate("OriginCert", {
36 * ca: false,
37 * certificates: leafPem,
38 * privateKey: yield* Config.Redacted("ORIGIN_CLIENT_KEY"),
39 * });
40 * });
41 *
42 * export default class Api extends Cloudflare.Worker<Api>()(
43 * "Api",
44 * { main: import.meta.url },
45 * Effect.gen(function* () {
46 * const fetchOrigin = yield* Cloudflare.MtlsCertificate.Fetch(yield* OriginCert);
47 * return {
48 * fetch: Effect.gen(function* () {
49 * const response = yield* fetchOrigin(
50 * HttpClientRequest.get("https://origin.example.com/orders"),
51 * );
52 * return HttpServerResponse.text(yield* response.text);
53 * }),
54 * };
55 * }).pipe(Effect.provide(Cloudflare.MtlsCertificate.FetchBinding)),
56 * ) {}
57 * ```
58 *
59 * ### Requests with bodies
60 * **Example:** POST JSON to the origin
61 * ```typescript
62 * const response = yield* fetchOrigin(
63 * HttpClientRequest.post("https://origin.example.com/orders").pipe(
64 * HttpClientRequest.bodyJsonUnsafe({ sku: "abc", quantity: 1 }),
65 * ),
66 * );
67 * ```
68 *
69 * @see https://developers.cloudflare.com/workers/runtime-apis/bindings/mtls/
70 *
71 * @binding
72 * @product mTLS Certificates
73 * @category SSL/TLS & Certificates
74 */
75 export interface Fetch extends Binding.Service<
76 Fetch,
77 "Cloudflare.MtlsCertificate.Fetch",
78 (
79 certificate: MtlsCertificate,
80 ) => Effect.Effect<
81 (
82 request: HttpClientRequest.HttpClientRequest,
83 ) => Effect.Effect<
84 HttpClientResponse.HttpClientResponse,
85 HttpClientError.RequestError,
86 RuntimeContext
87 >
88 >
89 > {}
90
91 export const Fetch = Binding.Service<Fetch>("Cloudflare.MtlsCertificate.Fetch");