Skip to content

Commit

Permalink
feat: resolve nested env values in feature function
Browse files Browse the repository at this point in the history
  • Loading branch information
hmerritt committed Feb 9, 2024
1 parent f50ac20 commit 418a554
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@tanstack/router-devtools": "^1.15.23",
"@tanstack/router-vite-plugin": "^1.15.22",
"dayjs": "^1.11.10",
"lodash-es": "^4.17.21",
"plausible-tracker": "^0.3.8",
"react": "^18.2.0",
"react-device-detect": "^2.2.3",
Expand All @@ -49,6 +50,7 @@
"@testing-library/user-event": "^14.5.2",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/css": "^0.0.37",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.11.7",
"@types/react": "^18.0.21",
"@types/react-dom": "^18.2.18",
Expand Down
18 changes: 17 additions & 1 deletion src/lib/global/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { get } from "lodash-es";

import { DeepKeyofPaths } from "lib/type-assertions";

import { parseEnv, setGlobalValue } from "./utils";

/**
Expand Down Expand Up @@ -27,9 +31,21 @@ export const env = Object.freeze({
someOtherFeature: false
});

/**
* Resolve value from env object.
*
* Supports resolving values nested in objects.
*
* @example envGet("plausible.enable") -> true
*/
export const envGet = (key: EnvKeys) => {
return get(env, key);
};

export type EnvObj = typeof env;
export type EnvKeys = keyof EnvObj;
export type EnvKeys = DeepKeyofPaths<EnvObj>;

export const injectEnv = () => {
setGlobalValue("env", env);
setGlobalValue("envGet", envGet);
};
4 changes: 2 additions & 2 deletions src/lib/global/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type EnvKeys, env } from "./env";
import { type EnvKeys, env, envGet } from "./env";
import { setGlobalValue } from "./utils";

/**
Expand All @@ -18,7 +18,7 @@ export const feature = (mode: FeatureFlags, options: FeatureOptions = {}): boole
}

// Feature is truthy in featureFlags{}
if (env[mode] && !isFalse(env[mode])) {
if (envGet(mode) && !isFalse(envGet(mode))) {
return true;
}

Expand Down
13 changes: 8 additions & 5 deletions src/lib/global/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import type { EnvObj } from "./env";
import type { EnvKeys, EnvObj } from "./env";
import type { FeatureFlags, FeatureOptions } from "./featureFlags";
import type { LogStoreType } from "./log";

type LogFunction = (logLevel: any, ...args: any[]) => void;
type LognFunction = (namespace: string, logLevel: any, ...args: any[]) => void;
type FeatureFunction = (mode: FeatureFlags, options?: FeatureOptions) => boolean;

declare global {
var log: LogFunction;
var logn: LogFunction;
var logn: LognFunction;
var debug: LogFunction;
var debugn: LogFunction;
var debugn: LognFunction;
var logStore: LogStoreType;
var env: EnvObj;
var envGet: (key: EnvKeys) => any;
var feature: FeatureFunction;
var getNumberOfEventListeners: () => number;
var getObjectOfEventListeners: () => Record<string, number>;

interface Window {
log: LogFunction;
logn: LogFunction;
logn: LognFunction;
debug: LogFunction;
debugn: LogFunction;
debugn: LognFunction;
logStore: LogStoreType;
env: EnvObj;
envGet: (key: EnvKeys) => any;
feature: FeatureFunction;
getNumberOfEventListeners: () => number;
getObjectOfEventListeners: () => Record<string, number>;
Expand Down
22 changes: 22 additions & 0 deletions src/lib/type-assertions.ts
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
export const getKeys = Object.keys as <T extends object>(obj: T) => Array<keyof T>;

/**
* Deep `keyof` of a nested object. INCLUDES root key as well as all nested.
*
* @example "a" | "b" | "nest" | "otherNest" | "nest.c" | "otherNest.c"
*/
export type DeepKeyofPaths<T> = T extends object
? {
[K in keyof T]: `${Exclude<K, symbol>}${"" | `.${DeepKeyofPaths<T[K]>}`}`;
}[keyof T]
: never;

/**
* Deep `keyof` of a nested object. DOES NOT INCLUDE root key, only shows leaf nodes.
*
* @example "a" | "b" | "nest.c" | "otherNest.c"
*/
export type DeepKeyofLeaves<T> = T extends object
? {
[K in keyof T]: `${Exclude<K, symbol>}${DeepKeyofLeaves<T[K]> extends never ? "" : `.${DeepKeyofLeaves<T[K]>}`}`;
}[keyof T]
: never;
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2906,6 +2906,22 @@ __metadata:
languageName: node
linkType: hard

"@types/lodash-es@npm:^4.17.12":
version: 4.17.12
resolution: "@types/lodash-es@npm:4.17.12"
dependencies:
"@types/lodash": "*"
checksum: 990a99e2243bebe9505cb5ad19fbc172beb4a8e00f9075c99fc06c46c2801ffdb40bc2867271cf580d5f48994fc9fb076ec92cd60a20e621603bf22114e5b077
languageName: node
linkType: hard

"@types/lodash@npm:*":
version: 4.14.202
resolution: "@types/lodash@npm:4.14.202"
checksum: a91acf3564a568c6f199912f3eb2c76c99c5a0d7e219394294213b3f2d54f672619f0fde4da22b29dc5d4c31457cd799acc2e5cb6bd90f9af04a1578483b6ff7
languageName: node
linkType: hard

"@types/node@npm:*":
version: 20.3.3
resolution: "@types/node@npm:20.3.3"
Expand Down Expand Up @@ -3247,6 +3263,7 @@ __metadata:
"@testing-library/user-event": ^14.5.2
"@trivago/prettier-plugin-sort-imports": ^4.3.0
"@types/css": ^0.0.37
"@types/lodash-es": ^4.17.12
"@types/node": ^20.11.7
"@types/react": ^18.0.21
"@types/react-dom": ^18.2.18
Expand All @@ -3264,6 +3281,7 @@ __metadata:
css: ^3.0.0
dayjs: ^1.11.10
happy-dom: 13.3.8
lodash-es: ^4.17.21
plausible-tracker: ^0.3.8
prettier: 3.2.5
react: ^18.2.0
Expand Down Expand Up @@ -5548,6 +5566,13 @@ __metadata:
languageName: node
linkType: hard

"lodash-es@npm:^4.17.21":
version: 4.17.21
resolution: "lodash-es@npm:4.17.21"
checksum: 05cbffad6e2adbb331a4e16fbd826e7faee403a1a04873b82b42c0f22090f280839f85b95393f487c1303c8a3d2a010048bf06151a6cbe03eee4d388fb0a12d2
languageName: node
linkType: hard

"lodash.debounce@npm:^4.0.8":
version: 4.0.8
resolution: "lodash.debounce@npm:4.0.8"
Expand Down

0 comments on commit 418a554

Please sign in to comment.