-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IS-310: Setup GrowthBook for BE (#926)
* feat: add growthbook as dep * feat: add to env example * feat: add polyfill deps for growthbook * feat: add utils * feat: setup types for growthbook * feat: add extended type for request handler * feat: add middleware * feat: update middleware * feat: add growthbook to convict * feat: use middleware across routes * feat: extract into constant file * fix: add GB key to .env.test * feat: enable auto-refresh * feat: introduce interface for GrowthBook * feat: add comment explaining new handler type * fix: make return of custom handler type void * fix: imports * feat: inject attributes from authN middleware * feat: inject site-name at authorization * feat: add type safety for growthbook property in req * fix: minor fixes * feat: inject attributes from authN middleware * feat: add growthbook as optional property in session context * feat: inject growthbook into session * feat: add type safety for GB * feat: add key for ggs whitelist * feat: update method to read from feature flag * chore: remove env for ggs whitelist * fix: remove unused constant * feat: enable dev mode only for dev node env * feat: remove dependency on ggs env var * feat: make growthbook optional in Express request * feat: add type safety * feat: fix tests * fix: conditional logic * fix: log statement * fix: remove role * feat: add better logging for debugging * fix: use logger instead of console * feat: update function signatures * feat: update tests * feat: refactor flags to use namespaced references
- Loading branch information
Showing
20 changed files
with
465 additions
and
76 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 @@ | ||
export const FEATURE_FLAGS = { | ||
GGS_WHITELISTED_REPOS: "ggs_whitelisted_repos", | ||
} as const |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./constants" | ||
export * from "./pages" | ||
export * from "./versions" | ||
export * from "./featureFlags" |
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,31 @@ | ||
import UserWithSiteSessionData from "@root/classes/UserWithSiteSessionData" | ||
import config from "@root/config/config" | ||
import logger from "@root/logger/logger" | ||
import { RequestHandlerWithGrowthbook } from "@root/types" | ||
import { getNewGrowthbookInstance } from "@root/utils/growthbook-utils" | ||
|
||
export const featureFlagMiddleware: RequestHandlerWithGrowthbook< | ||
never, | ||
unknown, | ||
unknown, | ||
never, | ||
{ userWithSiteSessionData: UserWithSiteSessionData } | ||
> = async (req, res, next) => { | ||
req.growthbook = getNewGrowthbookInstance(config.get("growthbook.clientKey")) | ||
|
||
// Clean up at the end of the request | ||
res.on("close", () => { | ||
if (req.growthbook) req.growthbook.destroy() | ||
}) | ||
|
||
// Wait for features to load (will be cached in-memory for future requests) | ||
req.growthbook | ||
.loadFeatures({ autoRefresh: true }) | ||
.then(() => next()) | ||
.catch((e: unknown) => { | ||
logger.error( | ||
`Failed to load features from GrowthBook: ${JSON.stringify(e)}` | ||
) | ||
next() | ||
}) | ||
} |
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
Oops, something went wrong.