-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(application-generic, shared): Add Rate Limiting feature flag (#4667
) * feat(shared): Add feature flag format type checking * feat(shared): Add request rate limiting feature flag * fix(api): Update merged digest ID feature flag to use correct format * feat(application-generic): Add request rate limiting FF use case * fix: Remove commented code * fix: Typo * feat(infra): Add missing .env file values
- Loading branch information
Showing
13 changed files
with
140 additions
and
12 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
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,6 @@ | ||
export enum FeatureFlagsKeysEnum { | ||
IS_TEMPLATE_STORE_ENABLED = 'IS_TEMPLATE_STORE_ENABLED', | ||
IS_TOPIC_NOTIFICATION_ENABLED = 'IS_TOPIC_NOTIFICATION_ENABLED', | ||
IS_MULTI_TENANCY_ENABLED = 'IS_MULTI_TENANCY_ENABLED', | ||
IS_USE_MERGED_DIGEST_ID_ENABLED = 'IS_USE_MERGED_DIGEST_ID_ENABLED', | ||
} |
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,54 @@ | ||
import { IFlagKey, testFlagEnumValidity } from './flags.types'; | ||
import { FeatureFlagsKeysEnum } from './feature-flags'; | ||
import { SystemCriticalFlagsEnum } from './system-critical-flags'; | ||
|
||
/** | ||
* Type Error tests for template literal types - Flag naming | ||
* `export` is specified to avoid false-positive issue of: | ||
* "<value> is declared but its value is never read." | ||
* | ||
* https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html | ||
*/ | ||
|
||
/** | ||
* IFlagKey tests | ||
*/ | ||
// Valid | ||
export const validFlag: IFlagKey = 'IS_SOMETHING_ENABLED'; | ||
|
||
// @ts-expect-error - Missing `IS_` prefix | ||
export const invalidPrefixFlag: IFlagKey = 'SOMETHING_ENABLED'; | ||
|
||
// @ts-expect-error - Missing `_ENABLED` suffix | ||
export const invalidSuffixFlag: IFlagKey = 'IS_SOMETHING'; | ||
|
||
// @ts-expect-error - Incorrect subject casing | ||
export const invalidSubjectFlag: IFlagKey = 'IS_something_ENABLED'; | ||
|
||
/** | ||
* testFlagEnumValidity Tests | ||
*/ | ||
enum ValidFlagsEnum { | ||
IS_SOMETHING_ENABLED = 'IS_SOMETHING_ENABLED', | ||
IS_SOMETHING_ELSE_ENABLED = 'IS_SOMETHING_ELSE_ENABLED', | ||
} | ||
testFlagEnumValidity(ValidFlagsEnum); | ||
|
||
enum InvalidKeyFlagsEnum { | ||
IS_SOMETHING_ENABLED = 'IS_SOMETHING_ENABLED', | ||
INVALID_ENABLED = 'IS_INVALID_ENABLED', | ||
} | ||
// @ts-expect-error - Invalid key - INVALID_ENABLED | ||
testFlagEnumValidity(InvalidKeyFlagsEnum); | ||
enum InvalidValueFlagsEnum { | ||
IS_SOMETHING_ENABLED = 'IS_SOMETHING_ENABLED', | ||
IS_INVALID_ENABLED = 'INVALID_ENABLED', | ||
} | ||
// @ts-expect-error - Invalid value on IS_INVALID_ENABLED: 'INVALID_ENABLED' | ||
testFlagEnumValidity(InvalidValueFlagsEnum); | ||
|
||
/** | ||
* Verifying declared FlagEnums | ||
*/ | ||
testFlagEnumValidity(FeatureFlagsKeysEnum); | ||
testFlagEnumValidity(SystemCriticalFlagsEnum); |
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,20 @@ | ||
/** | ||
* The required format for a boolean flag key. | ||
*/ | ||
export type IFlagKey = `IS_${Uppercase<string>}_ENABLED`; | ||
|
||
/** | ||
* Helper function to test that enum keys and values match correct format. | ||
* | ||
* It is not possible as of Typescript 5.2 to declare a type for an enum key or value in-line. | ||
* Therefore we must test the enum via a helper function that abstracts the enum to an object. | ||
* | ||
* If the test fails, you should review your `enum` to verify that both the | ||
* keys and values match the format specified by the `IFlagKey` template literal type. | ||
* ref: https://stackoverflow.com/a/58181315 | ||
* | ||
* @param testEnum - the Enum to type check | ||
*/ | ||
export declare function testFlagEnumValidity<TEnum extends IFlags, IFlags = Record<IFlagKey, IFlagKey>>( | ||
testEnum: TEnum & Record<Exclude<keyof TEnum, keyof IFlags>, ['Key must follow `IFlagKey` format']> | ||
): true; |
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,10 +1,3 @@ | ||
export enum FeatureFlagsKeysEnum { | ||
IS_TEMPLATE_STORE_ENABLED = 'IS_TEMPLATE_STORE_ENABLED', | ||
IS_TOPIC_NOTIFICATION_ENABLED = 'IS_TOPIC_NOTIFICATION_ENABLED', | ||
IS_MULTI_TENANCY_ENABLED = 'IS_MULTI_TENANCY_ENABLED', | ||
IS_USE_MERGED_DIGEST_ID = 'IS_USE_MERGED_DIGEST_ID_ENABLED', | ||
} | ||
|
||
export enum SystemCriticalFlagsEnum { | ||
IS_IN_MEMORY_CLUSTER_MODE_ENABLED = 'IS_IN_MEMORY_CLUSTER_MODE_ENABLED', | ||
} | ||
export * from './feature-flags'; | ||
export * from './system-critical-flags'; | ||
export * from './flags.types'; |
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,4 @@ | ||
export enum SystemCriticalFlagsEnum { | ||
IS_IN_MEMORY_CLUSTER_MODE_ENABLED = 'IS_IN_MEMORY_CLUSTER_MODE_ENABLED', | ||
IS_REQUEST_RATE_LIMITING_ENABLED = 'IS_REQUEST_RATE_LIMITING_ENABLED', | ||
} |
21 changes: 21 additions & 0 deletions
21
...on-generic/src/usecases/get-feature-flag/get-is-request-rate-limiting-enabled.use-case.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,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { SystemCriticalFlagsEnum } from '@novu/shared'; | ||
|
||
import { GetSystemCriticalFlag } from './get-system-critical-flag.use-case'; | ||
|
||
@Injectable() | ||
export class GetIsRequestRateLimitingEnabled extends GetSystemCriticalFlag { | ||
execute(): boolean { | ||
const value = process.env.IS_REQUEST_RATE_LIMITING_ENABLED; | ||
const fallbackValue = false; | ||
const defaultValue = this.prepareBooleanStringSystemCriticalFlag( | ||
value, | ||
fallbackValue | ||
); | ||
const key = SystemCriticalFlagsEnum.IS_REQUEST_RATE_LIMITING_ENABLED; | ||
|
||
this.log<boolean>(key, defaultValue); | ||
|
||
return defaultValue; | ||
} | ||
} |
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
37183b3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://dev.widget.novu.co as production
🚀 Deployed on https://653fd98f860705482376c060--dev-widget-novu.netlify.app