From 787e9a81e729056eccbc977f5430c9134844cadc Mon Sep 17 00:00:00 2001 From: Emilio Assuncao Date: Mon, 11 Mar 2024 15:25:33 -0500 Subject: [PATCH] Address PR feedback and add another testcase --- .changeset/twenty-laws-mix.md | 2 +- .../miniflare/src/plugins/hyperdrive/index.ts | 2 +- .../wrangler/src/__tests__/hyperdrive.test.ts | 24 ++++++++++++ packages/wrangler/src/hyperdrive/create.ts | 4 +- packages/wrangler/src/hyperdrive/update.ts | 38 +++++++++---------- 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/.changeset/twenty-laws-mix.md b/.changeset/twenty-laws-mix.md index 5b6888d3b401..222cb9935553 100644 --- a/.changeset/twenty-laws-mix.md +++ b/.changeset/twenty-laws-mix.md @@ -3,4 +3,4 @@ "wrangler": minor --- -URL decode components of the Hyperdrive config connection string +feature: URL decode components of the Hyperdrive config connection string diff --git a/packages/miniflare/src/plugins/hyperdrive/index.ts b/packages/miniflare/src/plugins/hyperdrive/index.ts index 256c475b0f9a..c61f062d12f8 100644 --- a/packages/miniflare/src/plugins/hyperdrive/index.ts +++ b/packages/miniflare/src/plugins/hyperdrive/index.ts @@ -82,7 +82,7 @@ export const HYPERDRIVE_PLUGIN: Plugin = { name: `${HYPERDRIVE_PLUGIN_NAME}:${name}`, }, database: decodeURIComponent(database), - user: url.username, + user: decodeURIComponent(url.username), password: decodeURIComponent(url.password), scheme, }, diff --git a/packages/wrangler/src/__tests__/hyperdrive.test.ts b/packages/wrangler/src/__tests__/hyperdrive.test.ts index 825d33c9d67f..377a41a82f2d 100644 --- a/packages/wrangler/src/__tests__/hyperdrive.test.ts +++ b/packages/wrangler/src/__tests__/hyperdrive.test.ts @@ -338,6 +338,30 @@ describe("hyperdrive commands", () => { `); }); + it("should handle disabling caching for a hyperdrive config", async () => { + mockHyperdriveRequest(); + await runWrangler( + "hyperdrive update 7a040c1eee714e91a30ea6707a2d125c --caching-disabled=true" + ); + expect(std.out).toMatchInlineSnapshot(` + "🚧 Updating '7a040c1eee714e91a30ea6707a2d125c' + ✅ Updated 7a040c1eee714e91a30ea6707a2d125c Hyperdrive config + { + \\"id\\": \\"7a040c1eee714e91a30ea6707a2d125c\\", + \\"name\\": \\"test123\\", + \\"origin\\": { + \\"host\\": \\"foo.us-east-2.aws.neon.tech\\", + \\"port\\": 5432, + \\"database\\": \\"neondb\\", + \\"user\\": \\"test\\" + }, + \\"caching\\": { + \\"disabled\\": true + } + }" + `); + }); + it("should handle updating a hyperdrive config's name", async () => { mockHyperdriveRequest(); await runWrangler( diff --git a/packages/wrangler/src/hyperdrive/create.ts b/packages/wrangler/src/hyperdrive/create.ts index 75d2d8a7195f..6815ff927f10 100644 --- a/packages/wrangler/src/hyperdrive/create.ts +++ b/packages/wrangler/src/hyperdrive/create.ts @@ -89,7 +89,7 @@ export async function handler( port: parseInt(url.port), scheme: url.protocol.replace(":", ""), database: decodeURIComponent(url.pathname.replace("/", "")), - user: url.username, + user: decodeURIComponent(url.username), password: decodeURIComponent(url.password), }, caching: { @@ -103,4 +103,4 @@ export async function handler( JSON.stringify(database, null, 2) ); } -} \ No newline at end of file +} diff --git a/packages/wrangler/src/hyperdrive/update.ts b/packages/wrangler/src/hyperdrive/update.ts index e2798e08fed0..95581c442757 100644 --- a/packages/wrangler/src/hyperdrive/update.ts +++ b/packages/wrangler/src/hyperdrive/update.ts @@ -62,12 +62,16 @@ export function options(yargs: CommonYargsArgv) { } const requiredOriginOptions = [ - "origin-host", - "origin-port", + "originHost", + "originPort", "database", - "origin-user", - "origin-password", -]; + "originUser", + "originPassword", +] as const; + +function camelToKebab(str: string): string { + return str.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase(); +} function isOptionSet(args: T, key: keyof T): boolean { return key in args && args[key] !== undefined; @@ -78,15 +82,15 @@ export async function handler( ) { // check if all or none of the required origin fields are set, since we don't allow partial updates of the origin const allOriginFieldsSet = requiredOriginOptions.every((field) => - isOptionSet(args, field as keyof typeof options) + isOptionSet(args, field) ); const noOriginFieldSet = requiredOriginOptions.every( - (field) => !isOptionSet(args, field as keyof typeof options) + (field) => !isOptionSet(args, field) ); if (!allOriginFieldsSet && !noOriginFieldSet) { throw new Error( - `When updating the origin, all of the following must be set: ${requiredOriginOptions.join( + `When updating the origin, all of the following must be set: ${requiredOriginOptions.map(option => camelToKebab(option)).join( ", " )}` ); @@ -104,26 +108,20 @@ export async function handler( if (allOriginFieldsSet) { database.origin = { + scheme: args.originScheme ?? "postgresql", host: args.originHost, port: args.originPort, database: args.database, user: args.originUser, password: args.originPassword, }; - if (args.originScheme !== undefined) { - database.origin.scheme = args.originScheme; - } else { - database.origin.scheme = "postgresql"; // setting default if not passed - } } - if (args.cachingDisabled || args.maxAge || args.swr) { - database.caching = { - disabled: args.cachingDisabled, - maxAge: args.maxAge, - staleWhileRevalidate: args.swr, - }; - } + database.caching = { + disabled: args.cachingDisabled, + maxAge: args.maxAge, + staleWhileRevalidate: args.swr, + }; const updated = await patchConfig(config, args.id, database); logger.log(