diff --git a/src/content/docs/durable-objects/get-started.mdx b/src/content/docs/durable-objects/get-started.mdx
index 1825da1ab383047..2ae87f45748900b 100644
--- a/src/content/docs/durable-objects/get-started.mdx
+++ b/src/content/docs/durable-objects/get-started.mdx
@@ -1,11 +1,11 @@
---
-title: Get started
+title: Getting started
pcx_content_type: get-started
sidebar:
order: 2
---
-import { Render, TabItem, Tabs, PackageManagers, WranglerConfig } from "~/components";
+import { Render, TabItem, Tabs, PackageManagers, WranglerConfig, TypeScriptExample } from "~/components";
This guide will instruct you through:
@@ -72,56 +72,30 @@ If you do not use JavaScript or TypeScript, you will need a [shim](https://devel
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.
-
-
-```js
-export class MyDurableObject extends DurableObject {
- constructor(ctx, env) {}
-}
-```
-
-
-
+
```ts
-export class MyDurableObject extends DurableObject {
+export class MyDurableObject extends DurableObject {
constructor(ctx: DurableObjectState, env: Env) {
// Required, as we're extending the base class.
super(ctx, env)
}
}
```
-
-
+
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.
Your file should now look like:
-
-
-```js
-export class MyDurableObject extends DurableObject {
- constructor(ctx: DurableObjectState, env: Env) {}
-
- async sayHello() {
- let result = this.ctx.storage.sql
- .exec("SELECT 'Hello, World!' as greeting")
- .one();
- return result.greeting;
- }
-}
-```
-
-
-
+
```ts
-export class MyDurableObject extends DurableObject {
+export class MyDurableObject extends DurableObject {
constructor(ctx: DurableObjectState, env: Env) {
// Required, as we're extending the base class.
super(ctx, env)
}
- async sayHello() {
+ async sayHello():Promise {
let result = this.ctx.storage.sql
.exec("SELECT 'Hello, World!' as greeting")
.one();
@@ -129,8 +103,7 @@ export class MyDurableObject extends DurableObject {
}
}
```
-
-
+
In the code above, you have:
@@ -151,39 +124,21 @@ A Worker is used to [access Durable Objects](/durable-objects/best-practices/cre
To communicate with a Durable Object, the Worker's fetch handler should look like the following:
-
-
-```js
-export default {
- async fetch(request, env) {
- let id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
-
- let stub = env.MY_DURABLE_OBJECT.get(id);
-
- let greeting = await stub.sayHello();
-
- return new Response(greeting);
- },
-};
-```
-
-
-
+
```ts
export default {
async fetch(request, env, ctx): Promise {
- let id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
+ const id:DurableObjectId = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
- let stub = env.MY_DURABLE_OBJECT.get(id);
+ const stub = env.MY_DURABLE_OBJECT.get(id);
- let greeting = await stub.sayHello();
+ const greeting = await stub.sayHello();
return new Response(greeting);
},
} satisfies ExportedHandler;
```
-
-
+
In the code above, you have:
@@ -262,15 +217,16 @@ Preview your Durable Object Worker at `..workers.de
Your final code should look like this:
+
```ts title="index.ts"
import { DurableObject } from "cloudflare:workers";
-export class MyDurableObject extends DurableObject {
+export class MyDurableObject extends DurableObject {
constructor(ctx: DurableObjectState, env: Env) {
// Required, as we are extending the base class.
super(ctx, env)
}
- async sayHello() {
+ async sayHello():Promise {
let result = this.ctx.storage.sql
.exec("SELECT 'Hello, World!' as greeting")
.one();
@@ -278,17 +234,18 @@ export class MyDurableObject extends DurableObject {
}
}
export default {
- async fetch(request, env, ctx): Promise {
- let id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
+ async fetch(request, env, ctx):Promise {
+ const id:DurableObjectId = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
- let stub = env.MY_DURABLE_OBJECT.get(id);
+ const stub = env.MY_DURABLE_OBJECT.get(id);
- let greeting = await stub.sayHello();
+ const greeting = await stub.sayHello();
return new Response(greeting);
},
} satisfies ExportedHandler;
```
+
By finishing this tutorial, you have: