Skip to content

Commit 7c0188a

Browse files
authored
feat: add fromEnv() helper for consistent env ops across Node/Bun/Deno (#50)
1 parent bedb42a commit 7c0188a

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
runs-on: ubuntu-latest
2727
strategy:
2828
matrix:
29-
node-version: [18, latest]
29+
node-version: [18, 20, latest]
3030
steps:
3131
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
3232
- name: Use Node.js ${{ matrix.node-version }}

src/helpers.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2023-Present The Kubernetes Fluent Client Authors
3+
4+
import { describe, expect, test } from "@jest/globals";
5+
6+
import { fromEnv } from "./helpers";
7+
8+
describe("helpers", () => {
9+
test("fromEnv for NodeJS", () => {
10+
expect(() => {
11+
fromEnv("MY_MISSING_ENV_VAR");
12+
}).toThrowError("Environment variable MY_MISSING_ENV_VAR is not set");
13+
14+
process.env.MY_ENV_VAR = "my-value";
15+
expect(fromEnv("MY_ENV_VAR")).toEqual("my-value");
16+
delete process.env.MY_ENV_VAR;
17+
});
18+
});

src/helpers.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2023-Present The Kubernetes Fluent Client Authors
3+
4+
declare const Deno: {
5+
env: {
6+
get(name: string): string | undefined;
7+
};
8+
};
9+
10+
/**
11+
* Get an environment variable (Node, Deno or Bun), or throw an error if it's not set.
12+
*
13+
* @example
14+
* const value = fromEnv("MY_ENV_VAR");
15+
* console.log(value);
16+
* // => "my-value"
17+
*
18+
* @example
19+
* const value = fromEnv("MY_MISSING_ENV_VAR");
20+
* // => Error: Environment variable MY_MISSING_ENV_VAR is not set
21+
*
22+
* @param name The name of the environment variable to get.
23+
* @returns The value of the environment variable.
24+
* @throws An error if the environment variable is not set.
25+
*/
26+
export function fromEnv(name: string): string {
27+
let envValue: string | undefined;
28+
29+
// Check for Node.js or Bun
30+
if (typeof process !== "undefined" && typeof process.env !== "undefined") {
31+
envValue = process.env[name];
32+
}
33+
// Check for Deno
34+
else if (typeof Deno !== "undefined") {
35+
envValue = Deno.env.get(name);
36+
}
37+
// Otherwise, throw an error
38+
else {
39+
throw new Error("Unknown runtime environment");
40+
}
41+
42+
if (typeof envValue === "undefined") {
43+
throw new Error(`Environment variable ${name} is not set`);
44+
}
45+
46+
return envValue;
47+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ export { GenericKind } from "./types";
2525
export * from "./types";
2626

2727
export * as K8sClientNode from "@kubernetes/client-node";
28+
29+
export { fromEnv } from "./helpers";

0 commit comments

Comments
 (0)