From 74d719fb8d2ce1e877b3c70da2a495386084d892 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Somhairle=20MacLe=C3=B2id?= Date: Mon, 23 Sep 2024 16:19:53 +0100 Subject: [PATCH] Add missing bindings to `init --from-dash` (#6791) --- .changeset/pink-months-promise.md | 5 + packages/wrangler/src/__tests__/init.test.ts | 120 +++++++++++++++++++ packages/wrangler/src/init.ts | 89 +++++++++++++- 3 files changed, 208 insertions(+), 6 deletions(-) create mode 100644 .changeset/pink-months-promise.md diff --git a/.changeset/pink-months-promise.md b/.changeset/pink-months-promise.md new file mode 100644 index 000000000000..bdd1b696ecda --- /dev/null +++ b/.changeset/pink-months-promise.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +fix: Add missing binding to `init --from-dash` diff --git a/packages/wrangler/src/__tests__/init.test.ts b/packages/wrangler/src/__tests__/init.test.ts index dc854d8a752d..a71258700070 100644 --- a/packages/wrangler/src/__tests__/init.test.ts +++ b/packages/wrangler/src/__tests__/init.test.ts @@ -2566,6 +2566,46 @@ describe("init", () => { name: "UNSAFE_BINDING_TWO", data: 1337, }, + { + type: "inherit", + name: "INHERIT_BINDING", + }, + { + type: "pipelines", + name: "PIPELINE_BINDING", + id: "some-id", + }, + { + type: "mtls_certificate", + name: "MTLS_BINDING", + certificate_id: "some-id", + }, + { + type: "hyperdrive", + name: "HYPER_BINDING", + id: "some-id", + }, + { + type: "vectorize", + name: "VECTOR_BINDING", + index_name: "some-name", + }, + { + type: "queue", + name: "queue_BINDING", + queue_name: "some-name", + delivery_delay: 1, + }, + { + type: "send_email", + name: "EMAIL_BINDING", + destination_address: "some@address.com", + allowed_destination_addresses: ["some2@address.com"], + }, + { + type: "version_metadata", + name: "Version_BINDING", + }, ], routes = [ { @@ -2721,6 +2761,53 @@ describe("init", () => { type: "another unsafe thing", data: 1337, }, + { + name: "INHERIT_BINDING", + type: "inherit", + }, + ], + }, + vectorize: [ + { + binding: "VECTOR_BINDING", + index_name: "some-name", + }, + ], + send_email: [ + { + allowed_destination_addresses: ["some2@address.com"], + destination_address: "some@address.com", + name: "EMAIL_BINDING", + }, + ], + version_metadata: { + binding: "Version_BINDING", + }, + hyperdrive: [ + { + binding: "HYPER_BINDING", + id: "some-id", + }, + ], + mtls_certificates: [ + { + binding: "MTLS_BINDING", + certificate_id: "some-id", + }, + ], + pipelines: [ + { + binding: "PIPELINE_BINDING", + pipeline: "some-id", + }, + ], + queues: { + producers: [ + { + binding: "queue_BINDING", + delivery_delay: 1, + queue: "some-name", + }, ], }, wasm_modules: { @@ -3250,6 +3337,39 @@ describe("init", () => { type = \\"another unsafe thing\\" name = \\"UNSAFE_BINDING_TWO\\" data = 1_337 + + [[unsafe.bindings]] + type = \\"inherit\\" + name = \\"INHERIT_BINDING\\" + + [[pipelines]] + binding = \\"PIPELINE_BINDING\\" + pipeline = \\"some-id\\" + + [[mtls_certificates]] + binding = \\"MTLS_BINDING\\" + certificate_id = \\"some-id\\" + + [[hyperdrive]] + binding = \\"HYPER_BINDING\\" + id = \\"some-id\\" + + [[vectorize]] + binding = \\"VECTOR_BINDING\\" + index_name = \\"some-name\\" + + [[queues.producers]] + binding = \\"queue_BINDING\\" + queue = \\"some-name\\" + delivery_delay = 1 + + [[send_email]] + name = \\"EMAIL_BINDING\\" + destination_address = \\"some@address.com\\" + allowed_destination_addresses = [ \\"some2@address.com\\" ] + + [version_metadata] + binding = \\"Version_BINDING\\" " `); expect(std.out).toContain("cd isolinear-optical-chip"); diff --git a/packages/wrangler/src/init.ts b/packages/wrangler/src/init.ts index d7c5cd06d819..4d3728cd8cd8 100644 --- a/packages/wrangler/src/init.ts +++ b/packages/wrangler/src/init.ts @@ -5,13 +5,14 @@ import TOML from "@iarna/toml"; import { execa } from "execa"; import { findUp } from "find-up"; import { version as wranglerVersion } from "../package.json"; +import { assertNever } from "./api/startDevWorker/utils"; import { fetchResult } from "./cfetch"; import { fetchWorker } from "./cfetch/internal"; import { readConfig } from "./config"; import { getDatabaseInfoFromId } from "./d1/utils"; import { confirm, select } from "./dialogs"; import { getC3CommandFromEnv } from "./environment-variables/misc-variables"; -import { UserError } from "./errors"; +import { FatalError, UserError } from "./errors"; import { getGitVersioon, initializeGit, isInsideGitRepo } from "./git-client"; import { logger } from "./logger"; import { getPackageManager } from "./package-manager"; @@ -1167,16 +1168,92 @@ export async function mapBindings( }; } break; - default: { - // If we don't know what the type is, its an unsafe binding - // eslint-disable-next-line @typescript-eslint/no-explicit-any - if (!(binding as any)?.type) { - break; + case "secret_text": + // Ignore secrets + break; + case "version_metadata": { + { + configObj.version_metadata = { + binding: binding.name, + }; } + break; + } + case "send_email": { + configObj.send_email = [ + ...(configObj.send_email ?? []), + { + name: binding.name, + destination_address: binding.destination_address, + allowed_destination_addresses: + binding.allowed_destination_addresses, + }, + ]; + break; + } + case "queue": + configObj.queues ??= { producers: [] }; + configObj.queues.producers = [ + ...(configObj.queues.producers ?? []), + { + binding: binding.name, + queue: binding.queue_name, + delivery_delay: binding.delivery_delay, + }, + ]; + break; + case "vectorize": + configObj.vectorize = [ + ...(configObj.vectorize ?? []), + { + binding: binding.name, + index_name: binding.index_name, + }, + ]; + break; + case "hyperdrive": + configObj.hyperdrive = [ + ...(configObj.hyperdrive ?? []), + { + binding: binding.name, + id: binding.id, + }, + ]; + break; + case "mtls_certificate": + configObj.mtls_certificates = [ + ...(configObj.mtls_certificates ?? []), + { + binding: binding.name, + certificate_id: binding.certificate_id, + }, + ]; + break; + case "pipelines": + configObj.pipelines = [ + ...(configObj.pipelines ?? []), + { + binding: binding.name, + pipeline: binding.id, + }, + ]; + break; + case "assets": + throw new FatalError( + "`wrangler init --from-dash` is not yet supported for Workers with Assets" + ); + case "inherit": + configObj.unsafe = { + bindings: [...(configObj.unsafe?.bindings ?? []), binding], + metadata: configObj.unsafe?.metadata ?? undefined, + }; + break; + default: { configObj.unsafe = { bindings: [...(configObj.unsafe?.bindings ?? []), binding], metadata: configObj.unsafe?.metadata ?? undefined, }; + assertNever(binding); } }