Skip to content

Commit

Permalink
[wrangler] feat: add support for environments in getPlatformProxy (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-piotrowicz committed Apr 26, 2024
1 parent 086e10d commit 327a456
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 10 deletions.
15 changes: 15 additions & 0 deletions .changeset/wicked-donkeys-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"wrangler": minor
---

feat: add support for environments in `getPlatformProxy`

allow `getPlatformProxy` to target environments by allowing users to specify an `environment` option

Example usage:

```js
const { env } = await getPlatformProxy({
environment: "production",
});
```
3 changes: 3 additions & 0 deletions fixtures/get-platform-proxy/.dev.vars.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
MY_DEV_VAR = "my-PRODUCTION-dev-var-value"
MY_VAR_A = "my-PRODUCTION-dev-var-a"
MY_KV = "my-dev-kv-PRODUCTION"
63 changes: 62 additions & 1 deletion fixtures/get-platform-proxy/tests/get-platform-proxy.env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Env = {
MY_SERVICE_B: Fetcher;
MY_RPC: Service;
MY_KV: KVNamespace;
MY_KV_PROD: KVNamespace;
MY_DO_A: DurableObjectNamespace;
MY_DO_B: DurableObjectNamespace;
MY_BUCKET: R2Bucket;
Expand All @@ -38,7 +39,7 @@ type Env = {

const wranglerTomlFilePath = path.join(__dirname, "..", "wrangler.toml");

describe("getPlatformProxy - bindings", () => {
describe("getPlatformProxy - env", () => {
let devWorkers: UnstableDevWorker[];

beforeEach(() => {
Expand Down Expand Up @@ -265,6 +266,66 @@ describe("getPlatformProxy - bindings", () => {
await dispose();
}
});

describe("with a target environment", () => {
it("should provide bindings targeting a specified environment and also inherit top-level ones", async () => {
const { env, dispose } = await getPlatformProxy<Env>({
configPath: wranglerTomlFilePath,
environment: "production",
});
try {
expect(env.MY_VAR).not.toBe("my-var-value");
expect(env.MY_VAR).toBe("my-PRODUCTION-var-value");
expect(env.MY_JSON_VAR).toEqual({ test: true, production: true });

expect(env.MY_KV).toBeTruthy();
expect(env.MY_KV_PROD).toBeTruthy();
} finally {
await dispose();
}
});

it("should not provide bindings targeting an environment when none was specified", async () => {
const { env, dispose } = await getPlatformProxy<Env>({
configPath: wranglerTomlFilePath,
});
try {
expect(env.MY_VAR).not.toBe("my-PRODUCTION-var-value");
expect(env.MY_VAR).toBe("my-var-value");
expect(env.MY_JSON_VAR).toEqual({ test: true });

expect(env.MY_KV).toBeTruthy();
expect(env.MY_KV_PROD).toBeFalsy();
} finally {
await dispose();
}
});

it("should provide secrets targeting a specified environment", async () => {
const { env, dispose } = await getPlatformProxy<Env>({
configPath: wranglerTomlFilePath,
environment: "production",
});
try {
const { MY_DEV_VAR } = env;
expect(MY_DEV_VAR).not.toEqual("my-dev-var-value");
expect(MY_DEV_VAR).toEqual("my-PRODUCTION-dev-var-value");
} finally {
await dispose();
}
});

it("should error if a non-existent environment is provided", async () => {
await expect(
getPlatformProxy({
configPath: wranglerTomlFilePath,
environment: "non-existent-environment",
})
).rejects.toThrow(
/No environment found in configuration with name "non-existent-environment"/
);
});
});
});

/**
Expand Down
6 changes: 6 additions & 0 deletions fixtures/get-platform-proxy/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ bindings = [
binding = "MY_D1"
database_name = "test-db"
database_id = "000000000-0000-0000-0000-000000000000"

[env.production]

vars = { MY_VAR = "my-PRODUCTION-var-value", MY_JSON_VAR = { test = true, production = true } }

kv_namespaces = [{ binding = "MY_KV_PROD", id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}]
10 changes: 7 additions & 3 deletions packages/wrangler/src/api/integrations/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import type { MiniflareOptions, ModuleRule, WorkerOptions } from "miniflare";
* Options for the `getPlatformProxy` utility
*/
export type GetPlatformProxyOptions = {
/**
* The name of the environment to use
*/
environment?: string;
/**
* The path to the config object to use (default `wrangler.toml`)
*/
Expand Down Expand Up @@ -82,13 +86,13 @@ export async function getPlatformProxy<
>(
options: GetPlatformProxyOptions = {}
): Promise<PlatformProxy<Env, CfProperties>> {
const env = options.environment;

const rawConfig = readConfig(options.configPath, {
experimentalJsonConfig: options.experimentalJsonConfig,
env,
});

// getBindingsProxy doesn't currently support selecting an environment
const env = undefined;

const miniflareOptions = await getMiniflareOptionsFromConfig(
rawConfig,
env,
Expand Down
13 changes: 7 additions & 6 deletions packages/wrangler/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,29 @@ export type {
ConfigModuleRuleType,
} from "./environment";

type ReadConfigCommandArgs<CommandArgs> = CommandArgs &
Pick<OnlyCamelCase<CommonYargsOptions>, "experimentalJsonConfig"> &
Partial<Pick<OnlyCamelCase<CommonYargsOptions>, "env">>;

/**
* Get the Wrangler configuration; read it from the give `configPath` if available.
*/
export function readConfig<CommandArgs>(
configPath: string | undefined,
// Include command specific args as well as the wrangler global flags
args: CommandArgs &
Pick<OnlyCamelCase<CommonYargsOptions>, "experimentalJsonConfig">,
args: ReadConfigCommandArgs<CommandArgs>,
requirePagesConfig: true
): Omit<Config, "pages_build_output_dir"> & { pages_build_output_dir: string };
export function readConfig<CommandArgs>(
configPath: string | undefined,
// Include command specific args as well as the wrangler global flags
args: CommandArgs &
Pick<OnlyCamelCase<CommonYargsOptions>, "experimentalJsonConfig">,
args: ReadConfigCommandArgs<CommandArgs>,
requirePagesConfig?: boolean
): Config;
export function readConfig<CommandArgs>(
configPath: string | undefined,
// Include command specific args as well as the wrangler global flags
args: CommandArgs &
Pick<OnlyCamelCase<CommonYargsOptions>, "experimentalJsonConfig">,
args: ReadConfigCommandArgs<CommandArgs>,
requirePagesConfig?: boolean
): Config {
let rawConfig: RawConfig = {};
Expand Down

0 comments on commit 327a456

Please sign in to comment.