-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup fflip feature toggles and use one for CatEndpoints
- Loading branch information
Jonas Verhoelen
committed
May 21, 2019
1 parent
17686c4
commit b4a333a
Showing
11 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
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
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,12 @@ | ||
import * as fflip from 'fflip' | ||
|
||
export const criteria: fflip.Criteria[] = [ | ||
{ | ||
id: 'isPaidUser', | ||
check: (user: any, needsToBePaid: boolean) => user && user.isPaid === needsToBePaid | ||
}, | ||
{ | ||
id: 'shareOfUsers', | ||
check: (user: any, share: number) => user && user.id % 100 < share * 100 | ||
} | ||
] |
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,17 @@ | ||
import * as fflip from 'fflip' | ||
|
||
export const FeatureToggles: { [ key: string ]: string } = { | ||
CLOSED_BETA: 'CLOSED_BETA', | ||
WITH_CAT_STATISTICS: 'WITH_CAT_STATISTICS' | ||
} | ||
|
||
export const features: fflip.Feature[] = [ | ||
{ | ||
id: FeatureToggles.CLOSED_BETA, | ||
criteria: { isPaidUser: true, shareOfUsers: 0.5 } | ||
}, | ||
{ | ||
id: FeatureToggles.WITH_CAT_STATISTICS, | ||
enabled: true | ||
} | ||
] |
25 changes: 25 additions & 0 deletions
25
service/server/middlewares/feature-toggles/setupFeatureToggles.ts
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,25 @@ | ||
import { Express, NextFunction, Response, Request } from 'express' | ||
import * as fflip from 'fflip' | ||
import * as FFlipExpressIntegration from 'fflip-express' | ||
import { criteria } from './criteria' | ||
import { features } from './features' | ||
|
||
const createFFlipExpressIntegration = () => new FFlipExpressIntegration(fflip, { | ||
cookieName: 'fflip', | ||
manualRoutePath: '/api/toggles/local/:name/:action' | ||
}) | ||
|
||
export const applyFeatureToggles = (server: Express) => { | ||
fflip.config({ criteria, features }) | ||
const fflipExpressIntegration = createFFlipExpressIntegration() | ||
|
||
server.use(fflipExpressIntegration.middleware) | ||
server.use((req: Request, _: Response, next: NextFunction) => { | ||
try { | ||
req.fflip.setForUser(req.user) | ||
} catch (err) { | ||
console.error('Error while binding feature toggles to req.user') | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// tslint:disable no-implicit-dependencies | ||
|
||
import 'express-serve-static-core' | ||
|
||
declare module 'express-serve-static-core' { | ||
interface Request { | ||
fflip: { | ||
features: { [s: string]: boolean } | ||
setForUser(user: any): void | ||
has(featureName: string): boolean | ||
} | ||
user?: any | ||
} | ||
} |
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,22 @@ | ||
/* tslint:disable no-namespace */ | ||
|
||
declare module 'fflip-express' { | ||
import * as FFlip from 'fflip' | ||
import { CookieOptions, Handler } from 'express' | ||
|
||
class FFlipExpressIntegration { | ||
public middleware: Handler | ||
public manualRoute: Handler | ||
constructor(fflip: FFlip, options: FFlipExpressIntegration.Options) | ||
} | ||
|
||
namespace FFlipExpressIntegration { | ||
export interface Options { | ||
cookieName?: string | ||
cookieOptions?: CookieOptions | ||
manualRoutePath?: string | ||
} | ||
} | ||
|
||
export = FFlipExpressIntegration | ||
} |
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,45 @@ | ||
/* tslint:disable no-namespace */ | ||
declare module 'fflip' { | ||
|
||
class FFlip { } | ||
|
||
namespace FFlip { | ||
type GetFeaturesSync = () => Feature[] | ||
type GetFeaturesAsync = (callback: (features: Feature[]) => void) => void | ||
type CriteriaConfig = StringMap | StringMap[] | ||
|
||
export interface Config { | ||
criteria: Criteria[] | ||
features: Feature[] | GetFeaturesSync | GetFeaturesAsync | ||
reload?: number | ||
} | ||
|
||
export interface Criteria { | ||
id: string | ||
check(user: any, config: any): boolean | ||
} | ||
|
||
export interface Feature { | ||
id: string | ||
criteria?: CriteriaConfig | ||
enabled?: boolean | ||
[s: string]: any | ||
} | ||
|
||
export interface Features { | ||
[featureName: string]: boolean | ||
} | ||
|
||
export interface StringMap { | ||
$veto?: boolean | ||
[s: string]: any | ||
} | ||
|
||
export function config(config: Config): void | ||
export function isFeatureEnabledForUser(featureName: string, user: any): boolean | ||
export function getFeaturesForUser(user: any): Features | ||
export function reload(): void | ||
} | ||
|
||
export = FFlip | ||
} |