A wrapper of Cloudflare Workers's Durable Object for Hono.
MIT License
Bot releases are visible (Hide)
Published by github-actions[bot] 11 months ago
Published by github-actions[bot] 11 months ago
Published by github-actions[bot] 11 months ago
#21 f659d6c
Thanks @sor4chi! - Support for three handlers about Hibernation Websocket API.
webSocketMessage
handlerwebSocketClose
handlerwebSocketError
handlerYou can use these handlers same way as alarm
handler in Hono DO.
const DO = generateHonoObject("/", () => {});
DO.alarm(async () => {});
DO.webSocketMessage(async () => {});
DO.webSocketClose(async () => {});
DO.webSocketError(async () => {});
generateHonoObject("/", () => {})
.alarm(async () => {})
.webSocketMessage(async () => {})
.webSocketClose(async () => {})
.webSocketError(async () => {});
generateHonoObject("/", () => {}, {
alarm: async () => {},
webSocketMessage: async () => {},
webSocketClose: async () => {},
webSocketError: async () => {},
});
Take care for registering multiple handlers for same event.
If you register so, you will get an error.
Changed the interface of how to configure AlarmHandler
in generateHonoObject
argument.
generateHonoObject(
"/",
() => {},
() => {
console.log("alarm");
},
);
generateHonoObject("/", () => {}, {
alarm: () => {
console.log("alarm");
},
});
This is because we want to support many fields of Durable Object as handlers.
Published by github-actions[bot] about 1 year ago
#19 9722471
Thanks @sor4chi! - Fixed an issue where the BasePath
type in generateHonoObject
did not inherit the basePath of the first argument.
// Before
generateHonoObject("/foo", (app) => {
app.get("/bar", (c) => {
c; // $ExpectType Context<Env, "/bar", {}>
});
});
// After
generateHonoObject("/foo", (app) => {
app.get("/bar", (c) => {
c; // $ExpectType Context<Env, "/foo/bar", {}>
});
});
Published by github-actions[bot] about 1 year ago
#14 1d59c26
Thanks @sor4chi! - Support Alarms API
You can now create and manage alarms using the Alarms API. See the Alarms API documentation for details.
import { generateHonoObject } from "hono-do";
const SECONDS = 1000;
declare module "hono-do" {
interface HonoObjectVars {
count: number; // custom fields (vars) type support
}
}
export const Batcher = generateHonoObject("/batcher", async (app, { storage }, vars) => {
vars.count = (await storage.get<number>("count")) ?? 0;
app.post("/", async (c) => {
vars.count++;
const currentAlarm = await storage.getAlarm();
if (currentAlarm == null) {
await storage.setAlarm(Date.now() + 10 * SECONDS);
}
return c.json({ queued: vars.count });
});
}).alarm(async ({ storage }, vars) => {
console.log("Alarm fired, count is", vars.count);
await storage.deleteAll();
vars.count = 0;
});
#2 7de88ad
Thanks @sor4chi! - Implement storage helper as sub package
You can use state.storage
type-safely like hooks in React.
import { defineStorage } from "hono-do/storage";
export const Counter = generateHonoObject("/counter", async (app, { storage }) => {
const [getValue, setValue, delValue] = await defineStorage(storage, "value", 0);
await getValue(); // 0
await setValue(1);
await getValue(); // 1
await setValue((value) => value + 1); // 2
await getValue(); // 2
await delValue();
await getValue(); // 0
});
Published by github-actions[bot] about 1 year ago