Skip to content

Commit 5b85dc9

Browse files
authored
Add support for new bindings to wrangler types (#5089)
* Add support for new bindings to `wrangler types` * Actually Workers AI is also Fetcher * Add test * Add changeset * Format changelog * Use `unknown` for AI binding * Make bindings config type validation stricter * Fix typecheck failure as discussed
1 parent bb3db47 commit 5b85dc9

File tree

4 files changed

+112
-3
lines changed

4 files changed

+112
-3
lines changed

.changeset/curvy-ghosts-destroy.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: include all currently existing bindings in `wrangler types`
6+
7+
Add support for Email Send, Vectorize, Hyperdrive, mTLS, Browser Rendering and Workers AI bindings in `wrangler types`
8+
9+
For example, from the following `wrangler.toml` setup:
10+
11+
```toml
12+
[browser]
13+
binding = "BROWSER"
14+
15+
[ai]
16+
binding = "AI"
17+
18+
[[send_email]]
19+
name = "SEND_EMAIL"
20+
21+
[[vectorize]]
22+
binding = "VECTORIZE"
23+
index_name = "VECTORIZE_NAME"
24+
25+
[[hyperdrive]]
26+
binding = "HYPERDRIVE"
27+
id = "HYPERDRIVE_ID"
28+
29+
[[mtls_certificates]]
30+
binding = "MTLS"
31+
certificate_id = "MTLS_CERTIFICATE_ID"
32+
```
33+
34+
Previously, nothing would have been included in the generated Environment.
35+
Now, the following will be generated:
36+
37+
```ts
38+
interface Env {
39+
SEND_EMAIL: SendEmail;
40+
VECTORIZE: VectorizeIndex;
41+
HYPERDRIVE: Hyperdrive;
42+
MTLS: Fetcher;
43+
BROWSER: Fetcher;
44+
AI: Fetcher;
45+
}
46+
```

packages/wrangler/src/__tests__/type-generation.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import { dedent } from "../utils/dedent";
44
import { mockConsoleMethods } from "./helpers/mock-console";
55
import { runInTempDir } from "./helpers/run-in-tmp";
66
import { runWrangler } from "./helpers/run-wrangler";
7-
import type { Config } from "../config";
7+
import type { EnvironmentNonInheritable } from "../config/environment";
88

9-
const bindingsConfigMock: Partial<Config> = {
9+
const bindingsConfigMock: Omit<
10+
EnvironmentNonInheritable,
11+
"define" | "tail_consumers" | "constellation" | "cloudchamber"
12+
> &
13+
Record<string, unknown> = {
1014
kv_namespaces: [{ binding: "TEST_KV_NAMESPACE", id: "1234" }],
1115
vars: {
1216
SOMETHING: "asdasdfasdf",
@@ -62,6 +66,18 @@ const bindingsConfigMock: Partial<Config> = {
6266
dispatch_namespaces: [
6367
{ binding: "NAMESPACE_BINDING", namespace: "NAMESPACE_ID" },
6468
],
69+
send_email: [{ name: "SEND_EMAIL_BINDING" }],
70+
vectorize: [{ binding: "VECTORIZE_BINDING", index_name: "VECTORIZE_NAME" }],
71+
hyperdrive: [{ binding: "HYPERDRIVE_BINDING", id: "HYPERDRIVE_ID" }],
72+
mtls_certificates: [
73+
{ binding: "MTLS_BINDING", certificate_id: "MTLS_CERTIFICATE_ID" },
74+
],
75+
browser: {
76+
binding: "BROWSER_BINDING",
77+
},
78+
ai: {
79+
binding: "AI_BINDING",
80+
},
6581
logfwdr: {
6682
bindings: [{ name: "LOGFWDR_BINDING", destination: "LOGFWDR_DESTINATION" }],
6783
},
@@ -201,6 +217,12 @@ describe("generateTypes()", () => {
201217
SOME_TEXT_BLOB2: string;
202218
testing_unsafe: any;
203219
TEST_QUEUE_BINDING: Queue;
220+
SEND_EMAIL_BINDING: SendEmail;
221+
VECTORIZE_BINDING: VectorizeIndex;
222+
HYPERDRIVE_BINDING: Hyperdrive;
223+
MTLS_BINDING: Fetcher;
224+
BROWSER_BINDING: Fetcher;
225+
AI_BINDING: unknown;
204226
}
205227
declare module \\"*.txt\\" {
206228
const value: string;

packages/wrangler/src/config/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ export type DurableObjectBindings = {
328328
* If any of these fields are defined at the top-level then they should also be specifically defined
329329
* for each named environment.
330330
*/
331-
interface EnvironmentNonInheritable {
331+
export interface EnvironmentNonInheritable {
332332
/**
333333
* A map of values to substitute when deploying your worker.
334334
*

packages/wrangler/src/type-generation.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ export async function typesHandler(
9595
rules: config.rules,
9696
queues: config.queues,
9797
constellation: config.constellation,
98+
send_email: config.send_email,
99+
vectorize: config.vectorize,
100+
hyperdrive: config.hyperdrive,
101+
mtls_certificates: config.mtls_certificates,
102+
browser: config.browser,
103+
ai: config.ai,
98104
secrets,
99105
};
100106

@@ -240,6 +246,41 @@ async function generateTypes(
240246
}
241247
}
242248

249+
if (configToDTS.send_email) {
250+
for (const sendEmail of configToDTS.send_email) {
251+
envTypeStructure.push(`${sendEmail.name}: SendEmail;`);
252+
}
253+
}
254+
255+
if (configToDTS.vectorize) {
256+
for (const vectorize of configToDTS.vectorize) {
257+
envTypeStructure.push(`${vectorize.binding}: VectorizeIndex;`);
258+
}
259+
}
260+
261+
if (configToDTS.hyperdrive) {
262+
for (const hyperdrive of configToDTS.hyperdrive) {
263+
envTypeStructure.push(`${hyperdrive.binding}: Hyperdrive;`);
264+
}
265+
}
266+
267+
if (configToDTS.mtls_certificates) {
268+
for (const mtlsCertificate of configToDTS.mtls_certificates) {
269+
envTypeStructure.push(`${mtlsCertificate.binding}: Fetcher;`);
270+
}
271+
}
272+
273+
if (configToDTS.browser) {
274+
// The BrowserWorker type in @cloudflare/puppeteer is of type
275+
// { fetch: typeof fetch }, but workers-types doesn't include it
276+
// and Fetcher is valid for the purposes of handing it to puppeteer
277+
envTypeStructure.push(`${configToDTS.browser.binding}: Fetcher;`);
278+
}
279+
280+
if (configToDTS.ai) {
281+
envTypeStructure.push(`${configToDTS.ai.binding}: unknown;`);
282+
}
283+
243284
const modulesTypeStructure: string[] = [];
244285
if (configToDTS.rules) {
245286
const moduleTypeMap = {

0 commit comments

Comments
 (0)