Bindings and services
Inject Cloudflare env bindings and DurableObjectState through Bindings and DurableState.
On Cloudflare, platform bindings and Durable Object storage reach your services through two framework tokens: Bindings for Worker env, and DurableState for a DO instance’s DurableObjectState.
Do not construct these classes yourself. The runtime adapter seeds them per execution context.
Bindings (Worker env)
Section titled “Bindings (Worker env)”import { FlareService } from "@flare-ts/core";import { Bindings } from "@flare-ts/core/cloudflare";
class KvRoom extends FlareService { public static override deps = [Bindings] as const;
async get(key: string) { const kv = this.inject(Bindings).env.MY_KV; return kv.get(key); }}Register the service with host.scoped(KvRoom) and inject it from routes as usual. env is typed from your Worker’s Cloudflare.Env (for example via wrangler types).
DurableState (per DO instance)
Section titled “DurableState (per DO instance)”Inside a FlareDurableObject, DurableState exposes the instance context:
import { FlareService } from "@flare-ts/core";import { DurableState, FlareDurableObject } from "@flare-ts/core/cloudflare";
class Counter extends FlareService { public static override deps = [DurableState] as const;
async increment() { const storage = this.inject(DurableState).storage; const n = (await storage.get<number>("n")) ?? 0; await storage.put("n", n + 1); return n + 1; }}
class CounterRoom extends FlareDurableObject { public static override deps = [Counter] as const;}Each DO instance gets its own lazy container. host.scoped() services resolve per instance, not per Worker isolate globally.
Inject from routes
Section titled “Inject from routes”const room = host.durableObject(CounterRoom);
room.http.get( "/inc", { inject: { counter: Counter } }, async (_ctx, scope) => new FlareResponse(200, { n: await scope.counter.increment() }),);Worker vs DO context
Section titled “Worker vs DO context”| Service | Seeded on | Provides |
|---|---|---|
Bindings | Worker fetch and DO instance | env bindings |
DurableState | DO instance only | state, storage, id |
A stateless Worker route has Bindings but no DurableState. A mounted DO route has both.
Related
Section titled “Related”- Durable Objects: registration and mount
- Using DI:
static depsand lifetimes - Deploy: wrangler binding entries