Skip to content

Commit

Permalink
update exapmle & update readme (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
naporin0624 authored Aug 6, 2024
1 parent cb8c9b5 commit d9839ce
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 209 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-dragons-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"y-durableobjects": patch
---

change readme hono env description
82 changes: 56 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,6 @@ tag = "v1"
new_classes = ["YDurableObjects"]
```

## Extending Hono with Bindings

To integrate `y-durableobjects` with Hono, extend the `Env` interface to include the `Bindings` type for better type safety and IntelliSense support in your editor.

```typescript
export type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace;
};

declare module "hono" {
interface Env {
Bindings: Bindings;
}
}
```

This allows you to use `Y_DURABLE_OBJECTS` directly in your Hono application with full type support.

## Usage

### With Hono shorthand
Expand All @@ -80,11 +62,19 @@ This allows you to use `Y_DURABLE_OBJECTS` directly in your Hono application wit
import { Hono } from "hono";
import { YDurableObjects, yRoute } from "y-durableobjects";

const app = new Hono();
type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace<YDurableObjects<Env>>;
};

type Env = {
Bindings: Bindings;
};

const app = new Hono<Env>();

const route = app.route(
"/editor",
yRoute((env) => env.Y_DURABLE_OBJECTS),
yRoute<Env>((env) => env.Y_DURABLE_OBJECTS),
);

export default route;
Expand All @@ -101,7 +91,15 @@ import { Hono } from "hono";
import { YDurableObjects, type YDurableObjectsAppType } from "y-durableobjects";
import { upgrade } from "y-durableobjects/helpers/upgrade";

const app = new Hono();
type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace<YDurableObjects<Env>>;
};

type Env = {
Bindings: Bindings;
};

const app = new Hono<Env>();
app.get("/editor/:id", upgrade(), async (c) => {
const id = c.env.Y_DURABLE_OBJECTS.idFromName(c.req.param("id"));
const stub = c.env.Y_DURABLE_OBJECTS.get(id);
Expand Down Expand Up @@ -143,7 +141,15 @@ import { Hono } from "hono";
import { YDurableObjects } from "y-durableobjects";
import { fromUint8Array } from "js-base64";

const app = new Hono();
type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace<YDurableObjects<Env>>;
};

type Env = {
Bindings: Bindings;
};

const app = new Hono<Env>();

app.get("/rooms/:id/state", async (c) => {
const roomId = c.req.param("id");
Expand All @@ -170,7 +176,15 @@ Example usage in Hono:
import { Hono } from "hono";
import { YDurableObjects } from "y-durableobjects";

const app = new Hono();
type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace<YDurableObjects<Env>>;
};

type Env = {
Bindings: Bindings;
};

const app = new Hono<Env>();

app.post("/rooms/:id/update", async (c) => {
const roomId = c.req.param("id");
Expand Down Expand Up @@ -224,10 +238,18 @@ export class CustomDurableObject extends YDurableObjects {
import { Hono } from "hono";
import { YDurableObjects, yRoute } from "y-durableobjects";

const app = new Hono();
type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace<YDurableObjects<Env>>;
};

type Env = {
Bindings: Bindings;
};

const app = new Hono<Env>();
const route = app.route(
"/editor",
yRoute((env) => env.Y_DURABLE_OBJECTS),
yRoute<Env>((env) => env.Y_DURABLE_OBJECTS),
);

export default route;
Expand All @@ -242,7 +264,15 @@ import { Hono } from "hono";
import { YDurableObjects, YDurableObjectsAppType } from "y-durableobjects";
import { upgrade } from "y-durableobjects/helpers/upgrade";

const app = new Hono();
type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace<YDurableObjects<Env>>;
};

type Env = {
Bindings: Bindings;
};

const app = new Hono<Env>();
app.get("/editor/:id", upgrade(), async (c) => {
const id = c.env.Y_DURABLE_OBJECTS.idFromName(c.req.param("id"));
const stub = c.env.Y_DURABLE_OBJECTS.get(id);
Expand Down
2 changes: 1 addition & 1 deletion example/apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@lexical/react": "^0.13.1",
"@lexical/rich-text": "^0.13.1",
"@lexical/table": "^0.13.1",
"hono": "^4.4.10",
"hono": "^4.5.3",
"lexical": "^0.13.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
10 changes: 5 additions & 5 deletions example/apps/workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
},
"devDependencies": {
"@biomejs/biome": "^1.4.1",
"@cloudflare/workers-types": "^4.20231218.0",
"typescript": "^5.2.2",
"wrangler": "^3.51.2"
"@cloudflare/workers-types": "^4.20240729.0",
"typescript": "^5.5.4",
"wrangler": "^3.68.0"
},
"dependencies": {
"hono": "^4.4.10",
"y-durableobjects": "^0.4.0"
"hono": "^4.5.3",
"y-durableobjects": "^1.0.0"
}
}
5 changes: 3 additions & 2 deletions example/apps/workers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Hono } from "hono";
import { cors } from "hono/cors";
import { YDurableObjects, yRoute } from "y-durableobjects";
import { Env } from "./types";

const app = new Hono();
const app = new Hono<Env>();
app.use("*", cors());

const route = app.route(
"/editor",
yRoute((env) => env.Y_DURABLE_OBJECTS),
yRoute<Env>((env) => env.Y_DURABLE_OBJECTS),
);

export default route;
Expand Down
14 changes: 7 additions & 7 deletions example/apps/workers/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace;
import type { YDurableObjects } from "y-durableobjects";

type Bindings = {
Y_DURABLE_OBJECTS: DurableObjectNamespace<YDurableObjects<Env>>;
};

declare module "hono" {
interface Env {
Bindings: Bindings;
}
}
export type Env = {
Bindings: Bindings;
};
Loading

0 comments on commit d9839ce

Please sign in to comment.