Skip to content

Commit

Permalink
endpoint falls back to FAUNA_ENDPOINT env variable (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptpaterson authored Aug 31, 2023
1 parent c5dacdd commit 9d87555
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
13 changes: 13 additions & 0 deletions __tests__/functional/client-configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ beforeEach(() => {
process.env = { ...PROCESS_ENV };
});

afterEach(() => {
process.env = PROCESS_ENV;
});

describe("ClientConfiguration", () => {
it("Client exposes a default client configuration", () => {
process.env["FAUNA_SECRET"] = "foo";
Expand All @@ -24,6 +28,15 @@ describe("ClientConfiguration", () => {
);
});

it("can be initialized with an endpoint from the environment", async () => {
process.env["FAUNA_ENDPOINT"] = "https://localhost:9999/";

const client = new Client({ secret: "secret" });
expect(client.clientConfiguration.endpoint?.toString()).toEqual(
"https://localhost:9999/"
);
});

it("Client respectes passed in client configuration over defaults", () => {
// TODO: when the Client accepts an http client add a mock that validates
// the configuration changes were applied.
Expand Down
41 changes: 35 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ type RequiredClientConfig = ClientConfiguration &

const DEFAULT_CLIENT_CONFIG: Omit<
ClientConfiguration & RequiredClientConfig,
"secret"
"secret" | "endpoint"
> = {
client_timeout_buffer_ms: 5000,
endpoint: endpoints.default,
format: "tagged",
http2_session_idle_ms: 5000,
http2_max_streams: 100,
Expand Down Expand Up @@ -106,6 +105,7 @@ export class Client {
...DEFAULT_CLIENT_CONFIG,
...clientConfiguration,
secret: this.#getSecret(clientConfiguration),
endpoint: this.#getEndpoint(clientConfiguration),
};

this.#validateConfiguration();
Expand Down Expand Up @@ -295,20 +295,20 @@ export class Client {
}

#getSecret(partialClientConfig?: ClientConfiguration): string {
let fallback = undefined;
let env_secret = undefined;
if (
typeof process !== "undefined" &&
process &&
typeof process === "object" &&
process.env &&
typeof process.env === "object"
) {
fallback = process.env["FAUNA_SECRET"];
env_secret = process.env["FAUNA_SECRET"];
}

const maybeSecret = partialClientConfig?.secret || fallback;
const maybeSecret = partialClientConfig?.secret ?? env_secret;
if (maybeSecret === undefined) {
throw new Error(
throw new TypeError(
"You must provide a secret to the driver. Set it \
in an environmental variable named FAUNA_SECRET or pass it to the Client\
constructor."
Expand All @@ -317,6 +317,35 @@ in an environmental variable named FAUNA_SECRET or pass it to the Client\
return maybeSecret;
}

#getEndpoint(partialClientConfig?: ClientConfiguration): URL {
// If the user explicitly sets the endpoint to undefined, we should throw a
// TypeError, rather than override with the default endpoint.
if (
partialClientConfig &&
"endpoint" in partialClientConfig &&
partialClientConfig.endpoint === undefined
) {
throw new TypeError(
`ClientConfiguration option endpoint must be defined.`
);
}

let env_endpoint: URL | undefined = undefined;
if (
typeof process !== "undefined" &&
process &&
typeof process === "object" &&
process.env &&
typeof process.env === "object"
) {
env_endpoint = process.env["FAUNA_ENDPOINT"]
? new URL(process.env["FAUNA_ENDPOINT"])
: undefined;
}

return partialClientConfig?.endpoint ?? env_endpoint ?? endpoints.default;
}

#getServiceError(failure: QueryFailure, httpStatus: number): ServiceError {
switch (httpStatus) {
case 400:
Expand Down

0 comments on commit 9d87555

Please sign in to comment.