Skip to content

Instantly share code, notes, and snippets.

@maxsei
Created June 6, 2026 17:03
Show Gist options
  • Select an option

  • Save maxsei/51a9a4c63efb9a946a8e09e4586bdfb3 to your computer and use it in GitHub Desktop.

Select an option

Save maxsei/51a9a4c63efb9a946a8e09e4586bdfb3 to your computer and use it in GitHub Desktop.
typed wrangler injection
import {
unstable_readConfig,
unstable_convertConfigBindingsToStartWorkerBindings,
} from "wrangler";
import * as fs from "node:fs";
import * as z from "zod";
import tmp from 'tmp';
const WranglerOverrides = z.object({
aiGatewayId: z.string(),
hyperdrive: z.discriminatedUnion("type", [
z.object({
type: z.literal("local"),
local: z.object({
localConnectionString: z.string(),
}),
}),
z.object({
type: z.literal("hyperdrive"),
hyperdrive: z.object({
id: z.string(),
}),
}),
]),
});
const { input } = JSON.parse(fs.readFileSync(0, "utf-8"));
const wranglerOverrides = WranglerOverrides.parse(input);
const wranglerConfigRaw = {
$schema: "node_modules/wrangler/config-schema.json",
name: "fllwp",
main: "src/index.tsx",
compatibility_date: "2025-05-31",
compatibility_flags: ["nodejs_compat"],
ai: {
binding: "AI",
},
hyperdrive: [
{
binding: "HYPERDRIVE",
...wranglerOverrides.hyperdrive[wranglerOverrides.hyperdrive.type]
},
],
vars: {
AI_GATEWAY_ID: wranglerOverrides.aiGatewayId,
},
assets: {
binding: "ASSETS",
},
};
tmp.file(null, (err, path, fd, cleanupCallback) => {
if (err) throw err;
const config = unstable_readConfig({config: path});
fs.writeFileSync(fd, JSON.stringify(wranglerConfigRaw))
fs.close(fd)
console.log(
JSON.stringify({
config: JSON.stringify(config),
}),
);
cleanupCallback();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment