Skip to content

Commit

Permalink
feat: warn on missing environment variables
Browse files Browse the repository at this point in the history
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
coderbyheart committed Dec 30, 2021
1 parent eae26c2 commit a0c1385
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
7 changes: 5 additions & 2 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const { createClient } = require("@supabase/supabase-js")
const { fromEnv } = require("./utils/fromEnv.js")

const supabaseUrl = process.env.GATSBY_APP_SUPABASE_URL || "empty"
const supabaseAnonKey = process.env.GATSBY_APP_SUPABASE_ANON_KEY || "empty"
const { supabaseUrl, supabaseAnonKey } = fromEnv({
supabaseUrl: "GATSBY_APP_SUPABASE_URL",
supabaseAnonKey: "GATSBY_APP_SUPABASE_ANON_KEY",
})(process.env)

const supabase = createClient(supabaseUrl, supabaseAnonKey)

Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
"jest-transform-stub",
},
moduleNameMapper: {
"@/(.*)": "<rootDir>/src/$1",
"@/(.*)": "<rootDir>/$1",
},
preset: "ts-jest",
testEnvironment: "jsdom",
Expand All @@ -26,7 +26,7 @@ module.exports = {
"<rootDir>/tests/setup/setup-files.ts",
"<rootDir>/tests/setup/setup-after-env.ts",
],
testMatch: ["<rootDir>/src/**/*.test.ts(x)"],
testMatch: ["<rootDir>/**/*.test.ts?(x)"],
moduleDirectories: ["node_modules", "src"],
testURL: `http://localhost`,
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
Expand Down
8 changes: 5 additions & 3 deletions supabaseClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { createClient } from "@supabase/supabase-js"
import { fromEnv } from "./utils/fromEnv"

const supabaseUrl: string = process.env.GATSBY_APP_SUPABASE_URL || "empty"
const supabaseAnonKey: string =
process.env.GATSBY_APP_SUPABASE_ANON_KEY || "empty"
const { supabaseUrl, supabaseAnonKey } = fromEnv({
supabaseUrl: "GATSBY_APP_SUPABASE_URL",
supabaseAnonKey: "GATSBY_APP_SUPABASE_ANON_KEY",
})(process.env)

const supabase = createClient(supabaseUrl, supabaseAnonKey)

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
"noUnusedParameters": true,
"removeComments": false
},
"include": ["./src/**/*.ts", "./src/**/*.tsx"],
"include": ["./src/**/*.ts", "./src/**/*.tsx", "./utils/**/*.ts"],
"exclude": ["node_modules", "setup-test-env.js"]
}
9 changes: 9 additions & 0 deletions utils/fromEnv.js
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 }
}, {})
14 changes: 14 additions & 0 deletions utils/fromEnv.test.ts
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!"
)
})
})
9 changes: 9 additions & 0 deletions utils/fromEnv.ts
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>)

0 comments on commit a0c1385

Please sign in to comment.