nest-feature-flags 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +25 -0
- package/.prettierrc +8 -0
- package/LICENSE +21 -0
- package/README.md +153 -0
- package/dist/decorators/feature-flag.decorator.d.ts +2 -0
- package/dist/decorators/feature-flag.decorator.js +8 -0
- package/dist/decorators/feature-flag.decorator.js.map +1 -0
- package/dist/feature-flags.module.d.ts +6 -0
- package/dist/feature-flags.module.js +53 -0
- package/dist/feature-flags.module.js.map +1 -0
- package/dist/guards/feature-flag.guard.d.ts +9 -0
- package/dist/guards/feature-flag.guard.js +46 -0
- package/dist/guards/feature-flag.guard.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/feature-flag-options.interface.d.ts +10 -0
- package/dist/interfaces/feature-flag-options.interface.js +3 -0
- package/dist/interfaces/feature-flag-options.interface.js.map +1 -0
- package/dist/interfaces/feature-flag.interface.d.ts +20 -0
- package/dist/interfaces/feature-flag.interface.js +3 -0
- package/dist/interfaces/feature-flag.interface.js.map +1 -0
- package/dist/services/feature-flag.service.d.ts +12 -0
- package/dist/services/feature-flag.service.js +86 -0
- package/dist/services/feature-flag.service.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/jest.config.js +23 -0
- package/package.json +77 -0
- package/src/decorators/feature-flag.decorator.ts +8 -0
- package/src/feature-flags.module.ts +42 -0
- package/src/guards/feature-flag.guard.ts +35 -0
- package/src/index.ts +13 -0
- package/src/interfaces/feature-flag-options.interface.ts +25 -0
- package/src/interfaces/feature-flag.interface.ts +84 -0
- package/src/services/feature-flag.service.spec.ts +51 -0
- package/src/services/feature-flag.service.ts +99 -0
- package/tsconfig.json +25 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
parser: '@typescript-eslint/parser',
|
|
3
|
+
parserOptions: {
|
|
4
|
+
project: 'tsconfig.json',
|
|
5
|
+
tsconfigRootDir: __dirname,
|
|
6
|
+
sourceType: 'module',
|
|
7
|
+
},
|
|
8
|
+
plugins: ['@typescript-eslint/eslint-plugin'],
|
|
9
|
+
extends: [
|
|
10
|
+
'plugin:@typescript-eslint/recommended',
|
|
11
|
+
'plugin:prettier/recommended',
|
|
12
|
+
],
|
|
13
|
+
root: true,
|
|
14
|
+
env: {
|
|
15
|
+
node: true,
|
|
16
|
+
jest: true,
|
|
17
|
+
},
|
|
18
|
+
ignorePatterns: ['.eslintrc.js'],
|
|
19
|
+
rules: {
|
|
20
|
+
'@typescript-eslint/interface-name-prefix': 'off',
|
|
21
|
+
'@typescript-eslint/explicit-function-return-type': 'off',
|
|
22
|
+
'@typescript-eslint/explicit-module-boundary-types': 'off',
|
|
23
|
+
'@typescript-eslint/no-explicit-any': 'warn',
|
|
24
|
+
},
|
|
25
|
+
};
|
package/.prettierrc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alireza Aminzadeh
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# nest-feature-flags
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/nest-feature-flags)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
Professional feature flag management system for NestJS with A/B testing, gradual rollouts, user targeting, and real-time updates.
|
|
7
|
+
|
|
8
|
+
## 🚀 Features
|
|
9
|
+
|
|
10
|
+
- **Feature Toggles** - Enable/disable features instantly
|
|
11
|
+
- **Gradual Rollouts** - Percentage-based feature releases
|
|
12
|
+
- **A/B Testing** - Run experiments with user segmentation
|
|
13
|
+
- **User Targeting** - Target specific users or groups
|
|
14
|
+
- **Real-time Updates** - Changes reflect immediately
|
|
15
|
+
- **Type-Safe** - Full TypeScript support
|
|
16
|
+
- **Production-Ready** - Built for enterprise applications
|
|
17
|
+
|
|
18
|
+
## 📦 Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install nest-feature-flags
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 🔧 Quick Start
|
|
25
|
+
|
|
26
|
+
### 1. Import Module
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { Module } from '@nestjs/common';
|
|
30
|
+
import { FeatureFlagsModule } from 'nest-feature-flags';
|
|
31
|
+
|
|
32
|
+
@Module({
|
|
33
|
+
imports: [
|
|
34
|
+
FeatureFlagsModule.forRoot({
|
|
35
|
+
flags: {
|
|
36
|
+
'new-ui': { enabled: true },
|
|
37
|
+
'beta-feature': { enabled: false },
|
|
38
|
+
'gradual-rollout': { enabled: true, percentage: 50 },
|
|
39
|
+
},
|
|
40
|
+
}),
|
|
41
|
+
],
|
|
42
|
+
})
|
|
43
|
+
export class AppModule {}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### 2. Use in Services
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { Injectable } from '@nestjs/common';
|
|
50
|
+
import { FeatureFlagService } from 'nest-feature-flags';
|
|
51
|
+
|
|
52
|
+
@Injectable()
|
|
53
|
+
export class MyService {
|
|
54
|
+
constructor(private featureFlags: FeatureFlagService) {}
|
|
55
|
+
|
|
56
|
+
async doSomething(userId: string) {
|
|
57
|
+
if (await this.featureFlags.isEnabled('new-ui', { userId })) {
|
|
58
|
+
return 'New UI experience';
|
|
59
|
+
}
|
|
60
|
+
return 'Classic UI';
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 3. Use Guards
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { Controller, Get, UseGuards } from '@nestjs/common';
|
|
69
|
+
import { FeatureFlagGuard, RequireFeature } from 'nest-feature-flags';
|
|
70
|
+
|
|
71
|
+
@Controller('beta')
|
|
72
|
+
export class BetaController {
|
|
73
|
+
@Get()
|
|
74
|
+
@UseGuards(FeatureFlagGuard)
|
|
75
|
+
@RequireFeature('beta-feature')
|
|
76
|
+
async betaEndpoint() {
|
|
77
|
+
return { message: 'Beta feature active!' };
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## 📖 Documentation
|
|
83
|
+
|
|
84
|
+
### Feature Flag Configuration
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
{
|
|
88
|
+
'flag-name': {
|
|
89
|
+
enabled: true, // Master switch
|
|
90
|
+
percentage: 50, // Gradual rollout (0-100)
|
|
91
|
+
userIds: ['user1', 'user2'], // Specific users
|
|
92
|
+
groups: ['beta-testers'], // User groups
|
|
93
|
+
startDate: new Date(), // Schedule activation
|
|
94
|
+
endDate: new Date(), // Schedule deactivation
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Advanced Usage
|
|
100
|
+
|
|
101
|
+
#### Percentage-based Rollout
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
FeatureFlagsModule.forRoot({
|
|
105
|
+
flags: {
|
|
106
|
+
'new-checkout': {
|
|
107
|
+
enabled: true,
|
|
108
|
+
percentage: 25, // 25% of users see this
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### User Targeting
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const isEnabled = await featureFlags.isEnabled('premium-feature', {
|
|
118
|
+
userId: 'user123',
|
|
119
|
+
groups: ['premium', 'beta'],
|
|
120
|
+
});
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### A/B Testing
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
const variant = await featureFlags.getVariant('experiment-1', {
|
|
127
|
+
userId: 'user123',
|
|
128
|
+
variants: ['control', 'variant-a', 'variant-b'],
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## 🎯 Use Cases
|
|
133
|
+
|
|
134
|
+
- **Canary Releases** - Roll out features gradually
|
|
135
|
+
- **A/B Testing** - Test different implementations
|
|
136
|
+
- **Beta Programs** - Limit features to beta users
|
|
137
|
+
- **Kill Switches** - Disable features instantly
|
|
138
|
+
- **Scheduled Releases** - Launch features at specific times
|
|
139
|
+
|
|
140
|
+
## 👤 Author
|
|
141
|
+
|
|
142
|
+
**Alireza Aminzadeh**
|
|
143
|
+
- Email: alireza.aminzadeh@hotmail.com
|
|
144
|
+
- GitHub: [@syeedalireza](https://github.com/syeedalireza)
|
|
145
|
+
- NPM: [@syeedalireza](https://www.npmjs.com/~syeedalireza)
|
|
146
|
+
|
|
147
|
+
## 📄 License
|
|
148
|
+
|
|
149
|
+
MIT License - see [LICENSE](./LICENSE) file
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
**Built with ❤️ for the NestJS community**
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequireFeature = exports.FEATURE_FLAG_KEY = void 0;
|
|
4
|
+
const common_1 = require("@nestjs/common");
|
|
5
|
+
exports.FEATURE_FLAG_KEY = 'feature_flag_key';
|
|
6
|
+
const RequireFeature = (flagKey) => (0, common_1.SetMetadata)(exports.FEATURE_FLAG_KEY, flagKey);
|
|
7
|
+
exports.RequireFeature = RequireFeature;
|
|
8
|
+
//# sourceMappingURL=feature-flag.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flag.decorator.js","sourceRoot":"","sources":["../../src/decorators/feature-flag.decorator.ts"],"names":[],"mappings":";;;AAAA,2CAA6C;AAEhC,QAAA,gBAAgB,GAAG,kBAAkB,CAAC;AAK5C,MAAM,cAAc,GAAG,CAAC,OAAe,EAAE,EAAE,CAAC,IAAA,oBAAW,EAAC,wBAAgB,EAAE,OAAO,CAAC,CAAC;AAA7E,QAAA,cAAc,kBAA+D"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { FeatureFlagsModuleOptions, FeatureFlagsModuleAsyncOptions } from './interfaces/feature-flag-options.interface';
|
|
3
|
+
export declare class FeatureFlagsModule {
|
|
4
|
+
static forRoot(options: FeatureFlagsModuleOptions): DynamicModule;
|
|
5
|
+
static forRootAsync(options: FeatureFlagsModuleAsyncOptions): DynamicModule;
|
|
6
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var FeatureFlagsModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.FeatureFlagsModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const feature_flag_service_1 = require("./services/feature-flag.service");
|
|
13
|
+
const feature_flag_guard_1 = require("./guards/feature-flag.guard");
|
|
14
|
+
let FeatureFlagsModule = FeatureFlagsModule_1 = class FeatureFlagsModule {
|
|
15
|
+
static forRoot(options) {
|
|
16
|
+
return {
|
|
17
|
+
module: FeatureFlagsModule_1,
|
|
18
|
+
providers: [
|
|
19
|
+
{
|
|
20
|
+
provide: feature_flag_service_1.OPTIONS_TOKEN,
|
|
21
|
+
useValue: options,
|
|
22
|
+
},
|
|
23
|
+
feature_flag_service_1.FeatureFlagService,
|
|
24
|
+
feature_flag_guard_1.FeatureFlagGuard,
|
|
25
|
+
],
|
|
26
|
+
exports: [feature_flag_service_1.FeatureFlagService, feature_flag_guard_1.FeatureFlagGuard],
|
|
27
|
+
global: true,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
static forRootAsync(options) {
|
|
31
|
+
return {
|
|
32
|
+
module: FeatureFlagsModule_1,
|
|
33
|
+
imports: options.imports || [],
|
|
34
|
+
providers: [
|
|
35
|
+
{
|
|
36
|
+
provide: feature_flag_service_1.OPTIONS_TOKEN,
|
|
37
|
+
useFactory: options.useFactory,
|
|
38
|
+
inject: options.inject || [],
|
|
39
|
+
},
|
|
40
|
+
feature_flag_service_1.FeatureFlagService,
|
|
41
|
+
feature_flag_guard_1.FeatureFlagGuard,
|
|
42
|
+
],
|
|
43
|
+
exports: [feature_flag_service_1.FeatureFlagService, feature_flag_guard_1.FeatureFlagGuard],
|
|
44
|
+
global: true,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
exports.FeatureFlagsModule = FeatureFlagsModule;
|
|
49
|
+
exports.FeatureFlagsModule = FeatureFlagsModule = FeatureFlagsModule_1 = __decorate([
|
|
50
|
+
(0, common_1.Global)(),
|
|
51
|
+
(0, common_1.Module)({})
|
|
52
|
+
], FeatureFlagsModule);
|
|
53
|
+
//# sourceMappingURL=feature-flags.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flags.module.js","sourceRoot":"","sources":["../src/feature-flags.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA+D;AAE/D,0EAAoF;AACpF,oEAA+D;AAIxD,IAAM,kBAAkB,0BAAxB,MAAM,kBAAkB;IAC7B,MAAM,CAAC,OAAO,CAAC,OAAkC;QAC/C,OAAO;YACL,MAAM,EAAE,oBAAkB;YAC1B,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,oCAAa;oBACtB,QAAQ,EAAE,OAAO;iBAClB;gBACD,yCAAkB;gBAClB,qCAAgB;aACjB;YACD,OAAO,EAAE,CAAC,yCAAkB,EAAE,qCAAgB,CAAC;YAC/C,MAAM,EAAE,IAAI;SACb,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,YAAY,CAAC,OAAuC;QACzD,OAAO;YACL,MAAM,EAAE,oBAAkB;YAC1B,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,EAAE;YAC9B,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,oCAAa;oBACtB,UAAU,EAAE,OAAO,CAAC,UAAW;oBAC/B,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,EAAE;iBAC7B;gBACD,yCAAkB;gBAClB,qCAAgB;aACjB;YACD,OAAO,EAAE,CAAC,yCAAkB,EAAE,qCAAgB,CAAC;YAC/C,MAAM,EAAE,IAAI;SACb,CAAC;IACJ,CAAC;CACF,CAAA;AAlCY,gDAAkB;6BAAlB,kBAAkB;IAF9B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,kBAAkB,CAkC9B"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
3
|
+
import { FeatureFlagService } from '../services/feature-flag.service';
|
|
4
|
+
export declare class FeatureFlagGuard implements CanActivate {
|
|
5
|
+
private readonly reflector;
|
|
6
|
+
private readonly featureFlagService;
|
|
7
|
+
constructor(reflector: Reflector, featureFlagService: FeatureFlagService);
|
|
8
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.FeatureFlagGuard = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const feature_flag_service_1 = require("../services/feature-flag.service");
|
|
16
|
+
const feature_flag_decorator_1 = require("../decorators/feature-flag.decorator");
|
|
17
|
+
let FeatureFlagGuard = class FeatureFlagGuard {
|
|
18
|
+
constructor(reflector, featureFlagService) {
|
|
19
|
+
this.reflector = reflector;
|
|
20
|
+
this.featureFlagService = featureFlagService;
|
|
21
|
+
}
|
|
22
|
+
async canActivate(context) {
|
|
23
|
+
const flagKey = this.reflector.get(feature_flag_decorator_1.FEATURE_FLAG_KEY, context.getHandler());
|
|
24
|
+
if (!flagKey) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
const request = context.switchToHttp().getRequest();
|
|
28
|
+
const userId = request.user?.id || request.userId;
|
|
29
|
+
const groups = request.user?.groups || [];
|
|
30
|
+
const enabled = await this.featureFlagService.isEnabled(flagKey, {
|
|
31
|
+
userId,
|
|
32
|
+
groups,
|
|
33
|
+
});
|
|
34
|
+
if (!enabled) {
|
|
35
|
+
throw new common_1.ForbiddenException(`Feature '${flagKey}' is not available`);
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
exports.FeatureFlagGuard = FeatureFlagGuard;
|
|
41
|
+
exports.FeatureFlagGuard = FeatureFlagGuard = __decorate([
|
|
42
|
+
(0, common_1.Injectable)(),
|
|
43
|
+
__metadata("design:paramtypes", [core_1.Reflector,
|
|
44
|
+
feature_flag_service_1.FeatureFlagService])
|
|
45
|
+
], FeatureFlagGuard);
|
|
46
|
+
//# sourceMappingURL=feature-flag.guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flag.guard.js","sourceRoot":"","sources":["../../src/guards/feature-flag.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAA+F;AAC/F,uCAAyC;AACzC,2EAAsE;AACtE,iFAAwE;AAGjE,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAC3B,YACmB,SAAoB,EACpB,kBAAsC;QADtC,cAAS,GAAT,SAAS,CAAW;QACpB,uBAAkB,GAAlB,kBAAkB,CAAoB;IACtD,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAS,yCAAgB,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;QAEnF,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QACpD,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,EAAE,EAAE,IAAI,OAAO,CAAC,MAAM,CAAC;QAClD,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,EAAE,MAAM,IAAI,EAAE,CAAC;QAE1C,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC,SAAS,CAAC,OAAO,EAAE;YAC/D,MAAM;YACN,MAAM;SACP,CAAC,CAAC;QAEH,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,IAAI,2BAAkB,CAAC,YAAY,OAAO,oBAAoB,CAAC,CAAC;QACxE,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAA;AA5BY,4CAAgB;2BAAhB,gBAAgB;IAD5B,IAAA,mBAAU,GAAE;qCAGmB,gBAAS;QACA,yCAAkB;GAH9C,gBAAgB,CA4B5B"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './feature-flags.module';
|
|
2
|
+
export * from './services/feature-flag.service';
|
|
3
|
+
export * from './guards/feature-flag.guard';
|
|
4
|
+
export * from './decorators/feature-flag.decorator';
|
|
5
|
+
export * from './interfaces/feature-flag.interface';
|
|
6
|
+
export * from './interfaces/feature-flag-options.interface';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./feature-flags.module"), exports);
|
|
18
|
+
__exportStar(require("./services/feature-flag.service"), exports);
|
|
19
|
+
__exportStar(require("./guards/feature-flag.guard"), exports);
|
|
20
|
+
__exportStar(require("./decorators/feature-flag.decorator"), exports);
|
|
21
|
+
__exportStar(require("./interfaces/feature-flag.interface"), exports);
|
|
22
|
+
__exportStar(require("./interfaces/feature-flag-options.interface"), exports);
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA,yDAAuC;AAGvC,kEAAgD;AAGhD,8DAA4C;AAC5C,sEAAoD;AAGpD,sEAAoD;AACpD,8EAA4D"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ModuleMetadata } from '@nestjs/common';
|
|
2
|
+
import { FeatureFlag } from './feature-flag.interface';
|
|
3
|
+
export interface FeatureFlagsModuleOptions {
|
|
4
|
+
flags: Record<string, FeatureFlag>;
|
|
5
|
+
defaultValue?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface FeatureFlagsModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
8
|
+
useFactory?: (...args: any[]) => Promise<FeatureFlagsModuleOptions> | FeatureFlagsModuleOptions;
|
|
9
|
+
inject?: any[];
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flag-options.interface.js","sourceRoot":"","sources":["../../src/interfaces/feature-flag-options.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface FeatureFlag {
|
|
2
|
+
enabled: boolean;
|
|
3
|
+
percentage?: number;
|
|
4
|
+
userIds?: string[];
|
|
5
|
+
groups?: string[];
|
|
6
|
+
startDate?: Date;
|
|
7
|
+
endDate?: Date;
|
|
8
|
+
description?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface FeatureFlagContext {
|
|
11
|
+
userId?: string;
|
|
12
|
+
groups?: string[];
|
|
13
|
+
attributes?: Record<string, any>;
|
|
14
|
+
}
|
|
15
|
+
export interface FeatureFlagResult {
|
|
16
|
+
key: string;
|
|
17
|
+
enabled: boolean;
|
|
18
|
+
reason: string;
|
|
19
|
+
variant?: string;
|
|
20
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flag.interface.js","sourceRoot":"","sources":["../../src/interfaces/feature-flag.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FeatureFlag, FeatureFlagContext, FeatureFlagResult } from '../interfaces/feature-flag.interface';
|
|
2
|
+
import { FeatureFlagsModuleOptions } from '../interfaces/feature-flag-options.interface';
|
|
3
|
+
declare const OPTIONS_TOKEN = "FEATURE_FLAGS_OPTIONS";
|
|
4
|
+
export declare class FeatureFlagService {
|
|
5
|
+
private readonly options;
|
|
6
|
+
constructor(options: FeatureFlagsModuleOptions);
|
|
7
|
+
isEnabled(key: string, context?: FeatureFlagContext): Promise<boolean>;
|
|
8
|
+
evaluate(key: string, context?: FeatureFlagContext): Promise<FeatureFlagResult>;
|
|
9
|
+
getAllFlags(): Record<string, FeatureFlag>;
|
|
10
|
+
private hashUserId;
|
|
11
|
+
}
|
|
12
|
+
export { OPTIONS_TOKEN };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.OPTIONS_TOKEN = exports.FeatureFlagService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const OPTIONS_TOKEN = 'FEATURE_FLAGS_OPTIONS';
|
|
18
|
+
exports.OPTIONS_TOKEN = OPTIONS_TOKEN;
|
|
19
|
+
let FeatureFlagService = class FeatureFlagService {
|
|
20
|
+
constructor(options) {
|
|
21
|
+
this.options = options;
|
|
22
|
+
}
|
|
23
|
+
async isEnabled(key, context) {
|
|
24
|
+
const result = await this.evaluate(key, context);
|
|
25
|
+
return result.enabled;
|
|
26
|
+
}
|
|
27
|
+
async evaluate(key, context) {
|
|
28
|
+
const flag = this.options.flags[key];
|
|
29
|
+
if (!flag) {
|
|
30
|
+
return {
|
|
31
|
+
key,
|
|
32
|
+
enabled: this.options.defaultValue ?? false,
|
|
33
|
+
reason: 'Flag not found',
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if (!flag.enabled) {
|
|
37
|
+
return { key, enabled: false, reason: 'Flag disabled' };
|
|
38
|
+
}
|
|
39
|
+
const now = new Date();
|
|
40
|
+
if (flag.startDate && now < flag.startDate) {
|
|
41
|
+
return { key, enabled: false, reason: 'Not started yet' };
|
|
42
|
+
}
|
|
43
|
+
if (flag.endDate && now > flag.endDate) {
|
|
44
|
+
return { key, enabled: false, reason: 'Expired' };
|
|
45
|
+
}
|
|
46
|
+
if (context?.userId && flag.userIds?.includes(context.userId)) {
|
|
47
|
+
return { key, enabled: true, reason: 'User targeted' };
|
|
48
|
+
}
|
|
49
|
+
if (context?.groups && flag.groups) {
|
|
50
|
+
const hasGroup = context.groups.some(g => flag.groups.includes(g));
|
|
51
|
+
if (hasGroup) {
|
|
52
|
+
return { key, enabled: true, reason: 'Group targeted' };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (flag.percentage !== undefined) {
|
|
56
|
+
const hash = this.hashUserId(context?.userId || 'anonymous');
|
|
57
|
+
const userPercentage = hash % 100;
|
|
58
|
+
const enabled = userPercentage < flag.percentage;
|
|
59
|
+
return {
|
|
60
|
+
key,
|
|
61
|
+
enabled,
|
|
62
|
+
reason: enabled ? 'In percentage rollout' : 'Not in percentage rollout',
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return { key, enabled: true, reason: 'Fully enabled' };
|
|
66
|
+
}
|
|
67
|
+
getAllFlags() {
|
|
68
|
+
return this.options.flags;
|
|
69
|
+
}
|
|
70
|
+
hashUserId(userId) {
|
|
71
|
+
let hash = 0;
|
|
72
|
+
for (let i = 0; i < userId.length; i++) {
|
|
73
|
+
const char = userId.charCodeAt(i);
|
|
74
|
+
hash = (hash << 5) - hash + char;
|
|
75
|
+
hash = hash & hash;
|
|
76
|
+
}
|
|
77
|
+
return Math.abs(hash);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
exports.FeatureFlagService = FeatureFlagService;
|
|
81
|
+
exports.FeatureFlagService = FeatureFlagService = __decorate([
|
|
82
|
+
(0, common_1.Injectable)(),
|
|
83
|
+
__param(0, (0, common_1.Inject)(OPTIONS_TOKEN)),
|
|
84
|
+
__metadata("design:paramtypes", [Object])
|
|
85
|
+
], FeatureFlagService);
|
|
86
|
+
//# sourceMappingURL=feature-flag.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flag.service.js","sourceRoot":"","sources":["../../src/services/feature-flag.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAoD;AAIpD,MAAM,aAAa,GAAG,uBAAuB,CAAC;AA8FrC,sCAAa;AA3Ff,IAAM,kBAAkB,GAAxB,MAAM,kBAAkB;IAC7B,YAEmB,OAAkC;QAAlC,YAAO,GAAP,OAAO,CAA2B;IAClD,CAAC;IAKJ,KAAK,CAAC,SAAS,CAAC,GAAW,EAAE,OAA4B;QACvD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;QACjD,OAAO,MAAM,CAAC,OAAO,CAAC;IACxB,CAAC;IAKD,KAAK,CAAC,QAAQ,CAAC,GAAW,EAAE,OAA4B;QACtD,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAErC,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,OAAO;gBACL,GAAG;gBACH,OAAO,EAAE,IAAI,CAAC,OAAO,CA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