Can we use hono stacks with Durable Objects? #1707
-
Can we use hono stacks with Durable Objects? And how can we do it? @yusukebe I'm following this example for hono with durable objects: https://github.com/honojs/examples/tree/main/durable-objects |
Beta Was this translation helpful? Give feedback.
Answered by
ShravanSunder
Nov 17, 2023
Replies: 2 comments
-
@sor4chi is creating a nice one for DO. |
Beta Was this translation helpful? Give feedback.
0 replies
-
i've created a simple local helper for myself. The rpc client is fully typed to be used with durable object. Here's what i have. // hono type from DO class
export type DistributedLeaseHonoController = typeof DistributedLease['prototype']['app']; // helper
import { type DurableObjectNamespace } from '@cloudflare/workers-types';
import { type Hono } from 'hono';
import { hc } from 'hono/client';
import { getDurableObjectFromName } from '~~/helpers/getDurableObjectFromName';
export const createClientForDO = <THonoType extends Hono>(params: {
dObj: DurableObjectNamespace;
idString: string;
origin: string;
}) => {
const stub = getDurableObjectFromName(params.dObj, params.idString);
const doFetch = async (url: URL, init?: RequestInit): Promise<Response> => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
const response = await stub.fetch(url, init as any);
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-return
return response as any;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const client = hc<THonoType>(params.origin, { fetch: doFetch as any });
return client;
}; // usage with RPC and HC stacks
const leaseRouteClient = createClientForDO<DistributedLeaseHonoController>({
dObj: c.env.JobDistributedLease,
idString: 'default',
origin: new URL(c.req.url).origin,
});
const result = await leaseRouteClient.acquire.$get({
query: { entityUid: 'd843c5af-9e97-4fc5-9a06-a1062430283c' },
});
const data = await result.json(); thanks for the amazing library @yusukebe |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ShravanSunder
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i've created a simple local helper for myself. The rpc client is fully typed to be used with durable object.
Here's what i have.