@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 138 lines 4.66 KB
Raw
1 /**
2 * Deno HTTP client built on the fetch-based `HttpClient`.
3 *
4 * This module re-exports `effect/http/FetchHttpClient` and adds `makeClient`
5 * and `layerOptions`, which route every request through a `Deno.HttpClient`
6 * created from native `Deno.createHttpClient` options, such as a Unix domain
7 * socket transport or custom TLS certificates.
8 *
9 * @stability unstable
10 * @since 4.0.0
11 */
12 import * as Context from "effect/Context"
13 import * as Effect from "effect/Effect"
14 import * as FetchHttpClient from "effect/http/FetchHttpClient"
15 import * as HttpClient from "effect/http/HttpClient"
16 import * as Layer from "effect/Layer"
17 import type * as Scope from "effect/Scope"
18
19 /**
20 * @category re-exports
21 * @since 4.0.0
22 */
23 export * from "effect/http/FetchHttpClient"
24
25 /**
26 * Options accepted by `Deno.createHttpClient`, used by `makeClient` and
27 * `layerOptions`.
28 *
29 * **Details**
30 *
31 * Supported fields include `caCerts` for additional root certificates,
32 * `cert` and `key` for a client certificate in PEM format, `proxy` for an
33 * alternative transport (`http`, `https`, `socks5`, `tcp`, `unix` or `vsock`),
34 * `poolMaxIdlePerHost`, `poolIdleTimeout`, `http1`, `http2`, `allowHost` and
35 * `localAddress`.
36 *
37 * Deno has no option to skip certificate verification or to override the
38 * server name used for verification. Unix domain socket transports are not
39 * supported on Windows.
40 *
41 * @stability unstable
42 * @category models
43 * @since 4.0.6
44 */
45 export type Options = Parameters<typeof Deno.createHttpClient>[0]
46
47 /**
48 * Creates a `Deno.HttpClient` from the supplied options and closes it when
49 * the scope ends.
50 *
51 * **When to use**
52 *
53 * Use when you need the native client itself, for example to call `fetch`
54 * directly with `{ client }` or to share one client between several layers,
55 * with the same scoped cleanup `layerOptions` provides. Otherwise prefer
56 * `layerOptions`.
57 *
58 * @stability unstable
59 * @category constructors
60 * @since 4.0.6
61 */
62 export const makeClient = (options?: Options): Effect.Effect<Deno.HttpClient, never, Scope.Scope> =>
63 Effect.acquireRelease(
64 Effect.sync(() => Deno.createHttpClient(options ?? {})),
65 (client) => Effect.sync(() => client.close())
66 )
67
68 interface ClientRequestInit extends globalThis.RequestInit {
69 readonly client: Deno.HttpClient
70 }
71
72 /**
73 * Layer that provides an `HttpClient` sending every request through a
74 * `Deno.HttpClient` created with the supplied native options.
75 *
76 * **When to use**
77 *
78 * Use to send every request over a Unix domain socket with
79 * `{ proxy: { transport: "unix", path } }`, or over TLS with a custom CA and
80 * client certificate with `{ caCerts: [ca], cert, key }`.
81 *
82 * **Details**
83 *
84 * The native client is acquired in the layer's scope and closed when the
85 * scope ends. Before each request it is set as the `client` field of the
86 * `RequestInit` in effect for that request: the `RequestInit` service in the
87 * request context, or otherwise the one provided while building this layer.
88 * An application-wide `RequestInit` for `credentials`, `cache` or default
89 * headers therefore still applies, and only `client` is overridden. Method,
90 * headers, body and abort signal are set per request.
91 *
92 * **Example** (Sending requests over a Unix domain socket)
93 *
94 * ```ts
95 * import { DenoHttpClient } from "@effect/platform-deno"
96 *
97 * const DockerClient = DenoHttpClient.layerOptions({
98 * proxy: { transport: "unix", path: "/var/run/docker.sock" }
99 * })
100 * ```
101 *
102 * **Example** (Mutual TLS with a private CA)
103 *
104 * ```ts
105 * import { DenoHttpClient } from "@effect/platform-deno"
106 *
107 * declare const ca: string
108 * declare const cert: string
109 * declare const key: string
110 *
111 * const MtlsClient = DenoHttpClient.layerOptions({ caCerts: [ca], cert, key })
112 * ```
113 *
114 * @see {@link Options} for the accepted fields and limitations
115 * @see {@link makeClient} for acquiring the native client on its own
116 *
117 * @stability unstable
118 * @category layers
119 * @since 4.0.6
120 */
121 export const layerOptions = (options?: Options): Layer.Layer<HttpClient.HttpClient> =>
122 Layer.effect(HttpClient.HttpClient)(
123 Effect.gen(function*() {
124 const client = yield* makeClient(options)
125 const httpClient = yield* HttpClient.HttpClient
126 const defaults = Context.getOrUndefined(yield* Effect.context<never>(), FetchHttpClient.RequestInit)
127 return HttpClient.transformResponse(
128 httpClient,
129 Effect.updateContext((context: Context.Context<never>) => {
130 const init: ClientRequestInit = {
131 ...(Context.getOrUndefined(context, FetchHttpClient.RequestInit) ?? defaults),
132 client
133 }
134 return Context.add(context, FetchHttpClient.RequestInit, init)
135 })
136 )
137 })
138 ).pipe(Layer.provide(FetchHttpClient.layer))