Skip to content

Commit 518cdea

Browse files
committed
update references to @cloudflare/workers-types
1 parent 228d918 commit 518cdea

File tree

14 files changed

+29
-64
lines changed

14 files changed

+29
-64
lines changed

src/content/docs/agents/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ We built the `agents-sdk` with a few things in mind:
5656

5757
Agents built with `agents-sdk` can be deployed directly to Cloudflare and run on top of [Durable Objects](/durable-objects/) — which you can think of as stateful micro-servers that can scale to tens of millions — and are able to run wherever they need to. Run your Agents close to a user for low-latency interactivity, close to your data for throughput, and/or anywhere in between.
5858

59-
***
59+
---
6060

6161
#### Build on the Cloudflare Platform
6262

src/content/docs/ai-gateway/integrations/worker-binding-methods.mdx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import { Render, PackageManagers } from "~/components";
88

99
This guide provides an overview of how to use the latest Cloudflare Workers AI Gateway binding methods. You will learn how to set up an AI Gateway binding, access new methods, and integrate them into your Workers.
1010

11-
## Prerequisites
12-
13-
- Install and use the `@cloudflare/workers-types` library, version `4.20250124.3` or above.
14-
1511
## 1. Add an AI Binding to your Worker
1612

1713
To connect your Worker to Workers AI, add the following to your [Wrangler configuration file](/workers/wrangler/configuration/):
@@ -37,7 +33,7 @@ To perform an inference task using Workers AI and an AI Gateway, you can use the
3733
const resp = await env.AI.run("@cf/meta/llama-3.1-8b-instruct", {
3834
prompt: "tell me a joke"
3935
}, {
40-
gateway: {
36+
gateway: {
4137
id: "my-gateway"
4238
}
4339
});
@@ -65,9 +61,9 @@ The `patchLog` method allows you to send feedback, score, and metadata for a spe
6561

6662
```typescript
6763
gateway.patchLog('my-log-id', {
68-
feedback: 1,
69-
score: 100,
70-
metadata: {
64+
feedback: 1,
65+
score: 100,
66+
metadata: {
7167
user: "123"
7268
}
7369
});
@@ -78,7 +74,7 @@ gateway.patchLog('my-log-id', {
7874

7975
### 3.2. `getLog`: Read Log Details
8076

81-
The `getLog` method retrieves details of a specific log ID. It returns an object of type `Promise<AiGatewayLog>`. You can import the `AiGatewayLog` type from the `@cloudflare/workers-types` library.
77+
The `getLog` method retrieves details of a specific log ID. It returns an object of type `Promise<AiGatewayLog>`. If this type is missing, ensure you have run [`wrangler types`](/workers/languages/typescript/#generate-types)
8278

8379
```typescript
8480
const log = await gateway.getLog("my-log-id");
@@ -95,12 +91,12 @@ Refer to the [Universal endpoint documentation](/ai-gateway/providers/universal/
9591

9692
```typescript
9793
const resp = await gateway.run({
98-
provider: "workers-ai",
99-
endpoint: "@cf/meta/llama-3.1-8b-instruct",
100-
headers: {
94+
provider: "workers-ai",
95+
endpoint: "@cf/meta/llama-3.1-8b-instruct",
96+
headers: {
10197
authorization: "Bearer my-api-token"
102-
},
103-
query: {
98+
},
99+
query: {
104100
prompt: "tell me a joke"
105101
}
106102
});
@@ -118,4 +114,3 @@ With the new AI Gateway binding methods, you can now:
118114
- Execute universal requests to any AI Gateway provider with `run`.
119115

120116
These methods offer greater flexibility and control over your AI integrations, empowering you to build more sophisticated applications on the Cloudflare Workers platform.
121-

src/content/docs/d1/worker-api/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Refer to the relevant sections for the API documentation.
1818

1919
## TypeScript support
2020

21-
D1 Worker Bindings API is fully-typed via the [`@cloudflare/workers-types`](/workers/languages/typescript/#typescript) package, and also supports [generic types](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-types) as part of its TypeScript API. A generic type allows you to provide an optional `type parameter` so that a function understands the type of the data it is handling.
21+
D1 Worker Bindings API is fully-typed via the types generated by running [`wrangler types`](/workers/languages/typescript/#typescript) package, and also supports [generic types](https://www.typescriptlang.org/docs/handbook/2/generics.html#generic-types) as part of its TypeScript API. A generic type allows you to provide an optional `type parameter` so that a function understands the type of the data it is handling.
2222

2323
When using the query statement methods [`D1PreparedStatement::run`](/d1/worker-api/prepared-statements/#run), [`D1PreparedStatement::raw`](/d1/worker-api/prepared-statements/#raw) and [`D1PreparedStatement::first`](/d1/worker-api/prepared-statements/#first), you can provide a type representing each database row. D1's API will [return the result object](/d1/worker-api/return-object/#d1result) with the correct type.
2424

src/content/docs/pages/framework-guides/deploy-a-nuxt-site.mdx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,11 @@ In order to access bindings in a deployed application, you will need to [configu
121121

122122
### Add bindings to TypeScript projects
123123

124-
To get proper type support, you need to create a new `env.d.ts` file in the root of your project and declare a [binding](/pages/functions/bindings/).
124+
To get proper type support, you need to create a new `env.d.ts` file in the root of your project and declare a [binding](/pages/functions/bindings/). Make sure you have generated Cloudflare runtime types by running [`wrangler types`](/pages/functions/typescript/).
125125

126126
The following is an example of adding a `KVNamespace` binding:
127127

128128
```ts null {9}
129-
import {
130-
CfProperties,
131-
Request,
132-
ExecutionContext,
133-
KVNamespace,
134-
} from "@cloudflare/workers-types";
135129

136130
declare module "h3" {
137131
interface H3EventContext {

src/content/docs/pages/framework-guides/deploy-a-qwik-site.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The following code block shows an example of accessing a KV namespace in QwikCit
7070
// ...
7171

7272
export const useGetServerTime = routeLoader$(({ platform }) => {
73-
// the type `KVNamespace` comes from the @cloudflare/workers-types package
73+
// the type `KVNamespace` comes from types generated by running `wrangler types`
7474
const { MY_KV } = (platform.env as { MY_KV: KVNamespace }));
7575

7676
return {

src/content/docs/pages/framework-guides/deploy-an-astro-site.mdx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,11 @@ Use bindings in Astro components and API routes by using `context.locals` from [
109109

110110
Refer to the following example of how to access a KV namespace with TypeScript.
111111

112-
First, you need to define Cloudflare runtime and KV type by updating the `env.d.ts`:
112+
First, you need to define Cloudflare runtime and KV type by updating the `env.d.ts`. Make sure you have generated Cloudflare runtime types by running [`wrangler types`](/pages/functions/typescript/).
113113

114114
```typescript
115115
/// <reference types="astro/client" />
116116

117-
type KVNamespace = import("@cloudflare/workers-types").KVNamespace;
118117
type ENV = {
119118
// replace `MY_KV` with your KV namespace
120119
MY_KV: KVNamespace;
@@ -133,7 +132,6 @@ You can then access your KV from an API endpoint in the following way:
133132
import type { APIContext } from "astro";
134133

135134
export async function get({ locals }: APIContext) {
136-
// the type KVNamespace comes from the @cloudflare/workers-types package
137135
const { MY_KV } = locals.runtime.env;
138136

139137
return {

src/content/docs/pages/framework-guides/nextjs/ssr/bindings.mdx

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,7 @@ Add bindings to your Pages project by adding them to your [Wrangler configuratio
3030

3131
## TypeScript type declarations for bindings
3232

33-
To ensure that the `env` object from `getRequestContext().env` above has accurate TypeScript types, install [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) and create a [TypeScript declaration file](https://www.typescriptlang.org/docs/handbook/2/type-declarations.html).
34-
35-
Install Workers Types:
36-
37-
```sh
38-
npm install --save-dev @cloudflare/workers-types
39-
```
40-
41-
Add Workers Types to your `tsconfig.json` file, replacing the date below with your project's [compatibility date](/workers/configuration/compatibility-dates/):
42-
43-
```diff title="tsconfig.json"
44-
"types": [
45-
+ "@cloudflare/workers-types/2024-07-29"
46-
]
47-
```
48-
49-
Create an `env.d.ts` file in the root directory of your Next.js app, and explicitly declare the type of each binding:
50-
51-
```ts title="env.d.ts"
52-
interface CloudflareEnv {
53-
MY_KV_1: KVNamespace;
54-
MY_KV_2: KVNamespace;
55-
MY_R2: R2Bucket;
56-
MY_DO: DurableObjectNamespace;
57-
}
58-
```
33+
To ensure that the `env` object from `getRequestContext().env` above has accurate TypeScript types, make sure you have generated types by running [`wrangler types`](/workers/languages/typescript/#generate-types) and followed the setup steps.
5934

6035
## Other Cloudflare APIs (`cf`, `ctx`)
6136

src/content/docs/vectorize/reference/client-api.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,4 @@ Refer to the [bindings documentation](/workers/wrangler/configuration/#vectorize
220220

221221
New Workers projects created via `npm create cloudflare@latest` automatically include the relevant TypeScript types for Vectorize.
222222

223-
Older projects, or non-Workers projects looking to use Vectorize's [REST API](https://developers.cloudflare.com/api/resources/vectorize/subresources/indexes/methods/list/) in a TypeScript project, should ensure `@cloudflare/workers-types` version `4.20230922.0` or later is installed.
223+
If you have an older project, or a non-Workers projects looking to use Vectorize's [REST API](https://developers.cloudflare.com/api/resources/vectorize/subresources/indexes/methods/list/) in a TypeScript project, you should ensure you have a compatibility date later than `2023-09-22` and that you have generated types by running [`wrangler types`](/workers/languages/typescript/#generate-types).

src/content/docs/workers/runtime-apis/request.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ An object containing Cloudflare-specific properties that can be set on the `Requ
9999
fetch(event.request, { cf: { scrapeShield: false } })
100100
```
101101

102-
Invalid or incorrectly-named keys in the `cf` object will be silently ignored. Consider using TypeScript and [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) to ensure proper use of the `cf` object.
102+
Invalid or incorrectly-named keys in the `cf` object will be silently ignored. Consider using TypeScript and generating types by running [`wrangler types`](/workers/languages/typescript/#generate-types) to ensure proper use of the `cf` object.
103103

104104

105105

src/content/docs/workers/runtime-apis/rpc/typescript.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description: How TypeScript types for your Worker or Durable Object's RPC
1111

1212
---
1313

14-
The [`@cloudflare/workers-types`](https://www.npmjs.com/package/@cloudflare/workers-types) package provides the `Service` and `DurableObjectNamespace` types, each of which accepts a single type parameter for the server-side [`WorkerEntrypoint`](/workers/runtime-apis/bindings/service-bindings/rpc) or [`DurableObject`](/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#call-rpc-methods) types.
14+
The types generated by running [`wrangler types`](/workers/languages/typescript/#generate-types) provide the `Service` and `DurableObjectNamespace` types, each of which accepts a single type parameter for the server-side [`WorkerEntrypoint`](/workers/runtime-apis/bindings/service-bindings/rpc) or [`DurableObject`](/durable-objects/best-practices/create-durable-object-stubs-and-send-requests/#call-rpc-methods) types.
1515

1616
Using higher-order types, we automatically generate client-side stub types (e.g., forcing all methods to be async).
1717

0 commit comments

Comments
 (0)