Skip to content

Commit

Permalink
Merge pull request #15 from whats-good/kerem/config-4
Browse files Browse the repository at this point in the history
Kerem/config 4
  • Loading branch information
mechanical-turk authored Dec 1, 2023
2 parents 5fe39e3 + 9def7a8 commit ef49e3e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .changeset/large-spies-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@whatsgood/example-cloudflare-worker": minor
"@whatsgood/nexus": minor
---

Implicit config derivations from env vars removed. All configs should be explicit moving forward.
11 changes: 9 additions & 2 deletions examples/cloudflare-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import { NexusServer } from "@whatsgood/nexus";
type Env = Record<string, string>;

const server = NexusServer.create<Env>({
env: (ctx) => ctx,
providers: ["alchemy"],
// TODO: add a `createUserContext` function that generates the ctx object from the server context
providers: (ctx) => [
"base",
{
name: "alchemy",
key: ctx.NEXUS_PROVIDER_ALCHEMY_KEY,
},
],
globalAccessKey: (ctx) => ctx.NEXUS_GLOBAL_ACCESS_KEY,
chains: [84531],
});

Expand Down
24 changes: 2 additions & 22 deletions packages/nexus/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { z } from "zod";
import { globalSingletonRegistry } from "./registry/global-singleton-registry";
import type { Registry } from "./registry";
import { toUpperSnakeCase } from "./utils";

const RpxRelayRecoveryModeSchema = z.enum(["none", "cycle"]);
// none -> don't try to recover and fail immediately
Expand Down Expand Up @@ -35,8 +34,6 @@ type ChainConfigParam =
enabled?: boolean;
};

type Env = Partial<Record<string, string>>;

export type ConfigConstructorParams = ConstructorParameters<typeof Config>[0];

export class Config {
Expand All @@ -58,32 +55,21 @@ export class Config {
public readonly registry: Registry;

constructor(params: {
env?: Env;
providers: [ProviderConfigParam, ...ProviderConfigParam[]];
chains: [ChainConfigParam, ...ChainConfigParam[]];
globalAccessKey?: string;
recoveryMode?: RpcRelayRecoveryMode;
registry?: Registry;
}) {
const envRaw: Env = params.env || process.env;

// only allow keys that start with NEXUS_ to be used
// TODO: is this necessary now that we're not exporting the env object?
const env = Object.fromEntries(
Object.entries(envRaw).filter(([key]) => key.startsWith("NEXUS_"))
);

params.providers.forEach((provider) => {
if (typeof provider === "string") {
this.providers[provider] = {
enabled: true,
};
} else {
// TODO: scan all NEXUS_PROVIDER_*_KEY env vars and warn if there are any unused ones
// or alternatively automatically enable the provider if there is a key for it
this.providers[provider.name] = {
enabled: provider.enabled ?? true,
key: provider.key || env[this.getEnvSecretKeyName(provider.name)],
key: provider.key,
};
}
});
Expand All @@ -100,15 +86,9 @@ export class Config {
}
});

this.globalAccessKey =
params.globalAccessKey || env.NEXUS_GLOBAL_ACCESS_KEY;
this.globalAccessKey = params.globalAccessKey;
this.recoveryMode = params.recoveryMode ?? "cycle";

this.registry = params.registry || globalSingletonRegistry;
}

private getEnvSecretKeyName(name: string): string {
// TODO: update env vars and documentation to reflect this change
return `NEXUS_PROVIDER_${toUpperSnakeCase(name)}_KEY`;
}
}

0 comments on commit ef49e3e

Please sign in to comment.