New endpoint to set feature flag overrides.
This commit is contained in:
parent
39021dc485
commit
d261b91a65
|
@ -9,6 +9,7 @@ export enum Cookie {
|
||||||
ACCOUNT_RETURN_URL = "budibase:account:returnurl",
|
ACCOUNT_RETURN_URL = "budibase:account:returnurl",
|
||||||
DatasourceAuth = "budibase:datasourceauth",
|
DatasourceAuth = "budibase:datasourceauth",
|
||||||
OIDC_CONFIG = "budibase:oidc:config",
|
OIDC_CONFIG = "budibase:oidc:config",
|
||||||
|
FeatureFlags = "budibase:featureflags",
|
||||||
}
|
}
|
||||||
|
|
||||||
export { Header } from "@budibase/shared-core"
|
export { Header } from "@budibase/shared-core"
|
||||||
|
|
|
@ -460,6 +460,17 @@ export function setFeatureFlags(key: string, value: Record<string, boolean>) {
|
||||||
context.featureFlagCache[key] = value
|
context.featureFlagCache[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getFeatureFlagOverrides(): Record<string, boolean> {
|
||||||
|
return getCurrentContext()?.featureFlagOverrides || {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function doInFeatureFlagOverrideContext<T>(
|
||||||
|
value: Record<string, boolean>,
|
||||||
|
callback: () => Promise<T>
|
||||||
|
) {
|
||||||
|
return newContext({ featureFlagOverrides: value }, callback)
|
||||||
|
}
|
||||||
|
|
||||||
export function getTableForView(viewId: string): Table | undefined {
|
export function getTableForView(viewId: string): Table | undefined {
|
||||||
const context = getCurrentContext()
|
const context = getCurrentContext()
|
||||||
if (!context) {
|
if (!context) {
|
||||||
|
|
|
@ -24,5 +24,6 @@ export type ContextMap = {
|
||||||
featureFlagCache?: {
|
featureFlagCache?: {
|
||||||
[key: string]: Record<string, boolean>
|
[key: string]: Record<string, boolean>
|
||||||
}
|
}
|
||||||
|
featureFlagOverrides?: Record<string, boolean>
|
||||||
viewToTableCache?: Record<string, Table>
|
viewToTableCache?: Record<string, Table>
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { Ctx, FeatureFlagCookie } from "@budibase/types"
|
||||||
|
import { Middleware, Next } from "koa"
|
||||||
|
import { getCookie } from "../utils"
|
||||||
|
import { Cookie } from "../constants"
|
||||||
|
import { doInFeatureFlagOverrideContext } from "../context"
|
||||||
|
|
||||||
|
export default (async (ctx: Ctx, next: Next) => {
|
||||||
|
const cookie = getCookie<FeatureFlagCookie>(ctx, Cookie.FeatureFlags)
|
||||||
|
const flags = cookie?.flags || {}
|
||||||
|
await doInFeatureFlagOverrideContext(flags, async () => {
|
||||||
|
await next()
|
||||||
|
})
|
||||||
|
}) as Middleware
|
|
@ -20,5 +20,6 @@ export { default as correlation } from "../logging/correlation/middleware"
|
||||||
export { default as errorHandling } from "./errorHandling"
|
export { default as errorHandling } from "./errorHandling"
|
||||||
export { default as querystringToBody } from "./querystringToBody"
|
export { default as querystringToBody } from "./querystringToBody"
|
||||||
export { default as csp } from "./contentSecurityPolicy"
|
export { default as csp } from "./contentSecurityPolicy"
|
||||||
|
export { default as featureFlagCookie } from "./featureFlagCookie"
|
||||||
export * as joiValidator from "./joi-validator"
|
export * as joiValidator from "./joi-validator"
|
||||||
export { default as ip } from "./ip"
|
export { default as ip } from "./ip"
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
import { UserCtx, OverrideFeatureFlagRequest } from "@budibase/types"
|
||||||
|
import { Cookie, utils } from "@budibase/backend-core"
|
||||||
|
|
||||||
|
export async function override(ctx: UserCtx<OverrideFeatureFlagRequest, void>) {
|
||||||
|
const { flags = {} } = ctx.request.body
|
||||||
|
utils.setCookie(ctx, flags, Cookie.FeatureFlags)
|
||||||
|
}
|
|
@ -71,6 +71,7 @@ if (apiEnabled()) {
|
||||||
)
|
)
|
||||||
.use(pro.licensing())
|
.use(pro.licensing())
|
||||||
.use(currentApp)
|
.use(currentApp)
|
||||||
|
.use(middleware.featureFlagCookie)
|
||||||
|
|
||||||
// Add CSP as soon as possible - depends on licensing and currentApp
|
// Add CSP as soon as possible - depends on licensing and currentApp
|
||||||
if (!coreEnv.DISABLE_CONTENT_SECURITY_POLICY) {
|
if (!coreEnv.DISABLE_CONTENT_SECURITY_POLICY) {
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
import Router from "@koa/router"
|
||||||
|
import * as controller from "../controllers/features"
|
||||||
|
|
||||||
|
const router: Router = new Router()
|
||||||
|
|
||||||
|
router.patch("/api/features", controller.override)
|
||||||
|
|
||||||
|
export default router
|
|
@ -0,0 +1,3 @@
|
||||||
|
export interface OverrideFeatureFlagRequest {
|
||||||
|
flags: Record<string, boolean>
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ export * from "./backup"
|
||||||
export * from "./component"
|
export * from "./component"
|
||||||
export * from "./datasource"
|
export * from "./datasource"
|
||||||
export * from "./deployment"
|
export * from "./deployment"
|
||||||
|
export * from "./features"
|
||||||
export * from "./integration"
|
export * from "./integration"
|
||||||
export * from "./layout"
|
export * from "./layout"
|
||||||
export * from "./metadata"
|
export * from "./metadata"
|
||||||
|
|
|
@ -7,3 +7,7 @@ export interface SessionCookie {
|
||||||
sessionId: string
|
sessionId: string
|
||||||
userId: string
|
userId: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface FeatureFlagCookie {
|
||||||
|
flags: Record<string, boolean>
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
export enum FeatureFlag {
|
export enum FeatureFlag {
|
||||||
|
DEBUG_UI = "DEBUG_UI",
|
||||||
USE_ZOD_VALIDATOR = "USE_ZOD_VALIDATOR",
|
USE_ZOD_VALIDATOR = "USE_ZOD_VALIDATOR",
|
||||||
AI_JS_GENERATION = "AI_JS_GENERATION",
|
AI_JS_GENERATION = "AI_JS_GENERATION",
|
||||||
AI_TABLE_GENERATION = "AI_TABLE_GENERATION",
|
AI_TABLE_GENERATION = "AI_TABLE_GENERATION",
|
||||||
|
@ -14,6 +15,8 @@ export const FeatureFlagDefaults: Record<FeatureFlag, boolean> = {
|
||||||
|
|
||||||
// Account-portal
|
// Account-portal
|
||||||
[FeatureFlag.DIRECT_LOGIN_TO_ACCOUNT_PORTAL]: false,
|
[FeatureFlag.DIRECT_LOGIN_TO_ACCOUNT_PORTAL]: false,
|
||||||
|
|
||||||
|
[FeatureFlag.DEBUG_UI]: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FeatureFlags = typeof FeatureFlagDefaults
|
export type FeatureFlags = typeof FeatureFlagDefaults
|
||||||
|
|
Loading…
Reference in New Issue