@hazeljs/feature-toggle 0.2.0-alpha.6
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/LICENSE +192 -0
- package/README.md +144 -0
- package/dist/__tests__/feature-toggle.decorator.test.d.ts +2 -0
- package/dist/__tests__/feature-toggle.decorator.test.d.ts.map +1 -0
- package/dist/__tests__/feature-toggle.decorator.test.js +92 -0
- package/dist/__tests__/feature-toggle.service.test.d.ts +2 -0
- package/dist/__tests__/feature-toggle.service.test.d.ts.map +1 -0
- package/dist/__tests__/feature-toggle.service.test.js +95 -0
- package/dist/feature-toggle.decorator.d.ts +16 -0
- package/dist/feature-toggle.decorator.d.ts.map +1 -0
- package/dist/feature-toggle.decorator.js +23 -0
- package/dist/feature-toggle.guard.d.ts +7 -0
- package/dist/feature-toggle.guard.d.ts.map +1 -0
- package/dist/feature-toggle.guard.js +43 -0
- package/dist/feature-toggle.module.d.ts +9 -0
- package/dist/feature-toggle.module.d.ts.map +1 -0
- package/dist/feature-toggle.module.js +30 -0
- package/dist/feature-toggle.service.d.ts +27 -0
- package/dist/feature-toggle.service.d.ts.map +1 -0
- package/dist/feature-toggle.service.js +81 -0
- package/dist/feature-toggle.types.d.ts +16 -0
- package/dist/feature-toggle.types.d.ts.map +1 -0
- package/dist/feature-toggle.types.js +2 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
Copyright 2024 HazelJS Team
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
22
|
+
|
|
23
|
+
1. Definitions.
|
|
24
|
+
|
|
25
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
26
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
27
|
+
|
|
28
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
29
|
+
the copyright owner that is granting the License.
|
|
30
|
+
|
|
31
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
32
|
+
other entities that control, are controlled by, or are under common
|
|
33
|
+
control with that entity. For the purposes of this definition,
|
|
34
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
35
|
+
direction or management of such entity, whether by contract or
|
|
36
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
37
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
38
|
+
|
|
39
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
40
|
+
exercising permissions granted by this License.
|
|
41
|
+
|
|
42
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
43
|
+
including but not limited to software source code, documentation
|
|
44
|
+
source, and configuration files.
|
|
45
|
+
|
|
46
|
+
"Object" form shall mean any form resulting from mechanical
|
|
47
|
+
transformation or translation of a Source form, including but
|
|
48
|
+
not limited to compiled object code, generated documentation,
|
|
49
|
+
and conversions to other media types.
|
|
50
|
+
|
|
51
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
52
|
+
Object form, made available under the License, as indicated by a
|
|
53
|
+
copyright notice that is included in or attached to the work
|
|
54
|
+
(an example is provided in the Appendix below).
|
|
55
|
+
|
|
56
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
57
|
+
form, that is based on (or derived from) the Work and for which the
|
|
58
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
59
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
60
|
+
of this License, Derivative Works shall not include works that remain
|
|
61
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
62
|
+
the Work and Derivative Works thereof.
|
|
63
|
+
|
|
64
|
+
"Contribution" shall mean any work of authorship, including
|
|
65
|
+
the original version of the Work and any modifications or additions
|
|
66
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
67
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
68
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
69
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
70
|
+
means any form of electronic, verbal, or written communication sent
|
|
71
|
+
to the Licensor or its representatives, including but not limited to
|
|
72
|
+
communication on electronic mailing lists, source code control systems,
|
|
73
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
74
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
75
|
+
excluding communication that is conspicuously marked or otherwise
|
|
76
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
77
|
+
|
|
78
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
79
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
80
|
+
subsequently incorporated within the Work.
|
|
81
|
+
|
|
82
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
83
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
84
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
85
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
86
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
87
|
+
Work and such Derivative Works in Source or Object form.
|
|
88
|
+
|
|
89
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
90
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
91
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
92
|
+
(except as stated in this section) patent license to make, have made,
|
|
93
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
94
|
+
where such license applies only to those patent claims licensable
|
|
95
|
+
by such Contributor that are necessarily infringed by their
|
|
96
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
97
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
98
|
+
institute patent litigation against any entity (including a
|
|
99
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
100
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
101
|
+
or contributory patent infringement, then any patent licenses
|
|
102
|
+
granted to You under this License for that Work shall terminate
|
|
103
|
+
as of the date such litigation is filed.
|
|
104
|
+
|
|
105
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
106
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
107
|
+
modifications, and in Source or Object form, provided that You
|
|
108
|
+
meet the following conditions:
|
|
109
|
+
|
|
110
|
+
(a) You must give any other recipients of the Work or
|
|
111
|
+
Derivative Works a copy of this License; and
|
|
112
|
+
|
|
113
|
+
(b) You must cause any modified files to carry prominent notices
|
|
114
|
+
stating that You changed the files; and
|
|
115
|
+
|
|
116
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
117
|
+
that You distribute, all copyright, patent, trademark, and
|
|
118
|
+
attribution notices from the Source form of the Work,
|
|
119
|
+
excluding those notices that do not pertain to any part of
|
|
120
|
+
the Derivative Works; and
|
|
121
|
+
|
|
122
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
123
|
+
distribution, then any Derivative Works that You distribute must
|
|
124
|
+
include a readable copy of the attribution notices contained
|
|
125
|
+
within such NOTICE file, excluding those notices that do not
|
|
126
|
+
pertain to any part of the Derivative Works, in at least one
|
|
127
|
+
of the following places: within a NOTICE text file distributed
|
|
128
|
+
as part of the Derivative Works; within the Source form or
|
|
129
|
+
documentation, if provided along with the Derivative Works; or,
|
|
130
|
+
within a display generated by the Derivative Works, if and
|
|
131
|
+
wherever such third-party notices normally appear. The contents
|
|
132
|
+
of the NOTICE file are for informational purposes only and
|
|
133
|
+
do not modify the License. You may add Your own attribution
|
|
134
|
+
notices within Derivative Works that You distribute, alongside
|
|
135
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
136
|
+
that such additional attribution notices cannot be construed
|
|
137
|
+
as modifying the License.
|
|
138
|
+
|
|
139
|
+
You may add Your own copyright statement to Your modifications and
|
|
140
|
+
may provide additional or different license terms and conditions
|
|
141
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
142
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
143
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
144
|
+
the conditions stated in this License.
|
|
145
|
+
|
|
146
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
147
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
148
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
149
|
+
this License, without any additional terms or conditions.
|
|
150
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
151
|
+
the terms of any separate license agreement you may have executed
|
|
152
|
+
with Licensor regarding such Contributions.
|
|
153
|
+
|
|
154
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
155
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
156
|
+
except as required for reasonable and customary use in describing the
|
|
157
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
158
|
+
|
|
159
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
160
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
161
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
162
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
163
|
+
implied, including, without limitation, any warranties or conditions
|
|
164
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
165
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
166
|
+
appropriateness of using or redistributing the Work and assume any
|
|
167
|
+
risks associated with Your exercise of permissions under this License.
|
|
168
|
+
|
|
169
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
170
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
171
|
+
unless required by applicable law (such as deliberate and grossly
|
|
172
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
173
|
+
liable to You for damages, including any direct, indirect, special,
|
|
174
|
+
incidental, or consequential damages of any character arising as a
|
|
175
|
+
result of this License or out of the use or inability to use the
|
|
176
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
177
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
178
|
+
other commercial damages or losses), even if such Contributor
|
|
179
|
+
has been advised of the possibility of such damages.
|
|
180
|
+
|
|
181
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
182
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
183
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
184
|
+
or other liability obligations and/or rights consistent with this
|
|
185
|
+
License. However, in accepting such obligations, You may act only
|
|
186
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
187
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
188
|
+
defend, and hold each Contributor harmless for any liability
|
|
189
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
190
|
+
of your accepting any such warranty or additional liability.
|
|
191
|
+
|
|
192
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# @hazeljs/feature-toggle
|
|
2
|
+
|
|
3
|
+
**Feature flags with one decorator. Protect routes or branch in code.**
|
|
4
|
+
|
|
5
|
+
In-memory flags, optional env seeding, and `@FeatureToggle('name')` on controllers or methods. When the flag is off, the route returns 403. No external SDK — just core.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/@hazeljs/feature-toggle)
|
|
8
|
+
[](https://www.npmjs.com/package/@hazeljs/feature-toggle)
|
|
9
|
+
[](https://www.apache.org/licenses/LICENSE-2.0)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Decorator-first** – `@FeatureToggle('name')` on a controller or method; no manual guards
|
|
14
|
+
- **In-memory** – Simple flag store; optional seed from `initialFlags` or env prefix
|
|
15
|
+
- **Env integration** – Use `envPrefix` (e.g. `FEATURE_`) to load flags from environment variables
|
|
16
|
+
- **Programmatic API** – Inject `FeatureToggleService` and call `isEnabled()`, `set()`, `get()`
|
|
17
|
+
- **TypeScript** – Full type support, no extra dependencies beyond `@hazeljs/core`
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @hazeljs/feature-toggle
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
### 1. Register the module
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { HazelModule } from '@hazeljs/core';
|
|
31
|
+
import { FeatureToggleModule } from '@hazeljs/feature-toggle';
|
|
32
|
+
|
|
33
|
+
@HazelModule({
|
|
34
|
+
imports: [
|
|
35
|
+
FeatureToggleModule.forRoot({
|
|
36
|
+
initialFlags: { newCheckout: true },
|
|
37
|
+
envPrefix: 'FEATURE_', // FEATURE_X from env → flag "x"
|
|
38
|
+
}),
|
|
39
|
+
],
|
|
40
|
+
})
|
|
41
|
+
export class AppModule {}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 2. Protect routes with the decorator
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { Controller, Get } from '@hazeljs/core';
|
|
48
|
+
import { FeatureToggle } from '@hazeljs/feature-toggle';
|
|
49
|
+
|
|
50
|
+
@Controller('checkout')
|
|
51
|
+
export class CheckoutController {
|
|
52
|
+
@Get('new')
|
|
53
|
+
@FeatureToggle('newCheckout')
|
|
54
|
+
getNewCheckout() {
|
|
55
|
+
return { flow: 'new' };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@Get('legacy')
|
|
59
|
+
getLegacyCheckout() {
|
|
60
|
+
return { flow: 'legacy' };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Or require the flag for the whole controller:
|
|
65
|
+
@Controller('beta')
|
|
66
|
+
@FeatureToggle('betaApi')
|
|
67
|
+
export class BetaController {
|
|
68
|
+
@Get()
|
|
69
|
+
index() {
|
|
70
|
+
return { message: 'Beta API' };
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 3. Use in services (programmatic)
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { Service } from '@hazeljs/core';
|
|
79
|
+
import { FeatureToggleService } from '@hazeljs/feature-toggle';
|
|
80
|
+
|
|
81
|
+
@Service()
|
|
82
|
+
export class OrderService {
|
|
83
|
+
constructor(private readonly featureToggle: FeatureToggleService) {}
|
|
84
|
+
|
|
85
|
+
createOrder(data: OrderData) {
|
|
86
|
+
if (this.featureToggle.isEnabled('newCheckout')) {
|
|
87
|
+
return this.createWithNewFlow(data);
|
|
88
|
+
}
|
|
89
|
+
return this.createWithLegacyFlow(data);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## API
|
|
95
|
+
|
|
96
|
+
### FeatureToggleModule
|
|
97
|
+
|
|
98
|
+
- **`FeatureToggleModule.forRoot(options?)`**
|
|
99
|
+
Registers the module. Options:
|
|
100
|
+
- `initialFlags?: Record<string, boolean>` – Flags to set on load
|
|
101
|
+
- `envPrefix?: string` – Env var prefix (e.g. `FEATURE_`); vars like `FEATURE_NEW_UI` become flag `newUi`. Values `true`, `1`, `yes` (case-insensitive) → `true`.
|
|
102
|
+
|
|
103
|
+
### FeatureToggleService
|
|
104
|
+
|
|
105
|
+
- **`isEnabled(name: string): boolean`** – Returns whether the flag is on (default `false` if unset).
|
|
106
|
+
- **`get(name: string): boolean | undefined`** – Raw value; `undefined` if not set.
|
|
107
|
+
- **`set(name: string, value: boolean): void`** – Set or override a flag at runtime (in-memory).
|
|
108
|
+
|
|
109
|
+
### Decorator
|
|
110
|
+
|
|
111
|
+
- **`@FeatureToggle(featureName: string)`** – Method or class decorator. Applies a guard so that when the flag is disabled, the request is rejected (403) and the handler is not run.
|
|
112
|
+
|
|
113
|
+
## Environment variables
|
|
114
|
+
|
|
115
|
+
With `envPrefix: 'FEATURE_'`:
|
|
116
|
+
|
|
117
|
+
```env
|
|
118
|
+
FEATURE_NEW_CHECKOUT=true
|
|
119
|
+
FEATURE_BETA_API=0
|
|
120
|
+
FEATURE_NEW_UI=yes
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Flag names are derived in camelCase: `FEATURE_NEW_CHECKOUT` → `newCheckout`.
|
|
124
|
+
|
|
125
|
+
## Testing
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
npm test
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Contributing
|
|
132
|
+
|
|
133
|
+
Contributions are welcome! Please read our [Contributing Guide](../../CONTRIBUTING.md) for details.
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
Apache 2.0 © [HazelJS](https://hazeljs.com)
|
|
138
|
+
|
|
139
|
+
## Links
|
|
140
|
+
|
|
141
|
+
- [Documentation](https://hazeljs.com/docs/packages/feature-toggle)
|
|
142
|
+
- [GitHub](https://github.com/hazel-js/hazeljs)
|
|
143
|
+
- [Issues](https://github.com/hazel-js/hazeljs/issues)
|
|
144
|
+
- [Discord](https://discord.com/channels/1448263814238965833/1448263814859456575)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-toggle.decorator.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/feature-toggle.decorator.test.ts"],"names":[],"mappings":"AACA,OAAO,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,92 @@
|
|
|
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
|
+
/// <reference types="jest" />
|
|
13
|
+
require("reflect-metadata");
|
|
14
|
+
const feature_toggle_guard_1 = require("../feature-toggle.guard");
|
|
15
|
+
const feature_toggle_decorator_1 = require("../feature-toggle.decorator");
|
|
16
|
+
const feature_toggle_service_1 = require("../feature-toggle.service");
|
|
17
|
+
jest.mock('@hazeljs/core', () => {
|
|
18
|
+
const actual = jest.requireActual('@hazeljs/core');
|
|
19
|
+
return {
|
|
20
|
+
...actual,
|
|
21
|
+
Service: () => () => undefined,
|
|
22
|
+
UseGuards: (...guards) => {
|
|
23
|
+
return (target, propertyKey, descriptor) => {
|
|
24
|
+
if (propertyKey !== undefined && descriptor !== undefined) {
|
|
25
|
+
const existing = Reflect.getMetadata('hazel:guards', target, propertyKey) || [];
|
|
26
|
+
Reflect.defineMetadata('hazel:guards', [...existing, ...guards], target, propertyKey);
|
|
27
|
+
return descriptor;
|
|
28
|
+
}
|
|
29
|
+
const existing = Reflect.getMetadata('hazel:guards', target) || [];
|
|
30
|
+
Reflect.defineMetadata('hazel:guards', [...existing, ...guards], target);
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
describe('createFeatureToggleGuard', () => {
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
feature_toggle_service_1.FeatureToggleService.setOptions({});
|
|
38
|
+
});
|
|
39
|
+
it('returns a class that allows access when feature is enabled', () => {
|
|
40
|
+
const GuardClass = (0, feature_toggle_guard_1.createFeatureToggleGuard)('testFlag');
|
|
41
|
+
const service = new feature_toggle_service_1.FeatureToggleService();
|
|
42
|
+
service.set('testFlag', true);
|
|
43
|
+
const guard = new GuardClass(service);
|
|
44
|
+
expect(guard.canActivate({})).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
it('returns a class that denies access when feature is disabled', () => {
|
|
47
|
+
const GuardClass = (0, feature_toggle_guard_1.createFeatureToggleGuard)('testFlag');
|
|
48
|
+
const service = new feature_toggle_service_1.FeatureToggleService();
|
|
49
|
+
service.set('testFlag', false);
|
|
50
|
+
const guard = new GuardClass(service);
|
|
51
|
+
expect(guard.canActivate({})).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
it('returns the same guard class for the same feature name (caching)', () => {
|
|
54
|
+
const A = (0, feature_toggle_guard_1.createFeatureToggleGuard)('same');
|
|
55
|
+
const B = (0, feature_toggle_guard_1.createFeatureToggleGuard)('same');
|
|
56
|
+
expect(A).toBe(B);
|
|
57
|
+
});
|
|
58
|
+
it('returns different guard classes for different feature names', () => {
|
|
59
|
+
const A = (0, feature_toggle_guard_1.createFeatureToggleGuard)('one');
|
|
60
|
+
const B = (0, feature_toggle_guard_1.createFeatureToggleGuard)('two');
|
|
61
|
+
expect(A).not.toBe(B);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
describe('FeatureToggle decorator', () => {
|
|
65
|
+
it('applies guard metadata on method', () => {
|
|
66
|
+
class TestController {
|
|
67
|
+
handler() { }
|
|
68
|
+
}
|
|
69
|
+
__decorate([
|
|
70
|
+
(0, feature_toggle_decorator_1.FeatureToggle)('myFeature'),
|
|
71
|
+
__metadata("design:type", Function),
|
|
72
|
+
__metadata("design:paramtypes", []),
|
|
73
|
+
__metadata("design:returntype", void 0)
|
|
74
|
+
], TestController.prototype, "handler", null);
|
|
75
|
+
const guards = Reflect.getMetadata('hazel:guards', TestController.prototype, 'handler');
|
|
76
|
+
expect(guards).toBeDefined();
|
|
77
|
+
expect(Array.isArray(guards)).toBe(true);
|
|
78
|
+
expect(guards.length).toBe(1);
|
|
79
|
+
});
|
|
80
|
+
it('applies guard metadata on class', () => {
|
|
81
|
+
let TestController = class TestController {
|
|
82
|
+
handler() { }
|
|
83
|
+
};
|
|
84
|
+
TestController = __decorate([
|
|
85
|
+
(0, feature_toggle_decorator_1.FeatureToggle)('classFeature')
|
|
86
|
+
], TestController);
|
|
87
|
+
const guards = Reflect.getMetadata('hazel:guards', TestController);
|
|
88
|
+
expect(guards).toBeDefined();
|
|
89
|
+
expect(Array.isArray(guards)).toBe(true);
|
|
90
|
+
expect(guards.length).toBe(1);
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-toggle.service.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/feature-toggle.service.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/// <reference types="jest" />
|
|
4
|
+
const feature_toggle_service_1 = require("../feature-toggle.service");
|
|
5
|
+
const feature_toggle_module_1 = require("../feature-toggle.module");
|
|
6
|
+
jest.mock('@hazeljs/core', () => ({
|
|
7
|
+
Service: () => () => undefined,
|
|
8
|
+
HazelModule: () => () => undefined,
|
|
9
|
+
}));
|
|
10
|
+
describe('FeatureToggleService', () => {
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
feature_toggle_service_1.FeatureToggleService.setOptions({});
|
|
13
|
+
});
|
|
14
|
+
describe('isEnabled', () => {
|
|
15
|
+
it('returns false for unset flag', () => {
|
|
16
|
+
const svc = new feature_toggle_service_1.FeatureToggleService();
|
|
17
|
+
expect(svc.isEnabled('missing')).toBe(false);
|
|
18
|
+
});
|
|
19
|
+
it('returns true when flag is set to true', () => {
|
|
20
|
+
const svc = new feature_toggle_service_1.FeatureToggleService();
|
|
21
|
+
svc.set('onFlag', true);
|
|
22
|
+
expect(svc.isEnabled('onFlag')).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
it('returns false when flag is set to false', () => {
|
|
25
|
+
const svc = new feature_toggle_service_1.FeatureToggleService();
|
|
26
|
+
svc.set('offFlag', false);
|
|
27
|
+
expect(svc.isEnabled('offFlag')).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
describe('get', () => {
|
|
31
|
+
it('returns undefined for unset flag', () => {
|
|
32
|
+
const svc = new feature_toggle_service_1.FeatureToggleService();
|
|
33
|
+
expect(svc.get('missing')).toBeUndefined();
|
|
34
|
+
});
|
|
35
|
+
it('returns value when flag is set', () => {
|
|
36
|
+
const svc = new feature_toggle_service_1.FeatureToggleService();
|
|
37
|
+
svc.set('a', true);
|
|
38
|
+
svc.set('b', false);
|
|
39
|
+
expect(svc.get('a')).toBe(true);
|
|
40
|
+
expect(svc.get('b')).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe('set', () => {
|
|
44
|
+
it('sets and overwrites flag', () => {
|
|
45
|
+
const svc = new feature_toggle_service_1.FeatureToggleService();
|
|
46
|
+
svc.set('x', true);
|
|
47
|
+
expect(svc.get('x')).toBe(true);
|
|
48
|
+
svc.set('x', false);
|
|
49
|
+
expect(svc.get('x')).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
describe('initialFlags', () => {
|
|
53
|
+
it('loads initial flags from options', () => {
|
|
54
|
+
feature_toggle_service_1.FeatureToggleService.setOptions({
|
|
55
|
+
initialFlags: { foo: true, bar: false },
|
|
56
|
+
});
|
|
57
|
+
const svc = new feature_toggle_service_1.FeatureToggleService();
|
|
58
|
+
expect(svc.isEnabled('foo')).toBe(true);
|
|
59
|
+
expect(svc.isEnabled('bar')).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe('envPrefix', () => {
|
|
63
|
+
it('loads flags from env when envPrefix is set', () => {
|
|
64
|
+
const orig = process.env.FEATURE_TEST_A;
|
|
65
|
+
const origB = process.env.FEATURE_TEST_B;
|
|
66
|
+
process.env.FEATURE_TEST_A = 'true';
|
|
67
|
+
process.env.FEATURE_TEST_B = '0';
|
|
68
|
+
feature_toggle_service_1.FeatureToggleService.setOptions({ envPrefix: 'FEATURE_' });
|
|
69
|
+
const svc = new feature_toggle_service_1.FeatureToggleService();
|
|
70
|
+
// FEATURE_TEST_A -> testA (camelCase), FEATURE_TEST_B -> testB
|
|
71
|
+
expect(svc.isEnabled('testA')).toBe(true);
|
|
72
|
+
expect(svc.isEnabled('testB')).toBe(false);
|
|
73
|
+
if (orig !== undefined)
|
|
74
|
+
process.env.FEATURE_TEST_A = orig;
|
|
75
|
+
else
|
|
76
|
+
delete process.env.FEATURE_TEST_A;
|
|
77
|
+
if (origB !== undefined)
|
|
78
|
+
process.env.FEATURE_TEST_B = origB;
|
|
79
|
+
else
|
|
80
|
+
delete process.env.FEATURE_TEST_B;
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
describe('FeatureToggleModule', () => {
|
|
85
|
+
it('forRoot accepts options and returns module', () => {
|
|
86
|
+
const mod = feature_toggle_module_1.FeatureToggleModule.forRoot({
|
|
87
|
+
initialFlags: { x: true },
|
|
88
|
+
});
|
|
89
|
+
expect(mod).toBe(feature_toggle_module_1.FeatureToggleModule);
|
|
90
|
+
});
|
|
91
|
+
it('forRoot with no args returns module', () => {
|
|
92
|
+
const mod = feature_toggle_module_1.FeatureToggleModule.forRoot();
|
|
93
|
+
expect(mod).toBe(feature_toggle_module_1.FeatureToggleModule);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Protects a controller or route handler with a feature flag. When the flag is
|
|
3
|
+
* disabled, requests receive 403 without executing the handler.
|
|
4
|
+
*
|
|
5
|
+
* @param featureName - The feature flag name (must be enabled for access).
|
|
6
|
+
* @example
|
|
7
|
+
* @FeatureToggle('newCheckout')
|
|
8
|
+
* @Get('new')
|
|
9
|
+
* getNewCheckout() { ... }
|
|
10
|
+
*
|
|
11
|
+
* @FeatureToggle('betaApi')
|
|
12
|
+
* @Controller('beta')
|
|
13
|
+
* export class BetaController { ... }
|
|
14
|
+
*/
|
|
15
|
+
export declare function FeatureToggle(featureName: string): MethodDecorator & ClassDecorator;
|
|
16
|
+
//# sourceMappingURL=feature-toggle.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-toggle.decorator.d.ts","sourceRoot":"","sources":["../src/feature-toggle.decorator.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAAC,WAAW,EAAE,MAAM,GAAG,eAAe,GAAG,cAAc,CAGnF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FeatureToggle = FeatureToggle;
|
|
4
|
+
const core_1 = require("@hazeljs/core");
|
|
5
|
+
const feature_toggle_guard_1 = require("./feature-toggle.guard");
|
|
6
|
+
/**
|
|
7
|
+
* Protects a controller or route handler with a feature flag. When the flag is
|
|
8
|
+
* disabled, requests receive 403 without executing the handler.
|
|
9
|
+
*
|
|
10
|
+
* @param featureName - The feature flag name (must be enabled for access).
|
|
11
|
+
* @example
|
|
12
|
+
* @FeatureToggle('newCheckout')
|
|
13
|
+
* @Get('new')
|
|
14
|
+
* getNewCheckout() { ... }
|
|
15
|
+
*
|
|
16
|
+
* @FeatureToggle('betaApi')
|
|
17
|
+
* @Controller('beta')
|
|
18
|
+
* export class BetaController { ... }
|
|
19
|
+
*/
|
|
20
|
+
function FeatureToggle(featureName) {
|
|
21
|
+
const GuardClass = (0, feature_toggle_guard_1.createFeatureToggleGuard)(featureName);
|
|
22
|
+
return (0, core_1.UseGuards)(GuardClass);
|
|
23
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type CanActivate, type Type } from '@hazeljs/core';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an injectable guard class that allows access only when the given
|
|
4
|
+
* feature flag is enabled. Used internally by @FeatureToggle().
|
|
5
|
+
*/
|
|
6
|
+
export declare function createFeatureToggleGuard(featureName: string): Type<CanActivate>;
|
|
7
|
+
//# sourceMappingURL=feature-toggle.guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-toggle.guard.d.ts","sourceRoot":"","sources":["../src/feature-toggle.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,KAAK,WAAW,EAAyB,KAAK,IAAI,EAAE,MAAM,eAAe,CAAC;AAK/F;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC,WAAW,CAAC,CAsB/E"}
|
|
@@ -0,0 +1,43 @@
|
|
|
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.createFeatureToggleGuard = createFeatureToggleGuard;
|
|
13
|
+
const core_1 = require("@hazeljs/core");
|
|
14
|
+
const feature_toggle_service_1 = require("./feature-toggle.service");
|
|
15
|
+
const guardCache = new Map();
|
|
16
|
+
/**
|
|
17
|
+
* Creates an injectable guard class that allows access only when the given
|
|
18
|
+
* feature flag is enabled. Used internally by @FeatureToggle().
|
|
19
|
+
*/
|
|
20
|
+
function createFeatureToggleGuard(featureName) {
|
|
21
|
+
let GuardClass = guardCache.get(featureName);
|
|
22
|
+
if (GuardClass) {
|
|
23
|
+
return GuardClass;
|
|
24
|
+
}
|
|
25
|
+
let FeatureToggleGuardImpl = class FeatureToggleGuardImpl {
|
|
26
|
+
constructor(featureToggle) {
|
|
27
|
+
this.featureToggle = featureToggle;
|
|
28
|
+
}
|
|
29
|
+
canActivate(_context) {
|
|
30
|
+
return this.featureToggle.isEnabled(featureName);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
FeatureToggleGuardImpl = __decorate([
|
|
34
|
+
(0, core_1.Injectable)(),
|
|
35
|
+
__metadata("design:paramtypes", [feature_toggle_service_1.FeatureToggleService])
|
|
36
|
+
], FeatureToggleGuardImpl);
|
|
37
|
+
// Ensure the container resolves FeatureToggleService for the first constructor param
|
|
38
|
+
// (design:paramtypes may not be reliable for dynamically created classes)
|
|
39
|
+
Reflect.defineMetadata('hazel:inject', [feature_toggle_service_1.FeatureToggleService], FeatureToggleGuardImpl);
|
|
40
|
+
GuardClass = FeatureToggleGuardImpl;
|
|
41
|
+
guardCache.set(featureName, GuardClass);
|
|
42
|
+
return GuardClass;
|
|
43
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { FeatureToggleModuleOptions } from './feature-toggle.types';
|
|
2
|
+
export type { FeatureToggleModuleOptions } from './feature-toggle.types';
|
|
3
|
+
export declare class FeatureToggleModule {
|
|
4
|
+
/**
|
|
5
|
+
* Register FeatureToggleModule with optional options.
|
|
6
|
+
*/
|
|
7
|
+
static forRoot(options?: FeatureToggleModuleOptions): typeof FeatureToggleModule;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=feature-toggle.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-toggle.module.d.ts","sourceRoot":"","sources":["../src/feature-toggle.module.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AAEzE,YAAY,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AAEzE,qBAIa,mBAAmB;IAC9B;;OAEG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,0BAA0B,GAAG,OAAO,mBAAmB;CAMjF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
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 FeatureToggleModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.FeatureToggleModule = void 0;
|
|
11
|
+
const core_1 = require("@hazeljs/core");
|
|
12
|
+
const feature_toggle_service_1 = require("./feature-toggle.service");
|
|
13
|
+
let FeatureToggleModule = FeatureToggleModule_1 = class FeatureToggleModule {
|
|
14
|
+
/**
|
|
15
|
+
* Register FeatureToggleModule with optional options.
|
|
16
|
+
*/
|
|
17
|
+
static forRoot(options) {
|
|
18
|
+
if (options) {
|
|
19
|
+
feature_toggle_service_1.FeatureToggleService.setOptions(options);
|
|
20
|
+
}
|
|
21
|
+
return FeatureToggleModule_1;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
exports.FeatureToggleModule = FeatureToggleModule;
|
|
25
|
+
exports.FeatureToggleModule = FeatureToggleModule = FeatureToggleModule_1 = __decorate([
|
|
26
|
+
(0, core_1.HazelModule)({
|
|
27
|
+
providers: [feature_toggle_service_1.FeatureToggleService],
|
|
28
|
+
exports: [feature_toggle_service_1.FeatureToggleService],
|
|
29
|
+
})
|
|
30
|
+
], FeatureToggleModule);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { FeatureToggleModuleOptions } from './feature-toggle.types';
|
|
2
|
+
export declare class FeatureToggleService {
|
|
3
|
+
private static options;
|
|
4
|
+
private readonly flags;
|
|
5
|
+
constructor();
|
|
6
|
+
/**
|
|
7
|
+
* Set module options (called by FeatureToggleModule.forRoot).
|
|
8
|
+
*/
|
|
9
|
+
static setOptions(options: FeatureToggleModuleOptions): void;
|
|
10
|
+
private load;
|
|
11
|
+
private envKeyToFlagName;
|
|
12
|
+
private parseBool;
|
|
13
|
+
/**
|
|
14
|
+
* Returns true if the feature is enabled, false otherwise.
|
|
15
|
+
* Unset flags are treated as disabled.
|
|
16
|
+
*/
|
|
17
|
+
isEnabled(name: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Get the raw value for a flag (undefined if not set).
|
|
20
|
+
*/
|
|
21
|
+
get(name: string): boolean | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Set or override a flag at runtime (in-memory only).
|
|
24
|
+
*/
|
|
25
|
+
set(name: string, value: boolean): void;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=feature-toggle.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-toggle.service.d.ts","sourceRoot":"","sources":["../src/feature-toggle.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AAEpE,qBACa,oBAAoB;IAC/B,OAAO,CAAC,MAAM,CAAC,OAAO,CAAkC;IACxD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8B;;IAMpD;;OAEG;IACH,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,0BAA0B,GAAG,IAAI;IAI5D,OAAO,CAAC,IAAI;IAqBZ,OAAO,CAAC,gBAAgB;IASxB,OAAO,CAAC,SAAS;IAKjB;;;OAGG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIhC;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS;IAItC;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,IAAI;CAGxC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
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 FeatureToggleService_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.FeatureToggleService = void 0;
|
|
14
|
+
const core_1 = require("@hazeljs/core");
|
|
15
|
+
let FeatureToggleService = FeatureToggleService_1 = class FeatureToggleService {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.flags = new Map();
|
|
18
|
+
this.load();
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Set module options (called by FeatureToggleModule.forRoot).
|
|
22
|
+
*/
|
|
23
|
+
static setOptions(options) {
|
|
24
|
+
FeatureToggleService_1.options = options ?? {};
|
|
25
|
+
}
|
|
26
|
+
load() {
|
|
27
|
+
const opts = FeatureToggleService_1.options;
|
|
28
|
+
if (opts.initialFlags) {
|
|
29
|
+
for (const [name, value] of Object.entries(opts.initialFlags)) {
|
|
30
|
+
this.flags.set(name, value);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (opts.envPrefix) {
|
|
34
|
+
const prefix = opts.envPrefix.toUpperCase();
|
|
35
|
+
for (const [key, raw] of Object.entries(process.env)) {
|
|
36
|
+
if (key.startsWith(prefix) && raw !== undefined) {
|
|
37
|
+
const name = this.envKeyToFlagName(key.slice(prefix.length));
|
|
38
|
+
const value = this.parseBool(raw);
|
|
39
|
+
this.flags.set(name, value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
envKeyToFlagName(envKey) {
|
|
45
|
+
// FEATURE_NEW_CHECKOUT -> newCheckout
|
|
46
|
+
return envKey
|
|
47
|
+
.toLowerCase()
|
|
48
|
+
.split('_')
|
|
49
|
+
.map((part, i) => (i === 0 ? part : part.charAt(0).toUpperCase() + part.slice(1)))
|
|
50
|
+
.join('');
|
|
51
|
+
}
|
|
52
|
+
parseBool(value) {
|
|
53
|
+
const v = value.toLowerCase();
|
|
54
|
+
return v === 'true' || v === '1' || v === 'yes';
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Returns true if the feature is enabled, false otherwise.
|
|
58
|
+
* Unset flags are treated as disabled.
|
|
59
|
+
*/
|
|
60
|
+
isEnabled(name) {
|
|
61
|
+
return this.flags.get(name) ?? false;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Get the raw value for a flag (undefined if not set).
|
|
65
|
+
*/
|
|
66
|
+
get(name) {
|
|
67
|
+
return this.flags.has(name) ? this.flags.get(name) : undefined;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Set or override a flag at runtime (in-memory only).
|
|
71
|
+
*/
|
|
72
|
+
set(name, value) {
|
|
73
|
+
this.flags.set(name, value);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
exports.FeatureToggleService = FeatureToggleService;
|
|
77
|
+
FeatureToggleService.options = {};
|
|
78
|
+
exports.FeatureToggleService = FeatureToggleService = FeatureToggleService_1 = __decorate([
|
|
79
|
+
(0, core_1.Service)(),
|
|
80
|
+
__metadata("design:paramtypes", [])
|
|
81
|
+
], FeatureToggleService);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for FeatureToggleModule.forRoot()
|
|
3
|
+
*/
|
|
4
|
+
export interface FeatureToggleModuleOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Initial flag values to set on module load.
|
|
7
|
+
*/
|
|
8
|
+
initialFlags?: Record<string, boolean>;
|
|
9
|
+
/**
|
|
10
|
+
* Environment variable prefix. When set, any env var like PREFIX_NAME
|
|
11
|
+
* (e.g. FEATURE_NEW_UI) is read and stored as flag "newUi" (camelCase).
|
|
12
|
+
* Values: 'true', '1', 'yes' (case-insensitive) => true; others => false.
|
|
13
|
+
*/
|
|
14
|
+
envPrefix?: string;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=feature-toggle.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-toggle.types.d.ts","sourceRoot":"","sources":["../src/feature-toggle.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEvC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hazeljs/feature-toggle - Feature toggle module for HazelJS
|
|
3
|
+
*/
|
|
4
|
+
export { FeatureToggleModule, type FeatureToggleModuleOptions } from './feature-toggle.module';
|
|
5
|
+
export { FeatureToggleService } from './feature-toggle.service';
|
|
6
|
+
export { FeatureToggle } from './feature-toggle.decorator';
|
|
7
|
+
export { createFeatureToggleGuard } from './feature-toggle.guard';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,mBAAmB,EAAE,KAAK,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AAC/F,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @hazeljs/feature-toggle - Feature toggle module for HazelJS
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createFeatureToggleGuard = exports.FeatureToggle = exports.FeatureToggleService = exports.FeatureToggleModule = void 0;
|
|
7
|
+
var feature_toggle_module_1 = require("./feature-toggle.module");
|
|
8
|
+
Object.defineProperty(exports, "FeatureToggleModule", { enumerable: true, get: function () { return feature_toggle_module_1.FeatureToggleModule; } });
|
|
9
|
+
var feature_toggle_service_1 = require("./feature-toggle.service");
|
|
10
|
+
Object.defineProperty(exports, "FeatureToggleService", { enumerable: true, get: function () { return feature_toggle_service_1.FeatureToggleService; } });
|
|
11
|
+
var feature_toggle_decorator_1 = require("./feature-toggle.decorator");
|
|
12
|
+
Object.defineProperty(exports, "FeatureToggle", { enumerable: true, get: function () { return feature_toggle_decorator_1.FeatureToggle; } });
|
|
13
|
+
var feature_toggle_guard_1 = require("./feature-toggle.guard");
|
|
14
|
+
Object.defineProperty(exports, "createFeatureToggleGuard", { enumerable: true, get: function () { return feature_toggle_guard_1.createFeatureToggleGuard; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hazeljs/feature-toggle",
|
|
3
|
+
"version": "0.2.0-alpha.6",
|
|
4
|
+
"description": "Feature toggle module for HazelJS framework",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"test": "jest --coverage --passWithNoTests",
|
|
13
|
+
"lint": "eslint \"src/**/*.ts\"",
|
|
14
|
+
"lint:fix": "eslint \"src/**/*.ts\" --fix",
|
|
15
|
+
"clean": "rm -rf dist"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/jest": "^29.5.14",
|
|
19
|
+
"@types/node": "^20.17.50",
|
|
20
|
+
"@typescript-eslint/eslint-plugin": "^8.18.2",
|
|
21
|
+
"@typescript-eslint/parser": "^8.18.2",
|
|
22
|
+
"eslint": "^8.56.0",
|
|
23
|
+
"jest": "^29.7.0",
|
|
24
|
+
"ts-jest": "^29.1.2",
|
|
25
|
+
"typescript": "^5.3.3"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/hazel-js/hazeljs.git",
|
|
33
|
+
"directory": "packages/feature-toggle"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"hazeljs",
|
|
37
|
+
"feature-toggle",
|
|
38
|
+
"feature-flag",
|
|
39
|
+
"flags"
|
|
40
|
+
],
|
|
41
|
+
"author": "Muhammad Arslan <muhammad.arslan@hazeljs.com>",
|
|
42
|
+
"license": "Apache-2.0",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/hazeljs/hazel-js/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://hazeljs.com",
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"@hazeljs/core": ">=0.2.0-beta.0"
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "7d4d24452da000b3b74ed841c90d7718dbb7669f"
|
|
51
|
+
}
|