Skip to content

Commit 0119d45

Browse files
vicbsdnts
authored andcommitted
Use both TS and JS for snippets (cloudflare#23248)
1 parent 3d23bf2 commit 0119d45

File tree

12 files changed

+63
-29
lines changed

12 files changed

+63
-29
lines changed

src/content/docs/agents/api-reference/agents-api.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The Agents SDK exposes two main APIs:
1616

1717
:::note
1818

19-
Agents require [Cloudflare Durable Objects](/durable-objects/), see [Configuration](/agents/getting-started/testing-your-agent/#add-the-agent-configuration) to learn how to add the required bindings to your project.
19+
Agents require [Cloudflare Durable Objects](/durable-objects/), see [Configuration](/agents/getting-started/testing-your-agent/#add-the-agent-configuration) to learn how to add the required bindings to your project.
2020

2121
:::
2222

@@ -51,6 +51,8 @@ You can also define your own methods on an Agent: it's technically valid to publ
5151

5252
Your own methods can access the Agent's environment variables and bindings on `this.env`, state on `this.setState`, and call other methods on the Agent via `this.yourMethodName`.
5353

54+
<TypeScriptExample>
55+
5456
```ts
5557
import { Agent } from "agents";
5658

@@ -124,6 +126,8 @@ class MyAgent extends Agent<Env, State> {
124126
}
125127
```
126128

129+
</TypeScriptExample>
130+
127131
<TypeScriptExample>
128132

129133
```ts

src/content/docs/ai-gateway/providers/workersai.mdx

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

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

1010
Use AI Gateway for analytics, caching, and security on requests to [Workers AI](/workers-ai/). Workers AI integrates seamlessly with AI Gateway, allowing you to execute AI inference via API requests or through an environment binding for Workers scripts. The binding simplifies the process by routing requests through your AI Gateway with minimal setup.
1111

@@ -82,6 +82,8 @@ curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/workers-ai/v
8282

8383
You can integrate Workers AI with AI Gateway using an environment binding. To include an AI Gateway within your Worker, add the gateway as an object in your Workers AI request.
8484

85+
<TypeScriptExample>
86+
8587
```ts
8688
export interface Env {
8789
AI: Ai;
@@ -107,6 +109,8 @@ export default {
107109
} satisfies ExportedHandler<Env>;
108110
```
109111

112+
</TypeScriptExample>
113+
110114
For a detailed step-by-step guide on integrating Workers AI with AI Gateway using a binding, see [Integrations in AI Gateway](/ai-gateway/integrations/aig-workers-ai-binding/).
111115

112116
Workers AI supports the following parameters for AI gateways:

src/content/docs/autorag/how-to/bring-your-own-generation-model.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ import {
1414
WranglerConfig,
1515
MetaInfo,
1616
Type,
17+
TypeScriptExample,
1718
} from "~/components";
1819

1920
When using `AI Search`, AutoRAG leverages a Workers AI model to generate the response. If you want to use a model outside of Workers AI, you can use AutoRAG for search while leveraging a model outside of Workers AI to generate responses.
2021

2122
Here is an example of how you can use an OpenAI model to generate your responses. This example uses [Workers Binding](/autorag/usage/workers-binding/), but can be easily adapted to use the [REST API](/autorag/usage/rest-api/) instead.
2223

24+
<TypeScriptExample>
25+
2326
```ts
2427
import { openai } from "@ai-sdk/openai";
2528
import { generateText } from "ai";
@@ -81,3 +84,5 @@ export default {
8184
},
8285
} satisfies ExportedHandler<Env>;
8386
```
87+
88+
</TypeScriptExample>

src/content/docs/autorag/how-to/simple-search-engine.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ sidebar:
55
order: 5
66
---
77

8+
import { TypeScriptExample } from "~/components";
9+
810
By using the `search` method, you can implement a simple but fast search engine. This example uses [Workers Binding](/autorag/usage/workers-binding/), but can be easily adapted to use the [REST API](/autorag/usage/rest-api/) instead.
911

1012
To replicate this example remember to:
1113

1214
- Disable `rewrite_query`, as you want to match the original user query
1315
- Configure your AutoRAG to have small chunk sizes, usually 256 tokens is enough
1416

17+
<TypeScriptExample>
18+
1519
```ts
1620
export interface Env {
1721
AI: Ai;
@@ -34,3 +38,5 @@ export default {
3438
},
3539
} satisfies ExportedHandler<Env>;
3640
```
41+
42+
</TypeScriptExample>

src/content/docs/autorag/tutorial/brower-rendering-autorag-tutorial.mdx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ sidebar:
55
order: 4
66
---
77

8+
import { TypeScriptExample } from "~/components";
9+
810
AutoRAG is designed to work out of the box with data in R2 buckets. But what if your content lives on a website or needs to be rendered dynamically?
911

1012
In this tutorial, we’ll walk through how to:
@@ -62,6 +64,8 @@ npx wrangler r2 bucket create html-bucket
6264

6365
5. Replace the contents of `src/index.ts` with the following skeleton script:
6466

67+
<TypeScriptExample filename="src/index.ts">
68+
6569
```typescript
6670
import puppeteer from "@cloudflare/puppeteer";
6771

@@ -120,6 +124,8 @@ export default {
120124
} satisfies ExportedHandler<Env>;
121125
```
122126

127+
</TypeScriptExample>
128+
123129
6. Once the code is ready, you can deploy it to your Cloudflare account by running:
124130

125131
```bash

src/content/docs/bots/workers-templates/delay-action.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ pcx_content_type: how-to
33
title: Delay action
44
---
55

6+
import { TypeScriptExample } from "~/components";
7+
68
Customers with a Bot Management and a [Workers](/workers/) subscription can use the template below to introduce a delay to requests that are likely from bots.
79

810
The template sets a minimum and maximum delay, and delays requests where the bot score is less than 30 and the URI path starts with `/exampleURI`.
911

10-
```js title="Workers template"
12+
<TypeScriptExample>
13+
14+
```ts title="Workers template"
1115
// Configurable Variables
1216
const PATH_START = '/exampleURI';
1317
const DELAY_FROM = 5; // in seconds
@@ -32,3 +36,5 @@ export default {
3236
},
3337
} satisfies ExportedHandler<Env>;
3438
```
39+
40+
</TypeScriptExample>

src/content/docs/browser-rendering/platform/puppeteer.mdx

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ sidebar:
66
order: 10
77
---
88

9-
import { TabItem, Tabs, PackageManagers } from "~/components";
9+
import { PackageManagers, TypeScriptExample } from "~/components";
1010

1111
[Puppeteer](https://pptr.dev/) is one of the most popular libraries that abstract the lower-level DevTools protocol from developers and provides a high-level API that you can use to easily instrument Chrome/Chromium and automate browsing sessions. Puppeteer is used for tasks like creating screenshots, crawling pages, and testing web applications.
1212

@@ -22,24 +22,7 @@ Our version is open sourced and can be found in [Cloudflare's fork of Puppeteer]
2222

2323
Once the [browser binding](/browser-rendering/platform/wrangler/#bindings) is configured and the `@cloudflare/puppeteer` library is installed, Puppeteer can be used in a Worker:
2424

25-
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
26-
27-
```js
28-
import puppeteer from "@cloudflare/puppeteer";
29-
30-
export default {
31-
async fetch(request, env) {
32-
const browser = await puppeteer.launch(env.MYBROWSER);
33-
const page = await browser.newPage();
34-
await page.goto("https://example.com");
35-
const metrics = await page.metrics();
36-
await browser.close();
37-
return Response.json(metrics);
38-
},
39-
};
40-
```
41-
42-
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
25+
<TypeScriptExample>
4326

4427
```ts
4528
import puppeteer from "@cloudflare/puppeteer";
@@ -60,7 +43,7 @@ export default {
6043
} satisfies ExportedHandler<Env>;
6144
```
6245

63-
</TabItem> </Tabs>
46+
</TypeScriptExample>
6447

6548
This script [launches](https://pptr.dev/api/puppeteer.puppeteernode.launch) the `env.MYBROWSER` browser, opens a [new page](https://pptr.dev/api/puppeteer.browser.newpage), [goes to](https://pptr.dev/api/puppeteer.page.goto) [https://example.com/](https://example.com/), gets the page load [metrics](https://pptr.dev/api/puppeteer.page.metrics), [closes](https://pptr.dev/api/puppeteer.browser.close) the browser and prints metrics in JSON.
6649

src/content/docs/browser-rendering/workers-bindings/reuse-sessions.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar:
55
order: 3
66
---
77

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

1010
The best way to improve the performance of your browser rendering Worker is to reuse sessions. One way to do that is via [Durable Objects](/browser-rendering/workers-bindings/browser-rendering-with-do/), which allows you to keep a long running connection from a Worker to a browser. Another way is to keep the browser open after you've finished with it, and connect to that session each time you have a new request.
1111

@@ -61,6 +61,8 @@ The script below starts by fetching the current running sessions. If there are a
6161

6262
Take into account that if the browser is idle, i.e. does not get any command, for more than the current [limit](/browser-rendering/platform/limits/), it will close automatically, so you must have enough requests per minute to keep it alive.
6363

64+
<TypeScriptExample>
65+
6466
```ts
6567
import puppeteer from "@cloudflare/puppeteer";
6668

@@ -136,6 +138,8 @@ export default {
136138
};
137139
```
138140

141+
</TypeScriptExample>
142+
139143
Besides `puppeteer.sessions()`, we have added other methods to facilitate [Session Management](/browser-rendering/platform/puppeteer/#session-management).
140144

141145
## 5. Test

src/content/docs/cloudflare-for-platforms/cloudflare-for-saas/domain-support/custom-metadata.mdx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ head: []
77
description: Configure per-hostname settings such as URL rewriting and custom headers.
88
---
99

10-
import { Render, APIRequest } from "~/components";
10+
import { Render, APIRequest, TypeScriptExample } from "~/components";
1111

1212
You may wish to configure per-hostname (customer) settings beyond the scale of Page Rules or Rate Limiting, which have a maximum of 125 rules each.
1313

@@ -59,7 +59,9 @@ The metadata object will be accessible on each request using the `request.cf.hos
5959

6060
In the example below we will use the user_id in the Worker that was submitted using the API call above `"custom_metadata":{"customer_id":"12345","redirect_to_https": true,"security_tag":"low"}`, and set a request header to send the `customer_id` to the origin:
6161

62-
```js
62+
<TypeScriptExample>
63+
64+
```ts
6365
export default {
6466
/**
6567
* Fetch and add a X-Customer-Id header to the origin based on hostname
@@ -79,6 +81,8 @@ export default {
7981
} satisfies ExportedHandler<Env>;
8082
```
8183

84+
</TypeScriptExample>
85+
8286
## Accessing custom metadata in a rule expression
8387

8488
Use the [`cf.hostname.metadata`](/ruleset-engine/rules-language/fields/reference/cf.hostname.metadata/) field to access the metadata object in rule expressions. To obtain the different values from the JSON object, use the [`lookup_json_string`](/ruleset-engine/rules-language/functions/#lookup_json_string) function.

src/content/docs/cloudflare-one/tutorials/access-workers.mdx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ languages:
1111
- JavaScript
1212
---
1313

14+
import { TypeScriptExample } from "~/components";
15+
1416
This tutorial covers how to use a [Cloudflare Worker](/workers/) to add custom HTTP headers to traffic, and how to send those custom headers to your origin services protected by [Cloudflare Access](/cloudflare-one/policies/access/).
1517

1618
Some applications and networking implementations require specific custom headers to be passed to the origin, which can be difficult to implement for traffic moving through a Zero Trust proxy. You can configure a Worker to send the [user authorization headers](/cloudflare-one/identity/authorization-cookie/) required by Access.
@@ -33,7 +35,9 @@ Some applications and networking implementations require specific custom headers
3335

3436
5. Input the following Worker:
3537

36-
```js title="Worker with custom HTTP headers"
38+
<TypeScriptExample>
39+
40+
```ts title="Worker with custom HTTP headers"
3741
export default {
3842
async fetch(request, env, ctx): Promise<Response> {
3943
const { headers } = request;
@@ -47,6 +51,8 @@ export default {
4751
} satisfies ExportedHandler<Env>;
4852
```
4953

54+
</TypeScriptExample>
55+
5056
6. Select **Save and deploy**.
5157

5258
Your Worker is now ready to send custom headers to your Access-protected origin services.

0 commit comments

Comments
 (0)