H
Hono4mo ago
Paul

How can I mount Cloudflare Durable Objects to a route?

Durable Objects Docs: https://hono.dev/examples/cloudflare-durable-objects Durable Object example straight from docs. tldr; it's a class.
import { Hono } from 'hono'

export default class Counter {
value: number = 0
state: DurableObjectState
app: Hono = new Hono()

constructor(state: DurableObjectState) {
this.state = state
this.state.blockConcurrencyWhile(async () => {
const stored = await this.state.storage?.get<number>('value')
this.value = stored || 0
})

this.app.get('/increment', async (c) => {
const currentValue = ++this.value
await this.state.storage?.put('value', this.value)
return c.text(currentValue.toString())
})

this.app.get('/decrement', async (c) => {
const currentValue = --this.value
await this.state.storage?.put('value', this.value)
return c.text(currentValue.toString())
})

this.app.get('/', async (c) => {
return c.text(this.value.toString())
})
}

async fetch(request: Request) {
return this.app.fetch(request)
}
}
import { Hono } from 'hono'

export default class Counter {
value: number = 0
state: DurableObjectState
app: Hono = new Hono()

constructor(state: DurableObjectState) {
this.state = state
this.state.blockConcurrencyWhile(async () => {
const stored = await this.state.storage?.get<number>('value')
this.value = stored || 0
})

this.app.get('/increment', async (c) => {
const currentValue = ++this.value
await this.state.storage?.put('value', this.value)
return c.text(currentValue.toString())
})

this.app.get('/decrement', async (c) => {
const currentValue = --this.value
await this.state.storage?.put('value', this.value)
return c.text(currentValue.toString())
})

this.app.get('/', async (c) => {
return c.text(this.value.toString())
})
}

async fetch(request: Request) {
return this.app.fetch(request)
}
}
How can I mount this class on a route?
import durableObject from "./durable-object";

export const app = new Hono();
app.route("/durable-object", durableObject);
import durableObject from "./durable-object";

export const app = new Hono();
app.route("/durable-object", durableObject);
Error:
Argument of type 'typeof Counter' is not assignable to parameter of type 'Hono<Env, Schema, string>'.
Type 'typeof Counter' is missing the following properties from type 'Hono<Env, Schema, string>': #private, get, post, put, and 25 more.ts(2345)
Error (TS2345) |
Argument of type
is not assignable to parameter of type
.


Type
is missing the following properties from type
:
#private, get, post, put, and 25 more.
Argument of type 'typeof Counter' is not assignable to parameter of type 'Hono<Env, Schema, string>'.
Type 'typeof Counter' is missing the following properties from type 'Hono<Env, Schema, string>': #private, get, post, put, and 25 more.ts(2345)
Error (TS2345) |
Argument of type
is not assignable to parameter of type
.


Type
is missing the following properties from type
:
#private, get, post, put, and 25 more.
Cloudflare Durable Objects - Hono
Ultrafast web framework for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server