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
| 1 | /** |
| 2 | * Bun HTTP client built on the fetch-based `HttpClient`. |
| 3 | * |
| 4 | * This module re-exports `effect/http/FetchHttpClient` and adds |
| 5 | * `layerOptions`, which applies Bun's native fetch options, such as Unix |
| 6 | * domain sockets and custom TLS settings, to every request. |
| 7 | * |
| 8 | * @stability unstable |
| 9 | * @since 4.0.0 |
| 10 | */ |
| 11 | import * as Context from "effect/Context" |
| 12 | import * as Effect from "effect/Effect" |
| 13 | import * as FetchHttpClient from "effect/http/FetchHttpClient" |
| 14 | import * as HttpClient from "effect/http/HttpClient" |
| 15 | import * as Layer from "effect/Layer" |
| 16 | |
| 17 | /** |
| 18 | * @category re-exports |
| 19 | * @since 4.0.0 |
| 20 | */ |
| 21 | export * from "effect/http/FetchHttpClient" |
| 22 | |
| 23 | /** |
| 24 | * Bun fetch options applied to every request sent by `layerOptions`. |
| 25 | * |
| 26 | * **Details** |
| 27 | * |
| 28 | * This is Bun's `BunFetchRequestInit` without the fields the client sets per |
| 29 | * request: `method`, `body`, `signal` and `duplex`. `headers` are merged with |
| 30 | * the request headers, with request headers taking precedence. |
| 31 | * |
| 32 | * Bun-specific fields include `unix` for a Unix domain socket path, `tls` for |
| 33 | * `ca`, `cert`, `key`, `rejectUnauthorized` and `checkServerIdentity`, `proxy`, |
| 34 | * `session` for a shared connection pool, `protocol`, `decompress`, `verbose` |
| 35 | * and `s3`. Standard `RequestInit` fields such as `credentials` or `redirect` |
| 36 | * are accepted too. |
| 37 | * |
| 38 | * @stability unstable |
| 39 | * @category models |
| 40 | * @since 4.0.6 |
| 41 | */ |
| 42 | export type Options = Omit<BunFetchRequestInit, "method" | "body" | "signal" | "duplex"> |
| 43 | |
| 44 | /** |
| 45 | * Layer that provides an `HttpClient` sending every request through Bun's |
| 46 | * `fetch` with the supplied native options. |
| 47 | * |
| 48 | * **When to use** |
| 49 | * |
| 50 | * Use to send every request over a Unix domain socket with `{ unix: path }`, |
| 51 | * or over TLS with a custom CA and client certificate with |
| 52 | * `{ tls: { ca, cert, key } }`. |
| 53 | * |
| 54 | * **Details** |
| 55 | * |
| 56 | * Before each request the options are merged over the `RequestInit` in effect |
| 57 | * for that request: the `RequestInit` service in the request context, or |
| 58 | * otherwise the one provided while building this layer. An application-wide |
| 59 | * `RequestInit` for `credentials`, `cache` or default headers therefore still |
| 60 | * applies, and the native options win per top-level key. Method, body, abort |
| 61 | * signal and duplex mode are set per request, and request headers override |
| 62 | * the same keys in `headers`. |
| 63 | * |
| 64 | * **Example** (Sending requests over a Unix domain socket) |
| 65 | * |
| 66 | * ```ts |
| 67 | * import { BunHttpClient } from "@effect/platform-bun" |
| 68 | * |
| 69 | * const DockerClient = BunHttpClient.layerOptions({ unix: "/var/run/docker.sock" }) |
| 70 | * ``` |
| 71 | * |
| 72 | * **Example** (Mutual TLS with a private CA) |
| 73 | * |
| 74 | * ```ts |
| 75 | * import { BunHttpClient } from "@effect/platform-bun" |
| 76 | * |
| 77 | * declare const ca: string |
| 78 | * declare const cert: string |
| 79 | * declare const key: string |
| 80 | * |
| 81 | * const MtlsClient = BunHttpClient.layerOptions({ tls: { ca, cert, key } }) |
| 82 | * ``` |
| 83 | * |
| 84 | * @see {@link Options} for the accepted fields |
| 85 | * |
| 86 | * @stability unstable |
| 87 | * @category layers |
| 88 | * @since 4.0.6 |
| 89 | */ |
| 90 | export const layerOptions = (options: Options): Layer.Layer<HttpClient.HttpClient> => |
| 91 | Layer.effect(HttpClient.HttpClient)( |
| 92 | Effect.gen(function*() { |
| 93 | const client = yield* HttpClient.HttpClient |
| 94 | const defaults = Context.getOrUndefined(yield* Effect.context<never>(), FetchHttpClient.RequestInit) |
| 95 | return HttpClient.transformResponse( |
| 96 | client, |
| 97 | Effect.updateContext((context: Context.Context<never>) => |
| 98 | Context.add(context, FetchHttpClient.RequestInit, { |
| 99 | ...(Context.getOrUndefined(context, FetchHttpClient.RequestInit) ?? defaults), |
| 100 | ...options |
| 101 | }) |
| 102 | ) |
| 103 | ) |
| 104 | }) |
| 105 | ).pipe(Layer.provide(FetchHttpClient.layer)) |