forked from conference-buddy/conference-buddy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: warn on missing environment variables
This adds a helper function which extracts values from environment variables, and warns if they are not set. See conference-buddy#25
- Loading branch information
1 parent
eae26c2
commit a0c1385
Showing
7 changed files
with
45 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Pure-JS version of fromEnv.ts for Gatsby | ||
/** @type {import("./fromEnv").fromEnv } */ | ||
exports.fromEnv = def => env => | ||
Object.entries(def).reduce((res, [defKey, envKey]) => { | ||
const v = env[envKey] | ||
if (v === undefined || v.length === 0) | ||
throw new Error(`${envKey} is not defined in environment!`) | ||
return { ...res, [defKey]: v } | ||
}, {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { fromEnv } from "./fromEnv" | ||
|
||
describe("fromEnv()", () => { | ||
it("should return environment variables if defined", () => { | ||
const env = { FOO: "bar" } | ||
expect(fromEnv({ foo: "FOO" })(env)).toEqual({ foo: "bar" }) | ||
}) | ||
it("should throw an error if the environment variable is not defined", () => { | ||
const env = {} | ||
expect(() => fromEnv({ foo: "FOO" })(env)).toThrow( | ||
"FOO is not defined in environment!" | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const fromEnv = | ||
<T extends Record<string, string>>(def: T) => | ||
(env: NodeJS.ProcessEnv): Record<keyof T, string> => | ||
Object.entries(def).reduce((res, [defKey, envKey]) => { | ||
const v = env[envKey] | ||
if (v === undefined || v.length === 0) | ||
throw new Error(`${envKey} is not defined in environment!`) | ||
return { ...res, [defKey]: v } | ||
}, {} as Record<keyof T, string>) |