@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 143 lines 4.5 KB
Raw
1 import type * as cf from "@cloudflare/workers-types";
2 import * as Binding from "alchemy/Binding";
3 import type { RuntimeContext } from "alchemy/RuntimeContext";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import type { App } from "./App.ts";
7
8 /**
9 * Bind a {@link App} to a Worker and obtain the Effect-native flag-evaluation
10 * client (`get`, `getBooleanValue`, `getStringValue`, …).
11 *
12 * `ReadFlags` is a single identifier that is simultaneously the binding's
13 * Context tag, its type, and the callable —
14 * `yield* Cloudflare.Flagship.ReadFlags(app)`.
15 *
16 * **Example:** Evaluating a flag inside a Worker
17 * ```typescript
18 * const flags = yield* Cloudflare.Flagship.ReadFlags(MyApp);
19 * const enabled = yield* flags.getBooleanValue("new-checkout", false);
20 * ```
21 *
22 * @binding
23 * @product Flagship
24 * @category Developer Platform
25 */
26 export interface ReadFlags extends Binding.Service<
27 ReadFlags,
28 "Cloudflare.Flagship.ReadFlags",
29 (app: App) => Effect.Effect<ReadFlagsClient>
30 > {}
31
32 export const ReadFlags = Binding.Service<ReadFlags>("Cloudflare.Flagship.ReadFlags");
33
34 export class FlagshipError extends Data.TaggedError("FlagshipError")<{
35 message: string;
36 cause: unknown;
37 }> {}
38
39 /** An Effect produced by a {@link ReadFlagsClient} operation. */
40 type FlagshipEffect<A> = Effect.Effect<A, FlagshipError, RuntimeContext>;
41
42 // Re-exported so callers don't reach into the `@cloudflare/workers-types`
43 // namespace directly.
44 export type EvaluationContext = cf.FlagshipEvaluationContext;
45 export type EvaluationDetails<T> = cf.FlagshipEvaluationDetails<T>;
46
47 /**
48 * Effect-native client for a Cloudflare Flagship (feature flags) binding.
49 *
50 * Mirrors the runtime {@link cf.Flagship} binding one-to-one, translating each
51 * promise-returning method into an Effect. Flagship evaluation never throws —
52 * it falls back to the provided `defaultValue` — so the {@link FlagshipError}
53 * channel only surfaces unexpected runtime failures (e.g. a misconfigured
54 * binding). Use `Cloudflare.Flagship.ReadFlags(app)` inside a Worker's init
55 * phase to obtain it.
56 */
57 export interface ReadFlagsClient {
58 /**
59 * Effect resolving to the raw Cloudflare Flagship runtime binding.
60 */
61 raw: Effect.Effect<cf.Flagship, never, RuntimeContext>;
62 /**
63 * Get a flag value without type checking. Use when the flag type is not
64 * known at compile time.
65 */
66 get(
67 flagKey: string,
68 defaultValue?: unknown,
69 context?: EvaluationContext,
70 ): FlagshipEffect<unknown>;
71 /**
72 * Get a boolean flag value, falling back to `defaultValue` when evaluation
73 * fails or the flag type does not match.
74 */
75 getBooleanValue(
76 flagKey: string,
77 defaultValue: boolean,
78 context?: EvaluationContext,
79 ): FlagshipEffect<boolean>;
80 /**
81 * Get a string flag value, falling back to `defaultValue` when evaluation
82 * fails or the flag type does not match.
83 */
84 getStringValue(
85 flagKey: string,
86 defaultValue: string,
87 context?: EvaluationContext,
88 ): FlagshipEffect<string>;
89 /**
90 * Get a number flag value, falling back to `defaultValue` when evaluation
91 * fails or the flag type does not match.
92 */
93 getNumberValue(
94 flagKey: string,
95 defaultValue: number,
96 context?: EvaluationContext,
97 ): FlagshipEffect<number>;
98 /**
99 * Get a typed object flag value, falling back to `defaultValue` when
100 * evaluation fails or the flag type does not match.
101 */
102 getObjectValue<T extends object>(
103 flagKey: string,
104 defaultValue: T,
105 context?: EvaluationContext,
106 ): FlagshipEffect<T>;
107 /**
108 * Get a boolean flag value with full evaluation details (variant, reason,
109 * error code).
110 */
111 getBooleanDetails(
112 flagKey: string,
113 defaultValue: boolean,
114 context?: EvaluationContext,
115 ): FlagshipEffect<EvaluationDetails<boolean>>;
116 /**
117 * Get a string flag value with full evaluation details (variant, reason,
118 * error code).
119 */
120 getStringDetails(
121 flagKey: string,
122 defaultValue: string,
123 context?: EvaluationContext,
124 ): FlagshipEffect<EvaluationDetails<string>>;
125 /**
126 * Get a number flag value with full evaluation details (variant, reason,
127 * error code).
128 */
129 getNumberDetails(
130 flagKey: string,
131 defaultValue: number,
132 context?: EvaluationContext,
133 ): FlagshipEffect<EvaluationDetails<number>>;
134 /**
135 * Get a typed object flag value with full evaluation details (variant,
136 * reason, error code).
137 */
138 getObjectDetails<T extends object>(
139 flagKey: string,
140 defaultValue: T,
141 context?: EvaluationContext,
142 ): FlagshipEffect<EvaluationDetails<T>>;
143 }