Skip to content

Commit 316ad31

Browse files
committed
Using TypeScriptExample component
1 parent bb499e9 commit 316ad31

File tree

1 file changed

+9
-52
lines changed

1 file changed

+9
-52
lines changed

src/content/docs/durable-objects/get-started.mdx

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar:
55
order: 2
66
---
77

8-
import { Render, TabItem, Tabs, PackageManagers, WranglerConfig } from "~/components";
8+
import { Render, TabItem, Tabs, PackageManagers, WranglerConfig, TypeScriptExample } from "~/components";
99

1010
This guide will instruct you through:
1111

@@ -72,16 +72,7 @@ If you do not use JavaScript or TypeScript, you will need a [shim](https://devel
7272

7373
Your `MyDurableObject` class will have a constructor with two parameters. The first parameter, `ctx`, passed to the class constructor contains state specific to the Durable Object, including methods for accessing storage. The second parameter, `env`, contains any bindings you have associated with the Worker when you uploaded it.
7474

75-
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">
76-
77-
```js
78-
export class MyDurableObject extends DurableObject<Env> {
79-
constructor(ctx, env) {}
80-
}
81-
```
82-
83-
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
84-
75+
<TypeScriptExample>
8576
```ts
8677
export class MyDurableObject extends DurableObject<Env> {
8778
constructor(ctx: DurableObjectState, env: Env) {
@@ -90,30 +81,13 @@ export class MyDurableObject extends DurableObject<Env> {
9081
}
9182
}
9283
```
93-
94-
</TabItem> </Tabs>
84+
</TypeScriptExample>
9585

9686
Workers communicate with a Durable Object using [remote-procedure call](/workers/runtime-apis/rpc/#_top). Public methods on a Durable Object class are exposed as [RPC methods](/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/) to be called by another Worker.
9787

9888
Your file should now look like:
9989

100-
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">
101-
102-
```js
103-
export class MyDurableObject extends DurableObject<Env> {
104-
constructor(ctx: DurableObjectState, env: Env) {}
105-
106-
async sayHello():Promise<string> {
107-
let result = this.ctx.storage.sql
108-
.exec("SELECT 'Hello, World!' as greeting")
109-
.one();
110-
return result.greeting;
111-
}
112-
}
113-
```
114-
115-
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
116-
90+
<TypeScriptExample>
11791
```ts
11892
export class MyDurableObject extends DurableObject<Env> {
11993
constructor(ctx: DurableObjectState, env: Env) {
@@ -129,8 +103,7 @@ export class MyDurableObject extends DurableObject<Env> {
129103
}
130104
}
131105
```
132-
133-
</TabItem> </Tabs>
106+
</TypeScriptExample>
134107

135108
In the code above, you have:
136109

@@ -151,24 +124,7 @@ A Worker is used to [access Durable Objects](/durable-objects/best-practices/cre
151124

152125
To communicate with a Durable Object, the Worker's fetch handler should look like the following:
153126

154-
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">
155-
156-
```js
157-
export default {
158-
async fetch(request, env):Promise<string> {
159-
const id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
160-
161-
const stub = env.MY_DURABLE_OBJECT.get(id);
162-
163-
const greeting = await stub.sayHello();
164-
165-
return new Response(greeting);
166-
},
167-
};
168-
```
169-
170-
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
171-
127+
<TypeScriptExample>
172128
```ts
173129
export default {
174130
async fetch(request, env, ctx): Promise<Response> {
@@ -182,8 +138,7 @@ export default {
182138
},
183139
} satisfies ExportedHandler<Env>;
184140
```
185-
186-
</TabItem> </Tabs>
141+
</TypeScriptExample>
187142

188143
In the code above, you have:
189144

@@ -262,6 +217,7 @@ Preview your Durable Object Worker at `<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.de
262217

263218
Your final code should look like this:
264219

220+
<TypeScriptExample>
265221
```ts title="index.ts"
266222
import { DurableObject } from "cloudflare:workers";
267223
export class MyDurableObject extends DurableObject<Env> {
@@ -289,6 +245,7 @@ export default {
289245
},
290246
} satisfies ExportedHandler<Env>;
291247
```
248+
</TypeScriptExample>
292249

293250
By finishing this tutorial, you have:
294251

0 commit comments

Comments
 (0)