-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
"hono-do": minor | ||
--- | ||
|
||
Add `byName` handler for making DO proxy easier. | ||
|
||
## Without proxy | ||
|
||
```ts | ||
app.all("/counter/*", (c) => { | ||
const id = c.env.COUNTER.idFromName("Counter"); | ||
const obj = c.env.COUNTER.get(id); | ||
return obj.fetch(c.req.raw); | ||
}); | ||
``` | ||
|
||
## With proxy | ||
|
||
```ts | ||
app.use("/counter/*", Counter.byName("COUNTER", "Counter")); | ||
``` | ||
|
||
`.byName` is a handler to proxy the request to the DO object with the given name. | ||
The first argument is the name of the Binding namespace, the second is the name of the DO object. | ||
|
||
### Dynamic name | ||
|
||
```ts | ||
export const Room = generateHonoObject("/room/:roomId", (app) => { | ||
app.get("/", (c) => c.text(`Room ${c.req.param("roomId")}`)); | ||
}) | ||
|
||
app.use("/room/*", Room.byName("ROOM", (c) => c.req.param("roomId"))); | ||
``` | ||
|
||
Using callback as the second argument, the name of the DO object can be dynamic. | ||
You can separate objects for each request path. |