-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add feature flag support for filtered status code
- Loading branch information
1 parent
7f353e6
commit 6fca43d
Showing
6 changed files
with
80 additions
and
18 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
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,49 @@ | ||
import { Context, Next } from 'koa'; | ||
|
||
export interface FeatureFlags { | ||
[key: string]: boolean | string; | ||
} | ||
|
||
export const FEATURE_FILTER_CODE = 'filter-code'; | ||
|
||
export default class FeatureFlagMiddleware { | ||
public static async handle(ctx: Context, next: Next): Promise<void> { | ||
// Initialize ctx.state.features if it doesn't exist | ||
ctx.state.features = (ctx.state.features || {}) as FeatureFlags; | ||
|
||
// Get headers from the request | ||
const { headers } = ctx.request; | ||
|
||
// Filter headers that start with 'X-Feature-' | ||
const featureHeaders = Object.keys(headers).filter((key) => | ||
key.toLowerCase().startsWith('x-feature-'), | ||
); | ||
|
||
// Convert feature headers to feature flags in ctx.state.features | ||
featureHeaders.forEach((featureHeader) => { | ||
// Get the feature name by removing the prefix, and convert to camelCase | ||
const featureName = featureHeader | ||
.substring(10) | ||
.replace(/X-Feature-/g, '') | ||
.toLowerCase(); | ||
|
||
let value: string | boolean | undefined; | ||
const valueString = headers[featureHeader] as string; | ||
if (valueString === 'true' || valueString === '?1') { | ||
value = true; | ||
} else if (valueString === 'false' || valueString === '?0') { | ||
value = false; | ||
} else { | ||
value = valueString; | ||
} | ||
|
||
// Set the feature flag in ctx.state.features | ||
if (value !== undefined) { | ||
ctx.state.features[featureName] = value; | ||
} | ||
}); | ||
|
||
// Move to the next middleware | ||
await 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
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