Paul
Paul
Explore posts from servers
HHono
Created by Paul on 6/26/2024 in #help
Can the queue only be defined in the root?
When writing a Cloudflare queue with Honojs, it seems the queue needs to be exported as part of the default. This default export cannot be mounted in another route path. Do all queue handlers need to be exported from the root?
2 replies
HHono
Created by Paul on 6/25/2024 in #help
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.
1 replies